python library
This library contains the grammar and AST parser for the Python programming language.
Supports modern Python 3.12+ features including match/case pattern matching, PEP 695 type parameter syntax, walrus operator, f-strings, exception groups (except*), async/await, and indentation-based suite parsing.
For example:
final module = parsePython('def add(a: int, b: int) -> int:\n return a + b\n');
print(module.body.first);
Classes
- AliasNode
-
Import alias (
module as asname). - AnnAssignNode
-
Annotated assignment statement (
x: int = 1). - ArgNode
- Function argument definition.
- ArgumentsNode
-
Function parameter list (
def foo(a, b=1, *args, c=2, **kwargs):). - AssertNode
-
Assert statement (
assert test, msg). - AssignNode
-
Assignment statement (
x = y = 1). - AsyncForNode
-
Async for loop statement (
async for x in iter: body else: orelse). - AsyncFunctionDefNode
-
Asynchronous function definition statement (
async def name(params): body). - AsyncWithNode
-
Async with statement (
async with item1: body). - AttributeNode
-
Attribute access (
value.attr). - AugAssignNode
-
Augmented assignment statement (
x += 1). - AwaitNode
-
Await expression (
await coro). - BinOpNode
-
Binary operation (
left + right). - BoolOpNode
-
Boolean binary operation (
a and bora or b). - BreakNode
-
Break statement (
break). - CallNode
-
Function / method call expression (
f(a, b, c=1)). - ClassDefNode
-
Class definition statement (
class Name(bases): body). - CompareNode
-
Comparison expression with chaining support (
a < b <= c). - ComprehensionNode
-
Comprehension clause (
for target in iter if cond). - ConstantNode
- Literal constant (numbers, strings, booleans, None, Ellipsis).
- ContinueNode
-
Continue statement (
continue). - DeleteNode
-
Delete statement (
del x, y). - DictCompNode
-
Dict comprehension (
{k: v for k, v in data}). - DictNode
-
Dictionary literal (
{k1: v1, **rest}). - ExceptHandlerNode
-
Except handler in
trystatement (except Error as e:). - ExpressionModuleNode
- An expression parsed as a top-level module (eval context).
- ExpressionNode
- Base class for all Python expressions.
- ExprStatementNode
- An expression evaluated as a statement.
- FormattedValueNode
-
Formatted value inside an f-string (
{x!r:.2f}). - ForNode
-
For loop statement (
for x in iter: body else: orelse). - FunctionDefNode
-
Function definition statement (
def name(params): body). - GeneratorExpNode
-
Generator expression (
(x for x in data)). - GlobalNode
-
Global statement (
global a, b). - IfExpNode
-
Ternary conditional expression (
body if test else orelse). - IfNode
-
If statement (
if test: body elif ... else: orelse). - ImportFromNode
-
From-import statement (
from foo import bar as baz). - ImportNode
-
Import statement (
import foo, bar as baz). - InteractiveNode
- An interactive Python statement sequence.
- JoinedStrNode
-
Joined f-string (
f"hello {name}"). - KeywordNode
-
Keyword argument in calls or class headers (
name=valueor**kw). - LambdaNode
-
Lambda expression (
lambda x, y: x + y). - ListCompNode
-
List comprehension (
[x for x in data if cond]). - ListNode
-
List literal (
[a, b, c]). - MatchAsNode
-
As-pattern (
case pattern as name:or simple capturecase name:). - MatchCaseNode
-
Match case in
matchstatement (case pattern if guard: body). - MatchClassNode
-
Class pattern (
case Point(x, y, z=0):). - MatchMappingNode
-
Mapping pattern (
case {"name": name, **rest}:). - MatchNode
-
Pattern match statement (
match subject: case ...). - MatchOrNode
-
Or-pattern (
case p1 | p2 | p3:). - MatchSequenceNode
-
Sequence pattern (
case [x, y]:orcase (x, y):). - MatchSingletonNode
-
Singleton pattern (
case None:orcase True:orcase False:). - MatchStarNode
-
Star pattern in sequences (
case [first, *rest]:or wildcardcase _:). - MatchValueNode
-
Literal value pattern (
case 1:orcase "abc":). - ModNode
- Base class for top-level Python modules.
- ModuleNode
- A standard Python module consisting of a sequence of statements.
- NamedExprNode
-
Walrus named expression assignment (
target := value). - NameNode
-
Identifier variable name (
foo). - NonlocalNode
-
Nonlocal statement (
nonlocal a, b). - ParamSpecNode
-
Parameter specification (
**P). - PassNode
-
Pass statement (
pass). - PatternNode
-
Base class for pattern matching in
matchstatements. - PythonGrammarDefinition
- Complete grammar and parser definition for the Python programming language (3.12+).
- PythonNode
- Base class for all Python AST nodes.
- RaiseNode
-
Raise statement (
raise exc from cause). - ReturnNode
-
Return statement (
return value). - SetCompNode
-
Set comprehension (
{x for x in data if cond]). - SetNode
-
Set literal (
{1, 2, 3}). - SliceNode
-
Slice expression used in subscripts (
lower:upper:step). - StarredNode
-
Starred expression (
*args). - StatementNode
- Base class for all Python statements.
- SubscriptNode
-
Subscript indexing or slicing (
value[slice]). - TryNode
-
Try statement (
try: ... except: ... else: ... finally: ...). - TryStarNode
-
Try-star statement (
try: ... except* ExceptionGroup: ...). - TupleNode
-
Tuple literal (
(a, b, c)ora, b). - TypeAliasNode
-
PEP 695 type alias statement (
type X[T] = int). - TypeParamNode
-
PEP 695 type parameter (
[T, *Ts, **P]). - TypeVarParamNode
-
Simple type variable (
T: str = int). - TypeVarTupleNode
-
Type variable tuple (
*Ts). - UnaryOpNode
-
Unary operation (
-x,+x,~x,not x). - WhileNode
-
While loop statement (
while test: body else: orelse). - WithItemNode
-
With item (
context_expr as optional_vars). - WithNode
-
With statement (
with item1, item2: body). - YieldFromNode
-
Yield from expression (
yield from gen). - YieldNode
-
Yield expression (
yield value).
Functions
-
parsePython(
String input) → ModuleNode -
Parses the
inputPython source code into a ModuleNode.