1 use lexer::dfa::{Kind, NFAIndex, DFA, START};
2
interpret<'text>(dfa: &DFA, input: &'text str) -> Option<(NFAIndex, &'text str)>3 pub fn interpret<'text>(dfa: &DFA, input: &'text str) -> Option<(NFAIndex, &'text str)> {
4 let mut longest: Option<(NFAIndex, usize)> = None;
5 let mut state_index = START;
6
7 for (offset, ch) in input.char_indices() {
8 let state = &dfa.states[state_index.0];
9
10 let target = dfa
11 .state(state_index)
12 .test_edges
13 .iter()
14 .filter_map(|&(test, target)| {
15 if test.contains_char(ch) {
16 Some(target)
17 } else {
18 None
19 }
20 })
21 .next();
22
23 if let Some(target) = target {
24 state_index = target;
25 } else {
26 state_index = state.other_edge;
27 }
28
29 match dfa.state(state_index).kind {
30 Kind::Accepts(nfa) => {
31 longest = Some((nfa, offset + ch.len_utf8()));
32 }
33 Kind::Reject => {
34 break;
35 }
36 Kind::Neither => {}
37 }
38 }
39
40 longest.map(|(index, offset)| (index, &input[..offset]))
41 }
42