]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | # Copyright 2022 Bill Wendling, All Rights Reserved. |
2 | # | |
3 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | # you may not use this file except in compliance with the License. | |
5 | # You may obtain a copy of the License at | |
6 | # | |
7 | # http://www.apache.org/licenses/LICENSE-2.0 | |
8 | # | |
9 | # Unless required by applicable law or agreed to in writing, software | |
10 | # distributed under the License is distributed on an "AS IS" BASIS, | |
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | # See the License for the specific language governing permissions and | |
13 | # limitations under the License. | |
14 | """PyParser-related utilities. | |
15 | ||
16 | This module collects various utilities related to the parse trees produced by | |
17 | the pyparser. | |
18 | ||
19 | GetLogicalLine: produces a list of tokens from the logical lines within a | |
20 | range. | |
21 | GetTokensInSubRange: produces a sublist of tokens from a current token list | |
22 | within a range. | |
23 | GetTokenIndex: Get the index of a token. | |
24 | GetNextTokenIndex: Get the index of the next token after a given position. | |
25 | GetPrevTokenIndex: Get the index of the previous token before a given | |
26 | position. | |
27 | TokenStart: Convenience function to return the token's start as a tuple. | |
28 | TokenEnd: Convenience function to return the token's end as a tuple. | |
29 | """ | |
30 | ||
31 | ||
32 | def GetLogicalLine(logical_lines, node): | |
33 | """Get a list of tokens within the node's range from the logical lines.""" | |
34 | start = TokenStart(node) | |
35 | end = TokenEnd(node) | |
36 | tokens = [] | |
37 | ||
38 | for line in logical_lines: | |
39 | if line.start > end: | |
40 | break | |
41 | if line.start <= start or line.end >= end: | |
42 | tokens.extend(GetTokensInSubRange(line.tokens, node)) | |
43 | ||
44 | return tokens | |
45 | ||
46 | ||
47 | def GetTokensInSubRange(tokens, node): | |
48 | """Get a subset of tokens representing the node.""" | |
49 | start = TokenStart(node) | |
50 | end = TokenEnd(node) | |
51 | tokens_in_range = [] | |
52 | ||
53 | for tok in tokens: | |
54 | tok_range = (tok.lineno, tok.column) | |
55 | if tok_range >= start and tok_range < end: | |
56 | tokens_in_range.append(tok) | |
57 | ||
58 | return tokens_in_range | |
59 | ||
60 | ||
61 | def GetTokenIndex(tokens, pos): | |
62 | """Get the index of the token at pos.""" | |
63 | for index, token in enumerate(tokens): | |
64 | if (token.lineno, token.column) == pos: | |
65 | return index | |
66 | ||
67 | return None | |
68 | ||
69 | ||
70 | def GetNextTokenIndex(tokens, pos): | |
71 | """Get the index of the next token after pos.""" | |
72 | for index, token in enumerate(tokens): | |
73 | if (token.lineno, token.column) >= pos: | |
74 | return index | |
75 | ||
76 | return None | |
77 | ||
78 | ||
79 | def GetPrevTokenIndex(tokens, pos): | |
80 | """Get the index of the previous token before pos.""" | |
81 | for index, token in enumerate(tokens): | |
82 | if index > 0 and (token.lineno, token.column) >= pos: | |
83 | return index - 1 | |
84 | ||
85 | return None | |
86 | ||
87 | ||
88 | def TokenStart(node): | |
89 | return (node.lineno, node.col_offset) | |
90 | ||
91 | ||
92 | def TokenEnd(node): | |
93 | return (node.end_lineno, node.end_col_offset) | |
94 | ||
95 | ||
96 | ############################################################################# | |
97 | # Code for debugging # | |
98 | ############################################################################# | |
99 | ||
100 | ||
101 | def AstDump(node): | |
102 | import ast | |
103 | print(ast.dump(node, include_attributes=True, indent=4)) |