8000 Move format spec and conversion char into their own rules · python/cpython@ef5d5ab · GitHub
[go: up one dir, main page]

Skip to content

Commit ef5d5ab

Browse files
committed
Move format spec and conversion char into their own rules
1 parent 3eb7616 commit ef5d5ab

File tree

4 files changed

+1903
-1907
lines changed

4 files changed

+1903
-1907
lines changed

Grammar/python.gram

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,18 +880,15 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
880880
fstring_middle[expr_ty]:
881881
| fstring_replacement_field
882882
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
883-
# There are some shenanigans with the fstring_format_spec: Don't try to put it in its own rule
884-
# or otherwise it will try to parse the first token with the regular tokenizer mode (due to the EXTRA).
885-
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
886883
fstring_replacement_field[expr_ty]:
887-
| '{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
888-
conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) ? NULL : conv }
889-
] format=[
890-
':' spec=fstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
891-
] '}' {
884+
| '{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[fstring_conversion] format=[fstring_full_format_spec] '}' {
892885
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
893886
}
894887
| 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 }
895892
fstring_format_spec[expr_ty]:
896893
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
897894
| fstring_replacement_field

Parser/action_helpers.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,16 +965,15 @@ _PyPegen_check_legacy_stmt(Parser *p, expr_ty name) {
965965
return 0;
966966
}
967967

968-
int
968+
expr_ty
969969
_PyPegen_check_fstring_conversion(Parser *p, Token* symbol, expr_ty conv) {
970970
if (symbol->lineno != conv->lineno || symbol->end_col_offset != conv->col_offset) {
971-
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
971+
return RAISE_SYNTAX_ERROR_KNOWN_RANGE(
972972
symbol, conv,
973973
"f-string: conversion type must come right after the exclamanation mark"
974974
);
975-
return 1;
976975
}
977-
return 0;
976+
return conv;
978977
}
979978

980979

0 commit comments

Comments
 (0)
0