Skip to content

Commit

Permalink
refactor: Use Cursor directly instead of helper DescendentsUnordered
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Oct 28, 2023
1 parent 93fe84c commit 6ff92c2
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 123 deletions.
12 changes: 7 additions & 5 deletions crates/codegen/parser/runtime/src/support/choice_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::ControlFlow;

use crate::{cst, kinds::TokenKind, parse_error::ParseError, text_index::TextIndex};

use super::{context::Marker, parser_result::DescendentsIter, ParserContext, ParserResult};
use super::{context::Marker, ParserContext, ParserResult};

/// Starting from a given position in the input, this helper will try to pick (and remember) a best match. Settles on
/// a first full match if possible, otherwise on the best incomplete match.
Expand Down Expand Up @@ -145,9 +145,11 @@ pub fn total_not_skipped_span(result: &ParserResult) -> usize {
};

nodes
.descendents()
.filter_map(cst::Node::as_token)
.filter(|tok| tok.kind != TokenKind::SKIPPED)
.map(|tok| tok.text.len())
.iter()
.flat_map(cst::Node::cursor)
.filter_map(|node| match node {
cst::Node::Token(token) if token.kind != TokenKind::SKIPPED => Some(token.text.len()),
_ => None,
})
.sum()
}
3 changes: 2 additions & 1 deletion crates/codegen/parser/runtime/src/support/parser_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ where
errors.len() > 0,
topmost_rule
.children
.descendents()
.iter()
.flat_map(cst::Node::cursor)
.any(|x| x.as_token_with_kind(&[TokenKind::SKIPPED]).is_some())
);

Expand Down
37 changes: 2 additions & 35 deletions crates/codegen/parser/runtime/src/support/parser_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,6 @@ impl ParserResult {
}
}

// DFS iterator over the descendents of a node.
pub(crate) struct DescendentsUnordered<'a> {
stack: Vec<&'a cst::Node>,
}

impl<'a> Iterator for DescendentsUnordered<'a> {
type Item = &'a cst::Node;

fn next(&mut self) -> Option<&'a cst::Node> {
self.stack.pop().map(|node| {
if let Some(node) = node.as_rule() {
self.stack.extend(node.children.iter());
}

node
})
}
}

pub(crate) trait DescendentsIter<'a> {
fn descendents(self) -> DescendentsUnordered<'a>;
}

impl<'a, T> DescendentsIter<'a> for T
where
T: IntoIterator<Item = &'a cst::Node> + 'a,
{
fn descendents(self) -> DescendentsUnordered<'a> {
DescendentsUnordered {
stack: self.into_iter().collect(),
}
}
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Match {
pub nodes: Vec<cst::Node>,
Expand All @@ -126,7 +92,8 @@ impl Match {

pub fn is_full_recursive(&self) -> bool {
self.nodes
.descendents()
.iter()
.flat_map(cst::Node::cursor)
.all(|node| node.as_token_with_kind(&[TokenKind::SKIPPED]).is_none())
}
}
Expand Down

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

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

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

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

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

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

0 comments on commit 6ff92c2

Please sign in to comment.