|
9 | 9 | # This class is used to create the single `token` instance in this |
10 | 10 | # module. It is used to map between parser tokens and their ids and |
11 | 11 | # vice-versa. |
12 | | -# |
13 | | -# * `TreeAdaptor` |
14 | | -# |
15 | | -# This class is used by `java2python.compiler.tool`, where the |
16 | | -# `buildAST` function associates an instance of it to a parser. The |
17 | | -# `TreeAdaptor` class creates `LocalTree` instances. |
18 | | -# |
19 | | -# * `LocalTree` |
20 | | -# |
21 | | -# This class provides a slew of extra utility methods that are useful |
22 | | -# when inspecting and printing tree nodes. |
23 | | -# |
24 | 12 |
|
25 | 13 |
|
26 | 14 | # ANTLR notes: |
|
49 | 37 |
|
50 | 38 | from io import StringIO |
51 | 39 |
|
52 | | -from antlr3 import ANTLRStringStream as StringStream, CommonTokenStream as TokenStream |
53 | | -from antlr3.tree import CommonTreeAdaptor, CommonTree |
| 40 | +from antlr4 import CommonTokenStream as TokenStream |
54 | 41 |
|
55 | 42 | from java2python.lib import colors |
56 | 43 |
|
@@ -111,159 +98,3 @@ def title(name): |
111 | 98 |
|
112 | 99 | ## sometimes you really do only need one. |
113 | 100 | tokens = Tokens() |
114 | | - |
115 | | - |
116 | | -class TreeAdaptor(CommonTreeAdaptor): |
117 | | - """ TreeAdaptor -> defered tree node creator (for parsers). """ |
118 | | - |
119 | | - def __init__(self, lexer, parser): |
120 | | - # CommonTreeAdaptor doesn't need to be __init__'ed |
121 | | - self.lexer, self.parser = lexer, parser |
122 | | - |
123 | | - def createWithPayload(self, payload): |
124 | | - """ Returns a new tree for the calling parser. """ |
125 | | - return LocalTree(payload, self.lexer, self.parser) |
126 | | - |
127 | | - |
128 | | -class LocalTree(CommonTree): |
129 | | - """ LocalTree -> like CommonTree, but with more stuff. """ |
130 | | - colorTypeMap = { |
131 | | - 'CLASS' : colors.green, |
132 | | - 'JAVA_SOURCE' : colors.green, |
133 | | - 'VOID_METHOD_DECL' : colors.green, |
134 | | - 'IDENT' : colors.yellow, |
135 | | - 'TYPE' : colors.magenta, |
136 | | - 'EXPR' : colors.blue, |
137 | | - 'TRUE' : colors.yellow, |
138 | | - 'FALSE' : colors.yellow, |
139 | | - 'NULL' : colors.yellow, |
140 | | - } |
141 | | - |
142 | | - def __init__(self, payload, lexer=None, parser=None): |
143 | | - super(LocalTree, self).__init__(payload) |
144 | | - self.lexer, self.parser = lexer, parser |
145 | | - |
146 | | - def childrenOfType(self, type): |
147 | | - """ Returns a generator yielding children of this tree of the given type. """ |
148 | | - return (c for c in self.children if c.type==type) |
149 | | - |
150 | | - def colorType(self, tokenType): |
151 | | - """ Returns a color suitable for the given token type. """ |
152 | | - return self.colorTypeMap.get(tokenType, colors.white)(tokenType) |
153 | | - |
154 | | - def colorText(self, tokenType, tokenText): |
155 | | - """ Returns a colorized string from the given token type and text. """ |
156 | | - return self.colorTypeMap.get(tokenType, colors.white)(tokenText) |
157 | | - |
158 | | - def colorComments(self, token): |
159 | | - """ Formats, colors, and returns the comment text from the given token. """ |
160 | | - ttyp = tokens.map.get(token.type) |
161 | | - text = token.text.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t') |
162 | | - item = '{0} [{1}:{2}] {3}'.format(ttyp, token.start, token.stop, text) |
163 | | - yield colors.black(item) |
164 | | - |
165 | | - def dump(self, fd, level=0): |
166 | | - """ Writes a debug representation of this tree to the given file. """ |
167 | | - extras = lambda x, y:x and (x != y) |
168 | | - seen, nform = set(), '{0}{1}{2}{3}' |
169 | | - def innerDump(root, offset): |
170 | | - token, indent = root.token, ' ' * offset |
171 | | - start, stop = root.tokenStartIndex, root.tokenStopIndex |
172 | | - idxes, ttyp = '', tokens.map.get(token.type, '?') |
173 | | - line = token.line |
174 | | - if start and stop and start == stop: |
175 | | - idxes = 'start={}'.format(start) |
176 | | - elif start and stop: |
177 | | - idxes = 'start={}, stop={}'.format(start, stop) |
178 | | - if line: |
179 | | - idxes = 'line={}{}{}'.format(line, ', ' if idxes else '', idxes) |
180 | | - idxes = ' [{}]'.format(idxes) if idxes else '' |
181 | | - idxes = colors.black(idxes) |
182 | | - args = [indent, self.colorType(ttyp), '', idxes, ''] |
183 | | - if extras(token.text, ttyp): |
184 | | - args[2] = ' ' + self.colorText(ttyp, token.text) |
185 | | - for com in self.selectComments(start, seen): |
186 | | - for line in self.colorComments(com): |
187 | | - print('{0}{1}'.format(indent, line), file=fd) |
188 | | - print(nform.format(*args), file=fd) |
189 | | - for child in root.getChildren(): |
190 | | - innerDump(child, offset+1) |
191 | | - for com in self.selectComments(root.tokenStopIndex, seen): |
192 | | - for line in self.colorComments(com): |
193 | | - print('{0}{1}'.format(indent, line), file=fd) |
194 | | - innerDump(self, level) |
195 | | - |
196 | | - def dumps(self, level=0): |
197 | | - """ Dump this token to a string. """ |
198 | | - fd = StringIO() |
199 | | - self.dump(fd, level) |
200 | | - return fd.getvalue() |
201 | | - |
202 | | - def dupNode(self): |
203 | | - """ Called by the parser to create a duplicate of this tree. """ |
204 | | - get = lambda v:getattr(self, v, None) |
205 | | - return LocalTree(self, get('lexer'), get('parser')) |
206 | | - |
207 | | - def findChildren(self, pred=lambda c:True): |
208 | | - """ Depth-first search that yields nodes meeting the predicate. """ |
209 | | - for child in self.children: |
210 | | - if pred(child): |
211 | | - yield child |
212 | | - for sub in child.findChildren(pred): |
213 | | - yield sub |
214 | | - |
215 | | - def findChildrenOfType(self, type): |
216 | | - """ Depth-first search that yields nodes of the given type. """ |
217 | | - return self.findChildren(lambda c:c.type==type) |
218 | | - |
219 | | - def firstChild(self, default=None): |
220 | | - """ Returns the first child of this tree or the default. """ |
221 | | - try: |
222 | | - return self.children[0] |
223 | | - except (IndexError, ): |
224 | | - return default |
225 | | - |
226 | | - def firstChildOfType(self, type, default=None): |
227 | | - """ Returns the first child of this tree that matches the given type. """ |
228 | | - for child in self.children: |
229 | | - if child.type == type: |
230 | | - return child |
231 | | - return default |
232 | | - |
233 | | - @property |
234 | | - def isJavaSource(self): |
235 | | - """ True if this tree is the outer most type. """ |
236 | | - return self.token.type == tokens.JAVA_SOURCE |
237 | | - |
238 | | - @property |
239 | | - def parentType(self): |
240 | | - """ Returns the type of the parent tree. """ |
241 | | - return self.parent.type |
242 | | - |
243 | | - def parents(self, pred=lambda v:True): |
244 | | - """ Yield each parent in the family tree. """ |
245 | | - while self: |
246 | | - if pred(self): |
247 | | - yield self |
248 | | - self = self.parent |
249 | | - |
250 | | - @property |
251 | | - def parserTokens(self): |
252 | | - """ Returns the sequence of tokens used to create this tree. """ |
253 | | - return self.parser.input.tokens[self.tokenStartIndex:self.tokenStopIndex] |
254 | | - |
255 | | - def selectComments(self, stop, memo): |
256 | | - """ Returns the comment tokens for this tree up to the given index. """ |
257 | | - pred = lambda k:k.type in tokens.commentTypes and k.index not in memo |
258 | | - ctoks = [t for t in self.parser.input.tokens[0:stop] if pred(t)] |
259 | | - memo.update(t.index for t in ctoks) |
260 | | - return ctoks |
261 | | - |
262 | | - @property |
263 | | - def withinExpr(self): |
264 | | - """ True if this tree is contained within an expression. """ |
265 | | - self = getattr(self.parent, 'parent', None) # skip first expr |
266 | | - while self: |
267 | | - if self.type in (tokens.EXPR, ): |
268 | | - return True |
269 | | - self = self.parent |
0 commit comments