Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 148 additions & 148 deletions 296 Grammar/python.gram

Large diffs are not rendered by default.

401 changes: 171 additions & 230 deletions 401 Include/internal/pycore_ast.h

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions 54 Lib/test/test_peg_generator/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ def run_test(self, grammar_source, test_source):

def test_c_parser(self) -> None:
grammar_source = """
start[mod_ty]: a[asdl_stmt_seq*]=stmt* $ { Module(a, NULL, p->arena) }
start[mod_ty]: a[asdl_stmt_seq*]=stmt* $ { _PyAST_Module(a, NULL, p->arena) }
stmt[stmt_ty]: a=expr_stmt { a }
expr_stmt[stmt_ty]: a=expression NEWLINE { _Py_Expr(a, EXTRA) }
expression[expr_ty]: ( l=expression '+' r=term { _Py_BinOp(l, Add, r, EXTRA) }
| l=expression '-' r=term { _Py_BinOp(l, Sub, r, EXTRA) }
expr_stmt[stmt_ty]: a=expression NEWLINE { _PyAST_Expr(a, EXTRA) }
expression[expr_ty]: ( l=expression '+' r=term { _PyAST_BinOp(l, Add, r, EXTRA) }
| l=expression '-' r=term { _PyAST_BinOp(l, Sub, r, EXTRA) }
| t=term { t }
)
term[expr_ty]: ( l=term '*' r=factor { _Py_BinOp(l, Mult, r, EXTRA) }
| l=term '/' r=factor { _Py_BinOp(l, Div, r, EXTRA) }
term[expr_ty]: ( l=term '*' r=factor { _PyAST_BinOp(l, Mult, r, EXTRA) }
| l=term '/' r=factor { _PyAST_BinOp(l, Div, r, EXTRA) }
| f=factor { f }
)
factor[expr_ty]: ('(' e=expression ')' { e }
Expand Down Expand Up @@ -237,12 +237,12 @@ def test_nasty_mutually_left_recursive(self) -> None:

def test_return_stmt_noexpr_action(self) -> None:
grammar_source = """
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
statements[asdl_stmt_seq*]: a[asdl_stmt_seq*]=statement+ { a }
statement[stmt_ty]: simple_stmt
simple_stmt[stmt_ty]: small_stmt
small_stmt[stmt_ty]: return_stmt
return_stmt[stmt_ty]: a='return' NEWLINE { _Py_Return(NULL, EXTRA) }
return_stmt[stmt_ty]: a='return' NEWLINE { _PyAST_Return(NULL, EXTRA) }
"""
test_source = """
stmt = "return"
Expand All @@ -252,8 +252,8 @@ def test_return_stmt_noexpr_action(self) -> None:

def test_gather_action_ast(self) -> None:
grammar_source = """
start[mod_ty]: a[asdl_stmt_seq*]=';'.pass_stmt+ NEWLINE ENDMARKER { Module(a, NULL, p->arena) }
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA)}
start[mod_ty]: a[asdl_stmt_seq*]=';'.pass_stmt+ NEWLINE ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA)}
"""
test_source = """
stmt = "pass; pass"
Expand All @@ -263,12 +263,12 @@ def test_gather_action_ast(self) -> None:

def test_pass_stmt_action(self) -> None:
grammar_source = """
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
statements[asdl_stmt_seq*]: a[asdl_stmt_seq*]=statement+ { a }
statement[stmt_ty]: simple_stmt
simple_stmt[stmt_ty]: small_stmt
small_stmt[stmt_ty]: pass_stmt
pass_stmt[stmt_ty]: a='pass' NEWLINE { _Py_Pass(EXTRA) }
pass_stmt[stmt_ty]: a='pass' NEWLINE { _PyAST_Pass(EXTRA) }
"""
test_source = """
stmt = "pass"
Expand All @@ -278,7 +278,7 @@ def test_pass_stmt_action(self) -> None:

def test_if_stmt_action(self) -> None:
grammar_source = """
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | simple_stmt

Expand All @@ -290,11 +290,11 @@ def test_if_stmt_action(self) -> None:

compound_stmt: if_stmt

if_stmt: 'if' a=full_expression ':' b=block { _Py_If(a, b, NULL, EXTRA) }
if_stmt: 'if' a=full_expression ':' b=block { _PyAST_If(a, b, NULL, EXTRA) }

small_stmt[stmt_ty]: pass_stmt

pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA) }
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA) }

full_expression: NAME
"""
Expand All @@ -306,15 +306,15 @@ def test_if_stmt_action(self) -> None:

