Skip to content

Commit 7b9b973

Browse files
authored
Merge pull request python#55 from lysnikolaou/fstring-cleanup-grammar-actions
Cleanup grammar and actions // Remove unnecessary fstring code
2 parents 00e47ae + f1ba3f7 commit 7b9b973

File tree

6 files changed

+1358
-2566
lines changed

6 files changed

+1358
-2566
lines changed

Grammar/python.gram

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -877,30 +877,29 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
877877
# LITERALS
878878
# ========
879879

880-
gstring_middle[expr_ty]:
881-
| gstring_replacement_field
882-
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token2(p, t) }
883-
# There are some shenanigans with the gstring_format_spec: Don't try to put it in its own rule
880+
fstring_middle[expr_ty]:
881+
| fstring_replacement_field
882+
| 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
884884
# or otherwise it will try to parse the first token with the regular tokenizer mode (due to the EXTRA).
885885
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
886-
gstring_replacement_field[expr_ty]:
886+
fstring_replacement_field[expr_ty]:
887887
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
888888
conv_token="!" conv=NAME { _PyPegen_check_fstring_conversion(p, conv_token, conv) ? NULL : conv }
889889
] format=[
890-
':' spec=gstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
890+
':' spec=fstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
891891
] &&'}' {
892892
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
893893
}
894894
| invalid_replacement_field
895-
gstring_format_spec[expr_ty]:
896-
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token2(p, t) }
897-
| gstring_replacement_field
898-
gstring[expr_ty]:
899-
| a=FSTRING_START b=gstring_middle* c=FSTRING_END { deal_with_gstring2(p, a, (asdl_expr_seq*)b, c) }
900-
901-
string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_token(p, s) }
902-
strings[expr_ty] (memo): a[asdl_expr_seq*]=(gstring|string)+ { _PyPegen_concatenate_strings2(p, a, EXTRA) }
903-
# strings[expr_ty] (memo): a=STRING+ { _PyPegen_concatenate_strings(p, a) }
895+
fstring_format_spec[expr_ty]:
896+
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token(p, t) }
897+
| fstring_replacement_field
898+
fstring[expr_ty]:
899+
| a=FSTRING_START b=fstring_middle* c=FSTRING_END { _PyPegen_joined_str(p, a, (asdl_expr_seq*)b, c) }
900+
901+
string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_string(p, s) }
902+
strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
904903

905904
list[expr_ty]:
906905
| '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }

Parser/action_helpers.c

Lines changed: 8 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -854,96 +854,6 @@ _PyPegen_seq_delete_starred_exprs(Parser *p, asdl_seq *kwargs)
854854
return new_seq;
855855
}
856856

