| 5 |
| #include <iterator> |
| 6 | #include <map> |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 |
| 11 | // Thrown out of a search that has spent its budget. |
| 12 | struct SearchOutOfBudget {}; |
| 13 |
| 14 | // A place in the text that a search pays for moving from. |
| 15 | class BudgetedPosition { |
| 16 | public: |
| 17 | using iterator_category = std::random_access_iterator_tag; |
| 18 | using value_type = char; |
| 19 | using difference_type = std::ptrdiff_t; |
| 20 | using pointer = const char*; |
| 21 | using reference = const char&; |
| 22 |
| 23 | BudgetedPosition() = default; |
| 24 | BudgetedPosition(const char* position, std::ptrdiff_t* budget) |
| 25 | : m_position(position), m_budget(budget) {} |
| 26 |
| 27 | reference operator*() const { return *m_position; } |
| 28 | reference operator[](difference_type n) const { return m_position[n]; } |
| 29 |
| 30 | BudgetedPosition& operator++() { |
| 31 | pay(); |
| 32 | ++m_position; |
| 33 | return *this; |
| 34 | } |
| 35 | BudgetedPosition operator++(int) { |
| 36 | BudgetedPosition before = *this; |
| 37 | ++*this; |
| 38 | return before; |
| 39 | } |
| 40 | BudgetedPosition& operator--() { |
| 41 | pay(); |
| 42 | --m_position; |
| 43 | return *this; |
| 44 | } |
| 45 | BudgetedPosition operator--(int) { |
| 46 | BudgetedPosition before = *this; |
| 47 | --*this; |
| 48 | return before; |
| 49 | } |
| 50 | BudgetedPosition& operator+=(difference_type n) { |
| 51 | m_position += n; |
| 52 | return *this; |
| 53 | } |
| 54 | BudgetedPosition& operator-=(difference_type n) { |
| 55 | m_position -= n; |
| 56 | return *this; |
| 57 | } |
| 58 |
| 59 | friend BudgetedPosition operator+(BudgetedPosition a, difference_type n) { return a += n; } |
| 60 | friend BudgetedPosition operator+(difference_type n, BudgetedPosition a) { return a += n; } |
| 61 | friend BudgetedPosition operator-(BudgetedPosition a, difference_type n) { return a -= n; } |
| 62 | friend difference_type operator-(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 63 | return a.m_position - b.m_position; |
| 64 | } |
| 65 | friend bool operator==(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 66 | return a.m_position == b.m_position; |
| 67 | } |
| 68 | friend bool operator!=(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 69 | return a.m_position != b.m_position; |
| 70 | } |
| 71 | friend bool operator<(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 72 | return a.m_position < b.m_position; |
| 73 | } |
| 74 | friend bool operator>(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 75 | return a.m_position > b.m_position; |
| 76 | } |
| 77 | friend bool operator<=(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 78 | return a.m_position <= b.m_position; |
| 79 | } |
| 80 | friend bool operator>=(const BudgetedPosition& a, const BudgetedPosition& b) { |
| 81 | return a.m_position >= b.m_position; |
| 82 | } |
| 83 |
| 84 | // Takes no effect until the next move, so it can be charged from anywhere. |
| 85 | void charge(std::ptrdiff_t cost) const { |
| 86 | if (m_budget) { |
| 87 | *m_budget -= cost; |
| 88 | } |
| 89 | } |
| 90 |
| 91 | private: |
| 92 | void pay() { |
| 93 | if (--*m_budget < 0) { |
| 94 | throw SearchOutOfBudget{}; |
| 95 | } |
| 96 | } |
| 97 |
| 98 | const char* m_position = nullptr; |
| 99 | std::ptrdiff_t* m_budget = nullptr; |
| 100 | }; |
| 101 |
| 102 | namespace boost { |
| 103 | namespace BOOST_REGEX_DETAIL_NS { |
| 104 |
| 105 | // Boost's own, but for one thing. It looks for the last count of a repeat through the counts of |
| 106 | // everything repeated since, and in (?:a(?:b)+)?, as the HTML tag pattern has, it goes through |
| 107 | // every (?:b) so far each time it tries to leave one. That takes a search over a long tag from |
| 108 | // the square of its length to the cube: minutes over 64 KB, which no budget of moves can tell |
| 109 | // from a search that is only long. Here a count knows where the run of its own repeat ends, and |
| 110 | // a walk that still has to be long is paid for. |
| 111 | template <> |
| 112 | class repeater_count<BudgetedPosition> { |
| 113 | repeater_count** stack; |
| 114 | repeater_count* next; |
| 115 | repeater_count* beforeRun; |
| 116 | int state_id; |
| 117 | std::size_t count; |
| 118 | BudgetedPosition start_pos; |
| 119 |
| 120 | repeater_count* unwind_until(int n, repeater_count* p, int current_recursion_id, |
| 121 | std::ptrdiff_t& walked) { |
| 122 | while (p && (p->state_id != n)) { |
| 123 | if (-2 - current_recursion_id == p->state_id) return 0; |
| 124 | ++walked; |
| 125 | p = p->beforeRun; |
| 126 | if (p && (p->state_id < 0)) { |
| 127 | p = unwind_until(p->state_id, p, current_recursion_id, walked); |
| 128 | if (!p) return p; |
| 129 | p = p->next; |
| 130 | } |
| 131 | } |
| 132 | return p; |
| 133 | } |
| 134 |
| 135 | public: |
| 136 | repeater_count(repeater_count** s) |
| 137 | : stack(s), next(0), beforeRun(0), state_id(-1), count(0), start_pos() {} |
| 138 |
| 139 | repeater_count(int i, repeater_count** s, BudgetedPosition start, int current_recursion_id) |
| 140 | : start_pos(start) { |
| 141 | state_id = i; |
| 142 | stack = s; |
| 143 | next = *stack; |
| 144 | *stack = this; |
| 145 | beforeRun = (state_id >= 0 && next->state_id == state_id) ? next->beforeRun : next; |
| 146 | if ((state_id > next->state_id) && (next->state_id >= 0)) { |
| 147 | count = 0; |
| 148 | } else { |
| 149 | std::ptrdiff_t walked = 0; |
| 150 | repeater_count* p = unwind_until(state_id, next, current_recursion_id, walked); |
| 151 | start.charge(walked); |
| 152 | if (p) { |
| 153 | count = p->count; |
| 154 | start_pos = p->start_pos; |
| 155 | } else { |
| 156 | count = 0; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | ~repeater_count() { |
| 161 | if (next) *stack = next; |
| 162 | } |
| 163 |
| 164 | std::size_t get_count() { return count; } |
| 165 | int get_id() { return state_id; } |
| 166 | std::size_t operator++() { return ++count; } |
| 167 | bool check_null_repeat(const BudgetedPosition& pos, std::size_t max) { |
| 168 | bool result = (count == 0) ? false : (pos == start_pos); |
| 169 | if (result) { |
| 170 | count = max; |
| 171 | } else { |
| 172 | start_pos = pos; |
| 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | }; |
| 177 |
| 178 | } // namespace BOOST_REGEX_DETAIL_NS |
| 179 | } // namespace boost |
| 180 |
| 181 | class GrammarPtr; |
| 182 | class GrammarToken; |
| 183 | class Pattern; |
| 184 | class TokenList; |
| 185 |
| 186 | class LanguageTree; |
| 187 |
| 188 | struct Grammar { |
| 189 | std::vector<GrammarToken> tokens; |
| 190 | }; |
| 191 |
| 192 | class GrammarPtr { |
| 193 | public: |
| 194 | GrammarPtr(std::shared_ptr<LanguageTree> tree, size_t path); |
| 195 |
| 196 | const Grammar* operator->() const; |
| 197 | const Grammar* get() const; |
| 198 |
| 199 | private: |
| 200 | std::weak_ptr<LanguageTree> m_tree; |
| 201 | size_t m_path; |
| 202 | }; |
| 203 |
| 204 | class PatternRaw { |
| 205 | public: |
| 206 | PatternRaw(std::string_view pattern, boost::regex_constants::syntax_option_type flags, |
| 207 | bool lookbehind, bool greedy, std::string alias, std::shared_ptr<GrammarPtr> inside) |
| 208 | : m_regex(std::string{pattern}), |
| 209 | m_flags(flags), |
| 210 | m_lookbehind(lookbehind), |
| 211 | m_greedy(greedy), |
| 212 | m_alias(alias), |
| 213 | m_inside(inside) {} |
| 214 |
| 215 | std::shared_ptr<Pattern> realize(); |
| 216 |
| 217 | private: |
| 218 | std::string m_regex; |
| 219 | boost::regex_constants::syntax_option_type m_flags; |
| 220 | bool m_lookbehind; |
| 221 | bool m_greedy; |
| 222 | std::string m_alias; |
| 223 | std::shared_ptr<GrammarPtr> m_inside; |
| 224 | }; |
| 225 |
| 226 | class Pattern { |
| 227 | public: |
| 228 | Pattern(std::string_view pattern, boost::regex_constants::syntax_option_type flags, |
| 229 | bool lookbehind, bool greedy, std::string alias, std::shared_ptr<GrammarPtr> inside) |
| 230 | : m_regex(boost::regex(std::string{pattern}, flags | boost::regex_constants::optimize)), |
| 231 | m_lookbehind(lookbehind), |
| 232 | m_greedy(greedy), |
| 233 | m_alias(alias), |
| 234 | m_inside(inside) {} |
| 235 |
| 236 | // The first match from pos on, as where it starts, its length and how much of it Prism's |
| 237 | // lookbehind group took. |
| 238 | bool search(size_t pos, |
| 239 | std::string_view text, |
| 240 | size_t& start, |
| 241 | size_t& length, |
| 242 | size_t& lookbehindLength) const { |
| 243 | // Code takes under 4 million moves a search at 64 KB in the languages measured, Markdown's |
| 244 | // title pattern the most: from every start on a line, to its end and back. The moves of a |
| 245 | // search as bad as the HTML tag pattern over `<a b="<a b="...` take about 6 ns each. |
| 246 | constexpr std::ptrdiff_t kSearchBudget = 24000000; |
| 247 |
| 248 | std::ptrdiff_t budget = kSearchBudget; |
| 249 | boost::match_results<BudgetedPosition> m; |
| 250 |
| 251 | auto flags = boost::regex_constants::match_not_dot_newline; |
| 252 | if (pos > 0) { |
| 253 | // A search from JavaScript's lastIndex sees the text before it: ^ does not match |
| 254 | // there, and \b and lookbehinds read the character before. |
| 255 | flags |= boost::regex_constants::match_prev_avail; |
| 256 | } |
| 257 | if (!boost::regex_search(BudgetedPosition(text.data() + pos, &budget), |
| 258 | BudgetedPosition(text.data() + text.size(), &budget), m, m_regex, |
| 259 | flags)) { |
| 260 | return false; |
| 261 | } |
| 262 |
| 263 | start = pos + m.position(); |
| 264 | length = m[0].length(); |
| 265 | lookbehindLength = m_lookbehind && m[1].matched ? m[1].length() : 0; |
| 266 | return true; |
| 267 | } |
| 268 |
| 269 | bool lookbehind() const { return m_lookbehind; } |
| 270 |
| 271 | bool greedy() const { return m_greedy; } |
| 272 |
| 273 | std::string alias() const { return m_alias; } |
| 274 |
| 275 | const Grammar* inside() const; |
| 276 |
| 277 | private: |
| 278 | boost::regex m_regex; |
| 279 | bool m_lookbehind; |
| 280 | bool m_greedy; |
| 281 | std::string m_alias; |
| 282 | std::shared_ptr<GrammarPtr> m_inside; |
| 283 | }; |
| 284 |
| 285 | class PatternPtr { |
| 286 | public: |
| 287 | PatternPtr(std::shared_ptr<LanguageTree> tree, size_t path); |
| 288 |
| 289 | const Pattern* operator->() const { return get(); } |
| 290 |
| 291 | const Pattern* get() const; |
| 292 |
| 293 | private: |
| 294 | std::weak_ptr<LanguageTree> m_tree; |
| 295 | size_t m_path; |
| 296 | }; |
| 297 |
| 298 | class GrammarToken { |
| 299 | public: |
| 300 | GrammarToken(const std::string name, std::vector<PatternPtr> patterns) |
| 301 | : m_name(name), m_patterns(std::move(patterns)) {} |
| 302 |
| 303 | const std::string& name() const { return m_name; } |
| 304 |
| 305 | std::vector<PatternPtr>::const_iterator cbegin() const noexcept { return m_patterns.cbegin(); } |
| 306 |
| 307 | std::vector<PatternPtr>::const_iterator cend() const noexcept { return m_patterns.cend(); } |
| 308 |
| 309 | private: |
| 310 | std::string m_name; |
| 311 | const std::vector<PatternPtr> m_patterns; |
| 312 | }; |
| 313 |