Skip to content
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

fail on deeply nested trees #717

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 3 additions & 6 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions native/libcst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ trace = ["peg/trace"]

[dependencies]
paste = "1.0.4"
pyo3 = { version = "0.16.5", optional = true }
pyo3 = { version = "0.16", optional = true }
thiserror = "1.0.23"
peg = "0.8.0"
peg = { git = "https://github.com/zsol/rust-peg", branch = "stackoverflow" }
chic = "1.2.2"
itertools = "0.10.0"
once_cell = "1.5.2"
Expand Down
28 changes: 20 additions & 8 deletions native/libcst/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ impl<'input, 'a: 'input> ParseElem<'input> for TokVec<'a> {
}
}

const MAX_RECURSION_DEPTH: usize = 3000;

parser! {
pub grammar python<'a>(input: &'a str) for TokVec<'a> {

stack_limit 3000;

// Starting Rules

pub rule file(encoding: Option<&str>) -> Module<'input, 'a>
Expand Down Expand Up @@ -1117,7 +1121,7 @@ parser! {

rule strings() -> String<'input, 'a>
= s:(str:tok(STRING, "STRING") t:&_ {(make_string(str), t)}
/ str:fstring() t:&_ {(String::Formatted(str), t)})+ {
/ str:fstring() t:&_ {(String::Formatted(str), t)})+ {?
make_strings(s)
}

Expand Down Expand Up @@ -1171,7 +1175,7 @@ parser! {
// Comprehensions & generators

rule for_if_clauses() -> CompFor<'input, 'a>
= c:for_if_clause()+ { merge_comp_fors(c) }
= c:for_if_clause()+ {? merge_comp_fors(c) }

rule for_if_clause() -> CompFor<'input, 'a>
= asy:_async() f:lit("for") tgt:star_targets() i:lit("in")
Expand Down Expand Up @@ -2240,14 +2244,19 @@ fn make_bare_genexp<'input, 'a>(
}
}

fn merge_comp_fors<'input, 'a>(comp_fors: Vec<CompFor<'input, 'a>>) -> CompFor<'input, 'a> {
fn merge_comp_fors<'input, 'a>(
comp_fors: Vec<CompFor<'input, 'a>>,
) -> GrammarResult<CompFor<'input, 'a>> {
if comp_fors.len() > MAX_RECURSION_DEPTH {
return Err("shallower comprehension");
}
let mut it = comp_fors.into_iter().rev();
let first = it.next().expect("cant merge empty comp_fors");

it.fold(first, |acc, curr| CompFor {
Ok(it.fold(first, |acc, curr| CompFor {
inner_for_in: Some(Box::new(acc)),
..curr
})
}))
}

fn make_left_bracket<'input, 'a>(tok: TokenRef<'input, 'a>) -> LeftSquareBracket<'input, 'a> {
Expand Down Expand Up @@ -2816,10 +2825,13 @@ fn make_string<'input, 'a>(tok: TokenRef<'input, 'a>) -> String<'input, 'a> {

fn make_strings<'input, 'a>(
s: Vec<(String<'input, 'a>, TokenRef<'input, 'a>)>,
) -> String<'input, 'a> {
) -> GrammarResult<String<'input, 'a>> {
if s.len() > MAX_RECURSION_DEPTH {
return Err("shorter concatenated string");
}
let mut strings = s.into_iter().rev();
let (first, _) = strings.next().expect("no strings to make a string of");
strings.fold(first, |acc, (str, tok)| {
Ok(strings.fold(first, |acc, (str, tok)| {
let ret: String<'input, 'a> = String::Concatenated(ConcatenatedString {
left: Box::new(str),
right: Box::new(acc),
Expand All @@ -2828,7 +2840,7 @@ fn make_strings<'input, 'a>(
right_tok: tok,
});
ret
})
}))
}

fn make_fstring_expression<'input, 'a>(
Expand Down