Skip to content
Closed
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
8 changes: 4 additions & 4 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ _PyPegen_parse(Parser *p)
// The end
'''
file[mod_ty]: a=[statements] ENDMARKER { _PyPegen_make_module(p, a) }
interactive[mod_ty]: a=statement_newline { Interactive(a, p->arena) }
eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { Expression(a, p->arena) }
func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { FunctionType(a, b, p->arena) }
interactive[mod_ty]: a=statement_newline { _Py_Interactive(a, p->arena) }
eval[mod_ty]: a=expressions NEWLINE* ENDMARKER { _Py_Expression(a, p->arena) }
func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMARKER { _Py_FunctionType(a, b, p->arena) }
fstring[expr_ty]: star_expressions

# type_expressions allow */** but ignore them
Expand Down Expand Up @@ -856,7 +856,7 @@ invalid_except_block:
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
| 'except' expression ['as' NAME ] &&':'
| 'except' &&':'
| 'except' &&':'

invalid_match_stmt:
| "match" subject_expr !':' { CHECK_VERSION(void*, 10, "Pattern matching is", RAISE_SYNTAX_ERROR("expected ':'") ) }
Expand Down
67 changes: 0 additions & 67 deletions Include/internal/pycore_ast.h

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Lib/test/test_peg_generator/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ 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* $ { _Py_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) }
Expand Down Expand Up @@ -237,7 +237,7 @@ 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 { _Py_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
Expand All @@ -252,7 +252,7 @@ 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) }
start[mod_ty]: a[asdl_stmt_seq*]=';'.pass_stmt+ NEWLINE ENDMARKER { _Py_Module(a, NULL, p->arena) }
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA)}
"""
test_source = """
Expand All @@ -263,7 +263,7 @@ 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 { _Py_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
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 { _Py_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 Down Expand Up @@ -306,7 +306,7 @@ 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 { _Py_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) }
| a='from' '.' 'import' c=import_as_names_from {
Expand All @@ -326,7 +326,7 @@ 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 { _Py_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
Expand All @@ -352,7 +352,7 @@ 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) }
start[mod_ty]: a=expr ENDMARKER { _Py_Module(a, NULL, p->arena) }
expr[asdl_stmt_seq*]: a=listcomp NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, _Py_Expr(a, EXTRA)) }
listcomp[expr_ty]: (
a='[' b=NAME c=for_if_clauses d=']' { _Py_ListComp(b, c, EXTRA) }
Expand Down
25 changes: 8 additions & 17 deletions 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"_Py_{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
6 changes: 3 additions & 3 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ interactive_rule(Parser *p)
)
{
D(fprintf(stderr, "%*c+ interactive[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "statement_newline"));
_res = Interactive ( a , p -> arena );
_res = _Py_Interactive ( a , p -> arena );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
Expand Down Expand Up @@ -944,7 +944,7 @@ eval_rule(Parser *p)
)
{
D(fprintf(stderr, "%*c+ eval[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "expressions NEWLINE* $"));
_res = Expression ( a , p -> arena );
_res = _Py_Expression ( a , p -> arena );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func_type_rule(Parser *p)
)
{
D(fprintf(stderr, "%*c+ func_type[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' type_expressions? ')' '->' expression NEWLINE* $"));
_res = FunctionType ( a , b , p -> arena );
_res = _Py_FunctionType ( a , b , p -> arena );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
Expand Down
69 changes: 38 additions & 31 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ _PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
if (tco == NULL) {
return NULL;
}
return arg(a->arg, a->annotation, tco,
a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
return _Py_arg(a->arg, a->annotation, tco,
a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
}

static int
Expand Down Expand Up @@ -568,7 +568,7 @@ _PyPegen_dummy_name(Parser *p, ...)
if (!id) {
return NULL;
}
cache = Name(id, Load, 1, 0, 1, 0, p->arena);
cache = _Py_Name(id, Load, 1, 0, 1, 0, p->arena);
return cache;
}

Expand Down Expand Up @@ -919,8 +919,8 @@ _PyPegen_name_token(Parser *p)
p->error_indicator = 1;
return NULL;
}
return Name(id, Load, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset,
p->arena);
return _Py_Name(id, Load, t->lineno, t->col_offset, t->end_lineno,
t->end_col_offset, p->arena);
}

void *
Expand Down Expand Up @@ -1035,8 +1035,8 @@ _PyPegen_number_token(Parser *p)
return NULL;
}

return Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset,
p->arena);
return _Py_Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno,
t->end_col_offset, p->arena);
}

static int // bool
Expand Down Expand Up @@ -1551,7 +1551,7 @@ _PyPegen_alias_for_star(Parser *p)
Py_DECREF(str);
return NULL;
}
return alias(str, NULL, p->arena);
return _Py_alias(str, NULL, p->arena);
}

/* Creates a new asdl_seq* with the identifiers of all the names in seq */
Expand Down Expand Up @@ -1667,19 +1667,22 @@ _set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
static expr_ty
_set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Subscript(e->v.Subscript.value, e->v.Subscript.slice, ctx, EXTRA_EXPR(e, e));
return _Py_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
ctx, EXTRA_EXPR(e, e));
}

static expr_ty
_set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Attribute(e->v.Attribute.value, e->v.Attribute.attr, ctx, EXTRA_EXPR(e, e));
return _Py_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
ctx, EXTRA_EXPR(e, e));
}

static expr_ty
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx), ctx, EXTRA_EXPR(e, e));
return _Py_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
ctx, EXTRA_EXPR(e, e));
}

/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
Expand Down Expand Up @@ -1987,8 +1990,8 @@ _PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
kwarg = star_etc->kwarg;
}

return _Py_arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg,
posdefaults, p->arena);
return _Py_arguments(posonlyargs, posargs, vararg, kwonlyargs,
kwdefaults, kwarg, posdefaults, p->arena);
}

/* Constructs an empty arguments_ty object, that gets used when a function accepts no
Expand Down Expand Up @@ -2017,8 +2020,8 @@ _PyPegen_empty_arguments(Parser *p)
return NULL;
}

return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, posdefaults,
p->arena);
return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs,
kwdefaults, NULL, posdefaults, p->arena);
}

/* Encapsulates the value of an operator_ty into an AugOperator struct */
Expand Down Expand Up @@ -2047,23 +2050,25 @@ _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty f
p->arena);
}

return _Py_FunctionDef(function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
function_def->col_offset, function_def->end_lineno,
function_def->end_col_offset, p->arena);
return _Py_FunctionDef(
function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
function_def->col_offset, function_def->end_lineno,
function_def->end_col_offset, p->arena);
}

/* Construct a ClassDef equivalent to class_def, but with decorators */
stmt_ty
_PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def)
{
assert(class_def != NULL);
return _Py_ClassDef(class_def->v.ClassDef.name, class_def->v.ClassDef.bases,
class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
class_def->end_col_offset, p->arena);
return _Py_ClassDef(
class_def->v.ClassDef.name, class_def->v.ClassDef.bases,
class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
class_def->end_col_offset, p->arena);
}

/* Construct a KeywordOrStarred */
Expand Down Expand Up @@ -2214,8 +2219,9 @@ _PyPegen_concatenate_strings(Parser *p, asdl_seq *strings)
if (_PyArena_AddPyObject(p->arena, bytes_str) < 0) {
goto error;
}
return Constant(bytes_str, NULL, first->lineno, first->col_offset, last->end_lineno,
last->end_col_offset, p->arena);
return _Py_Constant(bytes_str, NULL, first->lineno,
first->col_offset, last->end_lineno,
last->end_col_offset, p->arena);
}

return _PyPegen_FstringParser_Finish(p, &state, first, last);
Expand Down Expand Up @@ -2244,14 +2250,15 @@ _PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
if (tag == NULL) {
return NULL;
}
type_ignore_ty ti = TypeIgnore(p->type_ignore_comments.items[i].lineno, tag, p->arena);
type_ignore_ty ti = _Py_TypeIgnore(p->type_ignore_comments.items[i].lineno,
tag, p->arena);
if (ti == NULL) {
return NULL;
}
asdl_seq_SET(type_ignores, i, ti);
}
}
return Module(a, type_ignores, p->arena);
return _Py_Module(a, type_ignores, p->arena);
}

// Error reporting helpers
Expand Down Expand Up @@ -2362,7 +2369,7 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,

if (b == NULL) {
return _Py_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
end_lineno, end_col_offset, arena);
end_lineno, end_col_offset, arena);

}

Expand Down
Loading