blob: 1fab51f92165d91061b585051fb7e0552234ce21 [file] [log] [blame]
Ethan Nicholasca82a922017-09-07 09:39:50 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_DFA
9#define SKSL_DFA
10
11#include <string>
12#include <vector>
13
14/**
15 * Tables representing a deterministic finite automaton for matching regular expressions.
16 */
17struct DFA {
Ethan Nicholas906126e2017-09-19 14:38:40 -040018 DFA(std::vector<int> charMappings, std::vector<std::vector<int>> transitions,
19 std::vector<int> accepts)
20 : fCharMappings(charMappings)
21 , fTransitions(transitions)
Ethan Nicholasca82a922017-09-07 09:39:50 -040022 , fAccepts(accepts) {}
23
Ethan Nicholas906126e2017-09-19 14:38:40 -040024 // maps chars to the row index of fTransitions, as multiple characters may map to the same row.
25 // starting from state s and looking at char c, the new state is
26 // fTransitions[fCharMappings[c]][s].
27 std::vector<int> fCharMappings;
Ethan Nicholasca82a922017-09-07 09:39:50 -040028
Ethan Nicholas906126e2017-09-19 14:38:40 -040029 // one row per character mapping, one column per state
Ethan Nicholasca82a922017-09-07 09:39:50 -040030 std::vector<std::vector<int>> fTransitions;
31
32 // contains, for each state, the token id we should report when matching ends in that state (-1
33 // for no match)
34 std::vector<int> fAccepts;
35};
36
37#endif