857-
expr_ty
858-
_PyPegen_concatenate_strings(Parser *p, asdl_seq *strings)
859-
{
860-
Py_ssize_t len = asdl_seq_LEN(strings);
861-
assert(len > 0);
862-
863-
Token *first = asdl_seq_GET_UNTYPED(strings, 0);
864-
Token *last = asdl_seq_GET_UNTYPED(strings, len - 1);
865-
866-
int bytesmode = 0;
867-
PyObject *bytes_str = NULL;
868-
869-
FstringParser state;
870-
_PyPegen_FstringParser_Init(&state);
871-
872-
for (Py_ssize_t i = 0; i < len; i++) {
873-
Token *t = asdl_seq_GET_UNTYPED(strings, i);
874-
875-
int this_bytesmode;
876-
int this_rawmode;
877-
PyObject *s;
878-
const char *fstr;
879-
Py_ssize_t fstrlen = -1;
880-
881-
if (_PyPegen_parsestr(p, &this_bytesmode, &this_rawmode, &s, &fstr, &fstrlen, t) != 0) {
882-
goto error;
883-
}
884-
885-
/* Check that we are not mixing bytes with unicode. */
886-
if (i != 0 && bytesmode != this_bytesmode) {
887-
RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals");
888-
Py_XDECREF(s);
889-
goto error;
890-
}
891-
bytesmode = this_bytesmode;
892-
893-
if (fstr != NULL) {
894-
assert(s == NULL && !bytesmode);
895-
896-
int result = _PyPegen_FstringParser_ConcatFstring(p, &state, &fstr, fstr + fstrlen,
897-
this_rawmode, 0, first, t, last);
898-
if (result < 0) {
899-
goto error;
900-
}
901-
}
902-
else {
903-
/* String or byte string. */
904-
assert(s != NULL && fstr == NULL);
905-
assert(bytesmode ? PyBytes_CheckExact(s) : PyUnicode_CheckExact(s));
906-
907-
if (bytesmode) {
908-
if (i == 0) {
909-
bytes_str = s;
910-
}
911-
else {
912-
PyBytes_ConcatAndDel(&bytes_str, s);
913-
if (!bytes_str) {
914-
goto error;
915-
}
916-
}
917-
}
918-
else {
919-
/* This is a regular string. Concatenate it. */
920-
if (_PyPegen_FstringParser_ConcatAndDel(&state, s) < 0) {
921-
goto error;
922-
}
923-
}
924-
}
925-
}
926-
927-
if (bytesmode) {
928-
if (_PyArena_AddPyObject(p->arena, bytes_str) < 0) {
929-
goto error;
930-
}
931-
return _PyAST_Constant(bytes_str, NULL, first->lineno,
932-
first->col_offset, last->end_lineno,
933-
last->end_col_offset, p->arena);
934-
}
935-
936-
return _PyPegen_FstringParser_Finish(p, &state, first, last);
937-
938-
error:
939-
Py_XDECREF(bytes_str);
940-
_PyPegen_FstringParser_Dealloc(&state);
941-
if (PyErr_Occurred()) {
942-
_Pypegen_raise_decode_error(p);
943-
}
944-
return NULL;
945-
}
946-
947857
expr_ty
948858
_PyPegen_ensure_imaginary(Parser *p, expr_ty exp)
949859
{
@@ -1325,7 +1235,8 @@ _PyPegen_decode_fstring_part(Parser* p, int is_raw, expr_ty constant) {
13251235
len = strlen(bstr);
13261236
}
13271237

1328-
PyObject *str = _PyPegen_DecodeFstring(p, is_raw, bstr, len, NULL);
1238+
is_raw = is_raw || strchr(bstr, '\\') == NULL;
1239+
PyObject *str = _PyPegen_decode_string(p, is_raw, bstr, len, NULL);
13291240
if (str == NULL) {
13301241
_Pypegen_raise_decode_error(p);
13311242
return NULL;
@@ -1380,7 +1291,7 @@ unpack_top_level_joined_strs(Parser *p, asdl_expr_seq *raw_expressions)
13801291
}
13811292

13821293
expr_ty
1383-
deal_with_gstring2(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b) {
1294+
_PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b) {
13841295
asdl_expr_seq *expr = unpack_top_level_joined_strs(p, raw_expressions);
13851296
Py_ssize_t n_items = asdl_seq_LEN(expr);
13861297

@@ -1411,8 +1322,7 @@ deal_with_gstring2(Parser *p, Token* a, asdl_expr_seq* raw_expressions, Token*b)
14111322
p->arena);
14121323
}
14131324

1414-
// Hack: remove!
1415-
expr_ty _PyPegen_constant_from_token2(Parser* p, Token* tok) {
1325+
expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
14161326
char* bstr = PyBytes_AsString(tok->bytes);
14171327
if (bstr == NULL) {
14181328
return NULL;
@@ -1430,18 +1340,13 @@ expr_ty _PyPegen_constant_from_token2(Parser* p, Token* tok) {
14301340
p->arena);
14311341
}
14321342

1433-
// Hack: remove!
1434-
expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok) {
1343+
expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok) {
14351344
char* the_str = PyBytes_AsString(tok->bytes);
14361345
if (the_str == NULL) {
14371346
return NULL;
14381347
}
1439-
int this_bytesmode;
1440-
int this_rawmode;
1441-
PyObject *s;
1442-
const char *fstr;
1443-
Py_ssize_t fstrlen = -1;
1444-
if (_PyPegen_parsestr(p, &this_bytesmode, &this_rawmode, &s, &fstr, &fstrlen, tok) != 0) {
1348+
PyObject *s = _PyPegen_parse_string(p, tok);
1349+
if (s == NULL) {
14451350
_Pypegen_raise_decode_error(p);
14461351
return NULL;
14471352
}
@@ -1514,7 +1419,7 @@ expr_ty _PyPegen_formatted_value(Parser *p, expr_ty expression, Token *debug, ex
15141419
}
15151420

15161421
expr_ty
1517-
_PyPegen_concatenate_strings2(Parser *p, asdl_expr_seq *strings,
1422+
_PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
15181423
int lineno, int col_offset, int end_lineno,
15191424
int end_col_offset, PyArena *arena)
15201425
{

0 commit comments

Comments
 (0)