10000 gh-111658: Improve parser to accept complicated block of code under interactive REPL by hsfzxjy · Pull Request #111659 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111658: Improve parser to accept complicated block of code under interactive REPL #111659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(
statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | a[asdl_stmt_seq*]=simple_stmts { a }

statement_newline[asdl_stmt_seq*]:
| a=compound_stmt NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
| a[asdl_seq*]=compound_stmt+ b=(simple_stmts | NEWLINE { _Py_asdl_stmt_seq_new(0, p->arena) }) { (asdl_stmt_seq*)_PyPegen_seq_concat(p, a, (asdl_seq*)b) }
| simple_stmts
| NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, CHECK(stmt_ty, _PyAST_Pass(EXTRA))) }
| ENDMARKER { _PyPegen_interactive_exit(p) }
Expand Down
27 changes: 27 additions & 0 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ _PyPegen_seq_append_to_end(Parser *p, asdl_seq *seq, void *a)
return new_seq;
}

/* Creates a new sequence that concats seq1 and seq2 */
asdl_seq *
_PyPegen_seq_concat(Parser *p, asdl_seq *seq1, asdl_seq *seq2)
{
if (!seq1) {
return seq2;
}

if (!seq2) {
return seq1;
}

Py_ssize_t size1 = asdl_seq_LEN(seq1), size2 = asdl_seq_LEN(seq2);
asdl_seq *new_seq = (asdl_seq*)_Py_asdl_generic_seq_new(size1 + size2, p->arena);
if (!new_seq) {
return NULL;
}

for (Py_ssize_t i = 0; i < size1; i++) {
asdl_seq_SET_UNTYPED(new_seq, i, asdl_seq_GET_UNTYPED(seq1, i));
}
for (Py_ssize_t i = 0; i < size2; i++) {
asdl_seq_SET_UNTYPED(new_seq, i + size1, asdl_seq_GET_UNTYPED(seq2, i));
}
return new_seq;
}

static Py_ssize_t
_get_flattened_seq_size(asdl_seq *seqs)
{
Expand Down
Loading
0