Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Dec 3, 2023
1 parent dc62c2d commit 82b9683
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 84 deletions.
43 changes: 15 additions & 28 deletions crates/codegen/parser/runtime/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,7 @@ impl Cursor {
return false;
}

if !self.go_to_first_child() {
return self.go_to_next_non_descendent();
}

true
self.go_to_first_child() || self.go_to_next_non_descendent()
}

/// Attempts to go to current node's next non-descendent.
Expand Down Expand Up @@ -199,6 +195,7 @@ impl Cursor {
return false;
}
}

while self.go_to_last_child() {}

true
Expand Down Expand Up @@ -344,7 +341,7 @@ impl Cursor {

if self.current.child_number > 0 {
if let Some(parent_path_element) = self.path.last() {
let new_child_number = self.current.child_number + 1;
let new_child_number = self.current.child_number - 1;
let new_child = parent_path_element.rule_node.children[new_child_number].clone();

self.current = PathNode {
Expand All @@ -363,47 +360,37 @@ impl Cursor {
///
/// Returns `false` if the cursor is finished and at the root.
pub fn go_to_next_token(&mut self) -> bool {
while self.go_to_next() {
if matches!(&self.current.node, Node::Token(_)) {
return true;
}
}

false
self.go_to_next_matching(|node| matches!(node, Node::Token(_)))
}

/// Attempts to go to the next token with any of the given kinds, according to the DFS pre-order traversal.
///
/// Returns `false` if the cursor is finished and at the root.
pub fn go_to_next_token_with_kinds(&mut self, kinds: &[TokenKind]) -> bool {
while self.go_to_next() {
if matches!(&self.current.node, Node::Token(token) if kinds.contains(&token.kind)) {
return true;
}
}

false
self.go_to_next_matching(
|node| matches!(node, Node::Token(token) if kinds.contains(&token.kind)),
)
}

/// Attempts to go to the next rule, according to the DFS pre-order traversal.
///
/// Returns `false` if the cursor is finished and at the root.
pub fn go_to_next_rule(&mut self) -> bool {
while self.go_to_next() {
if matches!(&self.current.node, Node::Rule(_)) {
return true;
}
}

false
self.go_to_next_matching(|node| matches!(node, Node::Rule(_)))
}

/// Attempts to go to the next rule with any of the given kinds, according to the DFS pre-order traversal.
///
/// Returns `false` if the cursor is finished and at the root.
pub fn go_to_next_rule_with_kinds(&mut self, kinds: &[RuleKind]) -> bool {
self.go_to_next_matching(
|node| matches!(node, Node::Rule(rule) if kinds.contains(&rule.kind)),
)
}

fn go_to_next_matching(&mut self, pred: impl Fn(&Node) -> bool) -> bool {
while self.go_to_next() {
if matches!(&self.current.node, Node::Rule(rule) if kinds.contains(&rule.kind)) {
if pred(&self.current.node) {
return true;
}
}
Expand Down
43 changes: 15 additions & 28 deletions crates/solidity/outputs/cargo/crate/src/generated/cursor.rs

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

43 changes: 15 additions & 28 deletions crates/solidity/outputs/npm/crate/src/generated/cursor.rs

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

0 comments on commit 82b9683

Please sign in to comment.