From 8156cec33ee2a57102b8004a7da6f0eefb33961c Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sat, 28 Oct 2023 19:17:25 +0200 Subject: [PATCH] WIP: Try to simplify the existing Cursor helpers --- crates/codegen/parser/runtime/src/cursor.rs | 29 ++++--------------- .../cargo/crate/src/generated/cursor.rs | 29 ++++--------------- .../outputs/npm/crate/src/generated/cursor.rs | 29 ++++--------------- 3 files changed, 15 insertions(+), 72 deletions(-) diff --git a/crates/codegen/parser/runtime/src/cursor.rs b/crates/codegen/parser/runtime/src/cursor.rs index 6aefb636eb..de10407054 100644 --- a/crates/codegen/parser/runtime/src/cursor.rs +++ b/crates/codegen/parser/runtime/src/cursor.rs @@ -369,31 +369,19 @@ impl Cursor { } pub fn find_token_with_kind(&mut self, kinds: &[TokenKind]) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_with_kind(kinds).cloned() { - return Some(token_node); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_with_kind(kinds).cloned()) } pub fn find_token_matching) -> bool>( &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_matching(&predicate) { - return Some(token_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_matching(&predicate).cloned()) } pub fn find_rule_with_kind(&mut self, kinds: &[RuleKind]) -> Option> { + // TODO: It doesn't seem to work the same way? + // self.find_map(|node| node.as_rule_with_kind(kinds).cloned()) while !self.is_completed { if let Some(rule_node) = self.current.node.as_rule_with_kind(kinds) { return Some(rule_node.clone()); @@ -408,13 +396,6 @@ impl Cursor { &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(rule_node) = self.current.node.as_rule_matching(&predicate) { - return Some(rule_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_rule_matching(&predicate).cloned()) } } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/cursor.rs b/crates/solidity/outputs/cargo/crate/src/generated/cursor.rs index 8ed48081d6..688f4d5a4e 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/cursor.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/cursor.rs @@ -371,31 +371,19 @@ impl Cursor { } pub fn find_token_with_kind(&mut self, kinds: &[TokenKind]) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_with_kind(kinds).cloned() { - return Some(token_node); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_with_kind(kinds).cloned()) } pub fn find_token_matching) -> bool>( &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_matching(&predicate) { - return Some(token_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_matching(&predicate).cloned()) } pub fn find_rule_with_kind(&mut self, kinds: &[RuleKind]) -> Option> { + // TODO: It doesn't seem to work the same way? + // self.find_map(|node| node.as_rule_with_kind(kinds).cloned()) while !self.is_completed { if let Some(rule_node) = self.current.node.as_rule_with_kind(kinds) { return Some(rule_node.clone()); @@ -410,13 +398,6 @@ impl Cursor { &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(rule_node) = self.current.node.as_rule_matching(&predicate) { - return Some(rule_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_rule_matching(&predicate).cloned()) } } diff --git a/crates/solidity/outputs/npm/crate/src/generated/cursor.rs b/crates/solidity/outputs/npm/crate/src/generated/cursor.rs index 8ed48081d6..688f4d5a4e 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/cursor.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/cursor.rs @@ -371,31 +371,19 @@ impl Cursor { } pub fn find_token_with_kind(&mut self, kinds: &[TokenKind]) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_with_kind(kinds).cloned() { - return Some(token_node); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_with_kind(kinds).cloned()) } pub fn find_token_matching) -> bool>( &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(token_node) = self.current.node.as_token_matching(&predicate) { - return Some(token_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_token_matching(&predicate).cloned()) } pub fn find_rule_with_kind(&mut self, kinds: &[RuleKind]) -> Option> { + // TODO: It doesn't seem to work the same way? + // self.find_map(|node| node.as_rule_with_kind(kinds).cloned()) while !self.is_completed { if let Some(rule_node) = self.current.node.as_rule_with_kind(kinds) { return Some(rule_node.clone()); @@ -410,13 +398,6 @@ impl Cursor { &mut self, predicate: F, ) -> Option> { - while !self.is_completed { - if let Some(rule_node) = self.current.node.as_rule_matching(&predicate) { - return Some(rule_node.clone()); - } - self.go_to_next(); - } - - None + self.find_map(|node| node.as_rule_matching(&predicate).cloned()) } }