-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.py
119 lines (116 loc) · 4.29 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# vim:ts=4:et
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <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:
def __init__(self, filename, text, single="{}()':", quotes=True):
self.filename = filename
if text[0:3] == "\xef\xbb\xbf":
text = text[3:]
self.text = text
self.single = single
self.quotes = quotes
self.pos = 0
self.line = 1
self.unget = False
def error(self, msg):
raise ScriptError(self.filename, self.line, msg)
def tokenAvailable(self, crossline=False):
if self.unget:
return True
while self.pos < len(self.text):
while self.pos < len(self.text) and self.text[self.pos].isspace():
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 getLine(self):
start = self.pos
end = start
while self.pos < len(self.text):
if self.text[self.pos] == "\n":
self.line += 1
self.pos += 1
break
if self.text[self.pos:self.pos + 2] == "//":
break
self.pos += 1
end = self.pos
if self.unget:
self.unget = False
self.token = self.token + self.text[start:end]
else:
self.token = self.text[start:end]
return self.pos < len(self.text)
def getToken(self, crossline=False):
if self.unget:
self.unget = False
return self.token
if not self.tokenAvailable(crossline):
if not crossline:
self.error("line is incomplete")
return None
if self.quotes and self.text[self.pos] == "\"":
self.pos += 1
start = self.pos
if self.text[self.pos] == len(self.text):
self.error("EOF inside quoted string")
while self.text[self.pos] != "\"":
if self.pos == len(self.text):
self.error("EOF inside quoted string")
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