-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Python Parser
Michael J. Sullivan edited this page Dec 5, 2017
·
8 revisions
NOTE: We are migrating to a different parser and lexer derived fromthe CPython "ast" module.
The mypy parser uses the CPython "ast" module to convert source code into an abstract syntax tree (AST). (The term parse tree is sometimes used informally as a synonym.)
You can use the parse.py module as a script to experiment with it (I assume that mypy is an alias to the mypy.py script):
$ cd mypy # mypy repo
$ cat samples/hello.py
print('Hello, world')
$ mypy parse.py samples/hello.py
MypyFile:1(
samples/hello.py
ExpressionStmt:1(
CallExpr:1(
NameExpr(print)
Args(
StrExpr(Hello, world)))))
The names MypyFile
, ExpressionStmt
, CallExpr
, NameExpr
and StrExpr
refer to AST node classes defined in mypy/nodes.py.
The numbers after colons are line numbers.
The parser does only a minimal amount of consistency checks. As such it also accepts many invalid programs. The next compiler pass, SemanticAnalyzer, performs additional checks.