From 32099050d6d41ff9538c4f7c4991b66254cad024 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 24 Jul 2020 09:26:54 -0700 Subject: [PATCH] node_types: Fix panic when field is associated with a hidden token Fixes #695 --- cli/src/generate/node_types.rs | 35 +++++++++++++++++++++++++++++++++- cli/src/main.rs | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cli/src/generate/node_types.rs b/cli/src/generate/node_types.rs index 039d7190..7a5768a5 100644 --- a/cli/src/generate/node_types.rs +++ b/cli/src/generate/node_types.rs @@ -216,7 +216,7 @@ pub(crate) fn get_variable_info( .or_insert(ChildQuantity::zero()); // Inherit the types and quantities of hidden children associated with fields. - if child_is_hidden { + if child_is_hidden && child_symbol.is_non_terminal() { let child_variable_info = &result[child_symbol.index]; did_change |= extend_sorted( &mut field_info.types, @@ -352,6 +352,7 @@ pub(crate) fn get_variable_info( for (_, field_info) in variable_info.fields.iter_mut() { field_info.types.retain(child_type_is_visible); } + variable_info.fields.retain(|_, v| !v.types.is_empty()); variable_info .children_without_fields .types @@ -1174,6 +1175,38 @@ mod tests { ); } + #[test] + fn test_node_types_with_fields_on_hidden_tokens() { + let node_types = get_node_types(InputGrammar { + name: String::new(), + extra_symbols: Vec::new(), + external_tokens: Vec::new(), + expected_conflicts: Vec::new(), + variables_to_inline: Vec::new(), + word_token: None, + supertype_symbols: vec![], + variables: vec![Variable { + name: "script".to_string(), + kind: VariableType::Named, + rule: Rule::seq(vec![ + Rule::field("a".to_string(), Rule::pattern("hi")), + Rule::field("b".to_string(), Rule::pattern("bye")), + ]), + }], + }); + + assert_eq!( + node_types, + [NodeInfoJSON { + kind: "script".to_string(), + named: true, + fields: Some(BTreeMap::new()), + children: None, + subtypes: None + }] + ); + } + #[test] fn test_node_types_with_multiple_rules_same_alias_name() { let node_types = get_node_types(InputGrammar { diff --git a/cli/src/main.rs b/cli/src/main.rs index 0668d08d..2f8c6dd5 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -99,7 +99,7 @@ fn run() -> error::Result<()> { .subcommand( SubCommand::with_name("tags") .arg(Arg::with_name("quiet").long("quiet").short("q")) - .arg(Arg::with_name("time").long("quiet").short("t")) + .arg(Arg::with_name("time").long("time").short("t")) .arg(Arg::with_name("scope").long("scope").takes_value(true)) .arg(Arg::with_name("paths-file").long("paths").takes_value(true)) .arg(