@@ -916,7 +916,8 @@ fstring_middle[expr_ty]:
916916 | t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
917917fstring_replacement_field[expr_ty]:
918918 | '{' a=annotated_rhs debug_expr='='? conversion=[fstring_conversion] format=[fstring_full_format_spec] rbrace='}' {
919- _PyPegen_formatted_value(p, a, debug_expr, conversion, format, rbrace, EXTRA) }
919+ (TOK_GET_MODE(p->tok)->tstring ? _PyPegen_interpolation : _PyPegen_formatted_value)(
920+ p, a, debug_expr, conversion, format, rbrace, EXTRA) }
920921 | invalid_replacement_field
921922fstring_conversion[ResultTokenWithMetadata*]:
922923 | conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) }
@@ -928,15 +929,8 @@ fstring_format_spec[expr_ty]:
928929fstring[expr_ty]:
929930 | a=FSTRING_START b=fstring_middle* c=FSTRING_END { _PyPegen_joined_str(p, a, (asdl_expr_seq*)b, c) }
930931
931- tstring_middle[expr_ty]:
932- | tstring_replacement_field
933- | t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
934- tstring_replacement_field[expr_ty]:
935- | '{' a=annotated_rhs debug_expr='='? conversion=[fstring_conversion] format=[fstring_full_format_spec] rbrace='}' {
936- _PyPegen_interpolation(p, a, debug_expr, conversion, format, rbrace, EXTRA) }
937- | invalid_replacement_field
938- tstring[expr_ty]:
939- | a=TSTRING_START b=tstring_middle* c=FSTRING_END { _PyPegen_template_str(p, a, (asdl_expr_seq*)b, c) }
932+ tstring[expr_ty] (memo):
933+ | a=TSTRING_START b=fstring_middle* c=FSTRING_END { _PyPegen_template_str(p, a, (asdl_expr_seq*)b, c) }
940934
941935string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_string(p, s) }
942936strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
@@ -1196,7 +1190,8 @@ invalid_expression:
11961190 RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
11971191 | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
11981192 | a='lambda' [lambda_params] b=':' &FSTRING_MIDDLE {
1199- RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") }
1193+ RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "%c-string: lambda expressions are not allowed without parentheses",
1194+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
12001195
12011196invalid_named_expression(memo):
12021197 | a=expression ':=' expression {
@@ -1439,26 +1434,36 @@ invalid_starred_expression:
14391434 | '*' { RAISE_SYNTAX_ERROR("Invalid star expression") }
14401435
14411436invalid_replacement_field:
1442- | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
1443- | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }
1444- | '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before ':'") }
1445- | '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '}'") }
1446- | '{' !annotated_rhs { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting a valid expression after '{'")}
1437+ | '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "%c-string: valid expression required before '='",
1438+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
1439+ | '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "%c-string: valid expression required before '!'",
1440+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
1441+ | '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "%c-string: valid expression required before ':'",
1442+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
1443+ | '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "%c-string: valid expression required before '}'",
1444+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
1445+ | '{' !annotated_rhs { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting a valid expression after '{'",
1446+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f')}
14471447 | '{' annotated_rhs !('=' | '!' | ':' | '}') {
1448- PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '=', or '!', or ':', or '}'") }
1448+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting '=', or '!', or ':', or '}'",
1449+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
14491450 | '{' annotated_rhs '=' !('!' | ':' | '}') {
1450- PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '!', or ':', or '}'") }
1451+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting '!', or ':', or '}'",
1452+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
14511453 | '{' annotated_rhs '='? invalid_conversion_character
14521454 | '{' annotated_rhs '='? ['!' NAME] !(':' | '}') {
1453- PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting ':' or '}'") }
1455+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting ':' or '}'",
1456+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
14541457 | '{' annotated_rhs '='? ['!' NAME] ':' fstring_format_spec* !'}' {
1455- PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}', or format specs") }
1458+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting '}', or format specs",
1459+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
14561460 | '{' annotated_rhs '='? ['!' NAME] !'}' {
1457- PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '}'") }
1461+ PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c-string: expecting '}'",
1462+ TOK_GET_MODE(p->tok)->tstring ? 't' : 'f') }
14581463
14591464invalid_conversion_character:
1460- | '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f -string: missing conversion character") }
1461- | '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f -string: invalid conversion character") }
1465+ | '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c -string: missing conversion character") }
1466+ | '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("%c -string: invalid conversion character") }
14621467
14631468invalid_arithmetic:
14641469 | sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
0 commit comments