]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/yapf/pyparser/pyparser_utils.py
1 # Copyright 2022 Bill Wendling, All Rights Reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 This module collects various utilities related to the parse trees produced by
19 GetLogicalLine: produces a list of tokens from the logical lines within a
21 GetTokensInSubRange: produces a sublist of tokens from a current token list
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
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.
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
)
38 for line
in logical_lines
:
41 if line
.start
<= start
or line
.end
>= end
:
42 tokens
.extend(GetTokensInSubRange(line
.tokens
, node
))
47 def GetTokensInSubRange(tokens
, node
):
48 """Get a subset of tokens representing the node."""
49 start
= TokenStart(node
)
54 tok_range
= (tok
.lineno
, tok
.column
)
55 if tok_range
>= start
and tok_range
< end
:
56 tokens_in_range
.append(tok
)
58 return tokens_in_range
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
:
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
:
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
:
89 return (node
.lineno
, node
.col_offset
)
93 return (node
.end_lineno
, node
.end_col_offset
)
96 #############################################################################
97 # Code for debugging #
98 #############################################################################
103 print(ast
.dump(node
, include_attributes
=True, indent
=4))