third_party/rust/lalrpop-snap/src/lexer/dfa/interpret.rs
author Dorel Luca <dluca@mozilla.com>
Sat, 12 Jan 2019 03:43:46 +0200
changeset 453620 def9811f0311
parent 412088 a97cccaa866a
permissions -rw-r--r--
Backed out 2 changesets (bug 1516337) for build bustage. CLOSED TREE Backed out changeset 3c4b8e03e722 (bug 1516337) Backed out changeset 4fc377013db5 (bug 1516337)

use lexer::dfa::{Kind, NFAIndex, DFA, START};

pub fn interpret<'text>(dfa: &DFA, input: &'text str) -> Option<(NFAIndex, &'text str)> {
    let mut longest: Option<(NFAIndex, usize)> = None;
    let mut state_index = START;

    for (offset, ch) in input.char_indices() {
        let state = &dfa.states[state_index.0];

        let target = dfa.state(state_index)
            .test_edges
            .iter()
            .filter_map(|&(test, target)| {
                if test.contains_char(ch) {
                    Some(target)
                } else {
                    None
                }
            })
            .next();

        if let Some(target) = target {
            state_index = target;
        } else {
            state_index = state.other_edge;
        }

        match dfa.state(state_index).kind {
            Kind::Accepts(nfa) => {
                longest = Some((nfa, offset + ch.len_utf8()));
            }
            Kind::Reject => {
                break;
            }
            Kind::Neither => {}
        }
    }

    longest.map(|(index, offset)| (index, &input[..offset]))
}