mirror of
https://github.com/python/cpython.git
synced 2025-07-25 12:14:38 +00:00

The original tool wasn't working right and it was simpler to create a new one, partially re-using some of the old code. At this point the tool runs properly on the master. (Try: ./python Tools/c-analyzer/c-analyzer.py analyze.) It take ~40 seconds on my machine to analyze the full CPython code base. Note that we'll need to iron out some OS-specific stuff (e.g. preprocessor). We're okay though since this tool isn't used yet in our workflow. We will also need to verify the analysis results in detail before activating the check in CI, though I'm pretty sure it's close. https://bugs.python.org/issue36876
54 lines
1,007 B
Python
54 lines
1,007 B
Python
import re
|
|
import textwrap
|
|
|
|
from ._regexes import _ind, STRING_LITERAL
|
|
|
|
|
|
def parse(text, anon_name):
|
|
context = None
|
|
data = None
|
|
for m in DELIMITER_RE.find_iter(text):
|
|
before, opened, closed = m.groups()
|
|
delim = opened or closed
|
|
|
|
handle_segment = HANDLERS[context][delim]
|
|
result, context, data = handle_segment(before, delim, data)
|
|
if result:
|
|
yield result
|
|
|
|
|
|
DELIMITER = textwrap.dedent(rf'''
|
|
(
|
|
(?:
|
|
[^'"()\[\]{};]*
|
|
{_ind(STRING_LITERAL, 3)}
|
|
}*
|
|
[^'"()\[\]{};]+
|
|
)? # <before>
|
|
(?:
|
|
(
|
|
[(\[{]
|
|
) # <open>
|
|
|
|
|
(
|
|
[)\]};]
|
|
) # <close>
|
|
)?
|
|
''')
|
|
DELIMITER_RE = re.compile(DELIMITER, re.VERBOSE)
|
|
|
|
_HANDLERS = {
|
|
None: { # global
|
|
# opened
|
|
'{': ...,
|
|
'[': None,
|
|
'(': None,
|
|
# closed
|
|
'}': None,
|
|
']': None,
|
|
')': None,
|
|
';': ...,
|
|
},
|
|
'': {
|
|
},
|
|
}
|