🎨 Use base struct syntax to clean up grammar expectations
This commit is contained in:
parent
584b55df8d
commit
82ceebc10d
7 changed files with 33 additions and 130 deletions
|
|
@ -26,7 +26,7 @@ pub(crate) enum PrecedenceEntry {
|
|||
Symbol(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct InputGrammar {
|
||||
pub name: String,
|
||||
pub variables: Vec<Variable>,
|
||||
|
|
@ -66,7 +66,7 @@ pub(crate) struct ProductionStep {
|
|||
pub field_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct Production {
|
||||
pub steps: Vec<ProductionStep>,
|
||||
pub dynamic_precedence: i32,
|
||||
|
|
@ -159,15 +159,6 @@ impl Production {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Production {
|
||||
fn default() -> Self {
|
||||
Production {
|
||||
dynamic_precedence: 0,
|
||||
steps: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Variable {
|
||||
pub fn named(name: &str, rule: Rule) -> Self {
|
||||
|
|
|
|||
|
|
@ -725,14 +725,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_simple() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "v1".to_string(),
|
||||
|
|
@ -755,6 +747,7 @@ mod tests {
|
|||
rule: Rule::string("y"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(node_types.len(), 3);
|
||||
|
|
@ -821,14 +814,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_simple_extras() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
extra_symbols: vec![Rule::named("v3")],
|
||||
word_token: None,
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "v1".to_string(),
|
||||
|
|
@ -852,6 +838,7 @@ mod tests {
|
|||
rule: Rule::string("y"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(node_types.len(), 4);
|
||||
|
|
@ -928,13 +915,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_supertypes() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
supertype_symbols: vec!["_v2".to_string()],
|
||||
variables: vec![
|
||||
Variable {
|
||||
|
|
@ -962,6 +942,7 @@ mod tests {
|
|||
rule: Rule::string("y"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1016,14 +997,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_for_children_without_fields() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "v1".to_string(),
|
||||
|
|
@ -1054,6 +1027,7 @@ mod tests {
|
|||
rule: Rule::string("y"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1115,13 +1089,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_inlined_rules() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables_to_inline: vec!["v2".to_string()],
|
||||
variables: vec![
|
||||
Variable {
|
||||
|
|
@ -1141,6 +1108,7 @@ mod tests {
|
|||
rule: Rule::string("b"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1171,14 +1139,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_for_aliased_nodes() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "thing".to_string(),
|
||||
|
|
@ -1220,6 +1180,7 @@ mod tests {
|
|||
rule: Rule::pattern("[\\w-]+"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(node_types.iter().find(|t| t.kind == "foo_identifier"), None);
|
||||
|
|
@ -1248,14 +1209,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_multiple_valued_fields() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "a".to_string(),
|
||||
|
|
@ -1279,6 +1232,7 @@ mod tests {
|
|||
rule: Rule::string("c"),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1317,14 +1271,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_fields_on_hidden_tokens() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![Variable {
|
||||
name: "script".to_string(),
|
||||
kind: VariableType::Named,
|
||||
|
|
@ -1333,6 +1279,7 @@ mod tests {
|
|||
Rule::field("b".to_string(), Rule::pattern("bye")),
|
||||
]),
|
||||
}],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1350,14 +1297,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_multiple_rules_same_alias_name() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "script".to_string(),
|
||||
|
|
@ -1386,6 +1325,7 @@ mod tests {
|
|||
]),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
@ -1477,14 +1417,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_node_types_with_tokens_aliased_to_match_rules() {
|
||||
let node_types = get_node_types(InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
variables: vec![
|
||||
Variable {
|
||||
name: "a".to_string(),
|
||||
|
|
@ -1508,6 +1440,7 @@ mod tests {
|
|||
]),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
|
|
|
|||
|
|
@ -283,13 +283,7 @@ mod tests {
|
|||
fn build_grammar(variables: Vec<Variable>) -> ExtractedSyntaxGrammar {
|
||||
ExtractedSyntaxGrammar {
|
||||
variables,
|
||||
extra_symbols: Vec::new(),
|
||||
external_tokens: Vec::new(),
|
||||
supertype_symbols: Vec::new(),
|
||||
expected_conflicts: Vec::new(),
|
||||
variables_to_inline: Vec::new(),
|
||||
precedence_orderings: Vec::new(),
|
||||
word_token: None,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -197,13 +197,7 @@ mod tests {
|
|||
}],
|
||||
},
|
||||
],
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![],
|
||||
word_token: None,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let lexical_grammar = LexicalGrammar {
|
||||
|
|
|
|||
|
|
@ -493,13 +493,7 @@ mod test {
|
|||
fn build_grammar(variables: Vec<Variable>) -> InternedGrammar {
|
||||
InternedGrammar {
|
||||
variables,
|
||||
extra_symbols: Vec::new(),
|
||||
external_tokens: Vec::new(),
|
||||
supertype_symbols: Vec::new(),
|
||||
expected_conflicts: Vec::new(),
|
||||
variables_to_inline: Vec::new(),
|
||||
precedence_orderings: Vec::new(),
|
||||
word_token: None,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -243,13 +243,7 @@ mod tests {
|
|||
InputGrammar {
|
||||
variables,
|
||||
name: "the_language".to_string(),
|
||||
extra_symbols: Vec::new(),
|
||||
external_tokens: Vec::new(),
|
||||
supertype_symbols: Vec::new(),
|
||||
expected_conflicts: Vec::new(),
|
||||
variables_to_inline: Vec::new(),
|
||||
precedence_orderings: Vec::new(),
|
||||
word_token: None,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,21 @@ pub(crate) struct ExtractedLexicalGrammar {
|
|||
pub separators: Vec<Rule>,
|
||||
}
|
||||
|
||||
impl<T, U> Default for IntermediateGrammar<T, U> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
variables: Default::default(),
|
||||
extra_symbols: Default::default(),
|
||||
expected_conflicts: Default::default(),
|
||||
precedence_orderings: Default::default(),
|
||||
external_tokens: Default::default(),
|
||||
variables_to_inline: Default::default(),
|
||||
supertype_symbols: Default::default(),
|
||||
word_token: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Transform an input grammar into separate components that are ready
|
||||
/// for parse table construction.
|
||||
pub(crate) fn prepare_grammar(
|
||||
|
|
@ -158,13 +173,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_validate_precedences_with_undeclared_precedence() {
|
||||
let grammar = InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![
|
||||
vec![
|
||||
PrecedenceEntry::Name("a".to_string()),
|
||||
|
|
@ -194,6 +202,7 @@ mod tests {
|
|||
])),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = validate_precedences(&grammar);
|
||||
|
|
@ -206,13 +215,6 @@ mod tests {
|
|||
#[test]
|
||||
fn test_validate_precedences_with_conflicting_order() {
|
||||
let grammar = InputGrammar {
|
||||
name: String::new(),
|
||||
word_token: None,
|
||||
extra_symbols: vec![],
|
||||
external_tokens: vec![],
|
||||
supertype_symbols: vec![],
|
||||
expected_conflicts: vec![],
|
||||
variables_to_inline: vec![],
|
||||
precedence_orderings: vec![
|
||||
vec![
|
||||
PrecedenceEntry::Name("a".to_string()),
|
||||
|
|
@ -242,6 +244,7 @@ mod tests {
|
|||
])),
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let result = validate_precedences(&grammar);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue