From 6ff92c2b53d6648489208aa909b40a9d15e29703 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sat, 28 Oct 2023 17:45:44 +0200 Subject: [PATCH] refactor: Use Cursor directly instead of helper DescendentsUnordered --- .../runtime/src/support/choice_helper.rs | 12 +++--- .../runtime/src/support/parser_function.rs | 3 +- .../runtime/src/support/parser_result.rs | 37 +------------------ .../src/generated/support/choice_helper.rs | 12 +++--- .../src/generated/support/parser_function.rs | 3 +- .../src/generated/support/parser_result.rs | 37 +------------------ .../src/generated/support/choice_helper.rs | 12 +++--- .../src/generated/support/parser_function.rs | 3 +- .../src/generated/support/parser_result.rs | 37 +------------------ 9 files changed, 33 insertions(+), 123 deletions(-) diff --git a/crates/codegen/parser/runtime/src/support/choice_helper.rs b/crates/codegen/parser/runtime/src/support/choice_helper.rs index 27d6b6713a..8c6cd9ad5e 100644 --- a/crates/codegen/parser/runtime/src/support/choice_helper.rs +++ b/crates/codegen/parser/runtime/src/support/choice_helper.rs @@ -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. @@ -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() } diff --git a/crates/codegen/parser/runtime/src/support/parser_function.rs b/crates/codegen/parser/runtime/src/support/parser_function.rs index a9ca991104..10a45bf89c 100644 --- a/crates/codegen/parser/runtime/src/support/parser_function.rs +++ b/crates/codegen/parser/runtime/src/support/parser_function.rs @@ -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()) ); diff --git a/crates/codegen/parser/runtime/src/support/parser_result.rs b/crates/codegen/parser/runtime/src/support/parser_result.rs index e14889f620..aea9ebdc51 100644 --- a/crates/codegen/parser/runtime/src/support/parser_result.rs +++ b/crates/codegen/parser/runtime/src/support/parser_result.rs @@ -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 + 'a, -{ - fn descendents(self) -> DescendentsUnordered<'a> { - DescendentsUnordered { - stack: self.into_iter().collect(), - } - } -} - #[derive(PartialEq, Eq, Clone, Debug)] pub struct Match { pub nodes: Vec, @@ -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()) } } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/choice_helper.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/choice_helper.rs index 396e7d3f84..0b9ab410a2 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/choice_helper.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/choice_helper.rs @@ -5,7 +5,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. @@ -147,9 +147,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() } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/parser_function.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/parser_function.rs index 51311d9b4e..77ed39338c 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/parser_function.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/parser_function.rs @@ -97,7 +97,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()) ); diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/parser_result.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/parser_result.rs index 59d59d85cf..c177478291 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/parser_result.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/parser_result.rs @@ -77,40 +77,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 + 'a, -{ - fn descendents(self) -> DescendentsUnordered<'a> { - DescendentsUnordered { - stack: self.into_iter().collect(), - } - } -} - #[derive(PartialEq, Eq, Clone, Debug)] pub struct Match { pub nodes: Vec, @@ -128,7 +94,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()) } } diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/choice_helper.rs b/crates/solidity/outputs/npm/crate/src/generated/support/choice_helper.rs index 396e7d3f84..0b9ab410a2 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/choice_helper.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/choice_helper.rs @@ -5,7 +5,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. @@ -147,9 +147,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() } diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/parser_function.rs b/crates/solidity/outputs/npm/crate/src/generated/support/parser_function.rs index 51311d9b4e..77ed39338c 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/parser_function.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/parser_function.rs @@ -97,7 +97,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()) ); diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/parser_result.rs b/crates/solidity/outputs/npm/crate/src/generated/support/parser_result.rs index 59d59d85cf..c177478291 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/parser_result.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/parser_result.rs @@ -77,40 +77,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 + 'a, -{ - fn descendents(self) -> DescendentsUnordered<'a> { - DescendentsUnordered { - stack: self.into_iter().collect(), - } - } -} - #[derive(PartialEq, Eq, Clone, Debug)] pub struct Match { pub nodes: Vec, @@ -128,7 +94,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()) } }