File: GroupParser.py

package info (click to toggle)
python-stringtemplate3 3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,392 kB
  • sloc: python: 8,819; makefile: 20
file content (599 lines) | stat: -rw-r--r-- 18,309 bytes parent folder | download | duplicates (2)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
### $ANTLR 2.7.7 (2006-11-01): "group.g" -> "GroupParser.py"$
### import antlr and other modules ..
import sys
import antlr

### header action >>> 
from ASTExpr import *
import stringtemplate3
import traceback
### header action <<< 
### preamble action>>>

### preamble action <<<

### import antlr.Token 
from antlr import Token
### >>>The Known Token Types <<<
SKIP                = antlr.SKIP
INVALID_TYPE        = antlr.INVALID_TYPE
EOF_TYPE            = antlr.EOF_TYPE
EOF                 = antlr.EOF
NULL_TREE_LOOKAHEAD = antlr.NULL_TREE_LOOKAHEAD
MIN_USER_TYPE       = antlr.MIN_USER_TYPE
LITERAL_group = 4
ID = 5
COLON = 6
LITERAL_implements = 7
COMMA = 8
SEMI = 9
AT = 10
DOT = 11
LPAREN = 12
RPAREN = 13
DEFINED_TO_BE = 14
STRING = 15
BIGSTRING = 16
ASSIGN = 17
ANONYMOUS_TEMPLATE = 18
LBRACK = 19
RBRACK = 20
LITERAL_default = 21
STAR = 22
PLUS = 23
OPTIONAL = 24
SL_COMMENT = 25
ML_COMMENT = 26
NL = 27
WS = 28


