• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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_DFASTATE
9 #define SKSL_DFASTATE
10 
11 #include "LexUtil.h"
12 
13 struct DFAState {
14     struct Label {
15         std::vector<int> fStates;
16 
LabelDFAState::Label17         Label(std::vector<int> states)
18         : fStates(std::move(states)) {}
19 
20         bool operator==(const Label& other) const {
21             return fStates == other.fStates;
22         }
23 
24         bool operator!=(const Label& other) const {
25             return !(*this == other);
26         }
27 
descriptionDFAState::Label28         std::string description() const {
29             std::string result = "<";
30             const char* separator = "";
31             for (int s : fStates) {
32                 result += separator;
33                 result += std::to_string(s);
34                 separator = ", ";
35             }
36             result += ">";
37             return result;
38         }
39     };
40 
DFAStateDFAState41     DFAState()
42     : fId(INVALID)
43     , fLabel({}) {}
44 
DFAStateDFAState45     DFAState(int id, Label label)
46     : fId(id)
47     , fLabel(std::move(label)) {}
48 
49     DFAState(const DFAState& other) = delete;
50 
51     int fId;
52 
53     Label fLabel;
54 
55     bool fIsScanned = false;
56 };
57 
58 namespace std {
59     template<> struct hash<DFAState::Label> {
60         size_t operator()(const DFAState::Label& s) const {
61             int result = 0;
62             for (int i : s.fStates) {
63                 result = result * 101 + i;
64             }
65             return result;
66         }
67     };
68 } // namespace
69 
70 #endif
71