@@ -194,7 +194,7 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
194194
195195assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
196196
197- import_stmt[stmt_ty]:
197+ import_stmt[stmt_ty]:
198198 | invalid_import
199199 | import_name
200200 | import_from
@@ -415,8 +415,8 @@ try_stmt[stmt_ty]:
415415 | invalid_try_stmt
416416 | 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
417417 | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
418- | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
419- CHECK_VERSION(stmt_ty, 11, "Exception groups are",
418+ | 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
419+ CHECK_VERSION(stmt_ty, 11, "Exception groups are",
420420 _PyAST_TryStar(b, ex, el, f, EXTRA)) }
421421
422422
@@ -807,7 +807,7 @@ atom[expr_ty]:
807807 | 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
808808 | 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
809809 | 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
810- | &STRING strings
810+ | &( STRING|FSTRING_START) strings
811811 | NUMBER
812812 | &'(' (tuple | group | genexp)
813813 | &'[' (list | listcomp)
@@ -877,7 +877,26 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
877877# LITERALS
878878# ========
879879
880- strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
880+ fstring_middle[expr_ty]:
881+ | fstring_replacement_field
882+ | t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
883+ fstring_replacement_field[expr_ty]:
884+ | '{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[fstring_conversion] format=[fstring_full_format_spec] '}' {
885+ _PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
886+ }
887+ | invalid_replacement_field
888+ fstring_conversion[expr_ty]:
889+ | conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) }
890+ fstring_full_format_spec[expr_ty]:
891+ | ':' spec=fstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
892+ fstring_format_spec[expr_ty]:
893+ | t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
894+ | fstring_replacement_field
895+ fstring[expr_ty]:
896+ | a=FSTRING_START b=fstring_middle* c=FSTRING_END { _PyPegen_joined_str(p, a, (asdl_expr_seq*)b, c) }
897+
898+ string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_string(p, s) }
899+ strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
881900
882901list[expr_ty]:
883902 | '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
@@ -1118,6 +1137,8 @@ invalid_expression:
11181137 _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
11191138 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
11201139 | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
1140+ | a='lambda' [lambda_params] b=':' &(FSTRING_MIDDLE | fstring_replacement_field) {
1141+ RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") }
11211142
11221143invalid_named_expression(memo):
11231144 | a=expression ':=' expression {
@@ -1241,7 +1262,7 @@ invalid_group:
12411262invalid_import:
12421263 | a='import' dotted_name 'from' dotted_name {
12431264 RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
1244-
1265+
12451266invalid_import_from_targets:
12461267 | import_from_as_names ',' NEWLINE {
12471268 RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
@@ -1335,3 +1356,24 @@ invalid_kvpair:
13351356 | expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
13361357invalid_starred_expression:
13371358 | a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
1359+ invalid_replacement_field:
1360+ | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
1361+ | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }
1362+ | '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before ':'") }
1363+ | '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '}'") }
1364+ | '{' !(yield_expr | star_expressions) { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting a valid expression after '{'")}
1365+ | '{' (yield_expr | star_expressions) !('=' | '!' | ':' | '}') {
1366+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '=', or '!', or ':', or '}'") }
1367+ | '{' (yield_expr | star_expressions) '=' !('!' | ':' | '}') {
1368+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '!', or ':', or '}'") }
1369+ | '{' (yield_expr | star_expressions) '='? invalid_conversion_character
1370+ | '{' (yield_expr | star_expressions) '='? ['!' NAME] !(':' | '}') {
1371+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting ':' or '}'") }
1372+ | '{' (yield_expr | star_expressions) '='? ['!' NAME] ':' fstring_format_spec* !'}' {
1373+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}', or format specs") }
1374+ | '{' (yield_expr | star_expressions) '='? ['!' NAME] !'}' {
1375+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}'") }
1376+
1377+ invalid_conversion_character:
1378+ | '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: missing conversion character") }
1379+ | '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: invalid conversion character") }
0 commit comments