aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/skip.rs
blob: 2c4198a4f6e8c4e3983810f137c2ee34b0f0c07c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::tokenizer::{Event, TokenType};

/// To do.
pub fn opt(events: &[Event], index: usize, token_types: &[TokenType]) -> usize {
    skip_opt_with_direction(events, index, token_types, true)
}

/// To do.
pub fn opt_back(events: &[Event], index: usize, token_types: &[TokenType]) -> usize {
    skip_opt_with_direction(events, index, token_types, false)
}

/// To do.
fn skip_opt_with_direction(
    events: &[Event],
    index: usize,
    token_types: &[TokenType],
    forward: bool,
) -> usize {
    let mut index = index;

    while index < events.len() {
        let current = &events[index].token_type;

        if !token_types.contains(current) {
            break;
        }

        // assert_eq!(events[index].event_type, EventType::Enter);
        index = if forward { index + 1 } else { index - 1 };

        loop {
            if events[index].token_type == *current {
                // assert_eq!(events[index].event_type, EventType::Exit);
                index = if forward { index + 1 } else { index - 1 };
                break;
            }

            index = if forward { index + 1 } else { index - 1 };
        }
    }

    index
}