@@ -194,7 +194,7 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
194
194
195
195
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
196
196
197
- import_stmt[stmt_ty]:
197
+ import_stmt[stmt_ty]:
198
198
| invalid_import
199
199
| import_name
200
200
| import_from
@@ -415,8 +415,8 @@ try_stmt[stmt_ty]:
415
415
| invalid_try_stmt
416
416
| 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
417
417
| '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",
420
420
_PyAST_TryStar(b, ex, el, f, EXTRA)) }
421
421
422
422
@@ -807,7 +807,7 @@ atom[expr_ty]:
807
807
| 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
808
808
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
809
809
| 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
810
- | &STRING strings
810
+ | &( STRING|FSTRING_START) strings
811
811
| NUMBER
812
812
| &'(' (tuple | group | genexp)
813
813
| &'[' (list | listcomp)
@@ -877,7 +877,26 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
877
877
# LITERALS
878
878
# ========
879
879
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) }
881
900
882
901
list[expr_ty]:
883
902
| '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
@@ -1118,6 +1137,8 @@ invalid_expression:
1118
1137
_PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
1119
1138
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
1120
1139
| 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") }
1121
1142
1122
1143
invalid_named_expression(memo):
1123
1144
| a=expression ':=' expression {
@@ -1241,7 +1262,7 @@ invalid_group:
1241
1262
invalid_import:
1242
1263
| a='import' dotted_name 'from' dotted_name {
1243
1264
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
1244
-
1265
+
1245
1266
invalid_import_from_targets:
1246
1267
| import_from_as_names ',' NEWLINE {
1247
1268
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
@@ -1335,3 +1356,24 @@ invalid_kvpair:
1335
1356
| expression a=':' &('}'|',') {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
1336
1357
invalid_starred_expression:
1337
1358
| 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