RegexParser.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_REGEXPARSER
  8. #define SKSL_REGEXPARSER
  9. #include "src/sksl/lex/RegexNode.h"
  10. #include <stack>
  11. #include <string>
  12. /**
  13. * Turns a simple regular expression into a parse tree. The regular expression syntax supports only
  14. * the basic quantifiers ('*', '+', and '?'), alternation ('|'), character sets ('[a-z]'), and
  15. * groups ('()').
  16. */
  17. class RegexParser {
  18. public:
  19. RegexNode parse(std::string source);
  20. private:
  21. static constexpr char END = '\0';
  22. char peek();
  23. void expect(char c);
  24. RegexNode pop();
  25. /**
  26. * Matches a char literal, parenthesized group, character set, or dot ('.').
  27. */
  28. void term();
  29. /**
  30. * Matches a term followed by an optional quantifier ('*', '+', or '?').
  31. */
  32. void quantifiedTerm();
  33. /**
  34. * Matches a sequence of quantifiedTerms.
  35. */
  36. void sequence();
  37. /**
  38. * Returns a node representing the given escape character (e.g. escapeSequence('n') returns a
  39. * node which matches a newline character).
  40. */
  41. RegexNode escapeSequence(char c);
  42. /**
  43. * Matches a literal character or escape sequence.
  44. */
  45. void literal();
  46. /**
  47. * Matches a dot ('.').
  48. */
  49. void dot();
  50. /**
  51. * Matches a parenthesized group.
  52. */
  53. void group();
  54. /**
  55. * Matches a literal character, escape sequence, or character range from a character set.
  56. */
  57. void setItem();
  58. /**
  59. * Matches a character set.
  60. */
  61. void set();
  62. void regex();
  63. std::string fSource;
  64. size_t fIndex;
  65. std::stack<RegexNode> fStack;
  66. };
  67. #endif