Skip to content

Commit 40322f8

Browse files
committed
Update the grammar to use the new parser APIs
1 parent bdfd6e3 commit 40322f8

File tree

2 files changed

+577
-484
lines changed

2 files changed

+577
-484
lines changed

Grammar/python.gram

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -779,32 +779,32 @@ t_atom[expr_ty]:
779779

780780
# From here on, there are rules for invalid syntax with specialised error messages
781781
invalid_arguments:
782-
| args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
783-
| a=expression for_if_clauses ',' [args | expression for_if_clauses] {
784-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
782+
| a=args ',' '*' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable argument unpacking follows keyword argument unpacking") }
783+
| a=expression b=for_if_clauses ',' [args | expression for_if_clauses] {
784+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
785785
| a=args for_if_clauses { _PyPegen_nonparen_genexp_in_call(p, a) }
786-
| args ',' a=expression for_if_clauses {
787-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
786+
| args ',' a=expression b=for_if_clauses {
787+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, asdl_seq_GET(b, b->size-1)->target, "Generator expression must be parenthesized") }
788788
| a=args ',' args { _PyPegen_arguments_parsing_error(p, a) }
789789
invalid_kwarg:
790-
| expression a='=' {
791-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
792-
a, "expression cannot contain assignment, perhaps you meant \"==\"?") }
790+
| a=expression b='=' {
791+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
792+
a, b, "expression cannot contain assignment, perhaps you meant \"==\"?") }
793793

794794
invalid_expression:
795795
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
796796
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
797-
| !(NAME STRING | SOFT_KEYWORD) a=disjunction expression {
798-
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "invalid syntax. Perhaps you forgot a comma?") }
797+
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression {
798+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
799799

800800
invalid_named_expression:
801801
| a=expression ':=' expression {
802802
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
803803
a, "cannot use assignment expressions with %s", _PyPegen_get_expr_name(a)) }
804-
| a=NAME b='=' bitwise_or !('='|':='|',') {
805-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
804+
| a=NAME '=' b=bitwise_or !('='|':='|',') {
805+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
806806
| !(list|tuple|genexp|'True'|'None'|'False') a=bitwise_or b='=' bitwise_or !('='|':='|',') {
807-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(b, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
807+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot assign to %s here. Maybe you meant '==' instead of '='?",
808808
_PyPegen_get_expr_name(a)) }
809809

810810
invalid_assignment:
@@ -841,25 +841,28 @@ invalid_primary:
841841
invalid_comprehension:
842842
| ('[' | '(' | '{') a=starred_expression for_if_clauses {
843843
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
844-
| ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
845-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
844+
| ('[' | '{') a=star_named_expression ',' b=star_named_expressions for_if_clauses {
845+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, (expr_ty)_PyPegen_seq_last_item((asdl_seq*)b),
846+
"did you forget parentheses around the comprehension target?") }
847+
| ('[' | '{') a=star_named_expression b=',' for_if_clauses {
848+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "did you forget parentheses around the comprehension target?") }
846849
invalid_dict_comprehension:
847850
| '{' a='**' bitwise_or for_if_clauses '}' {
848851
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "dict unpacking cannot be used in dict comprehension") }
849852
invalid_parameters:
850-
| param_no_default* invalid_parameters_helper param_no_default {
851-
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
853+
| param_no_default* invalid_parameters_helper a=param_no_default {
854+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
852855
invalid_parameters_helper: # This is only there to avoid type errors
853856
| a=slash_with_default { _PyPegen_singleton_seq(p, a) }
854857
| param_with_default+
855858
invalid_lambda_parameters:
856-
| lambda_param_no_default* invalid_lambda_parameters_helper lambda_param_no_default {
857-
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
859+
| lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
860+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
858861
invalid_lambda_parameters_helper:
859862
| a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
860863
| lambda_param_with_default+
861864
invalid_star_etc:
862-
| '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
865+
| a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named arguments must follow bare *") }
863866
| '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
864867
invalid_lambda_star_etc:
865868
| '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
@@ -897,7 +900,7 @@ invalid_try_stmt:
897900
RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
898901
invalid_except_stmt:
899902
| 'except' a=expression ',' expressions ['as' NAME ] ':' {
900-
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "exception group must be parenthesized") }
903+
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "exception group must be parenthesized") }
901904
| a='except' expression ['as' NAME ] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
902905
| a='except' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
903906
invalid_finally_stmt:
@@ -942,10 +945,10 @@ invalid_class_def_raw:
942945

943946
invalid_double_starred_kvpairs:
944947
| ','.double_starred_kvpair+ ',' invalid_kvpair
945-
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
948+
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
946949
| expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
947950
invalid_kvpair:
948951
| a=expression !(':') {
949-
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "':' expected after dictionary key") }
950-
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
952+
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, a->end_lineno, -1, "':' expected after dictionary key") }
953+
| expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "cannot use a starred expression in a dictionary value") }
951954
| expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }

0 commit comments

Comments
 (0)