Skip to content

Commit

Permalink
parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Oct 17, 2024
1 parent a5a7f5e commit 338162e
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 5 deletions.
1 change: 1 addition & 0 deletions Grammar/Tokens
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ RARROW '->'
ELLIPSIS '...'
COLONEQUAL ':='
EXCLAMATION '!'
BACKTICK '`'

OP
TYPE_IGNORE
Expand Down
1 change: 1 addition & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ atom[expr_ty]:
| &'[' (list | listcomp)
| &'{' (dict | set | dictcomp | setcomp)
| '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
| '`' a=expression '`' { _PyAST_Repr(a, EXTRA) }

group[expr_ty]:
| '(' a=(yield_expr | named_expression) ')' { a }
Expand Down
17 changes: 12 additions & 5 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_ast_state.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Python
| SetComp(expr elt, comprehension* generators)
| DictComp(expr key, expr value, comprehension* generators)
| GeneratorExp(expr elt, comprehension* generators)
| Repr(expr value)
-- the grammar constrains where yield expressions can occur
| Await(expr value)
| Yield(expr? value)
Expand Down
40 changes: 40 additions & 0 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ validate_expr(struct validator *state, expr_ty exp, expr_context_ty ctx)
validate_expr(state, exp->v.IfExp.body, Load) &&
validate_expr(state, exp->v.IfExp.orelse, Load);
break;
case Repr_kind:
ret = validate_expr(state, exp->v.Repr.value, Load);
break;
case Dict_kind:
if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
PyErr_SetString(PyExc_ValueError,
Expand Down
3 changes: 3 additions & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
break;
case Repr_kind:
CALL(astfold_expr, expr_ty, node_->v.Repr.value);
break;
case IfExp_kind:
CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
Expand Down
11 changes: 11 additions & 0 deletions Python/ast_unparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ append_ast_ifexp(_PyUnicodeWriter *writer, expr_ty e, int level)
return 0;
}

static int
append_ast_repr(_PyUnicodeWriter *writer, expr_ty e)
{
APPEND_STR("`");
APPEND_EXPR(e->v.Repr.value, PR_TEST);
APPEND_STR("`");
return 0;
}

static int
append_ast_dict(_PyUnicodeWriter *writer, expr_ty e)
{
Expand Down Expand Up @@ -854,6 +863,8 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
return append_ast_lambda(writer, e, level);
case IfExp_kind:
return append_ast_ifexp(writer, e, level);
case Repr_kind:
return append_ast_repr(writer, e);
case Dict_kind:
return append_ast_dict(writer, e);
case Set_kind:
Expand Down
4 changes: 4 additions & 0 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -4883,6 +4883,10 @@ codegen_visit_expr(compiler *c, expr_ty e)
ADDOP_I(c, loc, COPY, 1);
VISIT(c, expr, e->v.NamedExpr.target);
break;
case Repr_kind:
// TODO
abort();
break;
case BoolOp_kind:
return codegen_boolop(c, e);
case BinOp_kind:
Expand Down
3 changes: 3 additions & 0 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,9 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
VISIT(st, expr, e->v.IfExp.body);
VISIT(st, expr, e->v.IfExp.orelse);
break;
case Repr_kind:
VISIT(st, expr, e->v.Repr.value);
break;
case Dict_kind:
VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
VISIT_SEQ(st, expr, e->v.Dict.values);
Expand Down

0 comments on commit 338162e

Please sign in to comment.