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