Implement Script.error properly.

It's not good to get an attirbute error when trying to raise another error.
This commit is contained in:
Bill Currie 2012-09-10 14:58:41 +09:00
parent dade60863e
commit f525be0880

View file

@ -19,6 +19,11 @@
# <pep8 compliant> # <pep8 compliant>
class ScriptError(Exception):
def __init__(self, fname, line, message):
Exception.__init__(self, "%s:%d: %s" % (fname, line, message))
self.line = line
class Script: class Script:
def __init__(self, filename, text, single="{}()':"): def __init__(self, filename, text, single="{}()':"):
self.filename = filename self.filename = filename
@ -27,6 +32,8 @@ class Script:
self.pos = 0 self.pos = 0
self.line = 1 self.line = 1
self.unget = False self.unget = False
def error(self, msg):
raise ScriptError(self.filename, self.line, msg)
def tokenAvailable(self, crossline=False): def tokenAvailable(self, crossline=False):
if self.unget: if self.unget:
return True return True
@ -60,16 +67,16 @@ class Script:
return self.token return self.token
if not self.tokenAvailable(crossline): if not self.tokenAvailable(crossline):
if not crossline: if not crossline:
self.error(self, "line is incomplete") self.error("line is incomplete")
return None return None
if self.text[self.pos] == "\"": if self.text[self.pos] == "\"":
self.pos += 1 self.pos += 1
start = self.pos start = self.pos
if self.text[self.pos] == len(self.text): if self.text[self.pos] == len(self.text):
self.error(self, "EOF inside quoted string") self.error("EOF inside quoted string")
while self.text[self.pos] != "\"": while self.text[self.pos] != "\"":
if self.pos == len(self.text): if self.pos == len(self.text):
self.error(self, "EOF inside quoted string") self.error("EOF inside quoted string")
return None return None
if self.text[self.pos] == "\n": if self.text[self.pos] == "\n":
self.line += 1 self.line += 1