xref: /third_party/skia/src/sksl/lex/DFA.h (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SKSL_DFA
9cb93a386Sopenharmony_ci#define SKSL_DFA
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include <string>
12cb93a386Sopenharmony_ci#include <vector>
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci/**
15cb93a386Sopenharmony_ci * Tables representing a deterministic finite automaton for matching regular expressions.
16cb93a386Sopenharmony_ci */
17cb93a386Sopenharmony_cistruct DFA {
18cb93a386Sopenharmony_ci    DFA(std::vector<int> charMappings, std::vector<std::vector<int>> transitions,
19cb93a386Sopenharmony_ci        std::vector<int> accepts)
20cb93a386Sopenharmony_ci    : fCharMappings(charMappings)
21cb93a386Sopenharmony_ci    , fTransitions(transitions)
22cb93a386Sopenharmony_ci    , fAccepts(accepts) {}
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    // maps chars to the row index of fTransitions, as multiple characters may map to the same row.
25cb93a386Sopenharmony_ci    // starting from state s and looking at char c, the new state is
26cb93a386Sopenharmony_ci    // fTransitions[fCharMappings[c]][s].
27cb93a386Sopenharmony_ci    std::vector<int> fCharMappings;
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    // one row per character mapping, one column per state
30cb93a386Sopenharmony_ci    std::vector<std::vector<int>> fTransitions;
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci    // contains, for each state, the token id we should report when matching ends in that state (-1
33cb93a386Sopenharmony_ci    // for no match)
34cb93a386Sopenharmony_ci    std::vector<int> fAccepts;
35cb93a386Sopenharmony_ci};
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci#endif
38