From 51c147053e831ad922753b8a4296cde67fa51dd2 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sat, 15 May 2021 17:06:32 -0500 Subject: [PATCH] feat: error out if an empty string is in the `extras` array This prevents never-ending loops in the parser --- cli/src/generate/parse_grammar.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cli/src/generate/parse_grammar.rs b/cli/src/generate/parse_grammar.rs index 5ae03f3f..fe863276 100644 --- a/cli/src/generate/parse_grammar.rs +++ b/cli/src/generate/parse_grammar.rs @@ -120,7 +120,22 @@ pub(crate) fn parse_grammar(input: &str) -> Result { precedence_orderings.push(ordering); } - let extra_symbols = grammar_json.extras.into_iter().map(parse_rule).collect(); + let extra_symbols = grammar_json + .extras + .into_iter() + .try_fold(Vec::new(), |mut acc, item| { + let rule = parse_rule(item); + if let Rule::String(ref value) = rule { + if value.is_empty() { + return Err(anyhow!( + "Rules in the `extras` array must not contain empty strings" + )); + } + } + acc.push(rule); + Ok(acc) + })?; + let external_tokens = grammar_json.externals.into_iter().map(parse_rule).collect(); Ok(InputGrammar {