mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 15:48:21 +00:00
Rewrite ReportError() and change EParse to report columns in chars.
ReportError() needed to account for terminal encodings that don't support the characters being printed. It was also reporting an inaccurate column number and its corresponding marker position, because the count was in bytes, not in characters, so that has been fixed. Now EParse.__init__() calls a new function GetErrLineCol() that calculates the line and column corresponding to an error position. The algorithm for finding the start of the line has also been changed in both ReportError() and EParse.__init__(); as a result, function fieldpos() has been removed. The exception's lno and cno fields have been changed to be 1-based, rather than 0-based. Thanks to @Jomik for the report. Fixes #5.
This commit is contained in:
parent
08c69eee0f
commit
c544b51e37
3 changed files with 35 additions and 24 deletions
|
@ -44,26 +44,20 @@ def isalphanum_(c):
|
|||
def ishex(c):
|
||||
return '0' <= c <= '9' or 'A' <= c <= 'F' or 'a' <= c <= 'f'
|
||||
|
||||
def fieldpos(inp, sep, n):
|
||||
"""Return the starting position of field n in a string inp that has zero or
|
||||
more fields separated by sep
|
||||
"""
|
||||
i = -1
|
||||
for n in xrange(n):
|
||||
i = inp.find(sep, i + 1)
|
||||
if i < 0:
|
||||
return i
|
||||
return i + 1
|
||||
def GetErrLineCol(parser):
|
||||
errorpos = parser.errorpos
|
||||
lno = parser.script.count('\n', 0, errorpos)
|
||||
lstart = parser.script.rfind('\n', 0, errorpos) + 1
|
||||
# Find column number in characters
|
||||
cno = len(parser.script[lstart:errorpos].decode('utf8'))
|
||||
return (lno + 1, cno + 1)
|
||||
|
||||
class EParse(Exception):
|
||||
|
||||
def __init__(self, parser, msg):
|
||||
self.errorpos = parser.errorpos
|
||||
self.lno = parser.script.count('\n', 0, self.errorpos)
|
||||
self.cno = self.errorpos - fieldpos(parser.script, '\n', self.lno)
|
||||
# Note the column number reported is in bytes.
|
||||
self.lno, self.cno = GetErrLineCol(parser)
|
||||
|
||||
msg = u"(Line %d char %d): ERROR: %s" % (self.lno + 1, self.cno + 1, msg)
|
||||
msg = u"(Line %d char %d): ERROR: %s" % (self.lno, self.cno, msg)
|
||||
super(EParse, self).__init__(msg)
|
||||
|
||||
class EParseUEOF(EParse):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue