ThirdParty/asttokens/asttokens/util.py

Tue, 09 Apr 2019 19:14:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 09 Apr 2019 19:14:11 +0200
changeset 6936
6b301c8a3c5b
parent 6929
0db8a4378ad7
permissions
-rw-r--r--

Removed the still existing six dependency of the asttokens package.

6929
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # Copyright 2016 Grist Labs, Inc.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2 #
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Licensed under the Apache License, Version 2.0 (the "License");
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # you may not use this file except in compliance with the License.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 # You may obtain a copy of the License at
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 #
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # http://www.apache.org/licenses/LICENSE-2.0
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 #
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # Unless required by applicable law or agreed to in writing, software
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 # distributed under the License is distributed on an "AS IS" BASIS,
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # See the License for the specific language governing permissions and
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # limitations under the License.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
6936
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
15 import sys
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
16 # define some compatibility methods like it is done in six.
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
17 if sys.version_info[0] == 2:
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
18 # Python 2
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
19 def iteritems(d):
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
20 return d.iteritems()
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
21 else:
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
22 # Python 3
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
23 def iteritems(d):
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
24 return iter(d.items())
6b301c8a3c5b Removed the still existing six dependency of the asttokens package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6929
diff changeset
25
6929
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 import ast
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 import collections
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 import token
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 def token_repr(tok_type, string):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """Returns a human-friendly representation of a token with the given type and string."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 # repr() prefixes unicode with 'u' on Python2 but not Python3; strip it out for consistency.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 return '%s:%s' % (token.tok_name[tok_type], repr(string).lstrip('u'))
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 class Token(collections.namedtuple('Token', 'type string start end line index startpos endpos')):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 TokenInfo is an 8-tuple containing the same 5 fields as the tokens produced by the tokenize
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 module, and 3 additional ones useful for this module:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 - [0] .type Token type (see token.py)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 - [1] .string Token (a string)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 - [2] .start Starting (row, column) indices of the token (a 2-tuple of ints)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 - [3] .end Ending (row, column) indices of the token (a 2-tuple of ints)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 - [4] .line Original line (string)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 - [5] .index Index of the token in the list of tokens that it belongs to.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 - [6] .startpos Starting character offset into the input text.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 - [7] .endpos Ending character offset into the input text.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 def __str__(self):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 return token_repr(self.type, self.string)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 def match_token(token, tok_type, tok_str=None):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 """Returns true if token is of the given type and, if a string is given, has that string."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 return token.type == tok_type and (tok_str is None or token.string == tok_str)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 def expect_token(token, tok_type, tok_str=None):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 Verifies that the given token is of the expected type. If tok_str is given, the token string
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 is verified too. If the token doesn't match, raises an informative ValueError.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 if not match_token(token, tok_type, tok_str):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 raise ValueError("Expected token %s, got %s on line %s col %s" % (
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 token_repr(tok_type, tok_str), str(token),
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 token.start[0], token.start[1] + 1))
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 # These were previously defined in tokenize.py and distinguishable by being greater than
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 # token.N_TOKEN. As of python3.7, they are in token.py, and we check for them explicitly.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 if hasattr(token, 'COMMENT'):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 def is_non_coding_token(token_type):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 These are considered non-coding tokens, as they don't affect the syntax tree.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 return token_type in (token.NL, token.COMMENT, token.ENCODING)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 else:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 def is_non_coding_token(token_type):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 These are considered non-coding tokens, as they don't affect the syntax tree.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 return token_type >= token.N_TOKENS
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 def iter_children(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 Yields all direct children of a AST node, skipping children that are singleton nodes.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 return iter_children_astroid(node) if hasattr(node, 'get_children') else iter_children_ast(node)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 def iter_children_func(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 Returns a slightly more optimized function to use in place of ``iter_children``, depending on
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 whether ``node`` is from ``ast`` or from the ``astroid`` module.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 return iter_children_astroid if hasattr(node, 'get_children') else iter_children_ast
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 def iter_children_astroid(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 # Don't attempt to process children of JoinedStr nodes, which we can't fully handle yet.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 if is_joined_str(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 return []
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 return node.get_children()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 SINGLETONS = {c for n, c in iteritems(ast.__dict__) if isinstance(c, type) and
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 issubclass(c, (ast.expr_context, ast.boolop, ast.operator, ast.unaryop, ast.cmpop))}
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 def iter_children_ast(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 # Don't attempt to process children of JoinedStr nodes, which we can't fully handle yet.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 if is_joined_str(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 return
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 for child in ast.iter_child_nodes(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 # Skip singleton children; they don't reflect particular positions in the code and break the
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 # assumptions about the tree consisting of distinct nodes. Note that collecting classes
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 # beforehand and checking them in a set is faster than using isinstance each time.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 if child.__class__ not in SINGLETONS:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 yield child
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 stmt_class_names = {n for n, c in iteritems(ast.__dict__)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 if isinstance(c, type) and issubclass(c, ast.stmt)}
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 expr_class_names = ({n for n, c in iteritems(ast.__dict__)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 if isinstance(c, type) and issubclass(c, ast.expr)} |
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 {'AssignName', 'DelName', 'Const', 'AssignAttr', 'DelAttr'})
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 # These feel hacky compared to isinstance() but allow us to work with both ast and astroid nodes
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 # in the same way, and without even importing astroid.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 def is_expr(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 """Returns whether node is an expression node."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 return node.__class__.__name__ in expr_class_names
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 def is_stmt(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 """Returns whether node is a statement node."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 return node.__class__.__name__ in stmt_class_names
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 def is_module(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """Returns whether node is a module node."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 return node.__class__.__name__ == 'Module'
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 def is_joined_str(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 """Returns whether node is a JoinedStr node, used to represent f-strings."""
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 # At the moment, nodes below JoinedStr have wrong line/col info, and trying to process them only
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 # leads to errors.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 return node.__class__.__name__ == 'JoinedStr'
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 # Sentinel value used by visit_tree().
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 _PREVISIT = object()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 def visit_tree(node, previsit, postvisit):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 Scans the tree under the node depth-first using an explicit stack. It avoids implicit recursion
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 via the function call stack to avoid hitting 'maximum recursion depth exceeded' error.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 It calls ``previsit()`` and ``postvisit()`` as follows:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 * ``previsit(node, par_value)`` - should return ``(par_value, value)``
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 ``par_value`` is as returned from ``previsit()`` of the parent.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 * ``postvisit(node, par_value, value)`` - should return ``value``
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 ``par_value`` is as returned from ``previsit()`` of the parent, and ``value`` is as
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 returned from ``previsit()`` of this node itself. The return ``value`` is ignored except
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 the one for the root node, which is returned from the overall ``visit_tree()`` call.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 For the initial node, ``par_value`` is None. Either ``previsit`` and ``postvisit`` may be None.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 if not previsit:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 previsit = lambda node, pvalue: (None, None)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 if not postvisit:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 postvisit = lambda node, pvalue, value: None
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 iter_children = iter_children_func(node)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 done = set()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 ret = None
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 stack = [(node, None, _PREVISIT)]
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 while stack:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 current, par_value, value = stack.pop()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 if value is _PREVISIT:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 assert current not in done # protect againt infinite loop in case of a bad tree.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 done.add(current)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 pvalue, post_value = previsit(current, par_value)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 stack.append((current, par_value, post_value))
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 # Insert all children in reverse order (so that first child ends up on top of the stack).
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 ins = len(stack)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 for n in iter_children(current):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 stack.insert(ins, (n, pvalue, _PREVISIT))
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 else:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 ret = postvisit(current, par_value, value)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 return ret
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 def walk(node):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 Recursively yield all descendant nodes in the tree starting at ``node`` (including ``node``
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 itself), using depth-first pre-order traversal (yieling parents before their children).
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 This is similar to ``ast.walk()``, but with a different order, and it works for both ``ast`` and
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 ``astroid`` trees. Also, as ``iter_children()``, it skips singleton nodes generated by ``ast``.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 iter_children = iter_children_func(node)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 done = set()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 stack = [node]
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 while stack:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 current = stack.pop()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 assert current not in done # protect againt infinite loop in case of a bad tree.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 done.add(current)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 yield current
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 # Insert all children in reverse order (so that first child ends up on top of the stack).
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 # This is faster than building a list and reversing it.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 ins = len(stack)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 for c in iter_children(current):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 stack.insert(ins, c)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 def replace(text, replacements):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 Replaces multiple slices of text with new values. This is a convenience method for making code
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 modifications of ranges e.g. as identified by ``ASTTokens.get_text_range(node)``. Replacements is
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 an iterable of ``(start, end, new_text)`` tuples.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 For example, ``replace("this is a test", [(0, 4, "X"), (8, 1, "THE")])`` produces
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 ``"X is THE test"``.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 p = 0
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 parts = []
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 for (start, end, new_text) in sorted(replacements):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 parts.append(text[p:start])
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 parts.append(new_text)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 p = end
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 parts.append(text[p:])
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240 return ''.join(parts)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 class NodeMethods(object):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 Helper to get `visit_{node_type}` methods given a node's class and cache the results.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 def __init__(self):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 self._cache = {}
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 def get(self, obj, cls):
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 Using the lowercase name of the class as node_type, returns `obj.visit_{node_type}`,
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 or `obj.visit_default` if the type-specific method is not found.
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 """
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 method = self._cache.get(cls)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 if not method:
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 name = "visit_" + cls.__name__.lower()
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 method = getattr(obj, name, obj.visit_default)
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 self._cache[cls] = method
0db8a4378ad7 ThirdParty: added the asttoken package with slight modifications
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 return method

eric ide

mercurial