2012-08-29 05:55:11 +00:00
|
|
|
# vim:ts=4:et
|
|
|
|
|
|
|
|
class Script:
|
|
|
|
def __init__(self, filename, text, single="{}()':"):
|
|
|
|
self.filename = filename
|
|
|
|
self.text = text
|
|
|
|
self.single = single
|
|
|
|
self.pos = 0
|
|
|
|
self.line = 1
|
|
|
|
self.unget = False
|
|
|
|
def tokenAvailable(self, crossline=False):
|
|
|
|
if self.unget:
|
|
|
|
return True
|
|
|
|
while self.pos < len(self.text):
|
2012-08-30 04:19:30 +00:00
|
|
|
while self.pos < len(self.text) and self.text[self.pos].isspace():
|
2012-08-29 05:55:11 +00:00
|
|
|
if self.text[self.pos] == "\n":
|
|
|
|
if not crossline:
|
|
|
|
return False
|
|
|
|
self.line += 1
|
|
|
|
self.pos += 1
|
|
|
|
if self.pos == len(self.text):
|
|
|
|
return False
|
|
|
|
if self.text[self.pos] in ["\x1a", "\x04"]:
|
|
|
|
# end of file characters
|
|
|
|
self.pos += 1
|
|
|
|
continue
|
|
|
|
if self.text[self.pos:self.pos + 2] == "//":
|
|
|
|
while (self.pos < len(self.text)
|
|
|
|
and self.text[self.pos] != "\n"):
|
|
|
|
self.pos += 1
|
|
|
|
if self.pos == len(self.text):
|
|
|
|
return False
|
|
|
|
if not crossline:
|
|
|
|
return False
|
|
|
|
continue
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
def getToken(self, crossline=False):
|
|
|
|
if self.unget:
|
|
|
|
self.unget = False
|
|
|
|
return self.token
|
|
|
|
if not self.tokenAvailable(crossline):
|
|
|
|
if not crossline:
|
2012-08-30 04:19:30 +00:00
|
|
|
self.error(self, "line is incomplete")
|
2012-08-29 05:55:11 +00:00
|
|
|
return None
|
|
|
|
if self.text[self.pos] == "\"":
|
|
|
|
self.pos += 1
|
|
|
|
start = self.pos
|
|
|
|
if self.text[self.pos] == len(self.text):
|
2012-08-30 04:19:30 +00:00
|
|
|
self.error(self, "EOF inside quoted string")
|
2012-08-29 05:55:11 +00:00
|
|
|
while self.text[self.pos] != "\"":
|
|
|
|
if self.pos == len(self.text):
|
2012-08-30 04:19:30 +00:00
|
|
|
self.error(self, "EOF inside quoted string")
|
2012-08-29 05:55:11 +00:00
|
|
|
return None
|
|
|
|
if self.text[self.pos] == "\n":
|
|
|
|
self.line += 1
|
|
|
|
self.pos += 1
|
|
|
|
self.token = self.text[start:self.pos]
|
|
|
|
self.pos += 1
|
|
|
|
else:
|
|
|
|
start = self.pos
|
|
|
|
if self.text[self.pos] in self.single:
|
|
|
|
self.pos += 1
|
|
|
|
else:
|
|
|
|
while (self.pos < len(self.text)
|
|
|
|
and not self.text[self.pos].isspace()
|
|
|
|
and self.text[self.pos] not in self.single):
|
|
|
|
self.pos += 1
|
|
|
|
self.token = self.text[start:self.pos]
|
|
|
|
return self.token
|
|
|
|
def ungetToken(self):
|
|
|
|
self.unget = True
|