File: TemplateParser.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 (286 lines) | stat: -rw-r--r-- 9,738 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
### $ANTLR 2.7.7 (2006-11-01): "template.g" -> "TemplateParser.py"$
### import antlr and other modules ..
import sys
import antlr

### header action >>> 
import stringtemplate3
from stringtemplate3.language.ChunkToken import ChunkToken
from stringtemplate3.language.StringRef import StringRef
from stringtemplate3.language.NewlineRef import NewlineRef
### 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 = 4
NEWLINE = 5
ACTION = 6
IF = 7
ELSEIF = 8
ELSE = 9
ENDIF = 10
REGION_REF = 11
REGION_DEF = 12
NL = 13
EXPR = 14
TEMPLATE = 15
IF_EXPR = 16
ESC_CHAR = 17
ESC = 18
HEX = 19
SUBTEMPLATE = 20
NESTED_PARENS = 21
INDENT = 22
COMMENT = 23


###/** A parser used to break up a single template into chunks, text literals
### *  and attribute expressions.
### */
class Parser(antlr.LLkParser):
    ### user action >>>
    def reportError(self, e):
       group = self.this.group
       if group == stringtemplate3.StringTemplate.defaultGroup:
           self.this.error("template parse error; template context is "+self.this.enclosingInstanceStackString, e)
    
       else:
           self.this.error("template parse error in group "+self.this.group.name+" line "+str(self.this.groupFileLine)+"; template context is "+self.this.enclosingInstanceStackString, e)
    ### user action <<<
    
    def __init__(self, *args, **kwargs):
        antlr.LLkParser.__init__(self, *args, **kwargs)
        self.tokenNames = _tokenNames
        ### __init__ header action >>> 
        self.this = None
        ### __init__ header action <<< 
        
    def template(self,
        this
    ):    
        
        s = None
        nl = None
        try:      ## for error handling
            pass
            while True:
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [LITERAL]:
                    pass
                    s = self.LT(1)
                    self.match(LITERAL)
                    this.addChunk(StringRef(this,s.getText()))
                elif la1 and la1 in [NEWLINE]:
                    pass
                    nl = self.LT(1)
                    self.match(NEWLINE)
                    if self.LA(1) != ELSE and self.LA(1) != ENDIF:
                       this.addChunk(NewlineRef(this,nl.getText()))
                elif la1 and la1 in [ACTION,IF,REGION_REF,REGION_DEF]:
                    pass
                    self.action(this)
                else:
                        break
                    
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_0)
        
    
    def action(self,
        this
    ):    
        
        a = None
        i = None
        ei = None
        rr = None
        rd = None
        try:      ## for error handling
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [ACTION]:
                pass
                a = self.LT(1)
                self.match(ACTION)
                indent = a.indentation
                c = this.parseAction(a.getText())
                c.indentation = indent
                this.addChunk(c)
            elif la1 and la1 in [IF]:
                pass
                i = self.LT(1)
                self.match(IF)
                c = this.parseAction(i.getText())
                # create and precompile the subtemplate
                subtemplate = stringtemplate3.StringTemplate(group=this.group)
                subtemplate.enclosingInstance = this
                subtemplate.name = i.getText() + "_subtemplate"
                this.addChunk(c)
                self.template(subtemplate)
                if c: c.subtemplate = subtemplate
                while True:
                    if (self.LA(1)==ELSEIF):
                        pass
                        ei = self.LT(1)
                        self.match(ELSEIF)
                        ec = this.parseAction(ei.getText())
                        # create and precompile the subtemplate
                        elseIfSubtemplate = stringtemplate3.StringTemplate(group=this.group)
                        elseIfSubtemplate.enclosingInstance = this
                        elseIfSubtemplate.name = ei.getText()+"_subtemplate"
                        self.template(elseIfSubtemplate)
                        if c is not None:
                           c.addElseIfSubtemplate(ec, elseIfSubtemplate)
                    else:
                        break
                    
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [ELSE]:
                    pass
                    self.match(ELSE)
                    # create and precompile the subtemplate
                    elseSubtemplate = stringtemplate3.StringTemplate(group=this.group)
                    elseSubtemplate.enclosingInstance = this
                    elseSubtemplate.name = "else_subtemplate"
                    self.template(elseSubtemplate)
                    if c: c.elseSubtemplate = elseSubtemplate
                elif la1 and la1 in [ENDIF]:
                    pass
                else:
                        raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                    
                self.match(ENDIF)
            elif la1 and la1 in [REGION_REF]:
                pass
                rr = self.LT(1)
                self.match(REGION_REF)
                # define implicit template and
                # convert <@r()> to <region__enclosingTemplate__r()>
                regionName = rr.getText()
                mangledRef = None
                err = False
                # watch out for <@super.r()>; that does NOT def implicit region
                # convert to <super.region__enclosingTemplate__r()>
                if regionName.startswith("super."):
                   #System.out.println("super region ref "+regionName);
                   regionRef = regionName[len("super."):len(regionName)]
                   templateScope = this.group.getUnMangledTemplateName(this.name)
                   scopeST = this.group.lookupTemplate(templateScope)
                   if scopeST is None:
                       this.group.error("reference to region within undefined template: "+
                           templateScope)
                       err = True
                
                   if not scopeST.containsRegionName(regionRef):
                       this.group.error("template "+templateScope+" has no region called "+
                           regionRef)
                       err = True
                
                   else:
                       mangledRef = this.group.getMangledRegionName(templateScope, regionRef)
                       mangledRef = "super." + mangledRef
                
                else:
                   regionST = this.group.defineImplicitRegionTemplate(this, regionName)
                   mangledRef = regionST.name
                
                if not err:
                   # treat as regular action: mangled template include
                   indent = rr.indentation
                   c = this.parseAction(mangledRef+"()")
                   c.indentation = indent
                   this.addChunk(c)
            elif la1 and la1 in [REGION_DEF]:
                pass
                rd = self.LT(1)
                self.match(REGION_DEF)
                combinedNameTemplateStr = rd.getText()
                indexOfDefSymbol = combinedNameTemplateStr.find("::=")
                if indexOfDefSymbol >= 1:
                   regionName = combinedNameTemplateStr[0:indexOfDefSymbol]
                   template = combinedNameTemplateStr[indexOfDefSymbol+3:len(combinedNameTemplateStr)]
                   regionST = this.group.defineRegionTemplate(
                       this,
                       regionName,
                       template,
                       stringtemplate3.REGION_EMBEDDED
                       )
                   # treat as regular action: mangled template include
                   indent = rd.indentation
                   c = this.parseAction(regionST.name + "()")
                   c.indentation = indent
                   this.addChunk(c)
                
                else:
                   this.error("embedded region definition screwed up")
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_1)
        
    

_tokenNames = [
    "<0>", 
    "EOF", 
    "<2>", 
    "NULL_TREE_LOOKAHEAD", 
    "LITERAL", 
    "NEWLINE", 
    "ACTION", 
    "IF", 
    "ELSEIF", 
    "ELSE", 
    "ENDIF", 
    "REGION_REF", 
    "REGION_DEF", 
    "NL", 
    "EXPR", 
    "TEMPLATE", 
    "IF_EXPR", 
    "ESC_CHAR", 
    "ESC", 
    "HEX", 
    "SUBTEMPLATE", 
    "NESTED_PARENS", 
    "INDENT", 
    "COMMENT"
]
    

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

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