/
nscr.py
258 lines (235 loc) · 9.97 KB
/
nscr.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Begin -- grammar generated by Yapps
import sys, re
from yapps import runtime
class NscrScanner(runtime.Scanner):
patterns = [
('"\\\\$"', re.compile('\\$')),
('"%"', re.compile('%')),
('"\\\\\\\\"', re.compile('\\\\')),
('"@"', re.compile('@')),
('"\\\\*"', re.compile('\\*')),
('"fchk"', re.compile('fchk')),
('"notif"', re.compile('notif')),
('"if"', re.compile('if')),
('","', re.compile(',')),
('"!sd"', re.compile('!sd')),
('":"', re.compile(':')),
('\\s+', re.compile('\\s+')),
(';.*', re.compile(';.*')),
('END', re.compile('$')),
('RAWNAME', re.compile('[_a-zA-Z][_a-zA-Z0-9]*')),
('RAWNUM', re.compile('-?[0-9]+')),
('RAWSTR', re.compile('("|`)[^\\1]*?\\1')),
('COLOR', re.compile('#[0-9a-fA-F]{6}')),
('TXTSTART', re.compile('`')),
('TXT', re.compile('[^@\\\\]+')),
('SPEC_CMD', re.compile('!s|!d|!w')),
('BOOL_AND', re.compile('&&?')),
('BOOL_CMP', re.compile('>=|<=|==|!=|<>|>|<|=')),
]
def __init__(self, str,*args,**kw):
runtime.Scanner.__init__(self,None,{';.*':None,'\\s+':None,},str,*args,**kw)
class Nscr(runtime.Parser):
Context = runtime.Context
def goal(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'goal', [])
stmt = self.stmt(_context)
END = self._scan('END', context=_context)
return stmt
def stmt(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'stmt', [])
_token = self._peek('END', '"\\\\*"', 'TXTSTART', '"!sd"', 'SPEC_CMD', 'RAWNAME', '"@"', '"\\\\\\\\"', '"if"', '"notif"', context=_context)
if _token == 'END':
return []
elif _token not in ['"\\\\*"', 'TXTSTART']:
sentence = self.sentence(_context)
return sentence
elif _token == '"\\\\*"':
LABEL = self.LABEL(_context)
return [[LABEL]]
else: # == 'TXTSTART'
TEXT = self.TEXT(_context)
return TEXT
def sentence(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'sentence', [])
cmd = self.cmd(_context)
v = [cmd]
while self._peek('":"', '"@"', '"\\\\\\\\"', 'END', context=_context) != 'END':
_token = self._peek('":"', '"@"', '"\\\\\\\\"', context=_context)
if _token == '":"':
self._scan('":"', context=_context)
cmd = self.cmd(_context)
v.append(cmd)
else: # in ['"@"', '"\\\\\\\\"']
BREAK_CMD = self.BREAK_CMD(_context)
v.append(BREAK_CMD)
return v
def cmd(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'cmd', [])
_token = self._peek('"!sd"', 'SPEC_CMD', 'RAWNAME', '"@"', '"\\\\\\\\"', '"if"', '"notif"', context=_context)
if _token == 'RAWNAME':
NAME = self.NAME(_context)
args = self.args(_context)
return [NAME, args]
elif _token == '"!sd"':
self._scan('"!sd"', context=_context)
return ["!sd", []]
elif _token == 'SPEC_CMD':
SPEC_CMD = self._scan('SPEC_CMD', context=_context)
NUM = self.NUM(_context)
return [SPEC_CMD, [NUM]]
elif _token not in ['"if"', '"notif"']:
BREAK_CMD = self.BREAK_CMD(_context)
return BREAK_CMD
elif _token == '"if"':
if_stmt = self.if_stmt(_context)
return if_stmt
else: # == '"notif"'
notif_stmt = self.notif_stmt(_context)
return notif_stmt
def args(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'args', [])
_token = self._peek('COLOR', 'RAWNUM', 'RAWNAME', 'RAWSTR', '"\\\\*"', '"%"', '"\\\\$"', '":"', '"@"', '"\\\\\\\\"', 'END', context=_context)
if _token not in ['COLOR', 'RAWNUM', 'RAWNAME', 'RAWSTR', '"\\\\*"', '"%"', '"\\\\$"']:
return []
else:
arg = self.arg(_context)
v = [arg]
while self._peek('","', '":"', '"@"', '"\\\\\\\\"', 'END', context=_context) == '","':
self._scan('","', context=_context)
arg = self.arg(_context)
v.append(arg)
return v
def arg(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'arg', [])
_token = self._peek('COLOR', 'RAWNUM', 'RAWNAME', 'RAWSTR', '"\\\\*"', '"%"', '"\\\\$"', context=_context)
if _token in ['RAWNUM', '"%"']:
NUM = self.NUM(_context)
return NUM
elif _token == 'RAWNAME':
NAME = self.NAME(_context)
return NAME
elif _token not in ['COLOR', '"\\\\*"']:
STR = self.STR(_context)
return STR
elif _token == '"\\\\*"':
LABEL = self.LABEL(_context)
return LABEL
else: # == 'COLOR'
COLOR = self._scan('COLOR', context=_context)
return COLOR
def if_stmt(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'if_stmt', [])
self._scan('"if"', context=_context)
conditions = self.conditions(_context)
sentence = self.sentence(_context)
return ["if", [conditions, sentence]]
def notif_stmt(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'notif_stmt', [])
self._scan('"notif"', context=_context)
conditions = self.conditions(_context)
sentence = self.sentence(_context)
return ["notif", [conditions, sentence]]
def conditions(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'conditions', [])
cond = self.cond(_context)
v = [cond]
while self._peek('BOOL_AND', '"!sd"', 'SPEC_CMD', 'RAWNAME', '"@"', '"\\\\\\\\"', '"if"', '"notif"', context=_context) == 'BOOL_AND':
BOOL_AND = self._scan('BOOL_AND', context=_context)
cond = self.cond(_context)
[].append(cond)
return v
def cond(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'cond', [])
_token = self._peek('"fchk"', 'RAWNUM', '"%"', context=_context)
if _token != '"fchk"':
NUM = self.NUM(_context)
first_num = NUM
BOOL_CMP = self._scan('BOOL_CMP', context=_context)
NUM = self.NUM(_context)
return [BOOL_CMP, first_num, NUM]
else: # == '"fchk"'
self._scan('"fchk"', context=_context)
STR = self.STR(_context)
return ["fchk", STR]
def LABEL(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'LABEL', [])
self._scan('"\\\\*"', context=_context)
NAME = self.NAME(_context)
return "*"+NAME
def NAME(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'NAME', [])
RAWNAME = self._scan('RAWNAME', context=_context)
return RAWNAME.lower()
def NUM(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'NUM', [])
_token = self._peek('RAWNUM', '"%"', context=_context)
if _token == 'RAWNUM':
RAWNUM = self._scan('RAWNUM', context=_context)
return int(RAWNUM)
else: # == '"%"'
NUMVAR = self.NUMVAR(_context)
return NUMVAR
def STR(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'STR', [])
_token = self._peek('RAWSTR', '"\\\\$"', context=_context)
if _token == 'RAWSTR':
RAWSTR = self._scan('RAWSTR', context=_context)
return RAWSTR
else: # == '"\\\\$"'
STRVAR = self.STRVAR(_context)
return STRVAR
def TEXT(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'TEXT', [])
TXTSTART = self._scan('TXTSTART', context=_context)
v = []
while self._peek('TXT', '"@"', '"\\\\\\\\"', 'END', context=_context) != 'END':
_token = self._peek('TXT', '"@"', '"\\\\\\\\"', context=_context)
if _token == 'TXT':
TXT = self._scan('TXT', context=_context)
v.append( ["text", [TXT]] )
else: # in ['"@"', '"\\\\\\\\"']
BREAK_CMD = self.BREAK_CMD(_context)
v.extend( [BREAK_CMD] )
return v+[ ["br", []] ]
def BREAK_CMD(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'BREAK_CMD', [])
_token = self._peek('"@"', '"\\\\\\\\"', context=_context)
if _token == '"@"':
self._scan('"@"', context=_context)
return ["click", []]
else: # == '"\\\\\\\\"'
self._scan('"\\\\\\\\"', context=_context)
return ["EOP", []]
def NUMVAR(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'NUMVAR', [])
self._scan('"%"', context=_context)
VARID = self.VARID(_context)
return ["%", VARID]
def STRVAR(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'STRVAR', [])
self._scan('"\\\\$"', context=_context)
VARID = self.VARID(_context)
return ["$", VARID]
def VARID(self, _parent=None):
_context = self.Context(_parent, self._scanner, 'VARID', [])
_token = self._peek('RAWNUM', 'RAWNAME', '"%"', context=_context)
if _token != 'RAWNAME':
NUM = self.NUM(_context)
return NUM
else: # == 'RAWNAME'
NAME = self.NAME(_context)
return NAME
def parse(rule, text):
P = Nscr(NscrScanner(text))
return runtime.wrap_error_reporter(P, rule)
if __name__ == '__main__':
from sys import argv, stdin
if len(argv) >= 2:
if len(argv) >= 3:
f = open(argv[2],'r')
else:
f = stdin
print parse(argv[1], f.read())
else: print >>sys.stderr, 'Args: <rule> [<filename>]'
# End -- grammar generated by Yapps