• 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_REGEXNODE
9 #define SKSL_REGEXNODE
10 
11 #include <string>
12 #include <vector>
13 
14 struct NFA;
15 
16 /**
17  * Represents a node in the parse tree of a regular expression.
18  */
19 struct RegexNode {
20     enum Kind {
21         kChar_Kind,
22         kCharset_Kind,
23         kConcat_Kind,
24         kDot_Kind,
25         kOr_Kind,
26         kPlus_Kind,
27         kRange_Kind,
28         kQuestion_Kind,
29         kStar_Kind
30     };
31 
RegexNodeRegexNode32     RegexNode(Kind kind)
33     : fKind(kind) {}
34 
RegexNodeRegexNode35     RegexNode(Kind kind, char payload)
36     : fKind(kind) {
37         fPayload.fChar = payload;
38     }
39 
RegexNodeRegexNode40     RegexNode(Kind kind, const char* children)
41     : fKind(kind) {
42         fPayload.fBool = false;
43         while (*children != '\0') {
44             fChildren.emplace_back(kChar_Kind, *children);
45             ++children;
46         }
47     }
48 
RegexNodeRegexNode49     RegexNode(Kind kind, RegexNode child)
50     : fKind(kind) {
51         fChildren.push_back(std::move(child));
52     }
53 
RegexNodeRegexNode54     RegexNode(Kind kind, RegexNode child1, RegexNode child2)
55     : fKind(kind) {
56         fChildren.push_back(std::move(child1));
57         fChildren.push_back(std::move(child2));
58     }
59 
60     /**
61      * Creates NFA states for this node, with a successful match against this node resulting in a
62      * transition to all of the states in the accept vector.
63      */
64     std::vector<int> createStates(NFA* nfa, const std::vector<int>& accept) const;
65 
66     std::string description() const;
67 
68     Kind fKind;
69 
70     union Payload {
71         char fChar;
72         bool fBool;
73     } fPayload;
74 
75     std::vector<RegexNode> fChildren;
76 };
77 
78 #endif
79