###/** Match a group of template definitions beginning
### *  with a group name declaration.  Templates are enclosed
### *  in double-quotes or <<...>> quotes for multi-line templates.
### *  Template names have arg lists that indicate the cardinality
### *  of the attribute: present, optional, zero-or-more, one-or-more.
### *  Here is a sample group file:
###
###        group nfa;
###
###        // an NFA has edges and states
###        nfa(states,edges) ::= <<
###        digraph NFA {
###        rankdir=LR;
###        <states; separator="\\n">
###        <edges; separator="\\n">
###        }
###        >>
###
###        state(name) ::= "node [shape = circle]; <name>;"
###
### */
class Parser(antlr.LLkParser):
    ### user action >>>
    def reportError(self, e):
       if self.group_:
           self.group_.error("template group parse error", e)
       else:
           sys.stderr.write("template group parse error: " + str(e) + '\n')
           traceback.print_exc()
    ### user action <<<
    
    def __init__(self, *args, **kwargs):
        antlr.LLkParser.__init__(self, *args, **kwargs)
        self.tokenNames = _tokenNames
        ### __init__ header action >>> 
        self.group_ = None
        ### __init__ header action <<< 
        
    def group(self,
        g
    ):    
        
        name = None
        s = None
        i = None
        i2 = None
        self.group_ = g
        try:      ## for error handling
            pass
            self.match(LITERAL_group)
            name = self.LT(1)
            self.match(ID)
            g.name = name.getText()
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [COLON]:
                pass
                self.match(COLON)
                s = self.LT(1)
                self.match(ID)
                g.superGroup = s.getText()
            elif la1 and la1 in [LITERAL_implements,SEMI]:
                pass
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [LITERAL_implements]:
                pass
                self.match(LITERAL_implements)
                i = self.LT(1)
                self.match(ID)
                g.implementInterface(i.getText())
                while True:
                    if (self.LA(1)==COMMA):
                        pass
                        self.match(COMMA)
                        i2 = self.LT(1)
                        self.match(ID)
                        g.implementInterface(i2.getText())
                    else:
                        break
                    
            elif la1 and la1 in [SEMI]:
                pass
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
            self.match(SEMI)
            while True:
                if (self.LA(1)==ID or self.LA(1)==AT) and (self.LA(2)==ID or self.LA(2)==LPAREN or self.LA(2)==DEFINED_TO_BE) and (self.LA(3)==ID or self.LA(3)==DOT or self.LA(3)==RPAREN):
                    pass
                    self.template(g)
                elif (self.LA(1)==ID) and (self.LA(2)==DEFINED_TO_BE) and (self.LA(3)==LBRACK):
                    pass
                    self.mapdef(g)
                else:
                    break
                
            self.match(EOF_TYPE)
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_0)
        
    
    def template(self,
        g
    ):    
        
        scope = None
        region = None
        name = None
        t = None
        bt = None
        alias = None
        target = None
        formalArgs = {}
        st = None
        ignore = False
        templateName = None
        line = self.LT(1).getLine()
        try:      ## for error handling
            if (self.LA(1)==ID or self.LA(1)==AT) and (self.LA(2)==ID or self.LA(2)==LPAREN):
                pass
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [AT]:
                    pass
                    self.match(AT)
                    scope = self.LT(1)
                    self.match(ID)
                    self.match(DOT)
                    region = self.LT(1)
                    self.match(ID)
                    templateName = g.getMangledRegionName(scope.getText(),region.getText())
                    if g.isDefinedInThisGroup(templateName):
                       g.error("group "+g.name+" line "+str(line)+": redefinition of template region: @"+
                           scope.getText()+"."+region.getText())
                       st = stringtemplate3.StringTemplate() # create bogus template to fill in
                    
                    else:
                       err = False
                       # @template.region() ::= "..."
                       scopeST = g.lookupTemplate(scope.getText())
                       if scopeST is None:
                           g.error("group "+g.name+" line "+str(line)+": reference to region within undefined template: "+
                               scope.getText())
                           err = True
                    
                       if not scopeST.containsRegionName(region.getText()):
                           g.error("group "+g.name+" line "+str(line)+": template "+scope.getText()+" has no region called "+
                               region.getText())
                           err = True
                    
                       if err:
                           st = stringtemplate3.StringTemplate()
                    
                       else:
                           st = g.defineRegionTemplate(
                               scope.getText(),
                               region.getText(),
                               None,
                               stringtemplate3.REGION_EXPLICIT
                           )
                elif la1 and la1 in [ID]:
                    pass
                    name = self.LT(1)
                    self.match(ID)
                    templateName = name.getText()
                    if g.isDefinedInThisGroup(templateName):
                       g.error("redefinition of template: " + templateName)
                       # create bogus template to fill in
                       st = stringtemplate3.StringTemplate()
                    else:
                       st = g.defineTemplate(templateName, None)
                else:
                        raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                    
                if st is not None:
                   st.groupFileLine = line
                self.match(LPAREN)
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [ID]:
                    pass
                    self.args(st)
                elif la1 and la1 in [RPAREN]:
                    pass
                    st.defineEmptyFormalArgumentList()
                else:
                        raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                    
                self.match(RPAREN)
                self.match(DEFINED_TO_BE)
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [STRING]:
                    pass
                    t = self.LT(1)
                    self.match(STRING)
                    st.template = t.getText()
                elif la1 and la1 in [BIGSTRING]:
                    pass
                    bt = self.LT(1)
                    self.match(BIGSTRING)
                    st.template = bt.getText()
                else:
                        raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                    
            elif (self.LA(1)==ID) and (self.LA(2)==DEFINED_TO_BE):
                pass
                alias = self.LT(1)
                self.match(ID)
                self.match(DEFINED_TO_BE)
                target = self.LT(1)
                self.match(ID)
                g.defineTemplateAlias(alias.getText(), target.getText())
            else:
                raise antlr.NoViableAltException(self.LT(1), self.getFilename())
            
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_1)
        
    
    def mapdef(self,
        g
    ):    
        
        name = None
        m = None
        try:      ## for error handling
            pass
            name = self.LT(1)
            self.match(ID)
            self.match(DEFINED_TO_BE)
            m=self.map()
            if g.getMap(name.getText()):
               g.error("redefinition of map: " + name.getText())
            elif g.isDefinedInThisGroup(name.getText()):
               g.error("redefinition of template as map: " + name.getText())
            else:
               g.defineMap(name.getText(), m)
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_1)
        
    
    def args(self,
        st
    ):    
        
        try:      ## for error handling
            pass
            self.arg(st)
            while True:
                if (self.LA(1)==COMMA):
                    pass
                    self.match(COMMA)
                    self.arg(st)
                else:
                    break
                
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_2)
        
    
    def arg(self,
        st
    ):    
        
        name = None
        s = None
        bs = None
        defaultValue = None
        try:      ## for error handling
            pass
            name = self.LT(1)
            self.match(ID)
            if (self.LA(1)==ASSIGN) and (self.LA(2)==STRING):
                pass
                self.match(ASSIGN)
                s = self.LT(1)
                self.match(STRING)
                defaultValue = stringtemplate3.StringTemplate(
                     template="$_val_$"
                     )
                defaultValue["_val_"] = s.getText()
                defaultValue.defineFormalArgument("_val_")
                defaultValue.name = ("<" + st.name + "'s arg " +
                                    name.getText() +
                                    " default value subtemplate>")
            elif (self.LA(1)==ASSIGN) and (self.LA(2)==ANONYMOUS_TEMPLATE):
                pass
                self.match(ASSIGN)
                bs = self.LT(1)
                self.match(ANONYMOUS_TEMPLATE)
                defaultValue = stringtemplate3.StringTemplate(
                     group=st.group,
                     template=bs.getText()
                     )
                defaultValue.name = ("<" + st.name + "'s arg " +
                                    name.getText() +
                                    " default value subtemplate>")
            elif (self.LA(1)==COMMA or self.LA(1)==RPAREN):
                pass
            else:
                raise antlr.NoViableAltException(self.LT(1), self.getFilename())
            
            st.defineFormalArgument(name.getText(), defaultValue)
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_3)
        
    
    def map(self):    
        mapping = {}
        
        try:      ## for error handling
            pass
            self.match(LBRACK)
            self.mapPairs(mapping)
            self.match(RBRACK)
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_1)
        
        return mapping
    
    def mapPairs(self,
        mapping
    ):    
        
        try:      ## for error handling
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [STRING]:
                pass
                self.keyValuePair(mapping)
                while True:
                    if (self.LA(1)==COMMA) and (self.LA(2)==STRING):
                        pass
                        self.match(COMMA)
                        self.keyValuePair(mapping)
                    else:
                        break
                    
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [COMMA]:
                    pass
                    self.match(COMMA)
                    self.defaultValuePair(mapping)
                elif la1 and la1 in [RBRACK]:
                    pass
                else:
                        raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                    
            elif la1 and la1 in [LITERAL_default]:
                pass
                self.defaultValuePair(mapping)
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_4)
        
    
    def keyValuePair(self,
        mapping
    ):    
        
        key = None
        try:      ## for error handling
            pass
            key = self.LT(1)
            self.match(STRING)
            self.match(COLON)
            v=self.keyValue()
            mapping[key.getText()] = v
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_5)
        
    
    def defaultValuePair(self,
        mapping
    ):    
        
        try:      ## for error handling
            pass
            self.match(LITERAL_default)
            self.match(COLON)
            v=self.keyValue()
            mapping[stringtemplate3.language.ASTExpr.DEFAULT_MAP_VALUE_NAME] = v
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_4)
        
    
    def keyValue(self):    
        value = None
        
        s1 = None
        s2 = None
        k = None
        try:      ## for error handling
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [STRING]:
                pass
                s1 = self.LT(1)
                self.match(STRING)
                value = stringtemplate3.StringTemplate(
                   group=self.group_, template=s1.getText()
                   )
            elif la1 and la1 in [BIGSTRING]:
                pass
                s2 = self.LT(1)
                self.match(BIGSTRING)
                value = stringtemplate3.StringTemplate(
                   group=self.group_,
                   template=s2.getText()
                   )
            elif la1 and la1 in [ID]:
                pass
                k = self.LT(1)
                self.match(ID)
                if not  k.getText() == "key" :
                    raise antlr.SemanticException(" k.getText() == \"key\" ")
                value = stringtemplate3.language.ASTExpr.MAP_KEY_VALUE
            elif la1 and la1 in [COMMA,RBRACK]:
                pass
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_5)
        
        return value
    

