File: InterfaceParser.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 (237 lines) | stat: -rw-r--r-- 6,815 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
### $ANTLR 2.7.7 (2006-11-01): "interface.g" -> "InterfaceParser.py"$
### import antlr and other modules ..
import sys
import antlr

### header action >>> 
#
# [The "BSD licence"]
# Copyright (c) 2003-2004 Terence Parr
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 


import sys
import traceback

from stringtemplate3.language.FormalArgument import FormalArgument
### 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_interface = 4
ID = 5
SEMI = 6
LITERAL_optional = 7
LPAREN = 8
RPAREN = 9
COMMA = 10
COLON = 11
SL_COMMENT = 12
ML_COMMENT = 13
WS = 14


###/** Match an ST group interface.  Just a list of template names with args.
### *  Here is a sample interface file:
### *
### *	interface nfa;
### *	nfa(states,edges);
### *	optional state(name);
### */
class Parser(antlr.LLkParser):
    ### user action >>>
    def reportError(self, e):
       if self.group_:
           self.group_.error("template group interface parse error", e)
       else:
           sys.stderr.write("template group interface 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.groupI = None
        ### __init__ header action <<< 
        
    def groupInterface(self,
        groupI
    ):    
        
        name = None
        self.groupI = groupI
        try:      ## for error handling
            pass
            self.match(LITERAL_interface)
            name = self.LT(1)
            self.match(ID)
            groupI.name = name.getText()
            self.match(SEMI)
            _cnt3= 0
            while True:
                if (self.LA(1)==ID or self.LA(1)==LITERAL_optional):
                    pass
                    self.template(groupI)
                else:
                    break
                
                _cnt3 += 1
            if _cnt3 < 1:
                raise antlr.NoViableAltException(self.LT(1), self.getFilename())
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_0)
        
    
    def template(self,
        groupI
    ):    
        
        opt = None
        name = None
        formalArgs = {} # leave blank if no args
        templateName = None
        try:      ## for error handling
            pass
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [LITERAL_optional]:
                pass
                opt = self.LT(1)
                self.match(LITERAL_optional)
            elif la1 and la1 in [ID]:
                pass
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
            name = self.LT(1)
            self.match(ID)
            self.match(LPAREN)
            la1 = self.LA(1)
            if False:
                pass
            elif la1 and la1 in [ID]:
                pass
                formalArgs=self.args()
            elif la1 and la1 in [RPAREN]:
                pass
            else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())
                
            self.match(RPAREN)
            self.match(SEMI)
            templateName = name.getText()
            groupI.defineTemplate(templateName, formalArgs, opt != None)
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_1)
        
    
    def args(self):    
        args={}
        
        a = None
        b = None
        try:      ## for error handling
            pass
            a = self.LT(1)
            self.match(ID)
            args[a.getText()] = FormalArgument(a.getText())
            while True:
                if (self.LA(1)==COMMA):
                    pass
                    self.match(COMMA)
                    b = self.LT(1)
                    self.match(ID)
                    args[b.getText()] = FormalArgument(b.getText())
                else:
                    break
                
        
        except antlr.RecognitionException as ex:
            self.reportError(ex)
            self.consume()
            self.consumeUntil(_tokenSet_2)
        
        return args
    

_tokenNames = [
    "<0>", 
    "EOF", 
    "<2>", 
    "NULL_TREE_LOOKAHEAD", 
    "\"interface\"", 
    "ID", 
    "SEMI", 
    "\"optional\"", 
    "LPAREN", 
    "RPAREN", 
    "COMMA", 
    "COLON", 
    "SL_COMMENT", 
    "ML_COMMENT", 
    "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 = [ 162, 0]
    return data
_tokenSet_1 = antlr.BitSet(mk_tokenSet_1())

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