RegexNode.h 1.6 KB

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