Expand description
Python abstract syntax tree.
Faithful to CPython’s surface grammar as far as pythonrs lowers it today:
every node here has a direct lowering in compiler.rs. Unlike Ruby, Python
is statement-oriented — most control flow is a Stmt that yields no value —
so the tree separates Stmt (blocks of these form suites) from Expr.
Structs§
- Alias
- An
importalias:name [as asname]. - Comprehension
- One
(target, iter, ifs)clause of a comprehension. - Except
Handler - One
exceptclause of atry. - Keyword
- A keyword argument at a call site:
name=value, or**mappingwhennameisNone. - Loc
- A node’s extent as CPython’s AST records it: 1-based lines and 0-based
UTF-8 BYTE columns, end exclusive (
lineno,col_offset,end_lineno,end_col_offset). The compiler raises its patternSyntaxErrors at this extent, andcompiler_errorreportscol_offset + 1— a byte column — as the exception’soffset. - Match
Case - One
case pattern [if guard]: bodyof amatch. - Params
- A formal-parameter list for a
def/lambda. - Pattern
- A
matchpattern (PEP 634): its shape and where it sits in the source. - Span
- A source span for traceback carets: character columns within a 1-based
line.line == 0marks “no span” (synthetic/desugared nodes). Whenanchor_end > anchor_start, the[anchor_start, anchor_end)sub-range renders the secondary caret^and the rest of[start, end)renders the primary caret~— CPython’s~^~binary-op and~~~^^^subscript/call anchoring. With no anchor the whole[start, end)renders^. - Stmt
- A statement plus its 1-based source line (for tracebacks and DAP markers).
- Type
Param - One PEP 695 type parameter:
T,*Tsor**P. - With
Item - One
withitem:context_expr [as optional_vars].
Enums§
- BinOp
- A binary arithmetic/bit operator (Python
a <op> b). Comparison and boolean operators are separate (Compare,BoolOp) because Python chains them. - BoolOp
- A boolean short-circuit operator.
- CmpOp
- A comparison operator (one link of a
Comparechain). - Expr
- A Python expression.
- FStr
Part - A formatted-string segment (
f"..."). - Pattern
Kind - The shape of a
Pattern. - Stmt
Kind - A Python statement.
- Type
Param Kind - Which
typingobject aTypeParamcreates. - UnOp
- A unary operator.