6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE basic_regex_parser.cpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares template class basic_regex_parser.
19 #ifndef BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP
20 #define BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP
24 #pragma warning(disable: 4103)
26 #ifdef BOOST_HAS_ABI_HEADERS
27 # include BOOST_ABI_PREFIX
34 namespace BOOST_REGEX_DETAIL_NS{
38 #pragma warning(disable:4244)
40 #pragma warning(disable:4800)
44 inline boost::intmax_t umax(mpl::false_ const&)
46 // Get out clause here, just in case numeric_limits is unspecialized:
47 return std::numeric_limits<boost::intmax_t>::is_specialized ? (std::numeric_limits<boost::intmax_t>::max)() : INT_MAX;
49 inline boost::intmax_t umax(mpl::true_ const&)
51 return (std::numeric_limits<std::size_t>::max)();
54 inline boost::intmax_t umax()
56 return umax(mpl::bool_<std::numeric_limits<boost::intmax_t>::digits >= std::numeric_limits<std::size_t>::digits>());
59 template <class charT, class traits>
60 class basic_regex_parser : public basic_regex_creator<charT, traits>
63 basic_regex_parser(regex_data<charT, traits>* data);
64 void parse(const charT* p1, const charT* p2, unsigned flags);
65 void fail(regex_constants::error_type error_code, std::ptrdiff_t position);
66 void fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos);
67 void fail(regex_constants::error_type error_code, std::ptrdiff_t position, const std::string& message)
69 fail(error_code, position, message, position);
74 bool parse_extended();
76 bool parse_open_paren();
77 bool parse_basic_escape();
78 bool parse_extended_escape();
79 bool parse_match_any();
80 bool parse_repeat(std::size_t low = 0, std::size_t high = (std::numeric_limits<std::size_t>::max)());
81 bool parse_repeat_range(bool isbasic);
85 void parse_set_literal(basic_char_set<charT, traits>& char_set);
86 bool parse_inner_set(basic_char_set<charT, traits>& char_set);
88 bool parse_perl_extension();
89 bool parse_perl_verb();
90 bool match_verb(const char*);
91 bool add_emacs_code(bool negate);
92 bool unwind_alts(std::ptrdiff_t last_paren_start);
93 digraph<charT> get_next_set_literal(basic_char_set<charT, traits>& char_set);
94 charT unescape_character();
95 regex_constants::syntax_option_type parse_options();
98 typedef bool (basic_regex_parser::*parser_proc_type)();
99 typedef typename traits::string_type string_type;
100 typedef typename traits::char_class_type char_class_type;
101 parser_proc_type m_parser_proc; // the main parser to use
102 const charT* m_base; // the start of the string being parsed
103 const charT* m_end; // the end of the string being parsed
104 const charT* m_position; // our current parser position
105 unsigned m_mark_count; // how many sub-expressions we have
106 int m_mark_reset; // used to indicate that we're inside a (?|...) block.
107 unsigned m_max_mark; // largest mark count seen inside a (?|...) block.
108 std::ptrdiff_t m_paren_start; // where the last seen ')' began (where repeats are inserted).
109 std::ptrdiff_t m_alt_insert_point; // where to insert the next alternative
110 bool m_has_case_change; // true if somewhere in the current block the case has changed
111 unsigned m_recursion_count; // How many times we've called parse_all.
112 #if defined(BOOST_MSVC) && defined(_M_IX86)
113 // This is an ugly warning suppression workaround (for warnings *inside* std::vector
114 // that can not otherwise be suppressed)...
115 BOOST_STATIC_ASSERT(sizeof(long) >= sizeof(void*));
116 std::vector<long> m_alt_jumps; // list of alternative in the current scope.
118 std::vector<std::ptrdiff_t> m_alt_jumps; // list of alternative in the current scope.
121 basic_regex_parser& operator=(const basic_regex_parser&);
122 basic_regex_parser(const basic_regex_parser&);
125 template <class charT, class traits>
126 basic_regex_parser<charT, traits>::basic_regex_parser(regex_data<charT, traits>* data)
127 : basic_regex_creator<charT, traits>(data), m_mark_count(0), m_mark_reset(-1), m_max_mark(0), m_paren_start(0), m_alt_insert_point(0), m_has_case_change(false), m_recursion_count(0)
131 template <class charT, class traits>
132 void basic_regex_parser<charT, traits>::parse(const charT* p1, const charT* p2, unsigned l_flags)
134 // pass l_flags on to base class:
137 m_position = m_base = p1;
139 // empty strings are errors:
142 ((l_flags & regbase::main_option_type) != regbase::perl_syntax_group)
143 || (l_flags & regbase::no_empty_expressions)
147 fail(regex_constants::error_empty, 0);
150 // select which parser to use:
151 switch(l_flags & regbase::main_option_type)
153 case regbase::perl_syntax_group:
155 m_parser_proc = &basic_regex_parser<charT, traits>::parse_extended;
157 // Add a leading paren with index zero to give recursions a target:
159 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
161 br->icase = this->flags() & regbase::icase;
164 case regbase::basic_syntax_group:
165 m_parser_proc = &basic_regex_parser<charT, traits>::parse_basic;
167 case regbase::literal:
168 m_parser_proc = &basic_regex_parser<charT, traits>::parse_literal;
171 // Ooops, someone has managed to set more than one of the main option flags,
172 // so this must be an error:
173 fail(regex_constants::error_unknown, 0, "An invalid combination of regular expression syntax flags was used.");
177 // parse all our characters:
178 bool result = parse_all();
180 // Unwind our alternatives:
183 // reset l_flags as a global scope (?imsx) may have altered them:
184 this->flags(l_flags);
185 // if we haven't gobbled up all the characters then we must
186 // have had an unexpected ')' :
189 fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_position), "Found a closing ) with no corresponding opening parenthesis.");
192 // if an error has been set then give up now:
193 if(this->m_pdata->m_status)
195 // fill in our sub-expression count:
196 this->m_pdata->m_mark_count = 1 + m_mark_count;
197 this->finalize(p1, p2);
200 template <class charT, class traits>
201 void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position)
203 // get the error message:
204 std::string message = this->m_pdata->m_ptraits->error_string(error_code);
205 fail(error_code, position, message);
208 template <class charT, class traits>
209 void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos)
211 if(0 == this->m_pdata->m_status) // update the error code if not already set
212 this->m_pdata->m_status = error_code;
213 m_position = m_end; // don't bother parsing anything else
215 #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
217 // Augment error message with the regular expression text:
219 if(start_pos == position)
220 start_pos = (std::max)(static_cast<std::ptrdiff_t>(0), position - static_cast<std::ptrdiff_t>(10));
221 std::ptrdiff_t end_pos = (std::min)(position + static_cast<std::ptrdiff_t>(10), static_cast<std::ptrdiff_t>(m_end - m_base));
222 if(error_code != regex_constants::error_empty)
224 if((start_pos != 0) || (end_pos != (m_end - m_base)))
225 message += " The error occurred while parsing the regular expression fragment: '";
227 message += " The error occurred while parsing the regular expression: '";
228 if(start_pos != end_pos)
230 message += std::string(m_base + start_pos, m_base + position);
231 message += ">>>HERE>>>";
232 message += std::string(m_base + position, m_base + end_pos);
238 #ifndef BOOST_NO_EXCEPTIONS
239 if(0 == (this->flags() & regex_constants::no_except))
241 boost::regex_error e(message, error_code, position);
245 (void)position; // suppress warnings.
249 template <class charT, class traits>
250 bool basic_regex_parser<charT, traits>::parse_all()
252 if (++m_recursion_count > 400)
254 // exceeded internal limits
255 fail(boost::regex_constants::error_complexity, m_position - m_base, "Exceeded nested brace limit.");
258 while(result && (m_position != m_end))
260 result = (this->*m_parser_proc)();
267 #pragma warning(push)
268 #pragma warning(disable:4702)
270 template <class charT, class traits>
271 bool basic_regex_parser<charT, traits>::parse_basic()
273 switch(this->m_traits.syntax_type(*m_position))
275 case regex_constants::syntax_escape:
276 return parse_basic_escape();
277 case regex_constants::syntax_dot:
278 return parse_match_any();
279 case regex_constants::syntax_caret:
281 this->append_state(syntax_element_start_line);
283 case regex_constants::syntax_dollar:
285 this->append_state(syntax_element_end_line);
287 case regex_constants::syntax_star:
288 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line))
289 return parse_literal();
293 return parse_repeat();
295 case regex_constants::syntax_plus:
296 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
297 return parse_literal();
301 return parse_repeat(1);
303 case regex_constants::syntax_question:
304 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
305 return parse_literal();
309 return parse_repeat(0, 1);
311 case regex_constants::syntax_open_set:
313 case regex_constants::syntax_newline:
314 if(this->flags() & regbase::newline_alt)
317 return parse_literal();
319 return parse_literal();
324 template <class charT, class traits>
325 bool basic_regex_parser<charT, traits>::parse_extended()
328 switch(this->m_traits.syntax_type(*m_position))
330 case regex_constants::syntax_open_mark:
331 return parse_open_paren();
332 case regex_constants::syntax_close_mark:
334 case regex_constants::syntax_escape:
335 return parse_extended_escape();
336 case regex_constants::syntax_dot:
337 return parse_match_any();
338 case regex_constants::syntax_caret:
341 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line));
343 case regex_constants::syntax_dollar:
346 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line));
348 case regex_constants::syntax_star:
349 if(m_position == this->m_base)
351 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"*\" cannot start a regular expression.");
355 return parse_repeat();
356 case regex_constants::syntax_question:
357 if(m_position == this->m_base)
359 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"?\" cannot start a regular expression.");
363 return parse_repeat(0,1);
364 case regex_constants::syntax_plus:
365 if(m_position == this->m_base)
367 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"+\" cannot start a regular expression.");
371 return parse_repeat(1);
372 case regex_constants::syntax_open_brace:
374 return parse_repeat_range(false);
375 case regex_constants::syntax_close_brace:
376 if((this->flags() & regbase::no_perl_ex) == regbase::no_perl_ex)
378 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
381 result = parse_literal();
383 case regex_constants::syntax_or:
385 case regex_constants::syntax_open_set:
387 case regex_constants::syntax_newline:
388 if(this->flags() & regbase::newline_alt)
391 return parse_literal();
392 case regex_constants::syntax_hash:
394 // If we have a mod_x flag set, then skip until
395 // we get to a newline character:
398 & (regbase::no_perl_ex|regbase::mod_x))
401 while((m_position != m_end) && !is_separator(*m_position++)){}
406 result = parse_literal();
415 template <class charT, class traits>
416 bool basic_regex_parser<charT, traits>::parse_literal()
418 // append this as a literal provided it's not a space character
419 // or the perl option regbase::mod_x is not set:
422 & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex))
424 || !this->m_traits.isctype(*m_position, this->m_mask_space))
425 this->append_literal(*m_position);
430 template <class charT, class traits>
431 bool basic_regex_parser<charT, traits>::parse_open_paren()
434 // skip the '(' and error check:
436 if(++m_position == m_end)
438 fail(regex_constants::error_paren, m_position - m_base);
442 // begin by checking for a perl-style (?...) extension:
445 ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0)
446 || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
449 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
450 return parse_perl_extension();
451 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_star)
452 return parse_perl_verb();
455 // update our mark count, and append the required state:
458 if(0 == (this->flags() & regbase::nosubs))
460 markid = ++m_mark_count;
461 #ifndef BOOST_NO_STD_DISTANCE
462 if(this->flags() & regbase::save_subexpression_location)
463 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 1, 0));
465 if(this->flags() & regbase::save_subexpression_location)
466 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 1, 0));
469 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
471 pb->icase = this->flags() & regbase::icase;
472 std::ptrdiff_t last_paren_start = this->getoffset(pb);
473 // back up insertion point for alternations, and set new point:
474 std::ptrdiff_t last_alt_point = m_alt_insert_point;
475 this->m_pdata->m_data.align();
476 m_alt_insert_point = this->m_pdata->m_data.size();
478 // back up the current flags in case we have a nested (?imsx) group:
480 regex_constants::syntax_option_type opts = this->flags();
481 bool old_case_change = m_has_case_change;
482 m_has_case_change = false; // no changes to this scope as yet...
484 // Back up branch reset data in case we have a nested (?|...)
486 int mark_reset = m_mark_reset;
489 // now recursively add more states, this will terminate when we get to a
494 // Unwind pushed alternatives:
496 if(0 == unwind_alts(last_paren_start))
501 if(m_has_case_change)
503 // the case has changed in one or more of the alternatives
504 // within the scoped (...) block: we have to add a state
505 // to reset the case sensitivity:
506 static_cast<re_case*>(
507 this->append_state(syntax_element_toggle_case, sizeof(re_case))
508 )->icase = opts & regbase::icase;
511 m_has_case_change = old_case_change;
513 // restore branch reset:
515 m_mark_reset = mark_reset;
517 // we either have a ')' or we have run out of characters prematurely:
519 if(m_position == m_end)
521 this->fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_end));
524 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
526 #ifndef BOOST_NO_STD_DISTANCE
527 if(markid && (this->flags() & regbase::save_subexpression_location))
528 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position);
530 if(markid && (this->flags() & regbase::save_subexpression_location))
531 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base);
535 // append closing parenthesis state:
537 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
539 pb->icase = this->flags() & regbase::icase;
540 this->m_paren_start = last_paren_start;
542 // restore the alternate insertion point:
544 this->m_alt_insert_point = last_alt_point;
546 // allow backrefs to this mark:
548 if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT))
549 this->m_backrefs |= 1u << (markid - 1);
554 template <class charT, class traits>
555 bool basic_regex_parser<charT, traits>::parse_basic_escape()
557 if(++m_position == m_end)
559 fail(regex_constants::error_paren, m_position - m_base);
563 switch(this->m_traits.escape_syntax_type(*m_position))
565 case regex_constants::syntax_open_mark:
566 return parse_open_paren();
567 case regex_constants::syntax_close_mark:
569 case regex_constants::syntax_plus:
570 if(this->flags() & regex_constants::bk_plus_qm)
573 return parse_repeat(1);
576 return parse_literal();
577 case regex_constants::syntax_question:
578 if(this->flags() & regex_constants::bk_plus_qm)
581 return parse_repeat(0, 1);
584 return parse_literal();
585 case regex_constants::syntax_open_brace:
586 if(this->flags() & regbase::no_intervals)
587 return parse_literal();
589 return parse_repeat_range(true);
590 case regex_constants::syntax_close_brace:
591 if(this->flags() & regbase::no_intervals)
592 return parse_literal();
593 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
595 case regex_constants::syntax_or:
596 if(this->flags() & regbase::bk_vbar)
599 result = parse_literal();
601 case regex_constants::syntax_digit:
602 return parse_backref();
603 case regex_constants::escape_type_start_buffer:
604 if(this->flags() & regbase::emacs_ex)
607 this->append_state(syntax_element_buffer_start);
610 result = parse_literal();
612 case regex_constants::escape_type_end_buffer:
613 if(this->flags() & regbase::emacs_ex)
616 this->append_state(syntax_element_buffer_end);
619 result = parse_literal();
621 case regex_constants::escape_type_word_assert:
622 if(this->flags() & regbase::emacs_ex)
625 this->append_state(syntax_element_word_boundary);
628 result = parse_literal();
630 case regex_constants::escape_type_not_word_assert:
631 if(this->flags() & regbase::emacs_ex)
634 this->append_state(syntax_element_within_word);
637 result = parse_literal();
639 case regex_constants::escape_type_left_word:
640 if(this->flags() & regbase::emacs_ex)
643 this->append_state(syntax_element_word_start);
646 result = parse_literal();
648 case regex_constants::escape_type_right_word:
649 if(this->flags() & regbase::emacs_ex)
652 this->append_state(syntax_element_word_end);
655 result = parse_literal();
658 if(this->flags() & regbase::emacs_ex)
668 basic_char_set<charT, traits> char_set;
671 char_set.add_class(this->m_word_mask);
672 if(0 == this->append_set(char_set))
674 fail(regex_constants::error_ctype, m_position - m_base);
684 return add_emacs_code(negate);
687 // not supported yet:
688 fail(regex_constants::error_escape, m_position - m_base, "The \\c and \\C escape sequences are not supported by POSIX basic regular expressions: try the Perl syntax instead.");
694 result = parse_literal();
700 template <class charT, class traits>
701 bool basic_regex_parser<charT, traits>::parse_extended_escape()
704 if(m_position == m_end)
706 fail(regex_constants::error_escape, m_position - m_base, "Incomplete escape sequence found.");
709 bool negate = false; // in case this is a character class escape: \w \d etc
710 switch(this->m_traits.escape_syntax_type(*m_position))
712 case regex_constants::escape_type_not_class:
715 case regex_constants::escape_type_class:
717 escape_type_class_jump:
718 typedef typename traits::char_class_type m_type;
719 m_type m = this->m_traits.lookup_classname(m_position, m_position+1);
722 basic_char_set<charT, traits> char_set;
725 char_set.add_class(m);
726 if(0 == this->append_set(char_set))
728 fail(regex_constants::error_ctype, m_position - m_base);
735 // not a class, just a regular unknown escape:
737 this->append_literal(unescape_character());
740 case regex_constants::syntax_digit:
741 return parse_backref();
742 case regex_constants::escape_type_left_word:
744 this->append_state(syntax_element_word_start);
746 case regex_constants::escape_type_right_word:
748 this->append_state(syntax_element_word_end);
750 case regex_constants::escape_type_start_buffer:
752 this->append_state(syntax_element_buffer_start);
754 case regex_constants::escape_type_end_buffer:
756 this->append_state(syntax_element_buffer_end);
758 case regex_constants::escape_type_word_assert:
760 this->append_state(syntax_element_word_boundary);
762 case regex_constants::escape_type_not_word_assert:
764 this->append_state(syntax_element_within_word);
766 case regex_constants::escape_type_Z:
768 this->append_state(syntax_element_soft_buffer_end);
770 case regex_constants::escape_type_Q:
772 case regex_constants::escape_type_C:
773 return parse_match_any();
774 case regex_constants::escape_type_X:
776 this->append_state(syntax_element_combining);
778 case regex_constants::escape_type_G:
780 this->append_state(syntax_element_restart_continue);
782 case regex_constants::escape_type_not_property:
785 case regex_constants::escape_type_property:
789 if(m_position == m_end)
791 fail(regex_constants::error_escape, m_position - m_base, "Incomplete property escape found.");
794 // maybe have \p{ddd}
795 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
797 const charT* base = m_position;
798 // skip forward until we find enclosing brace:
799 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
801 if(m_position == m_end)
803 fail(regex_constants::error_escape, m_position - m_base, "Closing } missing from property escape sequence.");
806 m = this->m_traits.lookup_classname(++base, m_position++);
810 m = this->m_traits.lookup_classname(m_position, m_position+1);
815 basic_char_set<charT, traits> char_set;
818 char_set.add_class(m);
819 if(0 == this->append_set(char_set))
821 fail(regex_constants::error_ctype, m_position - m_base);
826 fail(regex_constants::error_ctype, m_position - m_base, "Escape sequence was neither a valid property nor a valid character class name.");
829 case regex_constants::escape_type_reset_start_mark:
830 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
832 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
834 pb->icase = this->flags() & regbase::icase;
835 this->m_pdata->m_data.align();
839 goto escape_type_class_jump;
840 case regex_constants::escape_type_line_ending:
841 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
843 const charT* e = get_escape_R_string<charT>();
844 const charT* old_position = m_position;
845 const charT* old_end = m_end;
846 const charT* old_base = m_base;
849 m_end = e + traits::length(e);
850 bool r = parse_all();
851 m_position = ++old_position;
856 goto escape_type_class_jump;
857 case regex_constants::escape_type_extended_backref:
858 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
860 bool have_brace = false;
861 bool negative = false;
862 static const char* incomplete_message = "Incomplete \\g escape found.";
863 if(++m_position == m_end)
865 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
868 // maybe have \g{ddd}
869 regex_constants::syntax_type syn = this->m_traits.syntax_type(*m_position);
870 regex_constants::syntax_type syn_end = 0;
871 if((syn == regex_constants::syntax_open_brace)
872 || (syn == regex_constants::escape_type_left_word)
873 || (syn == regex_constants::escape_type_end_buffer))
875 if(++m_position == m_end)
877 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
883 case regex_constants::syntax_open_brace:
884 syn_end = regex_constants::syntax_close_brace;
886 case regex_constants::escape_type_left_word:
887 syn_end = regex_constants::escape_type_right_word;
890 syn_end = regex_constants::escape_type_end_buffer;
894 negative = (*m_position == static_cast<charT>('-'));
895 if((negative) && (++m_position == m_end))
897 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
900 const charT* pc = m_position;
901 boost::intmax_t i = this->m_traits.toi(pc, m_end, 10);
902 if((i < 0) && syn_end)
904 // Check for a named capture, get the leftmost one if there is more than one:
905 const charT* base = m_position;
906 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != syn_end))
910 i = hash_value_from_capture_name(base, m_position);
914 i = 1 + m_mark_count - i;
915 if(((i > 0) && (i < std::numeric_limits<unsigned>::digits) && (i - 1 < static_cast<boost::intmax_t>(sizeof(unsigned) * CHAR_BIT)) && (this->m_backrefs & (1u << (i-1)))) || ((i > 10000) && (this->m_pdata->get_id(i) > 0) && (this->m_pdata->get_id(i)-1 < static_cast<boost::intmax_t>(sizeof(unsigned) * CHAR_BIT)) && (this->m_backrefs & (1u << (this->m_pdata->get_id(i)-1)))))
918 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
920 pb->icase = this->flags() & regbase::icase;
924 fail(regex_constants::error_backref, m_position - m_base);
930 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != syn_end))
932 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
939 goto escape_type_class_jump;
940 case regex_constants::escape_type_control_v:
941 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
942 goto escape_type_class_jump;
945 this->append_literal(unescape_character());
951 template <class charT, class traits>
952 bool basic_regex_parser<charT, traits>::parse_match_any()
955 // we have a '.' that can match any character:
958 static_cast<re_dot*>(
959 this->append_state(syntax_element_wild, sizeof(re_dot))
960 )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
961 ? BOOST_REGEX_DETAIL_NS::force_not_newline
962 : this->flags() & regbase::mod_s ?
963 BOOST_REGEX_DETAIL_NS::force_newline : BOOST_REGEX_DETAIL_NS::dont_care);
967 template <class charT, class traits>
968 bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
971 bool pocessive = false;
972 std::size_t insert_point;
974 // when we get to here we may have a non-greedy ? mark still to come:
976 if((m_position != m_end)
978 (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
979 || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
983 // OK we have a perl or emacs regex, check for a '?':
984 if ((this->flags() & (regbase::main_option_type | regbase::mod_x | regbase::no_perl_ex)) == regbase::mod_x)
987 while ((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
990 if((m_position != m_end) && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question))
995 // for perl regexes only check for pocessive ++ repeats.
996 if((m_position != m_end)
997 && (0 == (this->flags() & regbase::main_option_type))
998 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
1004 if(0 == this->m_last_state)
1006 fail(regex_constants::error_badrepeat, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_position), "Nothing to repeat.");
1009 if(this->m_last_state->type == syntax_element_endmark)
1011 // insert a repeat before the '(' matching the last ')':
1012 insert_point = this->m_paren_start;
1014 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
1016 // the last state was a literal with more than one character, split it in two:
1017 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
1018 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
1020 // now append new state:
1021 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
1023 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
1024 insert_point = this->getoffset(this->m_last_state);
1028 // repeat the last state whatever it was, need to add some error checking here:
1029 switch(this->m_last_state->type)
1031 case syntax_element_start_line:
1032 case syntax_element_end_line:
1033 case syntax_element_word_boundary:
1034 case syntax_element_within_word:
1035 case syntax_element_word_start:
1036 case syntax_element_word_end:
1037 case syntax_element_buffer_start:
1038 case syntax_element_buffer_end:
1039 case syntax_element_alt:
1040 case syntax_element_soft_buffer_end:
1041 case syntax_element_restart_continue:
1042 case syntax_element_jump:
1043 case syntax_element_startmark:
1044 case syntax_element_backstep:
1045 // can't legally repeat any of the above:
1046 fail(regex_constants::error_badrepeat, m_position - m_base);
1052 insert_point = this->getoffset(this->m_last_state);
1055 // OK we now know what to repeat, so insert the repeat around it:
1057 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
1060 rep->greedy = greedy;
1061 rep->leading = false;
1062 // store our repeater position for later:
1063 std::ptrdiff_t rep_off = this->getoffset(rep);
1064 // and append a back jump to the repeat:
1065 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
1066 jmp->alt.i = rep_off - this->getoffset(jmp);
1067 this->m_pdata->m_data.align();
1068 // now fill in the alt jump for the repeat:
1069 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
1070 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
1072 // If the repeat is pocessive then bracket the repeat with a (?>...)
1073 // independent sub-expression construct:
1077 if(m_position != m_end)
1080 // Check for illegal following quantifier, we have to do this here, because
1081 // the extra states we insert below circumvents our usual error checking :-(
1083 bool contin = false;
1086 if ((this->flags() & (regbase::main_option_type | regbase::mod_x | regbase::no_perl_ex)) == regbase::mod_x)
1089 while ((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1092 if (m_position != m_end)
1094 switch (this->m_traits.syntax_type(*m_position))
1096 case regex_constants::syntax_star:
1097 case regex_constants::syntax_plus:
1098 case regex_constants::syntax_question:
1099 case regex_constants::syntax_open_brace:
1100 fail(regex_constants::error_badrepeat, m_position - m_base);
1102 case regex_constants::syntax_open_mark:
1103 // Do we have a comment? If so we need to skip it here...
1104 if ((m_position + 2 < m_end) && this->m_traits.syntax_type(*(m_position + 1)) == regex_constants::syntax_question
1105 && this->m_traits.syntax_type(*(m_position + 2)) == regex_constants::syntax_hash)
1107 while ((m_position != m_end)
1108 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark)) {
1120 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
1122 pb->icase = this->flags() & regbase::icase;
1123 jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
1124 this->m_pdata->m_data.align();
1125 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
1126 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
1128 pb->icase = this->flags() & regbase::icase;
1133 template <class charT, class traits>
1134 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
1136 static const char* incomplete_message = "Missing } in quantified repetition.";
1138 // parse a repeat-range:
1140 std::size_t min, max;
1143 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1145 if(this->m_position == this->m_end)
1147 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1149 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1152 // Treat the opening '{' as a literal character, rewind to start of error:
1154 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1155 return parse_literal();
1158 v = this->m_traits.toi(m_position, m_end, 10);
1160 if((v < 0) || (v > umax()))
1162 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1164 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1167 // Treat the opening '{' as a literal character, rewind to start of error:
1169 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1170 return parse_literal();
1172 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1174 if(this->m_position == this->m_end)
1176 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1178 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1181 // Treat the opening '{' as a literal character, rewind to start of error:
1183 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1184 return parse_literal();
1186 min = static_cast<std::size_t>(v);
1187 // see if we have a comma:
1188 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1190 // move on and error check:
1193 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1195 if(this->m_position == this->m_end)
1197 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1199 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1202 // Treat the opening '{' as a literal character, rewind to start of error:
1204 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1205 return parse_literal();
1207 // get the value if any:
1208 v = this->m_traits.toi(m_position, m_end, 10);
1209 max = ((v >= 0) && (v < umax())) ? (std::size_t)v : (std::numeric_limits<std::size_t>::max)();
1213 // no comma, max = min:
1217 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1219 // OK now check trailing }:
1220 if(this->m_position == this->m_end)
1222 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1224 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1227 // Treat the opening '{' as a literal character, rewind to start of error:
1229 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1230 return parse_literal();
1234 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1237 if(this->m_position == this->m_end)
1239 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1245 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1249 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1253 // Treat the opening '{' as a literal character, rewind to start of error:
1255 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1256 return parse_literal();
1259 // finally go and add the repeat, unless error:
1263 // Backtrack to error location:
1265 while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
1267 fail(regex_constants::error_badbrace, m_position - m_base);
1270 return parse_repeat(min, max);
1273 template <class charT, class traits>
1274 bool basic_regex_parser<charT, traits>::parse_alt()
1277 // error check: if there have been no previous states,
1278 // or if the last state was a '(' then error:
1281 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1284 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1286 ((this->flags() & regbase::no_empty_expressions) == 0)
1290 fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression cannot start with the alternation operator |.");
1294 // Reset mark count if required:
1296 if(m_max_mark < m_mark_count)
1297 m_max_mark = m_mark_count;
1298 if(m_mark_reset >= 0)
1299 m_mark_count = m_mark_reset;
1303 // we need to append a trailing jump:
1305 re_syntax_base* pj = this->append_state(BOOST_REGEX_DETAIL_NS::syntax_element_jump, sizeof(re_jump));
1306 std::ptrdiff_t jump_offset = this->getoffset(pj);
1308 // now insert the alternative:
1310 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1311 jump_offset += re_alt_size;
1312 this->m_pdata->m_data.align();
1313 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1315 // update m_alt_insert_point so that the next alternate gets
1316 // inserted at the start of the second of the two we've just created:
1318 this->m_alt_insert_point = this->m_pdata->m_data.size();
1320 // the start of this alternative must have a case changes state
1321 // if the current block has messed around with case changes:
1323 if(m_has_case_change)
1325 static_cast<re_case*>(
1326 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1327 )->icase = this->m_icase;
1330 // push the alternative onto our stack, a recursive
1331 // implementation here is easier to understand (and faster
1332 // as it happens), but causes all kinds of stack overflow problems
1333 // on programs with small stacks (COM+).
1335 m_alt_jumps.push_back(jump_offset);
1339 template <class charT, class traits>
1340 bool basic_regex_parser<charT, traits>::parse_set()
1342 static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1344 if(m_position == m_end)
1346 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1349 basic_char_set<charT, traits> char_set;
1351 const charT* base = m_position; // where the '[' was
1352 const charT* item_base = m_position; // where the '[' or '^' was
1354 while(m_position != m_end)
1356 switch(this->m_traits.syntax_type(*m_position))
1358 case regex_constants::syntax_caret:
1359 if(m_position == base)
1363 item_base = m_position;
1366 parse_set_literal(char_set);
1368 case regex_constants::syntax_close_set:
1369 if(m_position == item_base)
1371 parse_set_literal(char_set);
1377 if(0 == this->append_set(char_set))
1379 fail(regex_constants::error_ctype, m_position - m_base);
1384 case regex_constants::syntax_open_set:
1385 if(parse_inner_set(char_set))
1388 case regex_constants::syntax_escape:
1391 // look ahead and see if this is a character class shortcut
1395 if(this->m_traits.escape_syntax_type(*m_position)
1396 == regex_constants::escape_type_class)
1398 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1401 char_set.add_class(m);
1406 else if(this->m_traits.escape_syntax_type(*m_position)
1407 == regex_constants::escape_type_not_class)
1409 // negated character class:
1410 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1413 char_set.add_negated_class(m);
1418 // not a character class, just a regular escape:
1420 parse_set_literal(char_set);
1424 parse_set_literal(char_set);
1428 return m_position != m_end;
1431 template <class charT, class traits>
1432 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1434 static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1436 // we have either a character class [:name:]
1437 // a collating element [.name.]
1438 // or an equivalence class [=name=]
1440 if(m_end == ++m_position)
1442 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1445 switch(this->m_traits.syntax_type(*m_position))
1447 case regex_constants::syntax_dot:
1449 // a collating element is treated as a literal:
1452 parse_set_literal(char_set);
1454 case regex_constants::syntax_colon:
1456 // check that character classes are actually enabled:
1457 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1458 == (regbase::basic_syntax_group | regbase::no_char_classes))
1461 parse_set_literal(char_set);
1465 if(m_end == ++m_position)
1467 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1470 const charT* name_first = m_position;
1471 // skip at least one character, then find the matching ':]'
1472 if(m_end == ++m_position)
1474 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1477 while((m_position != m_end)
1478 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1480 const charT* name_last = m_position;
1481 if(m_end == m_position)
1483 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1486 if((m_end == ++m_position)
1487 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1489 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1493 // check for negated class:
1495 bool negated = false;
1496 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1501 typedef typename traits::char_class_type m_type;
1502 m_type m = this->m_traits.lookup_classname(name_first, name_last);
1505 if(char_set.empty() && (name_last - name_first == 1))
1507 // maybe a special case:
1509 if( (m_position != m_end)
1510 && (this->m_traits.syntax_type(*m_position)
1511 == regex_constants::syntax_close_set))
1513 if(this->m_traits.escape_syntax_type(*name_first)
1514 == regex_constants::escape_type_left_word)
1517 this->append_state(syntax_element_word_start);
1520 if(this->m_traits.escape_syntax_type(*name_first)
1521 == regex_constants::escape_type_right_word)
1524 this->append_state(syntax_element_word_end);
1529 fail(regex_constants::error_ctype, name_first - m_base);
1532 if(negated == false)
1533 char_set.add_class(m);
1535 char_set.add_negated_class(m);
1539 case regex_constants::syntax_equal:
1542 if(m_end == ++m_position)
1544 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1547 const charT* name_first = m_position;
1548 // skip at least one character, then find the matching '=]'
1549 if(m_end == ++m_position)
1551 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1554 while((m_position != m_end)
1555 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1557 const charT* name_last = m_position;
1558 if(m_end == m_position)
1560 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1563 if((m_end == ++m_position)
1564 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1566 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1569 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1570 if((0 == m.size()) || (m.size() > 2))
1572 fail(regex_constants::error_collate, name_first - m_base);
1581 char_set.add_equivalent(d);
1587 parse_set_literal(char_set);
1593 template <class charT, class traits>
1594 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1596 digraph<charT> start_range(get_next_set_literal(char_set));
1597 if(m_end == m_position)
1599 fail(regex_constants::error_brack, m_position - m_base);
1602 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1605 if(m_end == ++m_position)
1607 fail(regex_constants::error_brack, m_position - m_base);
1610 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1612 digraph<charT> end_range = get_next_set_literal(char_set);
1613 char_set.add_range(start_range, end_range);
1614 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1616 if(m_end == ++m_position)
1618 fail(regex_constants::error_brack, m_position - m_base);
1621 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1627 fail(regex_constants::error_range, m_position - m_base);
1634 char_set.add_single(start_range);
1637 template <class charT, class traits>
1638 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1640 digraph<charT> result;
1641 switch(this->m_traits.syntax_type(*m_position))
1643 case regex_constants::syntax_dash:
1644 if(!char_set.empty())
1646 // see if we are at the end of the set:
1647 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1649 fail(regex_constants::error_range, m_position - m_base);
1654 result.first = *m_position++;
1656 case regex_constants::syntax_escape:
1657 // check to see if escapes are supported first:
1658 if(this->flags() & regex_constants::no_escape_in_lists)
1660 result = *m_position++;
1664 result = unescape_character();
1666 case regex_constants::syntax_open_set:
1668 if(m_end == ++m_position)
1670 fail(regex_constants::error_collate, m_position - m_base);
1673 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1676 result.first = *m_position;
1680 if(m_end == ++m_position)
1682 fail(regex_constants::error_collate, m_position - m_base);
1685 const charT* name_first = m_position;
1686 // skip at least one character, then find the matching ':]'
1687 if(m_end == ++m_position)
1689 fail(regex_constants::error_collate, name_first - m_base);
1692 while((m_position != m_end)
1693 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1695 const charT* name_last = m_position;
1696 if(m_end == m_position)
1698 fail(regex_constants::error_collate, name_first - m_base);
1701 if((m_end == ++m_position)
1702 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1704 fail(regex_constants::error_collate, name_first - m_base);
1708 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1709 if(s.empty() || (s.size() > 2))
1711 fail(regex_constants::error_collate, name_first - m_base);
1714 result.first = s[0];
1716 result.second = s[1];
1722 result = *m_position++;
1728 // does a value fit in the specified charT type?
1730 template <class charT>
1731 bool valid_value(charT, boost::intmax_t v, const mpl::true_&)
1733 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1735 template <class charT>
1736 bool valid_value(charT, boost::intmax_t, const mpl::false_&)
1738 return true; // v will alsways fit in a charT
1740 template <class charT>
1741 bool valid_value(charT c, boost::intmax_t v)
1743 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(boost::intmax_t))>());
1746 template <class charT, class traits>
1747 charT basic_regex_parser<charT, traits>::unescape_character()
1750 #pragma warning(push)
1751 #pragma warning(disable:4127)
1754 if(m_position == m_end)
1756 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
1759 switch(this->m_traits.escape_syntax_type(*m_position))
1761 case regex_constants::escape_type_control_a:
1762 result = charT('\a');
1764 case regex_constants::escape_type_e:
1767 case regex_constants::escape_type_control_f:
1768 result = charT('\f');
1770 case regex_constants::escape_type_control_n:
1771 result = charT('\n');
1773 case regex_constants::escape_type_control_r:
1774 result = charT('\r');
1776 case regex_constants::escape_type_control_t:
1777 result = charT('\t');
1779 case regex_constants::escape_type_control_v:
1780 result = charT('\v');
1782 case regex_constants::escape_type_word_assert:
1783 result = charT('\b');
1785 case regex_constants::escape_type_ascii_control:
1787 if(m_position == m_end)
1789 // Rewind to start of escape:
1791 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1792 fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
1795 result = static_cast<charT>(*m_position % 32);
1797 case regex_constants::escape_type_hex:
1799 if(m_position == m_end)
1801 // Rewind to start of escape:
1803 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1804 fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
1807 // maybe have \x{ddd}
1808 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1811 if(m_position == m_end)
1813 // Rewind to start of escape:
1815 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1816 fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
1819 boost::intmax_t i = this->m_traits.toi(m_position, m_end, 16);
1820 if((m_position == m_end)
1822 || ((std::numeric_limits<charT>::is_specialized) && (i > (boost::intmax_t)(std::numeric_limits<charT>::max)()))
1823 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1825 // Rewind to start of escape:
1827 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1828 fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
1836 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
1837 boost::intmax_t i = this->m_traits.toi(m_position, m_position + len, 16);
1839 || !valid_value(charT(0), i))
1841 // Rewind to start of escape:
1843 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1844 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
1850 case regex_constants::syntax_digit:
1852 // an octal escape sequence, the first character must be a zero
1853 // followed by up to 3 octal digits:
1854 std::ptrdiff_t len = (std::min)(::boost::BOOST_REGEX_DETAIL_NS::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1855 const charT* bp = m_position;
1856 boost::intmax_t val = this->m_traits.toi(bp, bp + 1, 8);
1859 // Rewind to start of escape:
1861 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1862 // Oops not an octal escape after all:
1863 fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
1866 val = this->m_traits.toi(m_position, m_position + len, 8);
1867 if((val < 0) || (val > (boost::intmax_t)(std::numeric_limits<charT>::max)()))
1869 // Rewind to start of escape:
1871 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1872 fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
1875 return static_cast<charT>(val);
1877 case regex_constants::escape_type_named_char:
1880 if(m_position == m_end)
1882 // Rewind to start of escape:
1884 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1885 fail(regex_constants::error_escape, m_position - m_base);
1888 // maybe have \N{name}
1889 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1891 const charT* base = m_position;
1892 // skip forward until we find enclosing brace:
1893 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1895 if(m_position == m_end)
1897 // Rewind to start of escape:
1899 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1900 fail(regex_constants::error_escape, m_position - m_base);
1903 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
1906 // Rewind to start of escape:
1908 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1909 fail(regex_constants::error_collate, m_position - m_base);
1917 // fall through is a failure:
1918 // Rewind to start of escape:
1920 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1921 fail(regex_constants::error_escape, m_position - m_base);
1925 result = *m_position;
1931 #pragma warning(pop)
1935 template <class charT, class traits>
1936 bool basic_regex_parser<charT, traits>::parse_backref()
1938 BOOST_ASSERT(m_position != m_end);
1939 const charT* pc = m_position;
1940 boost::intmax_t i = this->m_traits.toi(pc, pc + 1, 10);
1941 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1943 // not a backref at all but an octal escape sequence:
1944 charT c = unescape_character();
1945 this->append_literal(c);
1947 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1950 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1952 pb->icase = this->flags() & regbase::icase;
1956 // Rewind to start of escape:
1958 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1959 fail(regex_constants::error_backref, m_position - m_base);
1965 template <class charT, class traits>
1966 bool basic_regex_parser<charT, traits>::parse_QE()
1969 #pragma warning(push)
1970 #pragma warning(disable:4127)
1973 // parse a \Q...\E sequence:
1975 ++m_position; // skip the Q
1976 const charT* start = m_position;
1980 while((m_position != m_end)
1981 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1983 if(m_position == m_end)
1985 // a \Q...\E sequence may terminate with the end of the expression:
1989 if(++m_position == m_end) // skip the escape
1991 fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
1994 // check to see if it's a \E:
1995 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1998 end = m_position - 2;
2001 // otherwise go round again:
2004 // now add all the character between the two escapes as literals:
2008 this->append_literal(*start);
2013 #pragma warning(pop)
2017 template <class charT, class traits>
2018 bool basic_regex_parser<charT, traits>::parse_perl_extension()
2020 if(++m_position == m_end)
2022 // Rewind to start of (? sequence:
2024 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2025 fail(regex_constants::error_perl_extension, m_position - m_base);
2029 // treat comments as a special case, as these
2030 // are the only ones that don't start with a leading
2033 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
2035 while((m_position != m_end)
2036 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
2041 // backup some state, and prepare the way:
2044 std::ptrdiff_t jump_offset = 0;
2045 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
2046 pb->icase = this->flags() & regbase::icase;
2047 std::ptrdiff_t last_paren_start = this->getoffset(pb);
2048 // back up insertion point for alternations, and set new point:
2049 std::ptrdiff_t last_alt_point = m_alt_insert_point;
2050 this->m_pdata->m_data.align();
2051 m_alt_insert_point = this->m_pdata->m_data.size();
2052 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
2053 bool restore_flags = true;
2054 regex_constants::syntax_option_type old_flags = this->flags();
2055 bool old_case_change = m_has_case_change;
2056 m_has_case_change = false;
2058 int mark_reset = m_mark_reset;
2059 int max_mark = m_max_mark;
2061 m_max_mark = m_mark_count;
2064 // select the actual extension used:
2066 switch(this->m_traits.syntax_type(*m_position))
2068 case regex_constants::syntax_or:
2069 m_mark_reset = m_mark_count;
2071 case regex_constants::syntax_colon:
2073 // a non-capturing mark:
2075 pb->index = markid = 0;
2078 case regex_constants::syntax_digit:
2081 // a recursive subexpression:
2083 v = this->m_traits.toi(m_position, m_end, 10);
2084 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2086 // Rewind to start of (? sequence:
2088 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2089 fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
2093 pb->index = markid = 0;
2094 re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
2097 static_cast<re_case*>(
2098 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2099 )->icase = this->flags() & regbase::icase;
2102 case regex_constants::syntax_plus:
2104 // A forward-relative recursive subexpression:
2107 v = this->m_traits.toi(m_position, m_end, 10);
2108 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2110 // Rewind to start of (? sequence:
2112 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2113 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2116 if ((std::numeric_limits<boost::intmax_t>::max)() - m_mark_count < v)
2118 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2122 goto insert_recursion;
2123 case regex_constants::syntax_dash:
2125 // Possibly a backward-relative recursive subexpression:
2128 v = this->m_traits.toi(m_position, m_end, 10);
2132 // Oops not a relative recursion at all, but a (?-imsx) group:
2133 goto option_group_jump;
2135 v = m_mark_count + 1 - v;
2138 // Rewind to start of (? sequence:
2140 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2141 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2144 goto insert_recursion;
2145 case regex_constants::syntax_equal:
2146 pb->index = markid = -1;
2148 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2149 this->m_pdata->m_data.align();
2150 m_alt_insert_point = this->m_pdata->m_data.size();
2152 case regex_constants::syntax_not:
2153 pb->index = markid = -2;
2155 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2156 this->m_pdata->m_data.align();
2157 m_alt_insert_point = this->m_pdata->m_data.size();
2159 case regex_constants::escape_type_left_word:
2161 // a lookbehind assertion:
2162 if(++m_position == m_end)
2164 // Rewind to start of (? sequence:
2166 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2167 fail(regex_constants::error_perl_extension, m_position - m_base);
2170 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
2171 if(t == regex_constants::syntax_not)
2172 pb->index = markid = -2;
2173 else if(t == regex_constants::syntax_equal)
2174 pb->index = markid = -1;
2177 // Probably a named capture which also starts (?< :
2180 goto named_capture_jump;
2183 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2184 this->append_state(syntax_element_backstep, sizeof(re_brace));
2185 this->m_pdata->m_data.align();
2186 m_alt_insert_point = this->m_pdata->m_data.size();
2189 case regex_constants::escape_type_right_word:
2191 // an independent sub-expression:
2193 pb->index = markid = -3;
2195 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2196 this->m_pdata->m_data.align();
2197 m_alt_insert_point = this->m_pdata->m_data.size();
2199 case regex_constants::syntax_open_mark:
2201 // a conditional expression:
2202 pb->index = markid = -4;
2203 if(++m_position == m_end)
2205 // Rewind to start of (? sequence:
2207 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2208 fail(regex_constants::error_perl_extension, m_position - m_base);
2211 v = this->m_traits.toi(m_position, m_end, 10);
2212 if(m_position == m_end)
2214 // Rewind to start of (? sequence:
2216 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2217 fail(regex_constants::error_perl_extension, m_position - m_base);
2220 if(*m_position == charT('R'))
2222 if(++m_position == m_end)
2224 // Rewind to start of (? sequence:
2226 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2227 fail(regex_constants::error_perl_extension, m_position - m_base);
2230 if(*m_position == charT('&'))
2232 const charT* base = ++m_position;
2233 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2235 if(m_position == m_end)
2237 // Rewind to start of (? sequence:
2239 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2240 fail(regex_constants::error_perl_extension, m_position - m_base);
2243 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
2247 v = -this->m_traits.toi(m_position, m_end, 10);
2249 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2250 br->index = v < 0 ? (v - 1) : 0;
2251 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2253 // Rewind to start of (? sequence:
2255 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2256 fail(regex_constants::error_perl_extension, m_position - m_base);
2259 if(++m_position == m_end)
2261 // Rewind to start of (? sequence:
2263 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2264 fail(regex_constants::error_perl_extension, m_position - m_base);
2268 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2270 const charT* base = ++m_position;
2271 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2273 if(m_position == m_end)
2275 // Rewind to start of (? sequence:
2277 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2278 fail(regex_constants::error_perl_extension, m_position - m_base);
2281 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2282 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2284 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2286 // Rewind to start of (? sequence:
2288 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2289 fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
2292 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2294 // Rewind to start of (? sequence:
2296 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2297 fail(regex_constants::error_perl_extension, m_position - m_base);
2300 if(++m_position == m_end)
2302 // Rewind to start of (? sequence:
2304 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2305 fail(regex_constants::error_perl_extension, m_position - m_base);
2309 else if(*m_position == charT('D'))
2311 const char* def = "DEFINE";
2312 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2313 ++m_position, ++def;
2314 if((m_position == m_end) || *def)
2316 // Rewind to start of (? sequence:
2318 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2319 fail(regex_constants::error_perl_extension, m_position - m_base);
2322 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2323 br->index = 9999; // special magic value!
2324 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2326 // Rewind to start of (? sequence:
2328 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2329 fail(regex_constants::error_perl_extension, m_position - m_base);
2332 if(++m_position == m_end)
2334 // Rewind to start of (? sequence:
2336 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2337 fail(regex_constants::error_perl_extension, m_position - m_base);
2343 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2345 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2347 // Rewind to start of (? sequence:
2349 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2350 fail(regex_constants::error_perl_extension, m_position - m_base);
2353 if(++m_position == m_end)
2355 // Rewind to start of (? sequence:
2357 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2358 fail(regex_constants::error_perl_extension, m_position - m_base);
2364 // verify that we have a lookahead or lookbehind assert:
2365 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2367 // Rewind to start of (? sequence:
2369 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2370 fail(regex_constants::error_perl_extension, m_position - m_base);
2373 if(++m_position == m_end)
2375 // Rewind to start of (? sequence:
2377 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2378 fail(regex_constants::error_perl_extension, m_position - m_base);
2381 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2383 if(++m_position == m_end)
2385 // Rewind to start of (? sequence:
2387 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2388 fail(regex_constants::error_perl_extension, m_position - m_base);
2391 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2392 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2394 // Rewind to start of (? sequence:
2396 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2397 fail(regex_constants::error_perl_extension, m_position - m_base);
2404 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2405 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2407 // Rewind to start of (? sequence:
2409 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2410 fail(regex_constants::error_perl_extension, m_position - m_base);
2418 case regex_constants::syntax_close_mark:
2419 // Rewind to start of (? sequence:
2421 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2422 fail(regex_constants::error_perl_extension, m_position - m_base);
2424 case regex_constants::escape_type_end_buffer:
2426 name_delim = *m_position;
2429 if(0 == (this->flags() & regbase::nosubs))
2431 markid = ++m_mark_count;
2432 #ifndef BOOST_NO_STD_DISTANCE
2433 if(this->flags() & regbase::save_subexpression_location)
2434 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2436 if(this->flags() & regbase::save_subexpression_location)
2437 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2441 const charT* base = ++m_position;
2442 if(m_position == m_end)
2444 // Rewind to start of (? sequence:
2446 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2447 fail(regex_constants::error_perl_extension, m_position - m_base);
2450 while((m_position != m_end) && (*m_position != name_delim))
2452 if(m_position == m_end)
2454 // Rewind to start of (? sequence:
2456 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2457 fail(regex_constants::error_perl_extension, m_position - m_base);
2460 this->m_pdata->set_name(base, m_position, markid);
2465 if(*m_position == charT('R'))
2469 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2471 // Rewind to start of (? sequence:
2473 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2474 fail(regex_constants::error_perl_extension, m_position - m_base);
2477 goto insert_recursion;
2479 if(*m_position == charT('&'))
2482 const charT* base = m_position;
2483 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2485 if(m_position == m_end)
2487 // Rewind to start of (? sequence:
2489 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2490 fail(regex_constants::error_perl_extension, m_position - m_base);
2493 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2494 goto insert_recursion;
2496 if(*m_position == charT('P'))
2499 if(m_position == m_end)
2501 // Rewind to start of (? sequence:
2503 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2504 fail(regex_constants::error_perl_extension, m_position - m_base);
2507 if(*m_position == charT('>'))
2510 const charT* base = m_position;
2511 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2513 if(m_position == m_end)
2515 // Rewind to start of (? sequence:
2517 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2518 fail(regex_constants::error_perl_extension, m_position - m_base);
2521 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2522 goto insert_recursion;
2526 // lets assume that we have a (?imsx) group and try and parse it:
2529 regex_constants::syntax_option_type opts = parse_options();
2530 if(m_position == m_end)
2532 // Rewind to start of (? sequence:
2534 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2535 fail(regex_constants::error_perl_extension, m_position - m_base);
2538 // make a note of whether we have a case change:
2539 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2540 pb->index = markid = 0;
2541 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2543 // update flags and carry on as normal:
2545 restore_flags = false;
2546 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2548 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2550 // update flags and carry on until the matching ')' is found:
2556 // Rewind to start of (? sequence:
2558 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2559 fail(regex_constants::error_perl_extension, m_position - m_base);
2563 // finally append a case change state if we need it:
2564 if(m_has_case_change)
2566 static_cast<re_case*>(
2567 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2568 )->icase = opts & regbase::icase;
2573 // now recursively add more states, this will terminate when we get to a
2578 // Unwind alternatives:
2580 if(0 == unwind_alts(last_paren_start))
2582 // Rewind to start of (? sequence:
2584 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2585 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block.");
2589 // we either have a ')' or we have run out of characters prematurely:
2591 if(m_position == m_end)
2593 // Rewind to start of (? sequence:
2595 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2596 this->fail(regex_constants::error_paren, ::boost::BOOST_REGEX_DETAIL_NS::distance(m_base, m_end));
2599 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2602 // restore the flags:
2606 // append a case change state if we need it:
2607 if(m_has_case_change)
2609 static_cast<re_case*>(
2610 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2611 )->icase = old_flags & regbase::icase;
2613 this->flags(old_flags);
2616 // set up the jump pointer if we have one:
2620 this->m_pdata->m_data.align();
2621 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2622 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2623 if((this->m_last_state == jmp) && (markid != -2))
2625 // Oops... we didn't have anything inside the assertion.
2626 // Note we don't get here for negated forward lookahead as (?!)
2627 // does have some uses.
2628 // Rewind to start of (? sequence:
2630 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2631 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
2636 // verify that if this is conditional expression, that we do have
2637 // an alternative, if not add one:
2641 re_syntax_base* b = this->getaddress(expected_alt_point);
2642 // Make sure we have exactly one alternative following this state:
2643 if(b->type != syntax_element_alt)
2645 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2646 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2648 else if(((std::ptrdiff_t)this->m_pdata->m_data.size() > (static_cast<re_alt*>(b)->alt.i + this->getoffset(b))) && (static_cast<re_alt*>(b)->alt.i > 0) && this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2650 // Can't have seen more than one alternative:
2651 // Rewind to start of (? sequence:
2653 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2654 fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
2659 // We must *not* have seen an alternative inside a (DEFINE) block:
2660 b = this->getaddress(b->next.i, b);
2661 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2663 // Rewind to start of (? sequence:
2665 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2666 fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
2670 // check for invalid repetition of next state:
2671 b = this->getaddress(expected_alt_point);
2672 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2673 if((b->type != syntax_element_assert_backref)
2674 && (b->type != syntax_element_startmark))
2676 // Rewind to start of (? sequence:
2678 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2679 fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
2684 // append closing parenthesis state:
2686 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2688 pb->icase = this->flags() & regbase::icase;
2689 this->m_paren_start = last_paren_start;
2691 // restore the alternate insertion point:
2693 this->m_alt_insert_point = last_alt_point;
2695 // and the case change data:
2697 m_has_case_change = old_case_change;
2699 // And the mark_reset data:
2701 if(m_max_mark > m_mark_count)
2703 m_mark_count = m_max_mark;
2705 m_mark_reset = mark_reset;
2706 m_max_mark = max_mark;
2711 #ifndef BOOST_NO_STD_DISTANCE
2712 if(this->flags() & regbase::save_subexpression_location)
2713 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2715 if(this->flags() & regbase::save_subexpression_location)
2716 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2719 // allow backrefs to this mark:
2721 if(markid < (int)(sizeof(unsigned) * CHAR_BIT))
2722 this->m_backrefs |= 1u << (markid - 1);
2727 template <class charT, class traits>
2728 bool basic_regex_parser<charT, traits>::match_verb(const char* verb)
2732 if(static_cast<charT>(*verb) != *m_position)
2734 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2735 fail(regex_constants::error_perl_extension, m_position - m_base);
2738 if(++m_position == m_end)
2741 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2742 fail(regex_constants::error_perl_extension, m_position - m_base);
2750 template <class charT, class traits>
2751 bool basic_regex_parser<charT, traits>::parse_perl_verb()
2753 if(++m_position == m_end)
2755 // Rewind to start of (* sequence:
2757 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2758 fail(regex_constants::error_perl_extension, m_position - m_base);
2764 if(++m_position == m_end)
2766 // Rewind to start of (* sequence:
2768 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2769 fail(regex_constants::error_perl_extension, m_position - m_base);
2772 if((this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark) || match_verb("AIL"))
2774 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2776 // Rewind to start of (* sequence:
2778 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2779 fail(regex_constants::error_perl_extension, m_position - m_base);
2783 this->append_state(syntax_element_fail);
2788 if(++m_position == m_end)
2790 // Rewind to start of (* sequence:
2792 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2793 fail(regex_constants::error_perl_extension, m_position - m_base);
2796 if(match_verb("CCEPT"))
2798 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2800 // Rewind to start of (* sequence:
2802 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2803 fail(regex_constants::error_perl_extension, m_position - m_base);
2807 this->append_state(syntax_element_accept);
2812 if(++m_position == m_end)
2814 // Rewind to start of (* sequence:
2816 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2817 fail(regex_constants::error_perl_extension, m_position - m_base);
2820 if(match_verb("OMMIT"))
2822 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2824 // Rewind to start of (* sequence:
2826 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2827 fail(regex_constants::error_perl_extension, m_position - m_base);
2831 static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_commit;
2832 this->m_pdata->m_disable_match_any = true;
2837 if(++m_position == m_end)
2839 // Rewind to start of (* sequence:
2841 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2842 fail(regex_constants::error_perl_extension, m_position - m_base);
2845 if(match_verb("RUNE"))
2847 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2849 // Rewind to start of (* sequence:
2851 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2852 fail(regex_constants::error_perl_extension, m_position - m_base);
2856 static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_prune;
2857 this->m_pdata->m_disable_match_any = true;
2862 if(++m_position == m_end)
2864 // Rewind to start of (* sequence:
2866 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2867 fail(regex_constants::error_perl_extension, m_position - m_base);
2870 if(match_verb("KIP"))
2872 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2874 // Rewind to start of (* sequence:
2876 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2877 fail(regex_constants::error_perl_extension, m_position - m_base);
2881 static_cast<re_commit*>(this->append_state(syntax_element_commit, sizeof(re_commit)))->action = commit_skip;
2882 this->m_pdata->m_disable_match_any = true;
2887 if(++m_position == m_end)
2889 // Rewind to start of (* sequence:
2891 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2892 fail(regex_constants::error_perl_extension, m_position - m_base);
2895 if(match_verb("HEN"))
2897 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2899 // Rewind to start of (* sequence:
2901 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2902 fail(regex_constants::error_perl_extension, m_position - m_base);
2906 this->append_state(syntax_element_then);
2907 this->m_pdata->m_disable_match_any = true;
2912 // Rewind to start of (* sequence:
2914 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2915 fail(regex_constants::error_perl_extension, m_position - m_base);
2919 template <class charT, class traits>
2920 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2923 // parses an emacs style \sx or \Sx construct.
2925 if(++m_position == m_end)
2927 // Rewind to start of sequence:
2929 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
2930 fail(regex_constants::error_escape, m_position - m_base);
2933 basic_char_set<charT, traits> char_set;
2937 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2943 char_set.add_class(this->m_mask_space);
2946 char_set.add_class(this->m_word_mask);
2949 char_set.add_single(digraph<charT>(charT('$')));
2950 char_set.add_single(digraph<charT>(charT('&')));
2951 char_set.add_single(digraph<charT>(charT('*')));
2952 char_set.add_single(digraph<charT>(charT('+')));
2953 char_set.add_single(digraph<charT>(charT('-')));
2954 char_set.add_single(digraph<charT>(charT('_')));
2955 char_set.add_single(digraph<charT>(charT('<')));
2956 char_set.add_single(digraph<charT>(charT('>')));
2959 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2962 char_set.add_single(digraph<charT>(charT('(')));
2963 char_set.add_single(digraph<charT>(charT('[')));
2964 char_set.add_single(digraph<charT>(charT('{')));
2967 char_set.add_single(digraph<charT>(charT(')')));
2968 char_set.add_single(digraph<charT>(charT(']')));
2969 char_set.add_single(digraph<charT>(charT('}')));
2972 char_set.add_single(digraph<charT>(charT('"')));
2973 char_set.add_single(digraph<charT>(charT('\'')));
2974 char_set.add_single(digraph<charT>(charT('`')));
2977 char_set.add_single(digraph<charT>(charT('\'')));
2978 char_set.add_single(digraph<charT>(charT(',')));
2979 char_set.add_single(digraph<charT>(charT('#')));
2982 char_set.add_single(digraph<charT>(charT(';')));
2985 char_set.add_single(digraph<charT>(charT('\n')));
2986 char_set.add_single(digraph<charT>(charT('\f')));
2989 fail(regex_constants::error_ctype, m_position - m_base);
2992 if(0 == this->append_set(char_set))
2994 fail(regex_constants::error_ctype, m_position - m_base);
3001 template <class charT, class traits>
3002 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
3004 // we have a (?imsx-imsx) group, convert it into a set of flags:
3005 regex_constants::syntax_option_type f = this->flags();
3006 bool breakout = false;
3012 f |= regex_constants::mod_s;
3013 f &= ~regex_constants::no_mod_s;
3016 f &= ~regex_constants::no_mod_m;
3019 f |= regex_constants::icase;
3022 f |= regex_constants::mod_x;
3028 if(++m_position == m_end)
3030 // Rewind to start of (? sequence:
3032 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
3033 fail(regex_constants::error_paren, m_position - m_base);
3041 if(*m_position == static_cast<charT>('-'))
3043 if(++m_position == m_end)
3045 // Rewind to start of (? sequence:
3047 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
3048 fail(regex_constants::error_paren, m_position - m_base);
3056 f &= ~regex_constants::mod_s;
3057 f |= regex_constants::no_mod_s;
3060 f |= regex_constants::no_mod_m;
3063 f &= ~regex_constants::icase;
3066 f &= ~regex_constants::mod_x;
3072 if(++m_position == m_end)
3074 // Rewind to start of (? sequence:
3076 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
3077 fail(regex_constants::error_paren, m_position - m_base);
3086 template <class charT, class traits>
3087 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
3090 // If we didn't actually add any states after the last
3091 // alternative then that's an error:
3093 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
3094 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
3097 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
3099 ((this->flags() & regbase::no_empty_expressions) == 0)
3103 fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
3107 // Fix up our alternatives:
3109 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
3112 // fix up the jump to point to the end of the states
3113 // that we've just added:
3115 std::ptrdiff_t jump_offset = m_alt_jumps.back();
3116 m_alt_jumps.pop_back();
3117 this->m_pdata->m_data.align();
3118 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
3119 BOOST_ASSERT(jmp->type == syntax_element_jump);
3120 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
3126 #pragma warning(pop)
3129 } // namespace BOOST_REGEX_DETAIL_NS
3130 } // namespace boost
3133 #pragma warning(push)
3134 #pragma warning(disable: 4103)
3136 #ifdef BOOST_HAS_ABI_HEADERS
3137 # include BOOST_ABI_SUFFIX
3140 #pragma warning(pop)