def test_same_name_different_types(self) -> None:
grammar_source = """
start[mod_ty]: a[asdl_stmt_seq*]=import_from+ NEWLINE ENDMARKER { Module(a, NULL, p->arena)}
start[mod_ty]: a[asdl_stmt_seq*]=import_from+ NEWLINE ENDMARKER { _PyAST_Module(a, NULL, p->arena)}
import_from[stmt_ty]: ( a='from' !'import' c=simple_name 'import' d=import_as_names_from {
_Py_ImportFrom(c->v.Name.id, d, 0, EXTRA) }
_PyAST_ImportFrom(c->v.Name.id, d, 0, EXTRA) }
| a='from' '.' 'import' c=import_as_names_from {
_Py_ImportFrom(NULL, c, 1, EXTRA) }
_PyAST_ImportFrom(NULL, c, 1, EXTRA) }
)
simple_name[expr_ty]: NAME
import_as_names_from[asdl_alias_seq*]: a[asdl_alias_seq*]=','.import_as_name_from+ { a }
import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _Py_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
"""
test_source = """
for stmt in ("from a import b as c", "from . import a as b"):
Expand All @@ -326,19 +326,19 @@ def test_same_name_different_types(self) -> None:

def test_with_stmt_with_paren(self) -> None:
grammar_source = """
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
compound_stmt[stmt_ty]: with_stmt
with_stmt[stmt_ty]: (
a='with' '(' b[asdl_withitem_seq*]=','.with_item+ ')' ':' c=block {
_Py_With(b, (asdl_stmt_seq*) _PyPegen_singleton_seq(p, c), NULL, EXTRA) }
_PyAST_With(b, (asdl_stmt_seq*) _PyPegen_singleton_seq(p, c), NULL, EXTRA) }
)
with_item[withitem_ty]: (
e=NAME o=['as' t=NAME { t }] { _Py_withitem(e, _PyPegen_set_expr_context(p, o, Store), p->arena) }
e=NAME o=['as' t=NAME { t }] { _PyAST_withitem(e, _PyPegen_set_expr_context(p, o, Store), p->arena) }
)
block[stmt_ty]: a=pass_stmt NEWLINE { a } | NEWLINE INDENT a=pass_stmt DEDENT { a }
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA) }
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA) }
"""
test_source = """
stmt = "with (\\n a as b,\\n c as d\\n): pass"
Expand All @@ -352,14 +352,14 @@ def test_with_stmt_with_paren(self) -> None:

def test_ternary_operator(self) -> None:
grammar_source = """
start[mod_ty]: a=expr ENDMARKER { Module(a, NULL, p->arena) }
expr[asdl_stmt_seq*]: a=listcomp NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, _Py_Expr(a, EXTRA)) }
start[mod_ty]: a=expr ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
expr[asdl_stmt_seq*]: a=listcomp NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, _PyAST_Expr(a, EXTRA)) }
listcomp[expr_ty]: (
a='[' b=NAME c=for_if_clauses d=']' { _Py_ListComp(b, c, EXTRA) }
a='[' b=NAME c=for_if_clauses d=']' { _PyAST_ListComp(b, c, EXTRA) }
)
for_if_clauses[asdl_comprehension_seq*]: (
a[asdl_comprehension_seq*]=(y=[ASYNC] 'for' a=NAME 'in' b=NAME c[asdl_expr_seq*]=('if' z=NAME { z })*
{ _Py_comprehension(_Py_Name(((expr_ty) a)->v.Name.id, Store, EXTRA), b, c, (y == NULL) ? 0 : 1, p->arena) })+ { a }
{ _PyAST_comprehension(_PyAST_Name(((expr_ty) a)->v.Name.id, Store, EXTRA), b, c, (y == NULL) ? 0 : 1, p->arena) })+ { a }
)
"""
test_source = """
Expand Down
25 changes: 8 additions & 17 deletions 25 Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ def visitProduct(self, product, name, depth):
self.emit("", depth)


def ast_func_name(name):
return f"_PyAST_{name}"


class PrototypeVisitor(EmitVisitor):
"""Generate function prototypes for the .h file"""

Expand Down Expand Up @@ -322,16 +326,7 @@ def emit_function(self, name, ctype, args, attrs, union=True):
argstr += ", PyArena *arena"
else:
argstr = "PyArena *arena"
margs = "a0"
for i in range(1, len(args)+1):
margs += ", a%d" % i
# bpo-43244: <winbase.h> defines Yield macro. Don't redefine it in
# pycore_ast.h: it is not needed outside Python-ast.c which calls
# directly _Py_Yield().
if name != "Yield":
self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
reflow=False)
self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
self.emit("%s %s(%s);" % (ctype, ast_func_name(name), argstr), False)

def visitProduct(self, prod, name):
self.emit_function(name, get_c_type(name),
Expand All @@ -340,10 +335,6 @@ def visitProduct(self, prod, name):
union=False)


def pyfunc_name(name):
return f"_Py_{name}"


class FunctionVisitor(PrototypeVisitor):
"""Visitor to generate constructor functions for AST."""

Expand All @@ -357,7 +348,7 @@ def emit(s, depth=0, reflow=True):
else:
argstr = "PyArena *arena"
self.emit("%s" % ctype, 0)
emit("%s(%s)" % (pyfunc_name(name), argstr))
emit("%s(%s)" % (ast_func_name(name), argstr))
emit("{")
emit("%s p;" % ctype, 1)
for argtype, argname, opt in args:
Expand Down Expand Up @@ -496,7 +487,7 @@ def complexSum(self, sum, name):
for f in t.fields:
self.visitField(f, t.name, sum=sum, depth=2)
args = [f.name for f in t.fields] + [a.name for a in sum.attributes]
self.emit("*out = %s(%s);" % (pyfunc_name(t.name), self.buildArgs(args)), 2)
self.emit("*out = %s(%s);" % (ast_func_name(t.name), self.buildArgs(args)), 2)
self.emit("if (*out == NULL) goto failed;", 2)
self.emit("return 0;", 2)
self.emit("}", 1)
Expand Down Expand Up @@ -529,7 +520,7 @@ def visitProduct(self, prod, name):
self.visitField(a, name, prod=prod, depth=1)
args = [f.name for f in prod.fields]
args.extend([a.name for a in prod.attributes])
self.emit("*out = %s(%s);" % (pyfunc_name(name), self.buildArgs(args)), 1)
self.emit("*out = %s(%s);" % (ast_func_name(name), self.buildArgs(args)), 1)
self.emit("return 0;", 1)
self.emit("failed:", 0)
self.emit("Py_XDECREF(tmp);", 1)
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.