- Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathdump-ast.py
executable file
·48 lines (38 loc) · 1.29 KB
/
dump-ast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""
Parse source files and print the abstract syntax trees.
"""
from __future__ importannotations
importargparse
importsys
frommypyimportdefaults
frommypy.errorsimportCompileError, Errors
frommypy.optionsimportOptions
frommypy.parseimportparse
defdump(fname: str, python_version: tuple[int, int], quiet: bool=False) ->None:
options=Options()
options.python_version=python_version
withopen(fname, "rb") asf:
s=f.read()
tree=parse(s, fname, None, errors=Errors(options), options=options)
ifnotquiet:
print(tree)
defmain() ->None:
# Parse a file and dump the AST (or display errors).
parser=argparse.ArgumentParser(
description="Parse source files and print the abstract syntax tree (AST)."
)
parser.add_argument("--quiet", action="store_true", help="do not print AST")
parser.add_argument("FILE", nargs="*", help="files to parse")
args=parser.parse_args()
status=0
forfnameinargs.FILE:
try:
dump(fname, defaults.PYTHON3_VERSION, args.quiet)
exceptCompileErrorase:
formsgine.messages:
sys.stderr.write("%s\n"%msg)
status=1
sys.exit(status)
if__name__=="__main__":
main()