-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpparser.py
409 lines (313 loc) · 9.62 KB
/
pparser.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# pparser.py
#
# (c)1999-2000 Philip Hunt
# Released under the GNU General Public Licence
#
# Parser for Parrot *.par files.
# This is used to scan Parrot files and put them into a
# representation in the form of instances of the classes
# defined in inrep.py.
#
# (Uses generic.py, which is John Aycock's generic parsing
# library)
#
# Last altered: 12-Feb-2000
# History:
# 13-Aug-1999 PhilHunt: created
#
# 18-Aug-1999 PhilHunt: 1st working version!
#
# 7-Sep-1999 id, text, normal attributes in any order;
# added comments.
from tokpos import *
from string import *
from inrep import *
debug = 0
#===========================================================
###### Debugging ######
#===========================================================
def sayWhere(info):
if debug:
import traceback
fromFun = traceback.extract_stack()[-2]
print ">>> %s() %s" %(fromFun[2], info)
#===========================================================
###### Lexical Analysis ######
#===========================================================
class ScannerNoRW(PosScanner):
def __init__(self):
GenericScanner.__init__(self)
def tokenize(self, input):
self.rv = [ ]
PosScanner.tokenize(self, input)
return self.rv
# later, add to my ScannerWithLineNumbers class
#def addToken(self, tokenType, tokenValue):
# self.rv.append(Token(tokenType, tokenValue,
# self.rowNum, self.colNum))
def t_whitespace(self, s):
r' \s+ '
pass
def t_comment1(self, s):
r' //[^\n]*\n '
#print "got comment1 <<<<{%s}>>>>" % s
pass
def t_comment2(self, s):
r' /\*(.|\n)*?\*/ '
#print "got comment2 <<<<{%s}>>>>" % s
pass
def t_ATSIGN(self, s):
r' @ '
self.addToken('@', None)
def t_EQUALS(self, s):
r' = '
self.addToken('=', None)
def t_DOT(self, s):
r' \. '
self.addToken('DOT', None)
def t_LCURLY(self, s):
r' \{ '
self.addToken('{', None)
def t_RCURLY(self, s):
r' \} '
self.addToken('}', None)
def t_STRING(self, s):
r' \"[^"]*\" '
self.addToken('STRING',s[1:-1])
def t_IDENTIFIER(self, s):
r' [A-Za-z_][A-Za-z_0-9]* '
self.addToken('IDENTIFIER', s)
def t_NUMBER(self, s):
r' -?[0-9]+ '
#t = Token(type='NUMBER', attr=s)
self.addToken('NUMBER', atoi(s))
#-----------------------------------------------------------
# extend scanner to understand reserved words:
class ParrotScanner(ScannerNoRW):
def __init__(self):
ScannerNoRW.__init__(self)
def t_SUB(self, s):
r' sub '
self.addToken('SUB', None)
def t_CSUB(self, s):
r' csub '
self.addToken('CSUB', None)
def t_FOR(self, s):
r' for '
self.addToken('FOR', None)
#===========================================================
###### Parsing ######
#===========================================================
class ParrotParser(GenericParser):
def __init__(self, sourceText, start='compositionFile'):
self.sourceText = sourceText
GenericParser.__init__(self, start)
self.gi = GlobalInfo()
def parse(self, tokenList):
self.tokenList = tokenList
GenericParser.parse(self, tokenList)
def p_compositionFile_1(self, args):
' compositionFile ::= compositionFile component '
sayWhere(args)
self.gi.addComponent(args[1])
args[1].parent = self.gi
def p_compositionFile_2(self, args):
' compositionFile ::= component '
sayWhere(args)
self.gi.addComponent(args[0])
args[0].parent = self.gi
return [args[0],]
def p_component(self, args):
' component ::= type attributes subComponents_opt '
sayWhere(args)
if type(args[2]) == type([]):
newComp = UIContainer(args[0], None)
isContainer = 1
else:
newComp = UIComponent(args[0], None)
isContainer = 0
if args[1]:
for attrNameValue in args[1]:
n = attrNameValue[0]
if n == '*sub':
if isContainer:
newComp.subAttr[attrNameValue[1]] = attrNameValue[2]
elif n == '*csub':
if isContainer:
newComp.subAttr[attrNameValue[1]] = attrNameValue[2]
newComp.attr[attrNameValue[1]] = attrNameValue[2]
elif n == '*for':
if isContainer:
attrForType = attrNameValue[1]
attrName = attrNameValue[2]
attrValue = attrNameValue[3]
newComp.addTypeWideAttr(attrForType, attrName, attrValue)
else:
#>>>>> it's a normal attribute
newComp.attr[n] = attrNameValue[1]
if args[2]:
for subComponent in args[2]:
subComponent.parent = newComp
newComp.addComponent(subComponent)
return newComp
def p_subComponents0(self, args):
' subComponents_opt ::= '
sayWhere(args)
return None
def p_subComponents1(self, args):
' subComponents_opt ::= { components } '
sayWhere(args)
return args[1]
def p_components0(self, args):
' components ::= components component '
sayWhere(args)
if type(args[0]) == type([]):
# it's already a list
componentList = args[0]
else:
componentList = [ ]
if debug: print 'componentList=', componentList
componentList.append(args[1])
return componentList
def p_components1(self, args):
' components ::= '
sayWhere(args)
return None
def p_type(self, args):
' type ::= IDENTIFIER '
sayWhere(args)
return args[0].attr
def p_componentId_opt(self, args):
'''
componentId_opt ::= @ IDENTIFIER
componentId_opt ::=
'''
sayWhere(args)
if len(args) >= 2:
return args[1].attr
else:
return None
def p_str_opt(self, args):
'''
str_opt ::= STRING
str_opt ::=
'''
sayWhere(args)
if len(args) >= 1:
return args[0].attr
else:
return None
def p_attributes(self, args):
'''
attributes ::= attributes attribute
attributes ::=
'''
sayWhere(args)
if len(args) >= 2:
attrList = args[0]
if type(attrList) != type([]):
attrList = [ ]
attrList.append(args[1])
return attrList
else:
return None
def p_attribute1(self, args):
' attribute ::= IDENTIFIER = value '
sayWhere(args)
return (args[0].attr, args[2])
def p_attribute2(self, args):
' attribute ::= STRING '
sayWhere(args)
return ('text', args[0].attr)
def p_attribute3(self, args):
' attribute ::= @ IDENTIFIER '
sayWhere(args)
return ('id', args[1].attr)
def p_attribute4(self, args):
' attribute ::= SUB DOT IDENTIFIER = value '
sayWhere(args)
return ('*sub', args[2].attr, args[4])
def p_attribute5(self, args):
' attribute ::= CSUB DOT IDENTIFIER = value '
sayWhere(args)
return ('*csub', args[2].attr, args[4])
def p_attribute6(self, args):
' attribute ::= FOR DOT IDENTIFIER DOT IDENTIFIER = value '
sayWhere(args)
return ('*for', args[2].attr, args[4].attr, args[6])
def p_value1(self, args):
' value ::= IDENTIFIER '
sayWhere(args)
return args[0].attr
def p_value2(self, args):
' value ::= STRING '
sayWhere(args)
return args[0].attr
def p_value3(self, args):
' value ::= NUMBER '
sayWhere(args)
return repr(args[0].attr)
# process error messages
def error(self, token):
lines = split(self.sourceText, '\n')
print "!!! Syntax error near line %d, col %d: !!!" % (
token.line, token.col)
# we need to subtract 1 from (token.line), because the
# user-visible way of counting lines starts from 1, but python
# internally starts from 0.
print lines[token.line - 1]
print ('-' * (token.col-1)) + '^'
raise Exception, "fatal error"
#===========================================================
#-----------------------------------------------------------
# module testing:
def par2_test1():
# test lexical analyser
print '-----test1-----'
s = ' fgdgsdg = 34 @ { eee } "hell" = 34 @ { eee } '
scanner = ParrotScanner()
res = scanner.tokenize(s)
for tok in res:
print tok.type,
if tok.attr != None:
print tok.attr
else:
print
def par2_test2():
# test parser
print '----- test 2 -----'
s = '''
button @Button1 "Press me!" fontSize=12
'''
s = '''
rowLayout border=2 borderStyle=embossed
sub.bcol=blue csub.fcol=red
for.button.tooltip="A button!"
{
button @Button1 "Press me!" fontSize=12
//label fcol="#00ff00" "My label" @label1
}
'''
### label fcol="#00ff00" "My label" @label1
scanner = ParrotScanner()
res = scanner.tokenize(s)
print '----- test 2: lexical analysis: -----'
for tok in res:
print tok.type,
if tok.attr != None:
print tok.attr
else:
print
print '----- test 2: parsing: -----'
parser = ParrotParser(s)
print 'res=', res
parser.parse(res)
print '----- test 2: result of parsing: -----'
parser.gi.outputNormalized('')
if __name__=='__main__':
#par2_test1()
par2_test2()
#par2_test3()
#ex2()
#-----------------------------------------------------------
#end