| 5 |
| #include <memory> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 |
| 9 | class GrammarPtr; |
| 10 | class GrammarToken; |
| 11 | class Pattern; |
| 12 | class TokenList; |
| 13 |
| 14 | class LanguageTree; |
| 15 |
| 16 | struct Grammar { |
| 17 | std::vector<GrammarToken> tokens; |
| 18 | }; |
| 19 |
| 20 | class GrammarPtr { |
| 21 | public: |
| 22 | GrammarPtr(std::shared_ptr<LanguageTree> tree, size_t path); |
| 23 |
| 24 | const Grammar* operator->() const; |
| 25 | const Grammar* get() const; |
| 26 |
| 27 | private: |
| 28 | std::weak_ptr<LanguageTree> m_tree; |
| 29 | size_t m_path; |
| 30 | }; |
| 31 |
| 32 | class PatternRaw { |
| 33 | public: |
| 34 | PatternRaw(std::string_view pattern, boost::regex_constants::syntax_option_type flags, |
| 35 | bool lookbehind, bool greedy, std::string alias, std::shared_ptr<GrammarPtr> inside) |
| 36 | : m_regex(std::string{pattern}), |
| 37 | m_flags(flags), |
| 38 | m_lookbehind(lookbehind), |
| 39 | m_greedy(greedy), |
| 40 | m_alias(alias), |
| 41 | m_inside(inside) {} |
| 42 |
| 43 | std::shared_ptr<Pattern> realize(); |
| 44 |
| 45 | private: |
| 46 | std::string m_regex; |
| 47 | boost::regex_constants::syntax_option_type m_flags; |
| 48 | bool m_lookbehind; |
| 49 | bool m_greedy; |
| 50 | std::string m_alias; |
| 51 | std::shared_ptr<GrammarPtr> m_inside; |
| 52 | }; |
| 53 |
| 54 | class Pattern { |
| 55 | public: |
| 56 | Pattern(std::string_view pattern, boost::regex_constants::syntax_option_type flags, |
| 57 | bool lookbehind, bool greedy, std::string alias, std::shared_ptr<GrammarPtr> inside) |
| 58 | : m_regex(boost::regex(std::string{pattern}, flags | boost::regex_constants::optimize)), |
| 59 | m_lookbehind(lookbehind), |
| 60 | m_greedy(greedy), |
| 61 | m_alias(alias), |
| 62 | m_inside(inside) {} |
| 63 |
| 64 | std::string_view match(bool& success, size_t& pos, std::string_view text) const { |
| 65 | boost::cmatch m; |
| 66 |
| 67 | auto flags = boost::regex_constants::match_not_dot_newline; |
| 68 | auto match = |
| 69 | boost::regex_search(text.data() + pos, text.data() + text.size(), m, m_regex, flags); |
| 70 | if (match) { |
| 71 | success = true; |
| 72 | pos += m.position(); |
| 73 |
| 74 | if (m_lookbehind && m[1].matched) { |
| 75 | // change the match to remove the text matched by the Prism lookbehind group |
| 76 | auto lookbehindLength = m[1].length(); |
| 77 | pos += lookbehindLength; |
| 78 |
| 79 | return text.substr(pos, m[0].length() - lookbehindLength); |
| 80 | } |
| 81 |
| 82 | return text.substr(pos, m[0].length()); |
| 83 | } |
| 84 |
| 85 | return {}; |
| 86 | } |
| 87 |
| 88 | bool lookbehind() const { return m_lookbehind; } |
| 89 |
| 90 | bool greedy() const { return m_greedy; } |
| 91 |
| 92 | std::string alias() const { return m_alias; } |
| 93 |
| 94 | const Grammar* inside() const; |
| 95 |
| 96 | private: |
| 97 | boost::regex m_regex; |
| 98 | bool m_lookbehind; |
| 99 | bool m_greedy; |
| 100 | std::string m_alias; |
| 101 | std::shared_ptr<GrammarPtr> m_inside; |
| 102 | }; |
| 103 |
| 104 | class PatternPtr { |
| 105 | public: |
| 106 | PatternPtr(std::shared_ptr<LanguageTree> tree, size_t path); |
| 107 |
| 108 | const Pattern* operator->() const { return get(); } |
| 109 |
| 110 | const Pattern* get() const; |
| 111 |
| 112 | private: |
| 113 | std::weak_ptr<LanguageTree> m_tree; |
| 114 | size_t m_path; |
| 115 | }; |
| 116 |
| 117 | class GrammarToken { |
| 118 | public: |
| 119 | GrammarToken(const std::string name, std::vector<PatternPtr> patterns) |
| 120 | : m_name(name), m_patterns(std::move(patterns)) {} |
| 121 |
| 122 | const std::string& name() const { return m_name; } |
| 123 |
| 124 | std::vector<PatternPtr>::const_iterator cbegin() const noexcept { return m_patterns.cbegin(); } |
| 125 |
| 126 | std::vector<PatternPtr>::const_iterator cend() const noexcept { return m_patterns.cend(); } |
| 127 |
| 128 | private: |
| 129 | std::string m_name; |
| 130 | const std::vector<PatternPtr> m_patterns; |
| 131 | }; |
| 132 |