aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/skip.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/skip.rs41
1 files changed, 34 insertions, 7 deletions
diff --git a/src/util/skip.rs b/src/util/skip.rs
index 971beb6..3307734 100644
--- a/src/util/skip.rs
+++ b/src/util/skip.rs
@@ -1,7 +1,7 @@
//! Utilities to deal with lists of events.
use crate::token::Token;
-use crate::tokenizer::Event;
+use crate::tokenizer::{Event, EventType};
/// Skip from `index`, optionally past `token_types`.
pub fn opt(events: &[Event], index: usize, token_types: &[Token]) -> usize {
@@ -13,33 +13,60 @@ pub fn opt_back(events: &[Event], index: usize, token_types: &[Token]) -> usize
skip_opt_with_direction(events, index, token_types, false)
}
+pub fn to(events: &[Event], mut index: usize, token_types: &[Token]) -> usize {
+ while index < events.len() {
+ let current = &events[index].token_type;
+
+ if token_types.contains(current) {
+ break;
+ }
+
+ index += 1;
+ }
+
+ index
+}
+
/// Skip internals.
fn skip_opt_with_direction(
events: &[Event],
- index: usize,
+ mut index: usize,
token_types: &[Token],
forward: bool,
) -> usize {
- let mut index = index;
+ let mut balance = 0;
+ let open = if forward {
+ EventType::Enter
+ } else {
+ EventType::Exit
+ };
while index < events.len() {
let current = &events[index].token_type;
- if !token_types.contains(current) {
+ if !token_types.contains(current) || events[index].event_type != open {
break;
}
- // assert_eq!(events[index].event_type, EventType::Enter);
index = if forward { index + 1 } else { index - 1 };
+ balance += 1;
loop {
- if events[index].token_type == *current {
- // assert_eq!(events[index].event_type, EventType::Exit);
+ balance = if events[index].event_type == open {
+ balance + 1
+ } else {
+ balance - 1
+ };
+
+ if events[index].token_type == *current && balance == 0 {
+ println!("close:it! {:?} {:?}", events[index].token_type, balance);
index = if forward { index + 1 } else { index - 1 };
+ println!("index:break: {:?}", index);
break;
}
index = if forward { index + 1 } else { index - 1 };
+ println!("index:loop: {:?}", index);
}
}