_tokenNames = [
    "<0>", 
    "EOF", 
    "<2>", 
    "NULL_TREE_LOOKAHEAD", 
    "\"group\"", 
    "ID", 
    "COLON", 
    "\"implements\"", 
    "COMMA", 
    "SEMI", 
    "AT", 
    "DOT", 
    "LPAREN", 
    "RPAREN", 
    "DEFINED_TO_BE", 
    "STRING", 
    "BIGSTRING", 
    "ASSIGN", 
    "ANONYMOUS_TEMPLATE", 
    "LBRACK", 
    "RBRACK", 
    "\"default\"", 
    "STAR", 
    "PLUS", 
    "OPTIONAL", 
    "SL_COMMENT", 
    "ML_COMMENT", 
    "NL", 
    "WS"
]
    

### generate bit set
def mk_tokenSet_0(): 
    ### var1
    data = [ 2, 0]
    return data
_tokenSet_0 = antlr.BitSet(mk_tokenSet_0())

### generate bit set
def mk_tokenSet_1(): 
    ### var1
    data = [ 1058, 0]
    return data
_tokenSet_1 = antlr.BitSet(mk_tokenSet_1())

### generate bit set
def mk_tokenSet_2(): 
    ### var1
    data = [ 8192, 0]
    return data
_tokenSet_2 = antlr.BitSet(mk_tokenSet_2())

### generate bit set
def mk_tokenSet_3(): 
    ### var1
    data = [ 8448, 0]
    return data
_tokenSet_3 = antlr.BitSet(mk_tokenSet_3())

### generate bit set
def mk_tokenSet_4(): 
    ### var1
    data = [ 1048576, 0]
    return data
_tokenSet_4 = antlr.BitSet(mk_tokenSet_4())

### generate bit set
def mk_tokenSet_5(): 
    ### var1
    data = [ 1048832, 0]
    return data
_tokenSet_5 = antlr.BitSet(mk_tokenSet_5())