Skip to content

Commit

Permalink
No names in scanners
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyBlakey committed Feb 26, 2023
1 parent 4700aa4 commit 03dcd38
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 46 deletions.
27 changes: 14 additions & 13 deletions crates/codegen/parser/src/combinator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ impl<'context> CombinatorNode<'context> {
tree: &'context CombinatorTree<'context>,
scanner: &ScannerRef,
) -> &'context CombinatorNode<'context> {
let name = scanner.name.clone();

if let Some(filter) = CharSet::from_scanner(tree, scanner.clone()) {
return tree
.context
.alloc_node(Self::CharacterFilter { name, filter });
.alloc_node(Self::CharacterFilter { name: None, filter });
}

if let Some(trie) = trie::from_scanner(tree, scanner.clone()) {
Expand Down Expand Up @@ -128,7 +126,10 @@ impl<'context> CombinatorNode<'context> {
if elements.len() == 1 {
return elements.pop().unwrap();
} else {
Self::Choice { name, elements }
Self::Choice {
name: None,
elements,
}
}
}

Expand All @@ -137,7 +138,7 @@ impl<'context> CombinatorNode<'context> {
expression,
close,
} => Self::DelimitedBy {
name,
name: None,
open: open.clone(),
expr: Self::from_scanner(tree, expression),
close: close.clone(),
Expand All @@ -153,14 +154,14 @@ impl<'context> CombinatorNode<'context> {

ScannerDefinition::Not(_) => {
if let Some(filter) = CharSet::from_scanner(tree, scanner.clone()) {
Self::CharacterFilter { name, filter }
Self::CharacterFilter { name: None, filter }
} else {
unimplemented!("¬ is only supported on characters or sets thereof")
}
}

ScannerDefinition::OneOrMore(expr) => Self::OneOrMore {
name,
name: None,
expr: Self::from_scanner(tree, expr),
},

Expand All @@ -169,7 +170,7 @@ impl<'context> CombinatorNode<'context> {
},

ScannerDefinition::Range { .. } => Self::CharacterFilter {
name,
name: None,
filter: CharSet::from_scanner(tree, scanner.clone()).unwrap(),
},

Expand All @@ -182,7 +183,7 @@ impl<'context> CombinatorNode<'context> {
min,
max,
} => Self::Repeated {
name,
name: None,
expr: Self::from_scanner(tree, expression),
min: *min,
max: *max,
Expand All @@ -192,19 +193,19 @@ impl<'context> CombinatorNode<'context> {
expression,
separator,
} => Self::SeparatedBy {
name,
name: None,
expr: Self::from_scanner(tree, expression),
separator: separator.clone(),
},

ScannerDefinition::Sequence(exprs) => Self::Sequence {
name,
name: None,
elements: exprs.iter().map(|e| Self::from_scanner(tree, e)).collect(),
},

ScannerDefinition::Terminal(_) => {
if let Some(filter) = CharSet::from_scanner(tree, scanner.clone()) {
Self::CharacterFilter { name, filter }
Self::CharacterFilter { name: None, filter }
} else {
Self::TerminalTrie {
trie: trie::from_scanner(tree, scanner.clone()).unwrap(),
Expand All @@ -213,7 +214,7 @@ impl<'context> CombinatorNode<'context> {
}

ScannerDefinition::ZeroOrMore(expr) => Self::ZeroOrMore {
name,
name: None,
expr: Self::from_scanner(tree, expr),
},
})
Expand Down
26 changes: 3 additions & 23 deletions crates/codegen/parser/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ impl<P: Clone + Debug> Trie<P> {
self.subtries.is_empty() && self.payload.is_none()
}

pub fn any_payload<F>(&self, f: F) -> bool
where
F: Fn(&P) -> bool,
{
// TODO: This is not efficient
self.iter().any(|(_, payload)| f(payload))
}

pub fn iter(&self) -> TrieIterator<P> {
TrieIterator {
subtrie_iterators: vec![self.subtries.iter()],
Expand Down Expand Up @@ -120,23 +112,16 @@ pub fn from_scanner(tree: &CombinatorTree, scanner: ScannerRef) -> Option<Termin
trie: &mut TerminalTrie,
tree: &CombinatorTree,
scanner: ScannerRef,
force_all_entries_to_be_named: bool,
) -> bool {
match &scanner.definition {
ScannerDefinition::Choice(exprs) => exprs.iter().fold(true, |accum, scanner| {
accum && collect_terminals(trie, tree, scanner.clone(), force_all_entries_to_be_named)
accum && collect_terminals(trie, tree, scanner.clone())
}),

ScannerDefinition::Terminal(string) => {
trie.insert(
&string,
scanner.name.clone().or_else(|| {
if !force_all_entries_to_be_named {
None
} else {
Some(naming::name_of_terminal_string(&string))
}
}),
None
);
true
}
Expand All @@ -158,19 +143,14 @@ pub fn from_scanner(tree: &CombinatorTree, scanner: ScannerRef) -> Option<Termin
)
}
},
force_all_entries_to_be_named,
),

_ => false,
}
}

let mut trie = TerminalTrie::new();
if collect_terminals(&mut trie, tree, scanner.clone(), false) && !trie.is_empty() {
if trie.any_payload(Option::is_some) {
trie = TerminalTrie::new();
collect_terminals(&mut trie, tree, scanner, true);
}
if collect_terminals(&mut trie, tree, scanner.clone()) && !trie.is_empty() {
Some(trie)
} else {
None
Expand Down
4 changes: 0 additions & 4 deletions crates/codegen/schema/generated/topic.schema.json

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

2 changes: 0 additions & 2 deletions crates/codegen/schema/src/types/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub type ScannerRef = std::rc::Rc<Scanner>;
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq, Eq, Hash)]
#[serde(deny_unknown_fields)]
pub struct Scanner {
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub lookahead: Option<ScannerRef>,
#[serde(flatten)]
Expand Down
4 changes: 0 additions & 4 deletions crates/codegen/schema/src/validation/ast/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::{node::Node, production::ConcreteAbstractPair};
pub type ScannerRef = Rc<Scanner>;

pub struct Scanner {
pub name: Option<Node<String>>,
pub lookahead: Option<ScannerRef>,
pub definition: Node<ScannerDefinition>,
}
Expand All @@ -17,9 +16,6 @@ impl ConcreteAbstractPair for Scanner {

fn new(cst_node: &cst::NodeRef, value: Rc<Self::AbstractType>) -> Rc<Self> {
return Rc::new(Self {
name: value.name.clone().and_then(|name| {
return Some(Node::new(cst_node.get("name"), name));
}),
lookahead: value.lookahead.clone().and_then(|lookahead| {
return Some(Scanner::new(cst_node.get("lookahead"), lookahead));
}),
Expand Down

0 comments on commit 03dcd38

Please sign in to comment.