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
38 #pragma warning(disable:4244 4800)
41 template <class charT, class traits>
42 class basic_regex_parser : public basic_regex_creator<charT, traits>
45 basic_regex_parser(regex_data<charT, traits>* data);
46 void parse(const charT* p1, const charT* p2, unsigned flags);
47 void fail(regex_constants::error_type error_code, std::ptrdiff_t position);
48 void fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos);
49 void fail(regex_constants::error_type error_code, std::ptrdiff_t position, const std::string& message)
51 fail(error_code, position, message, position);
56 bool parse_extended();
58 bool parse_open_paren();
59 bool parse_basic_escape();
60 bool parse_extended_escape();
61 bool parse_match_any();
62 bool parse_repeat(std::size_t low = 0, std::size_t high = (std::numeric_limits<std::size_t>::max)());
63 bool parse_repeat_range(bool isbasic);
67 void parse_set_literal(basic_char_set<charT, traits>& char_set);
68 bool parse_inner_set(basic_char_set<charT, traits>& char_set);
70 bool parse_perl_extension();
71 bool add_emacs_code(bool negate);
72 bool unwind_alts(std::ptrdiff_t last_paren_start);
73 digraph<charT> get_next_set_literal(basic_char_set<charT, traits>& char_set);
74 charT unescape_character();
75 regex_constants::syntax_option_type parse_options();
78 typedef bool (basic_regex_parser::*parser_proc_type)();
79 typedef typename traits::string_type string_type;
80 typedef typename traits::char_class_type char_class_type;
81 parser_proc_type m_parser_proc; // the main parser to use
82 const charT* m_base; // the start of the string being parsed
83 const charT* m_end; // the end of the string being parsed
84 const charT* m_position; // our current parser position
85 unsigned m_mark_count; // how many sub-expressions we have
86 int m_mark_reset; // used to indicate that we're inside a (?|...) block.
87 unsigned m_max_mark; // largest mark count seen inside a (?|...) block.
88 std::ptrdiff_t m_paren_start; // where the last seen ')' began (where repeats are inserted).
89 std::ptrdiff_t m_alt_insert_point; // where to insert the next alternative
90 bool m_has_case_change; // true if somewhere in the current block the case has changed
91 #if defined(BOOST_MSVC) && defined(_M_IX86)
92 // This is an ugly warning suppression workaround (for warnings *inside* std::vector
93 // that can not otherwise be suppressed)...
94 BOOST_STATIC_ASSERT(sizeof(long) >= sizeof(void*));
95 std::vector<long> m_alt_jumps; // list of alternative in the current scope.
97 std::vector<std::ptrdiff_t> m_alt_jumps; // list of alternative in the current scope.
100 basic_regex_parser& operator=(const basic_regex_parser&);
101 basic_regex_parser(const basic_regex_parser&);
104 template <class charT, class traits>
105 basic_regex_parser<charT, traits>::basic_regex_parser(regex_data<charT, traits>* data)
106 : 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)
110 template <class charT, class traits>
111 void basic_regex_parser<charT, traits>::parse(const charT* p1, const charT* p2, unsigned l_flags)
113 // pass l_flags on to base class:
116 m_position = m_base = p1;
118 // empty strings are errors:
121 ((l_flags & regbase::main_option_type) != regbase::perl_syntax_group)
122 || (l_flags & regbase::no_empty_expressions)
126 fail(regex_constants::error_empty, 0);
129 // select which parser to use:
130 switch(l_flags & regbase::main_option_type)
132 case regbase::perl_syntax_group:
134 m_parser_proc = &basic_regex_parser<charT, traits>::parse_extended;
136 // Add a leading paren with index zero to give recursions a target:
138 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
140 br->icase = this->flags() & regbase::icase;
143 case regbase::basic_syntax_group:
144 m_parser_proc = &basic_regex_parser<charT, traits>::parse_basic;
146 case regbase::literal:
147 m_parser_proc = &basic_regex_parser<charT, traits>::parse_literal;
150 // Ooops, someone has managed to set more than one of the main option flags,
151 // so this must be an error:
152 fail(regex_constants::error_unknown, 0, "An invalid combination of regular expression syntax flags was used.");
156 // parse all our characters:
157 bool result = parse_all();
159 // Unwind our alternatives:
162 // reset l_flags as a global scope (?imsx) may have altered them:
163 this->flags(l_flags);
164 // if we haven't gobbled up all the characters then we must
165 // have had an unexpected ')' :
168 fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_position), "Found a closing ) with no corresponding openening parenthesis.");
171 // if an error has been set then give up now:
172 if(this->m_pdata->m_status)
174 // fill in our sub-expression count:
175 this->m_pdata->m_mark_count = 1 + m_mark_count;
176 this->finalize(p1, p2);
179 template <class charT, class traits>
180 void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position)
182 // get the error message:
183 std::string message = this->m_pdata->m_ptraits->error_string(error_code);
184 fail(error_code, position, message);
187 template <class charT, class traits>
188 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)
190 if(0 == this->m_pdata->m_status) // update the error code if not already set
191 this->m_pdata->m_status = error_code;
192 m_position = m_end; // don't bother parsing anything else
195 // Augment error message with the regular expression text:
197 if(start_pos == position)
198 start_pos = (std::max)(static_cast<std::ptrdiff_t>(0), position - static_cast<std::ptrdiff_t>(10));
199 std::ptrdiff_t end_pos = (std::min)(position + static_cast<std::ptrdiff_t>(10), static_cast<std::ptrdiff_t>(m_end - m_base));
200 if(error_code != regex_constants::error_empty)
202 if((start_pos != 0) || (end_pos != (m_end - m_base)))
203 message += " The error occured while parsing the regular expression fragment: '";
205 message += " The error occured while parsing the regular expression: '";
206 if(start_pos != end_pos)
208 message += std::string(m_base + start_pos, m_base + position);
209 message += ">>>HERE>>>";
210 message += std::string(m_base + position, m_base + end_pos);
215 #ifndef BOOST_NO_EXCEPTIONS
216 if(0 == (this->flags() & regex_constants::no_except))
218 boost::regex_error e(message, error_code, position);
222 (void)position; // suppress warnings.
226 template <class charT, class traits>
227 bool basic_regex_parser<charT, traits>::parse_all()
230 while(result && (m_position != m_end))
232 result = (this->*m_parser_proc)();
238 #pragma warning(push)
239 #pragma warning(disable:4702)
241 template <class charT, class traits>
242 bool basic_regex_parser<charT, traits>::parse_basic()
244 switch(this->m_traits.syntax_type(*m_position))
246 case regex_constants::syntax_escape:
247 return parse_basic_escape();
248 case regex_constants::syntax_dot:
249 return parse_match_any();
250 case regex_constants::syntax_caret:
252 this->append_state(syntax_element_start_line);
254 case regex_constants::syntax_dollar:
256 this->append_state(syntax_element_end_line);
258 case regex_constants::syntax_star:
259 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line))
260 return parse_literal();
264 return parse_repeat();
266 case regex_constants::syntax_plus:
267 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
268 return parse_literal();
272 return parse_repeat(1);
274 case regex_constants::syntax_question:
275 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
276 return parse_literal();
280 return parse_repeat(0, 1);
282 case regex_constants::syntax_open_set:
284 case regex_constants::syntax_newline:
285 if(this->flags() & regbase::newline_alt)
288 return parse_literal();
290 return parse_literal();
295 template <class charT, class traits>
296 bool basic_regex_parser<charT, traits>::parse_extended()
299 switch(this->m_traits.syntax_type(*m_position))
301 case regex_constants::syntax_open_mark:
302 return parse_open_paren();
303 case regex_constants::syntax_close_mark:
305 case regex_constants::syntax_escape:
306 return parse_extended_escape();
307 case regex_constants::syntax_dot:
308 return parse_match_any();
309 case regex_constants::syntax_caret:
312 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line));
314 case regex_constants::syntax_dollar:
317 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line));
319 case regex_constants::syntax_star:
320 if(m_position == this->m_base)
322 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"*\" cannot start a regular expression.");
326 return parse_repeat();
327 case regex_constants::syntax_question:
328 if(m_position == this->m_base)
330 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"?\" cannot start a regular expression.");
334 return parse_repeat(0,1);
335 case regex_constants::syntax_plus:
336 if(m_position == this->m_base)
338 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"+\" cannot start a regular expression.");
342 return parse_repeat(1);
343 case regex_constants::syntax_open_brace:
345 return parse_repeat_range(false);
346 case regex_constants::syntax_close_brace:
347 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
349 case regex_constants::syntax_or:
351 case regex_constants::syntax_open_set:
353 case regex_constants::syntax_newline:
354 if(this->flags() & regbase::newline_alt)
357 return parse_literal();
358 case regex_constants::syntax_hash:
360 // If we have a mod_x flag set, then skip until
361 // we get to a newline character:
364 & (regbase::no_perl_ex|regbase::mod_x))
367 while((m_position != m_end) && !is_separator(*m_position++)){}
370 // Otherwise fall through:
372 result = parse_literal();
381 template <class charT, class traits>
382 bool basic_regex_parser<charT, traits>::parse_literal()
384 // append this as a literal provided it's not a space character
385 // or the perl option regbase::mod_x is not set:
388 & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex))
390 || !this->m_traits.isctype(*m_position, this->m_mask_space))
391 this->append_literal(*m_position);
396 template <class charT, class traits>
397 bool basic_regex_parser<charT, traits>::parse_open_paren()
400 // skip the '(' and error check:
402 if(++m_position == m_end)
404 fail(regex_constants::error_paren, m_position - m_base);
408 // begin by checking for a perl-style (?...) extension:
411 ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0)
412 || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
415 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
416 return parse_perl_extension();
419 // update our mark count, and append the required state:
422 if(0 == (this->flags() & regbase::nosubs))
424 markid = ++m_mark_count;
425 #ifndef BOOST_NO_STD_DISTANCE
426 if(this->flags() & regbase::save_subexpression_location)
427 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 1, 0));
429 if(this->flags() & regbase::save_subexpression_location)
430 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 1, 0));
433 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
435 pb->icase = this->flags() & regbase::icase;
436 std::ptrdiff_t last_paren_start = this->getoffset(pb);
437 // back up insertion point for alternations, and set new point:
438 std::ptrdiff_t last_alt_point = m_alt_insert_point;
439 this->m_pdata->m_data.align();
440 m_alt_insert_point = this->m_pdata->m_data.size();
442 // back up the current flags in case we have a nested (?imsx) group:
444 regex_constants::syntax_option_type opts = this->flags();
445 bool old_case_change = m_has_case_change;
446 m_has_case_change = false; // no changes to this scope as yet...
448 // Back up branch reset data in case we have a nested (?|...)
450 int mark_reset = m_mark_reset;
453 // now recursively add more states, this will terminate when we get to a
458 // Unwind pushed alternatives:
460 if(0 == unwind_alts(last_paren_start))
465 if(m_has_case_change)
467 // the case has changed in one or more of the alternatives
468 // within the scoped (...) block: we have to add a state
469 // to reset the case sensitivity:
470 static_cast<re_case*>(
471 this->append_state(syntax_element_toggle_case, sizeof(re_case))
472 )->icase = opts & regbase::icase;
475 m_has_case_change = old_case_change;
477 // restore branch reset:
479 m_mark_reset = mark_reset;
481 // we either have a ')' or we have run out of characters prematurely:
483 if(m_position == m_end)
485 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
488 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
489 #ifndef BOOST_NO_STD_DISTANCE
490 if(markid && (this->flags() & regbase::save_subexpression_location))
491 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position);
493 if(markid && (this->flags() & regbase::save_subexpression_location))
494 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base);
498 // append closing parenthesis state:
500 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
502 pb->icase = this->flags() & regbase::icase;
503 this->m_paren_start = last_paren_start;
505 // restore the alternate insertion point:
507 this->m_alt_insert_point = last_alt_point;
509 // allow backrefs to this mark:
511 if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT))
512 this->m_backrefs |= 1u << (markid - 1);
517 template <class charT, class traits>
518 bool basic_regex_parser<charT, traits>::parse_basic_escape()
522 switch(this->m_traits.escape_syntax_type(*m_position))
524 case regex_constants::syntax_open_mark:
525 return parse_open_paren();
526 case regex_constants::syntax_close_mark:
528 case regex_constants::syntax_plus:
529 if(this->flags() & regex_constants::bk_plus_qm)
532 return parse_repeat(1);
535 return parse_literal();
536 case regex_constants::syntax_question:
537 if(this->flags() & regex_constants::bk_plus_qm)
540 return parse_repeat(0, 1);
543 return parse_literal();
544 case regex_constants::syntax_open_brace:
545 if(this->flags() & regbase::no_intervals)
546 return parse_literal();
548 return parse_repeat_range(true);
549 case regex_constants::syntax_close_brace:
550 if(this->flags() & regbase::no_intervals)
551 return parse_literal();
552 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
554 case regex_constants::syntax_or:
555 if(this->flags() & regbase::bk_vbar)
558 result = parse_literal();
560 case regex_constants::syntax_digit:
561 return parse_backref();
562 case regex_constants::escape_type_start_buffer:
563 if(this->flags() & regbase::emacs_ex)
566 this->append_state(syntax_element_buffer_start);
569 result = parse_literal();
571 case regex_constants::escape_type_end_buffer:
572 if(this->flags() & regbase::emacs_ex)
575 this->append_state(syntax_element_buffer_end);
578 result = parse_literal();
580 case regex_constants::escape_type_word_assert:
581 if(this->flags() & regbase::emacs_ex)
584 this->append_state(syntax_element_word_boundary);
587 result = parse_literal();
589 case regex_constants::escape_type_not_word_assert:
590 if(this->flags() & regbase::emacs_ex)
593 this->append_state(syntax_element_within_word);
596 result = parse_literal();
598 case regex_constants::escape_type_left_word:
599 if(this->flags() & regbase::emacs_ex)
602 this->append_state(syntax_element_word_start);
605 result = parse_literal();
607 case regex_constants::escape_type_right_word:
608 if(this->flags() & regbase::emacs_ex)
611 this->append_state(syntax_element_word_end);
614 result = parse_literal();
617 if(this->flags() & regbase::emacs_ex)
627 basic_char_set<charT, traits> char_set;
630 char_set.add_class(this->m_word_mask);
631 if(0 == this->append_set(char_set))
633 fail(regex_constants::error_ctype, m_position - m_base);
643 return add_emacs_code(negate);
646 // not supported yet:
647 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.");
653 result = parse_literal();
659 template <class charT, class traits>
660 bool basic_regex_parser<charT, traits>::parse_extended_escape()
663 bool negate = false; // in case this is a character class escape: \w \d etc
664 switch(this->m_traits.escape_syntax_type(*m_position))
666 case regex_constants::escape_type_not_class:
669 case regex_constants::escape_type_class:
671 escape_type_class_jump:
672 typedef typename traits::char_class_type mask_type;
673 mask_type m = this->m_traits.lookup_classname(m_position, m_position+1);
676 basic_char_set<charT, traits> char_set;
679 char_set.add_class(m);
680 if(0 == this->append_set(char_set))
682 fail(regex_constants::error_ctype, m_position - m_base);
689 // not a class, just a regular unknown escape:
691 this->append_literal(unescape_character());
694 case regex_constants::syntax_digit:
695 return parse_backref();
696 case regex_constants::escape_type_left_word:
698 this->append_state(syntax_element_word_start);
700 case regex_constants::escape_type_right_word:
702 this->append_state(syntax_element_word_end);
704 case regex_constants::escape_type_start_buffer:
706 this->append_state(syntax_element_buffer_start);
708 case regex_constants::escape_type_end_buffer:
710 this->append_state(syntax_element_buffer_end);
712 case regex_constants::escape_type_word_assert:
714 this->append_state(syntax_element_word_boundary);
716 case regex_constants::escape_type_not_word_assert:
718 this->append_state(syntax_element_within_word);
720 case regex_constants::escape_type_Z:
722 this->append_state(syntax_element_soft_buffer_end);
724 case regex_constants::escape_type_Q:
726 case regex_constants::escape_type_C:
727 return parse_match_any();
728 case regex_constants::escape_type_X:
730 this->append_state(syntax_element_combining);
732 case regex_constants::escape_type_G:
734 this->append_state(syntax_element_restart_continue);
736 case regex_constants::escape_type_not_property:
739 case regex_constants::escape_type_property:
743 if(m_position == m_end)
745 fail(regex_constants::error_escape, m_position - m_base, "Incomplete property escape found.");
748 // maybe have \p{ddd}
749 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
751 const charT* base = m_position;
752 // skip forward until we find enclosing brace:
753 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
755 if(m_position == m_end)
757 fail(regex_constants::error_escape, m_position - m_base, "Closing } missing from property escape sequence.");
760 m = this->m_traits.lookup_classname(++base, m_position++);
764 m = this->m_traits.lookup_classname(m_position, m_position+1);
769 basic_char_set<charT, traits> char_set;
772 char_set.add_class(m);
773 if(0 == this->append_set(char_set))
775 fail(regex_constants::error_ctype, m_position - m_base);
780 fail(regex_constants::error_ctype, m_position - m_base, "Escape sequence was neither a valid property nor a valid character class name.");
783 case regex_constants::escape_type_reset_start_mark:
784 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
786 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
788 pb->icase = this->flags() & regbase::icase;
789 this->m_pdata->m_data.align();
793 goto escape_type_class_jump;
794 case regex_constants::escape_type_line_ending:
795 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
797 const charT* e = get_escape_R_string<charT>();
798 const charT* old_position = m_position;
799 const charT* old_end = m_end;
800 const charT* old_base = m_base;
803 m_end = e + traits::length(e);
804 bool r = parse_all();
805 m_position = ++old_position;
810 goto escape_type_class_jump;
811 case regex_constants::escape_type_extended_backref:
812 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
814 bool have_brace = false;
815 bool negative = false;
816 static const char* incomplete_message = "Incomplete \\g escape found.";
817 if(++m_position == m_end)
819 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
822 // maybe have \g{ddd}
823 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
825 if(++m_position == m_end)
827 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
832 negative = (*m_position == static_cast<charT>('-'));
833 if((negative) && (++m_position == m_end))
835 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
838 const charT* pc = m_position;
839 int i = this->m_traits.toi(pc, m_end, 10);
842 // Check for a named capture:
843 const charT* base = m_position;
844 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
846 i = this->m_pdata->get_id(base, m_position);
850 i = 1 + m_mark_count - i;
851 if((i > 0) && (this->m_backrefs & (1u << (i-1))))
854 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
856 pb->icase = this->flags() & regbase::icase;
860 fail(regex_constants::error_backref, m_position - m_base);
866 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
868 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
875 goto escape_type_class_jump;
876 case regex_constants::escape_type_control_v:
877 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
878 goto escape_type_class_jump;
881 this->append_literal(unescape_character());
887 template <class charT, class traits>
888 bool basic_regex_parser<charT, traits>::parse_match_any()
891 // we have a '.' that can match any character:
894 static_cast<re_dot*>(
895 this->append_state(syntax_element_wild, sizeof(re_dot))
896 )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
897 ? re_detail::force_not_newline
898 : this->flags() & regbase::mod_s ?
899 re_detail::force_newline : re_detail::dont_care);
903 template <class charT, class traits>
904 bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
907 bool pocessive = false;
908 std::size_t insert_point;
910 // when we get to here we may have a non-greedy ? mark still to come:
912 if((m_position != m_end)
914 (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
915 || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
919 // OK we have a perl or emacs regex, check for a '?':
920 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
925 // for perl regexes only check for pocessive ++ repeats.
926 if((0 == (this->flags() & regbase::main_option_type))
927 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
933 if(0 == this->m_last_state)
935 fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position), "Nothing to repeat.");
938 if(this->m_last_state->type == syntax_element_endmark)
940 // insert a repeat before the '(' matching the last ')':
941 insert_point = this->m_paren_start;
943 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
945 // the last state was a literal with more than one character, split it in two:
946 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
947 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
949 // now append new state:
950 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
952 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
953 insert_point = this->getoffset(this->m_last_state);
957 // repeat the last state whatever it was, need to add some error checking here:
958 switch(this->m_last_state->type)
960 case syntax_element_start_line:
961 case syntax_element_end_line:
962 case syntax_element_word_boundary:
963 case syntax_element_within_word:
964 case syntax_element_word_start:
965 case syntax_element_word_end:
966 case syntax_element_buffer_start:
967 case syntax_element_buffer_end:
968 case syntax_element_alt:
969 case syntax_element_soft_buffer_end:
970 case syntax_element_restart_continue:
971 case syntax_element_jump:
972 case syntax_element_startmark:
973 case syntax_element_backstep:
974 // can't legally repeat any of the above:
975 fail(regex_constants::error_badrepeat, m_position - m_base);
981 insert_point = this->getoffset(this->m_last_state);
984 // OK we now know what to repeat, so insert the repeat around it:
986 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
989 rep->greedy = greedy;
990 rep->leading = false;
991 // store our repeater position for later:
992 std::ptrdiff_t rep_off = this->getoffset(rep);
993 // and append a back jump to the repeat:
994 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
995 jmp->alt.i = rep_off - this->getoffset(jmp);
996 this->m_pdata->m_data.align();
997 // now fill in the alt jump for the repeat:
998 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
999 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
1001 // If the repeat is pocessive then bracket the repeat with a (?>...)
1002 // independent sub-expression construct:
1006 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
1008 pb->icase = this->flags() & regbase::icase;
1009 jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
1010 this->m_pdata->m_data.align();
1011 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
1012 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
1014 pb->icase = this->flags() & regbase::icase;
1019 template <class charT, class traits>
1020 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
1022 static const char* incomplete_message = "Missing } in quantified repetition.";
1024 // parse a repeat-range:
1026 std::size_t min, max;
1029 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1032 if(this->m_position == this->m_end)
1034 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1038 v = this->m_traits.toi(m_position, m_end, 10);
1040 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1044 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1047 else if(this->m_position == this->m_end)
1049 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1053 // see if we have a comma:
1054 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1056 // move on and error check:
1059 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1061 if(this->m_position == this->m_end)
1063 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1066 // get the value if any:
1067 v = this->m_traits.toi(m_position, m_end, 10);
1068 max = (v >= 0) ? v : (std::numeric_limits<std::size_t>::max)();
1072 // no comma, max = min:
1076 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1078 // OK now check trailing }:
1079 if(this->m_position == this->m_end)
1081 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1086 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1089 if(this->m_position == this->m_end)
1091 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1097 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1101 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1105 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1109 // finally go and add the repeat, unless error:
1113 // Backtrack to error location:
1115 while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
1117 fail(regex_constants::error_badbrace, m_position - m_base);
1120 return parse_repeat(min, max);
1123 template <class charT, class traits>
1124 bool basic_regex_parser<charT, traits>::parse_alt()
1127 // error check: if there have been no previous states,
1128 // or if the last state was a '(' then error:
1131 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1134 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1136 ((this->flags() & regbase::no_empty_expressions) == 0)
1140 fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression can start with the alternation operator |.");
1144 // Reset mark count if required:
1146 if(m_max_mark < m_mark_count)
1147 m_max_mark = m_mark_count;
1148 if(m_mark_reset >= 0)
1149 m_mark_count = m_mark_reset;
1153 // we need to append a trailing jump:
1155 re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump));
1156 std::ptrdiff_t jump_offset = this->getoffset(pj);
1158 // now insert the alternative:
1160 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1161 jump_offset += re_alt_size;
1162 this->m_pdata->m_data.align();
1163 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1165 // update m_alt_insert_point so that the next alternate gets
1166 // inserted at the start of the second of the two we've just created:
1168 this->m_alt_insert_point = this->m_pdata->m_data.size();
1170 // the start of this alternative must have a case changes state
1171 // if the current block has messed around with case changes:
1173 if(m_has_case_change)
1175 static_cast<re_case*>(
1176 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1177 )->icase = this->m_icase;
1180 // push the alternative onto our stack, a recursive
1181 // implementation here is easier to understand (and faster
1182 // as it happens), but causes all kinds of stack overflow problems
1183 // on programs with small stacks (COM+).
1185 m_alt_jumps.push_back(jump_offset);
1189 template <class charT, class traits>
1190 bool basic_regex_parser<charT, traits>::parse_set()
1192 static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1194 if(m_position == m_end)
1196 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1199 basic_char_set<charT, traits> char_set;
1201 const charT* base = m_position; // where the '[' was
1202 const charT* item_base = m_position; // where the '[' or '^' was
1204 while(m_position != m_end)
1206 switch(this->m_traits.syntax_type(*m_position))
1208 case regex_constants::syntax_caret:
1209 if(m_position == base)
1213 item_base = m_position;
1216 parse_set_literal(char_set);
1218 case regex_constants::syntax_close_set:
1219 if(m_position == item_base)
1221 parse_set_literal(char_set);
1227 if(0 == this->append_set(char_set))
1229 fail(regex_constants::error_ctype, m_position - m_base);
1234 case regex_constants::syntax_open_set:
1235 if(parse_inner_set(char_set))
1238 case regex_constants::syntax_escape:
1241 // look ahead and see if this is a character class shortcut
1245 if(this->m_traits.escape_syntax_type(*m_position)
1246 == regex_constants::escape_type_class)
1248 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1251 char_set.add_class(m);
1256 else if(this->m_traits.escape_syntax_type(*m_position)
1257 == regex_constants::escape_type_not_class)
1259 // negated character class:
1260 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1263 char_set.add_negated_class(m);
1268 // not a character class, just a regular escape:
1270 parse_set_literal(char_set);
1274 parse_set_literal(char_set);
1278 return m_position != m_end;
1281 template <class charT, class traits>
1282 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1284 static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1286 // we have either a character class [:name:]
1287 // a collating element [.name.]
1288 // or an equivalence class [=name=]
1290 if(m_end == ++m_position)
1292 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1295 switch(this->m_traits.syntax_type(*m_position))
1297 case regex_constants::syntax_dot:
1299 // a collating element is treated as a literal:
1302 parse_set_literal(char_set);
1304 case regex_constants::syntax_colon:
1306 // check that character classes are actually enabled:
1307 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1308 == (regbase::basic_syntax_group | regbase::no_char_classes))
1311 parse_set_literal(char_set);
1315 if(m_end == ++m_position)
1317 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1320 const charT* name_first = m_position;
1321 // skip at least one character, then find the matching ':]'
1322 if(m_end == ++m_position)
1324 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1327 while((m_position != m_end)
1328 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1330 const charT* name_last = m_position;
1331 if(m_end == m_position)
1333 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1336 if((m_end == ++m_position)
1337 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1339 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1343 // check for negated class:
1345 bool negated = false;
1346 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1351 typedef typename traits::char_class_type mask_type;
1352 mask_type m = this->m_traits.lookup_classname(name_first, name_last);
1355 if(char_set.empty() && (name_last - name_first == 1))
1357 // maybe a special case:
1359 if( (m_position != m_end)
1360 && (this->m_traits.syntax_type(*m_position)
1361 == regex_constants::syntax_close_set))
1363 if(this->m_traits.escape_syntax_type(*name_first)
1364 == regex_constants::escape_type_left_word)
1367 this->append_state(syntax_element_word_start);
1370 if(this->m_traits.escape_syntax_type(*name_first)
1371 == regex_constants::escape_type_right_word)
1374 this->append_state(syntax_element_word_end);
1379 fail(regex_constants::error_ctype, name_first - m_base);
1382 if(negated == false)
1383 char_set.add_class(m);
1385 char_set.add_negated_class(m);
1389 case regex_constants::syntax_equal:
1392 if(m_end == ++m_position)
1394 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1397 const charT* name_first = m_position;
1398 // skip at least one character, then find the matching '=]'
1399 if(m_end == ++m_position)
1401 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1404 while((m_position != m_end)
1405 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1407 const charT* name_last = m_position;
1408 if(m_end == m_position)
1410 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1413 if((m_end == ++m_position)
1414 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1416 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1419 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1420 if((0 == m.size()) || (m.size() > 2))
1422 fail(regex_constants::error_collate, name_first - m_base);
1431 char_set.add_equivalent(d);
1437 parse_set_literal(char_set);
1443 template <class charT, class traits>
1444 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1446 digraph<charT> start_range(get_next_set_literal(char_set));
1447 if(m_end == m_position)
1449 fail(regex_constants::error_brack, m_position - m_base);
1452 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1455 if(m_end == ++m_position)
1457 fail(regex_constants::error_brack, m_position - m_base);
1460 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1462 digraph<charT> end_range = get_next_set_literal(char_set);
1463 char_set.add_range(start_range, end_range);
1464 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1466 if(m_end == ++m_position)
1468 fail(regex_constants::error_brack, m_position - m_base);
1471 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1477 fail(regex_constants::error_range, m_position - m_base);
1484 char_set.add_single(start_range);
1487 template <class charT, class traits>
1488 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1490 digraph<charT> result;
1491 switch(this->m_traits.syntax_type(*m_position))
1493 case regex_constants::syntax_dash:
1494 if(!char_set.empty())
1496 // see if we are at the end of the set:
1497 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1499 fail(regex_constants::error_range, m_position - m_base);
1504 result.first = *m_position++;
1506 case regex_constants::syntax_escape:
1507 // check to see if escapes are supported first:
1508 if(this->flags() & regex_constants::no_escape_in_lists)
1510 result = *m_position++;
1514 result = unescape_character();
1516 case regex_constants::syntax_open_set:
1518 if(m_end == ++m_position)
1520 fail(regex_constants::error_collate, m_position - m_base);
1523 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1526 result.first = *m_position;
1530 if(m_end == ++m_position)
1532 fail(regex_constants::error_collate, m_position - m_base);
1535 const charT* name_first = m_position;
1536 // skip at least one character, then find the matching ':]'
1537 if(m_end == ++m_position)
1539 fail(regex_constants::error_collate, name_first - m_base);
1542 while((m_position != m_end)
1543 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1545 const charT* name_last = m_position;
1546 if(m_end == m_position)
1548 fail(regex_constants::error_collate, name_first - m_base);
1551 if((m_end == ++m_position)
1552 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1554 fail(regex_constants::error_collate, name_first - m_base);
1558 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1559 if(s.empty() || (s.size() > 2))
1561 fail(regex_constants::error_collate, name_first - m_base);
1564 result.first = s[0];
1566 result.second = s[1];
1572 result = *m_position++;
1578 // does a value fit in the specified charT type?
1580 template <class charT>
1581 bool valid_value(charT, int v, const mpl::true_&)
1583 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1585 template <class charT>
1586 bool valid_value(charT, int, const mpl::false_&)
1588 return true; // v will alsways fit in a charT
1590 template <class charT>
1591 bool valid_value(charT c, int v)
1593 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>());
1596 template <class charT, class traits>
1597 charT basic_regex_parser<charT, traits>::unescape_character()
1600 #pragma warning(push)
1601 #pragma warning(disable:4127)
1604 if(m_position == m_end)
1606 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
1609 switch(this->m_traits.escape_syntax_type(*m_position))
1611 case regex_constants::escape_type_control_a:
1612 result = charT('\a');
1614 case regex_constants::escape_type_e:
1617 case regex_constants::escape_type_control_f:
1618 result = charT('\f');
1620 case regex_constants::escape_type_control_n:
1621 result = charT('\n');
1623 case regex_constants::escape_type_control_r:
1624 result = charT('\r');
1626 case regex_constants::escape_type_control_t:
1627 result = charT('\t');
1629 case regex_constants::escape_type_control_v:
1630 result = charT('\v');
1632 case regex_constants::escape_type_word_assert:
1633 result = charT('\b');
1635 case regex_constants::escape_type_ascii_control:
1637 if(m_position == m_end)
1639 // Rewind to start of escape:
1641 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1642 fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
1645 result = static_cast<charT>(*m_position % 32);
1647 case regex_constants::escape_type_hex:
1649 if(m_position == m_end)
1651 // Rewind to start of escape:
1653 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1654 fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
1657 // maybe have \x{ddd}
1658 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1661 if(m_position == m_end)
1663 // Rewind to start of escape:
1665 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1666 fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
1669 int i = this->m_traits.toi(m_position, m_end, 16);
1670 if((m_position == m_end)
1672 || ((std::numeric_limits<charT>::is_specialized) && (i > (int)(std::numeric_limits<charT>::max)()))
1673 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1675 // Rewind to start of escape:
1677 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1678 fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
1686 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
1687 int i = this->m_traits.toi(m_position, m_position + len, 16);
1689 || !valid_value(charT(0), i))
1691 // Rewind to start of escape:
1693 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1694 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
1700 case regex_constants::syntax_digit:
1702 // an octal escape sequence, the first character must be a zero
1703 // followed by up to 3 octal digits:
1704 std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1705 const charT* bp = m_position;
1706 int val = this->m_traits.toi(bp, bp + 1, 8);
1709 // Rewind to start of escape:
1711 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1712 // Oops not an octal escape after all:
1713 fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
1716 val = this->m_traits.toi(m_position, m_position + len, 8);
1719 // Rewind to start of escape:
1721 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1722 fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
1725 return static_cast<charT>(val);
1727 case regex_constants::escape_type_named_char:
1730 if(m_position == m_end)
1732 // Rewind to start of escape:
1734 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1735 fail(regex_constants::error_escape, m_position - m_base);
1738 // maybe have \N{name}
1739 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1741 const charT* base = m_position;
1742 // skip forward until we find enclosing brace:
1743 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1745 if(m_position == m_end)
1747 // Rewind to start of escape:
1749 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1750 fail(regex_constants::error_escape, m_position - m_base);
1753 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
1756 // Rewind to start of escape:
1758 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1759 fail(regex_constants::error_collate, m_position - m_base);
1767 // fall through is a failure:
1768 // Rewind to start of escape:
1770 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1771 fail(regex_constants::error_escape, m_position - m_base);
1775 result = *m_position;
1781 #pragma warning(pop)
1785 template <class charT, class traits>
1786 bool basic_regex_parser<charT, traits>::parse_backref()
1788 BOOST_ASSERT(m_position != m_end);
1789 const charT* pc = m_position;
1790 int i = this->m_traits.toi(pc, pc + 1, 10);
1791 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1793 // not a backref at all but an octal escape sequence:
1794 charT c = unescape_character();
1795 this->append_literal(c);
1797 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1800 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1802 pb->icase = this->flags() & regbase::icase;
1806 // Rewind to start of escape:
1808 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1809 fail(regex_constants::error_backref, m_position - m_base);
1815 template <class charT, class traits>
1816 bool basic_regex_parser<charT, traits>::parse_QE()
1819 #pragma warning(push)
1820 #pragma warning(disable:4127)
1823 // parse a \Q...\E sequence:
1825 ++m_position; // skip the Q
1826 const charT* start = m_position;
1830 while((m_position != m_end)
1831 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1833 if(m_position == m_end)
1835 // a \Q...\E sequence may terminate with the end of the expression:
1839 if(++m_position == m_end) // skip the escape
1841 fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
1844 // check to see if it's a \E:
1845 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1848 end = m_position - 2;
1851 // otherwise go round again:
1854 // now add all the character between the two escapes as literals:
1858 this->append_literal(*start);
1863 #pragma warning(pop)
1867 template <class charT, class traits>
1868 bool basic_regex_parser<charT, traits>::parse_perl_extension()
1870 if(++m_position == m_end)
1872 // Rewind to start of (? sequence:
1874 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1875 fail(regex_constants::error_perl_extension, m_position - m_base);
1879 // treat comments as a special case, as these
1880 // are the only ones that don't start with a leading
1883 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
1885 while((m_position != m_end)
1886 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
1891 // backup some state, and prepare the way:
1894 std::ptrdiff_t jump_offset = 0;
1895 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
1896 pb->icase = this->flags() & regbase::icase;
1897 std::ptrdiff_t last_paren_start = this->getoffset(pb);
1898 // back up insertion point for alternations, and set new point:
1899 std::ptrdiff_t last_alt_point = m_alt_insert_point;
1900 this->m_pdata->m_data.align();
1901 m_alt_insert_point = this->m_pdata->m_data.size();
1902 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
1903 bool restore_flags = true;
1904 regex_constants::syntax_option_type old_flags = this->flags();
1905 bool old_case_change = m_has_case_change;
1906 m_has_case_change = false;
1908 int mark_reset = m_mark_reset;
1912 // select the actual extension used:
1914 switch(this->m_traits.syntax_type(*m_position))
1916 case regex_constants::syntax_or:
1917 m_mark_reset = m_mark_count;
1919 case regex_constants::syntax_colon:
1921 // a non-capturing mark:
1923 pb->index = markid = 0;
1926 case regex_constants::syntax_digit:
1929 // a recursive subexpression:
1931 v = this->m_traits.toi(m_position, m_end, 10);
1932 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1934 // Rewind to start of (? sequence:
1936 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1937 fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
1941 pb->index = markid = 0;
1942 re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
1945 static_cast<re_case*>(
1946 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1947 )->icase = this->flags() & regbase::icase;
1950 case regex_constants::syntax_plus:
1952 // A forward-relative recursive subexpression:
1955 v = this->m_traits.toi(m_position, m_end, 10);
1956 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1958 // Rewind to start of (? sequence:
1960 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1961 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
1965 goto insert_recursion;
1966 case regex_constants::syntax_dash:
1968 // Possibly a backward-relative recursive subexpression:
1971 v = this->m_traits.toi(m_position, m_end, 10);
1975 // Oops not a relative recursion at all, but a (?-imsx) group:
1976 goto option_group_jump;
1978 v = m_mark_count + 1 - v;
1981 // Rewind to start of (? sequence:
1983 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1984 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
1987 goto insert_recursion;
1988 case regex_constants::syntax_equal:
1989 pb->index = markid = -1;
1991 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1992 this->m_pdata->m_data.align();
1993 m_alt_insert_point = this->m_pdata->m_data.size();
1995 case regex_constants::syntax_not:
1996 pb->index = markid = -2;
1998 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1999 this->m_pdata->m_data.align();
2000 m_alt_insert_point = this->m_pdata->m_data.size();
2002 case regex_constants::escape_type_left_word:
2004 // a lookbehind assertion:
2005 if(++m_position == m_end)
2007 // Rewind to start of (? sequence:
2009 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2010 fail(regex_constants::error_perl_extension, m_position - m_base);
2013 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
2014 if(t == regex_constants::syntax_not)
2015 pb->index = markid = -2;
2016 else if(t == regex_constants::syntax_equal)
2017 pb->index = markid = -1;
2020 // Probably a named capture which also starts (?< :
2023 goto named_capture_jump;
2026 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2027 this->append_state(syntax_element_backstep, sizeof(re_brace));
2028 this->m_pdata->m_data.align();
2029 m_alt_insert_point = this->m_pdata->m_data.size();
2032 case regex_constants::escape_type_right_word:
2034 // an independent sub-expression:
2036 pb->index = markid = -3;
2038 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2039 this->m_pdata->m_data.align();
2040 m_alt_insert_point = this->m_pdata->m_data.size();
2042 case regex_constants::syntax_open_mark:
2044 // a conditional expression:
2045 pb->index = markid = -4;
2046 if(++m_position == m_end)
2048 // Rewind to start of (? sequence:
2050 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2051 fail(regex_constants::error_perl_extension, m_position - m_base);
2054 v = this->m_traits.toi(m_position, m_end, 10);
2055 if(*m_position == charT('R'))
2057 if(++m_position == m_end)
2059 // Rewind to start of (? sequence:
2061 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2062 fail(regex_constants::error_perl_extension, m_position - m_base);
2065 if(*m_position == charT('&'))
2067 const charT* base = ++m_position;
2068 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2070 if(m_position == m_end)
2072 // Rewind to start of (? sequence:
2074 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2075 fail(regex_constants::error_perl_extension, m_position - m_base);
2078 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
2082 v = -this->m_traits.toi(m_position, m_end, 10);
2084 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2085 br->index = v < 0 ? (v - 1) : 0;
2086 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2088 // Rewind to start of (? sequence:
2090 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2091 fail(regex_constants::error_perl_extension, m_position - m_base);
2094 if(++m_position == m_end)
2096 // Rewind to start of (? sequence:
2098 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2099 fail(regex_constants::error_perl_extension, m_position - m_base);
2103 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2105 const charT* base = ++m_position;
2106 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2108 if(m_position == m_end)
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);
2116 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2117 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2119 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2121 // Rewind to start of (? sequence:
2123 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2124 fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
2127 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2129 // Rewind to start of (? sequence:
2131 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2132 fail(regex_constants::error_perl_extension, m_position - m_base);
2135 if(++m_position == m_end)
2137 // Rewind to start of (? sequence:
2139 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2140 fail(regex_constants::error_perl_extension, m_position - m_base);
2144 else if(*m_position == charT('D'))
2146 const char* def = "DEFINE";
2147 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2148 ++m_position, ++def;
2149 if((m_position == m_end) || *def)
2151 // Rewind to start of (? sequence:
2153 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2154 fail(regex_constants::error_perl_extension, m_position - m_base);
2157 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2158 br->index = 9999; // special magic value!
2159 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2161 // Rewind to start of (? sequence:
2163 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2164 fail(regex_constants::error_perl_extension, m_position - m_base);
2167 if(++m_position == m_end)
2169 // Rewind to start of (? sequence:
2171 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2172 fail(regex_constants::error_perl_extension, m_position - m_base);
2178 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2180 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2182 // Rewind to start of (? sequence:
2184 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2185 fail(regex_constants::error_perl_extension, m_position - m_base);
2188 if(++m_position == m_end)
2190 // Rewind to start of (? sequence:
2192 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2193 fail(regex_constants::error_perl_extension, m_position - m_base);
2199 // verify that we have a lookahead or lookbehind assert:
2200 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2202 // Rewind to start of (? sequence:
2204 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2205 fail(regex_constants::error_perl_extension, m_position - m_base);
2208 if(++m_position == m_end)
2210 // Rewind to start of (? sequence:
2212 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2213 fail(regex_constants::error_perl_extension, m_position - m_base);
2216 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2218 if(++m_position == m_end)
2220 // Rewind to start of (? sequence:
2222 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2223 fail(regex_constants::error_perl_extension, m_position - m_base);
2226 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2227 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2229 // Rewind to start of (? sequence:
2231 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2232 fail(regex_constants::error_perl_extension, m_position - m_base);
2239 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2240 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2242 // Rewind to start of (? sequence:
2244 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2245 fail(regex_constants::error_perl_extension, m_position - m_base);
2253 case regex_constants::syntax_close_mark:
2254 // Rewind to start of (? sequence:
2256 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2257 fail(regex_constants::error_perl_extension, m_position - m_base);
2259 case regex_constants::escape_type_end_buffer:
2261 name_delim = *m_position;
2264 if(0 == (this->flags() & regbase::nosubs))
2266 markid = ++m_mark_count;
2267 #ifndef BOOST_NO_STD_DISTANCE
2268 if(this->flags() & regbase::save_subexpression_location)
2269 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2271 if(this->flags() & regbase::save_subexpression_location)
2272 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2276 const charT* base = ++m_position;
2277 if(m_position == m_end)
2279 // Rewind to start of (? sequence:
2281 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2282 fail(regex_constants::error_perl_extension, m_position - m_base);
2285 while((m_position != m_end) && (*m_position != name_delim))
2287 if(m_position == m_end)
2289 // Rewind to start of (? sequence:
2291 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2292 fail(regex_constants::error_perl_extension, m_position - m_base);
2295 this->m_pdata->set_name(base, m_position, markid);
2300 if(*m_position == charT('R'))
2304 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2306 // Rewind to start of (? sequence:
2308 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2309 fail(regex_constants::error_perl_extension, m_position - m_base);
2312 goto insert_recursion;
2314 if(*m_position == charT('&'))
2317 const charT* base = m_position;
2318 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2320 if(m_position == m_end)
2322 // Rewind to start of (? sequence:
2324 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2325 fail(regex_constants::error_perl_extension, m_position - m_base);
2328 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2329 goto insert_recursion;
2331 if(*m_position == charT('P'))
2334 if(m_position == m_end)
2336 // Rewind to start of (? sequence:
2338 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2339 fail(regex_constants::error_perl_extension, m_position - m_base);
2342 if(*m_position == charT('>'))
2345 const charT* base = m_position;
2346 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2348 if(m_position == m_end)
2350 // Rewind to start of (? sequence:
2352 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2353 fail(regex_constants::error_perl_extension, m_position - m_base);
2356 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2357 goto insert_recursion;
2361 // lets assume that we have a (?imsx) group and try and parse it:
2364 regex_constants::syntax_option_type opts = parse_options();
2365 if(m_position == m_end)
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 // make a note of whether we have a case change:
2374 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2375 pb->index = markid = 0;
2376 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2378 // update flags and carry on as normal:
2380 restore_flags = false;
2381 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2383 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2385 // update flags and carry on until the matching ')' is found:
2391 // Rewind to start of (? sequence:
2393 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2394 fail(regex_constants::error_perl_extension, m_position - m_base);
2398 // finally append a case change state if we need it:
2399 if(m_has_case_change)
2401 static_cast<re_case*>(
2402 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2403 )->icase = opts & regbase::icase;
2408 // now recursively add more states, this will terminate when we get to a
2413 // Unwind alternatives:
2415 if(0 == unwind_alts(last_paren_start))
2417 // Rewind to start of (? sequence:
2419 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2420 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block.");
2424 // we either have a ')' or we have run out of characters prematurely:
2426 if(m_position == m_end)
2428 // Rewind to start of (? sequence:
2430 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2431 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
2434 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2437 // restore the flags:
2441 // append a case change state if we need it:
2442 if(m_has_case_change)
2444 static_cast<re_case*>(
2445 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2446 )->icase = old_flags & regbase::icase;
2448 this->flags(old_flags);
2451 // set up the jump pointer if we have one:
2455 this->m_pdata->m_data.align();
2456 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2457 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2458 if(this->m_last_state == jmp)
2460 // Oops... we didn't have anything inside the assertion:
2461 // Rewind to start of (? sequence:
2463 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2464 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
2469 // verify that if this is conditional expression, that we do have
2470 // an alternative, if not add one:
2474 re_syntax_base* b = this->getaddress(expected_alt_point);
2475 // Make sure we have exactly one alternative following this state:
2476 if(b->type != syntax_element_alt)
2478 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2479 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2481 else if(this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2483 // Can't have seen more than one alternative:
2484 // Rewind to start of (? sequence:
2486 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2487 fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
2492 // We must *not* have seen an alternative inside a (DEFINE) block:
2493 b = this->getaddress(b->next.i, b);
2494 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2496 // Rewind to start of (? sequence:
2498 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2499 fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
2503 // check for invalid repetition of next state:
2504 b = this->getaddress(expected_alt_point);
2505 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2506 if((b->type != syntax_element_assert_backref)
2507 && (b->type != syntax_element_startmark))
2509 // Rewind to start of (? sequence:
2511 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2512 fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
2517 // append closing parenthesis state:
2519 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2521 pb->icase = this->flags() & regbase::icase;
2522 this->m_paren_start = last_paren_start;
2524 // restore the alternate insertion point:
2526 this->m_alt_insert_point = last_alt_point;
2528 // and the case change data:
2530 m_has_case_change = old_case_change;
2532 // And the mark_reset data:
2534 if(m_max_mark > m_mark_count)
2536 m_mark_count = m_max_mark;
2538 m_mark_reset = mark_reset;
2543 #ifndef BOOST_NO_STD_DISTANCE
2544 if(this->flags() & regbase::save_subexpression_location)
2545 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2547 if(this->flags() & regbase::save_subexpression_location)
2548 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2551 // allow backrefs to this mark:
2553 if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT)))
2554 this->m_backrefs |= 1u << (markid - 1);
2559 template <class charT, class traits>
2560 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2563 // parses an emacs style \sx or \Sx construct.
2565 if(++m_position == m_end)
2567 // Rewind to start of sequence:
2569 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
2570 fail(regex_constants::error_escape, m_position - m_base);
2573 basic_char_set<charT, traits> char_set;
2577 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2583 char_set.add_class(this->m_mask_space);
2586 char_set.add_class(this->m_word_mask);
2589 char_set.add_single(digraph<charT>(charT('$')));
2590 char_set.add_single(digraph<charT>(charT('&')));
2591 char_set.add_single(digraph<charT>(charT('*')));
2592 char_set.add_single(digraph<charT>(charT('+')));
2593 char_set.add_single(digraph<charT>(charT('-')));
2594 char_set.add_single(digraph<charT>(charT('_')));
2595 char_set.add_single(digraph<charT>(charT('<')));
2596 char_set.add_single(digraph<charT>(charT('>')));
2599 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2602 char_set.add_single(digraph<charT>(charT('(')));
2603 char_set.add_single(digraph<charT>(charT('[')));
2604 char_set.add_single(digraph<charT>(charT('{')));
2607 char_set.add_single(digraph<charT>(charT(')')));
2608 char_set.add_single(digraph<charT>(charT(']')));
2609 char_set.add_single(digraph<charT>(charT('}')));
2612 char_set.add_single(digraph<charT>(charT('"')));
2613 char_set.add_single(digraph<charT>(charT('\'')));
2614 char_set.add_single(digraph<charT>(charT('`')));
2617 char_set.add_single(digraph<charT>(charT('\'')));
2618 char_set.add_single(digraph<charT>(charT(',')));
2619 char_set.add_single(digraph<charT>(charT('#')));
2622 char_set.add_single(digraph<charT>(charT(';')));
2625 char_set.add_single(digraph<charT>(charT('\n')));
2626 char_set.add_single(digraph<charT>(charT('\f')));
2629 fail(regex_constants::error_ctype, m_position - m_base);
2632 if(0 == this->append_set(char_set))
2634 fail(regex_constants::error_ctype, m_position - m_base);
2641 template <class charT, class traits>
2642 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
2644 // we have a (?imsx-imsx) group, convert it into a set of flags:
2645 regex_constants::syntax_option_type f = this->flags();
2646 bool breakout = false;
2652 f |= regex_constants::mod_s;
2653 f &= ~regex_constants::no_mod_s;
2656 f &= ~regex_constants::no_mod_m;
2659 f |= regex_constants::icase;
2662 f |= regex_constants::mod_x;
2668 if(++m_position == m_end)
2670 // Rewind to start of (? sequence:
2672 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2673 fail(regex_constants::error_paren, m_position - m_base);
2679 if(*m_position == static_cast<charT>('-'))
2681 if(++m_position == m_end)
2683 // Rewind to start of (? sequence:
2685 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2686 fail(regex_constants::error_paren, m_position - m_base);
2694 f &= ~regex_constants::mod_s;
2695 f |= regex_constants::no_mod_s;
2698 f |= regex_constants::no_mod_m;
2701 f &= ~regex_constants::icase;
2704 f &= ~regex_constants::mod_x;
2710 if(++m_position == m_end)
2712 // Rewind to start of (? sequence:
2714 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2715 fail(regex_constants::error_paren, m_position - m_base);
2724 template <class charT, class traits>
2725 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
2728 // If we didn't actually add any states after the last
2729 // alternative then that's an error:
2731 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
2732 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
2735 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
2737 ((this->flags() & regbase::no_empty_expressions) == 0)
2741 fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
2745 // Fix up our alternatives:
2747 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
2750 // fix up the jump to point to the end of the states
2751 // that we've just added:
2753 std::ptrdiff_t jump_offset = m_alt_jumps.back();
2754 m_alt_jumps.pop_back();
2755 this->m_pdata->m_data.align();
2756 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2757 BOOST_ASSERT(jmp->type == syntax_element_jump);
2758 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
2764 #pragma warning(pop)
2767 } // namespace re_detail
2768 } // namespace boost
2771 #pragma warning(push)
2772 #pragma warning(disable: 4103)
2774 #ifdef BOOST_HAS_ABI_HEADERS
2775 # include BOOST_ABI_SUFFIX
2778 #pragma warning(pop)