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 regex_constants::syntax_type syn = this->m_traits.syntax_type(*m_position);
824 regex_constants::syntax_type syn_end = 0;
825 if((syn == regex_constants::syntax_open_brace)
826 || (syn == regex_constants::escape_type_left_word)
827 || (syn == regex_constants::escape_type_end_buffer))
829 if(++m_position == m_end)
831 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
837 case regex_constants::syntax_open_brace:
838 syn_end = regex_constants::syntax_close_brace;
840 case regex_constants::escape_type_left_word:
841 syn_end = regex_constants::escape_type_right_word;
844 syn_end = regex_constants::escape_type_end_buffer;
848 negative = (*m_position == static_cast<charT>('-'));
849 if((negative) && (++m_position == m_end))
851 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
854 const charT* pc = m_position;
855 int i = this->m_traits.toi(pc, m_end, 10);
856 if((i < 0) && syn_end)
858 // Check for a named capture, get the leftmost one if there is more than one:
859 const charT* base = m_position;
860 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != syn_end))
864 i = hash_value_from_capture_name(base, m_position);
868 i = 1 + m_mark_count - i;
869 if(((i > 0) && (this->m_backrefs & (1u << (i-1)))) || ((i > 10000) && (this->m_pdata->get_id(i) > 0) && (this->m_backrefs & (1u << (this->m_pdata->get_id(i)-1)))))
872 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
874 pb->icase = this->flags() & regbase::icase;
878 fail(regex_constants::error_backref, m_position - m_base);
884 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != syn_end))
886 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
893 goto escape_type_class_jump;
894 case regex_constants::escape_type_control_v:
895 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
896 goto escape_type_class_jump;
899 this->append_literal(unescape_character());
905 template <class charT, class traits>
906 bool basic_regex_parser<charT, traits>::parse_match_any()
909 // we have a '.' that can match any character:
912 static_cast<re_dot*>(
913 this->append_state(syntax_element_wild, sizeof(re_dot))
914 )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
915 ? re_detail::force_not_newline
916 : this->flags() & regbase::mod_s ?
917 re_detail::force_newline : re_detail::dont_care);
921 template <class charT, class traits>
922 bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
925 bool pocessive = false;
926 std::size_t insert_point;
928 // when we get to here we may have a non-greedy ? mark still to come:
930 if((m_position != m_end)
932 (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
933 || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
937 // OK we have a perl or emacs regex, check for a '?':
938 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
943 // for perl regexes only check for pocessive ++ repeats.
944 if((m_position != m_end)
945 && (0 == (this->flags() & regbase::main_option_type))
946 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
952 if(0 == this->m_last_state)
954 fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position), "Nothing to repeat.");
957 if(this->m_last_state->type == syntax_element_endmark)
959 // insert a repeat before the '(' matching the last ')':
960 insert_point = this->m_paren_start;
962 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
964 // the last state was a literal with more than one character, split it in two:
965 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
966 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
968 // now append new state:
969 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
971 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
972 insert_point = this->getoffset(this->m_last_state);
976 // repeat the last state whatever it was, need to add some error checking here:
977 switch(this->m_last_state->type)
979 case syntax_element_start_line:
980 case syntax_element_end_line:
981 case syntax_element_word_boundary:
982 case syntax_element_within_word:
983 case syntax_element_word_start:
984 case syntax_element_word_end:
985 case syntax_element_buffer_start:
986 case syntax_element_buffer_end:
987 case syntax_element_alt:
988 case syntax_element_soft_buffer_end:
989 case syntax_element_restart_continue:
990 case syntax_element_jump:
991 case syntax_element_startmark:
992 case syntax_element_backstep:
993 // can't legally repeat any of the above:
994 fail(regex_constants::error_badrepeat, m_position - m_base);
1000 insert_point = this->getoffset(this->m_last_state);
1003 // OK we now know what to repeat, so insert the repeat around it:
1005 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
1008 rep->greedy = greedy;
1009 rep->leading = false;
1010 // store our repeater position for later:
1011 std::ptrdiff_t rep_off = this->getoffset(rep);
1012 // and append a back jump to the repeat:
1013 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
1014 jmp->alt.i = rep_off - this->getoffset(jmp);
1015 this->m_pdata->m_data.align();
1016 // now fill in the alt jump for the repeat:
1017 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
1018 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
1020 // If the repeat is pocessive then bracket the repeat with a (?>...)
1021 // independent sub-expression construct:
1025 if(m_position != m_end)
1028 // Check for illegal following quantifier, we have to do this here, because
1029 // the extra states we insert below circumvents our usual error checking :-(
1031 switch(this->m_traits.syntax_type(*m_position))
1033 case regex_constants::syntax_star:
1034 case regex_constants::syntax_plus:
1035 case regex_constants::syntax_question:
1036 case regex_constants::syntax_open_brace:
1037 fail(regex_constants::error_badrepeat, m_position - m_base);
1041 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
1043 pb->icase = this->flags() & regbase::icase;
1044 jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
1045 this->m_pdata->m_data.align();
1046 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
1047 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
1049 pb->icase = this->flags() & regbase::icase;
1054 template <class charT, class traits>
1055 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
1057 static const char* incomplete_message = "Missing } in quantified repetition.";
1059 // parse a repeat-range:
1061 std::size_t min, max;
1064 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1067 if(this->m_position == this->m_end)
1069 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1073 v = this->m_traits.toi(m_position, m_end, 10);
1075 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1079 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1082 else if(this->m_position == this->m_end)
1084 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1088 // see if we have a comma:
1089 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1091 // move on and error check:
1094 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1096 if(this->m_position == this->m_end)
1098 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1101 // get the value if any:
1102 v = this->m_traits.toi(m_position, m_end, 10);
1103 max = (v >= 0) ? v : (std::numeric_limits<std::size_t>::max)();
1107 // no comma, max = min:
1111 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1113 // OK now check trailing }:
1114 if(this->m_position == this->m_end)
1116 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1121 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1124 if(this->m_position == this->m_end)
1126 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1132 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1136 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1140 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1144 // finally go and add the repeat, unless error:
1148 // Backtrack to error location:
1150 while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
1152 fail(regex_constants::error_badbrace, m_position - m_base);
1155 return parse_repeat(min, max);
1158 template <class charT, class traits>
1159 bool basic_regex_parser<charT, traits>::parse_alt()
1162 // error check: if there have been no previous states,
1163 // or if the last state was a '(' then error:
1166 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1169 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1171 ((this->flags() & regbase::no_empty_expressions) == 0)
1175 fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression can start with the alternation operator |.");
1179 // Reset mark count if required:
1181 if(m_max_mark < m_mark_count)
1182 m_max_mark = m_mark_count;
1183 if(m_mark_reset >= 0)
1184 m_mark_count = m_mark_reset;
1188 // we need to append a trailing jump:
1190 re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump));
1191 std::ptrdiff_t jump_offset = this->getoffset(pj);
1193 // now insert the alternative:
1195 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1196 jump_offset += re_alt_size;
1197 this->m_pdata->m_data.align();
1198 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1200 // update m_alt_insert_point so that the next alternate gets
1201 // inserted at the start of the second of the two we've just created:
1203 this->m_alt_insert_point = this->m_pdata->m_data.size();
1205 // the start of this alternative must have a case changes state
1206 // if the current block has messed around with case changes:
1208 if(m_has_case_change)
1210 static_cast<re_case*>(
1211 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1212 )->icase = this->m_icase;
1215 // push the alternative onto our stack, a recursive
1216 // implementation here is easier to understand (and faster
1217 // as it happens), but causes all kinds of stack overflow problems
1218 // on programs with small stacks (COM+).
1220 m_alt_jumps.push_back(jump_offset);
1224 template <class charT, class traits>
1225 bool basic_regex_parser<charT, traits>::parse_set()
1227 static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1229 if(m_position == m_end)
1231 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1234 basic_char_set<charT, traits> char_set;
1236 const charT* base = m_position; // where the '[' was
1237 const charT* item_base = m_position; // where the '[' or '^' was
1239 while(m_position != m_end)
1241 switch(this->m_traits.syntax_type(*m_position))
1243 case regex_constants::syntax_caret:
1244 if(m_position == base)
1248 item_base = m_position;
1251 parse_set_literal(char_set);
1253 case regex_constants::syntax_close_set:
1254 if(m_position == item_base)
1256 parse_set_literal(char_set);
1262 if(0 == this->append_set(char_set))
1264 fail(regex_constants::error_ctype, m_position - m_base);
1269 case regex_constants::syntax_open_set:
1270 if(parse_inner_set(char_set))
1273 case regex_constants::syntax_escape:
1276 // look ahead and see if this is a character class shortcut
1280 if(this->m_traits.escape_syntax_type(*m_position)
1281 == regex_constants::escape_type_class)
1283 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1286 char_set.add_class(m);
1291 else if(this->m_traits.escape_syntax_type(*m_position)
1292 == regex_constants::escape_type_not_class)
1294 // negated character class:
1295 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1298 char_set.add_negated_class(m);
1303 // not a character class, just a regular escape:
1305 parse_set_literal(char_set);
1309 parse_set_literal(char_set);
1313 return m_position != m_end;
1316 template <class charT, class traits>
1317 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1319 static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1321 // we have either a character class [:name:]
1322 // a collating element [.name.]
1323 // or an equivalence class [=name=]
1325 if(m_end == ++m_position)
1327 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1330 switch(this->m_traits.syntax_type(*m_position))
1332 case regex_constants::syntax_dot:
1334 // a collating element is treated as a literal:
1337 parse_set_literal(char_set);
1339 case regex_constants::syntax_colon:
1341 // check that character classes are actually enabled:
1342 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1343 == (regbase::basic_syntax_group | regbase::no_char_classes))
1346 parse_set_literal(char_set);
1350 if(m_end == ++m_position)
1352 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1355 const charT* name_first = m_position;
1356 // skip at least one character, then find the matching ':]'
1357 if(m_end == ++m_position)
1359 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1362 while((m_position != m_end)
1363 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1365 const charT* name_last = m_position;
1366 if(m_end == m_position)
1368 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1371 if((m_end == ++m_position)
1372 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1374 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1378 // check for negated class:
1380 bool negated = false;
1381 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1386 typedef typename traits::char_class_type mask_type;
1387 mask_type m = this->m_traits.lookup_classname(name_first, name_last);
1390 if(char_set.empty() && (name_last - name_first == 1))
1392 // maybe a special case:
1394 if( (m_position != m_end)
1395 && (this->m_traits.syntax_type(*m_position)
1396 == regex_constants::syntax_close_set))
1398 if(this->m_traits.escape_syntax_type(*name_first)
1399 == regex_constants::escape_type_left_word)
1402 this->append_state(syntax_element_word_start);
1405 if(this->m_traits.escape_syntax_type(*name_first)
1406 == regex_constants::escape_type_right_word)
1409 this->append_state(syntax_element_word_end);
1414 fail(regex_constants::error_ctype, name_first - m_base);
1417 if(negated == false)
1418 char_set.add_class(m);
1420 char_set.add_negated_class(m);
1424 case regex_constants::syntax_equal:
1427 if(m_end == ++m_position)
1429 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1432 const charT* name_first = m_position;
1433 // skip at least one character, then find the matching '=]'
1434 if(m_end == ++m_position)
1436 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1439 while((m_position != m_end)
1440 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1442 const charT* name_last = m_position;
1443 if(m_end == m_position)
1445 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1448 if((m_end == ++m_position)
1449 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1451 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1454 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1455 if((0 == m.size()) || (m.size() > 2))
1457 fail(regex_constants::error_collate, name_first - m_base);
1466 char_set.add_equivalent(d);
1472 parse_set_literal(char_set);
1478 template <class charT, class traits>
1479 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1481 digraph<charT> start_range(get_next_set_literal(char_set));
1482 if(m_end == m_position)
1484 fail(regex_constants::error_brack, m_position - m_base);
1487 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1490 if(m_end == ++m_position)
1492 fail(regex_constants::error_brack, m_position - m_base);
1495 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1497 digraph<charT> end_range = get_next_set_literal(char_set);
1498 char_set.add_range(start_range, end_range);
1499 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1501 if(m_end == ++m_position)
1503 fail(regex_constants::error_brack, m_position - m_base);
1506 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1512 fail(regex_constants::error_range, m_position - m_base);
1519 char_set.add_single(start_range);
1522 template <class charT, class traits>
1523 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1525 digraph<charT> result;
1526 switch(this->m_traits.syntax_type(*m_position))
1528 case regex_constants::syntax_dash:
1529 if(!char_set.empty())
1531 // see if we are at the end of the set:
1532 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1534 fail(regex_constants::error_range, m_position - m_base);
1539 result.first = *m_position++;
1541 case regex_constants::syntax_escape:
1542 // check to see if escapes are supported first:
1543 if(this->flags() & regex_constants::no_escape_in_lists)
1545 result = *m_position++;
1549 result = unescape_character();
1551 case regex_constants::syntax_open_set:
1553 if(m_end == ++m_position)
1555 fail(regex_constants::error_collate, m_position - m_base);
1558 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1561 result.first = *m_position;
1565 if(m_end == ++m_position)
1567 fail(regex_constants::error_collate, m_position - m_base);
1570 const charT* name_first = m_position;
1571 // skip at least one character, then find the matching ':]'
1572 if(m_end == ++m_position)
1574 fail(regex_constants::error_collate, name_first - m_base);
1577 while((m_position != m_end)
1578 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1580 const charT* name_last = m_position;
1581 if(m_end == m_position)
1583 fail(regex_constants::error_collate, name_first - m_base);
1586 if((m_end == ++m_position)
1587 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1589 fail(regex_constants::error_collate, name_first - m_base);
1593 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1594 if(s.empty() || (s.size() > 2))
1596 fail(regex_constants::error_collate, name_first - m_base);
1599 result.first = s[0];
1601 result.second = s[1];
1607 result = *m_position++;
1613 // does a value fit in the specified charT type?
1615 template <class charT>
1616 bool valid_value(charT, int v, const mpl::true_&)
1618 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1620 template <class charT>
1621 bool valid_value(charT, int, const mpl::false_&)
1623 return true; // v will alsways fit in a charT
1625 template <class charT>
1626 bool valid_value(charT c, int v)
1628 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>());
1631 template <class charT, class traits>
1632 charT basic_regex_parser<charT, traits>::unescape_character()
1635 #pragma warning(push)
1636 #pragma warning(disable:4127)
1639 if(m_position == m_end)
1641 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
1644 switch(this->m_traits.escape_syntax_type(*m_position))
1646 case regex_constants::escape_type_control_a:
1647 result = charT('\a');
1649 case regex_constants::escape_type_e:
1652 case regex_constants::escape_type_control_f:
1653 result = charT('\f');
1655 case regex_constants::escape_type_control_n:
1656 result = charT('\n');
1658 case regex_constants::escape_type_control_r:
1659 result = charT('\r');
1661 case regex_constants::escape_type_control_t:
1662 result = charT('\t');
1664 case regex_constants::escape_type_control_v:
1665 result = charT('\v');
1667 case regex_constants::escape_type_word_assert:
1668 result = charT('\b');
1670 case regex_constants::escape_type_ascii_control:
1672 if(m_position == m_end)
1674 // Rewind to start of escape:
1676 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1677 fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
1680 result = static_cast<charT>(*m_position % 32);
1682 case regex_constants::escape_type_hex:
1684 if(m_position == m_end)
1686 // Rewind to start of escape:
1688 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1689 fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
1692 // maybe have \x{ddd}
1693 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1696 if(m_position == m_end)
1698 // Rewind to start of escape:
1700 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1701 fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
1704 int i = this->m_traits.toi(m_position, m_end, 16);
1705 if((m_position == m_end)
1707 || ((std::numeric_limits<charT>::is_specialized) && (i > (int)(std::numeric_limits<charT>::max)()))
1708 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1710 // Rewind to start of escape:
1712 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1713 fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
1721 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
1722 int i = this->m_traits.toi(m_position, m_position + len, 16);
1724 || !valid_value(charT(0), i))
1726 // Rewind to start of escape:
1728 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1729 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
1735 case regex_constants::syntax_digit:
1737 // an octal escape sequence, the first character must be a zero
1738 // followed by up to 3 octal digits:
1739 std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1740 const charT* bp = m_position;
1741 int val = this->m_traits.toi(bp, bp + 1, 8);
1744 // Rewind to start of escape:
1746 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1747 // Oops not an octal escape after all:
1748 fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
1751 val = this->m_traits.toi(m_position, m_position + len, 8);
1754 // Rewind to start of escape:
1756 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1757 fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
1760 return static_cast<charT>(val);
1762 case regex_constants::escape_type_named_char:
1765 if(m_position == m_end)
1767 // Rewind to start of escape:
1769 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1770 fail(regex_constants::error_escape, m_position - m_base);
1773 // maybe have \N{name}
1774 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1776 const charT* base = m_position;
1777 // skip forward until we find enclosing brace:
1778 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1780 if(m_position == m_end)
1782 // Rewind to start of escape:
1784 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1785 fail(regex_constants::error_escape, m_position - m_base);
1788 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
1791 // Rewind to start of escape:
1793 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1794 fail(regex_constants::error_collate, m_position - m_base);
1802 // fall through is a failure:
1803 // Rewind to start of escape:
1805 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1806 fail(regex_constants::error_escape, m_position - m_base);
1810 result = *m_position;
1816 #pragma warning(pop)
1820 template <class charT, class traits>
1821 bool basic_regex_parser<charT, traits>::parse_backref()
1823 BOOST_ASSERT(m_position != m_end);
1824 const charT* pc = m_position;
1825 int i = this->m_traits.toi(pc, pc + 1, 10);
1826 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1828 // not a backref at all but an octal escape sequence:
1829 charT c = unescape_character();
1830 this->append_literal(c);
1832 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1835 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1837 pb->icase = this->flags() & regbase::icase;
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_backref, m_position - m_base);
1850 template <class charT, class traits>
1851 bool basic_regex_parser<charT, traits>::parse_QE()
1854 #pragma warning(push)
1855 #pragma warning(disable:4127)
1858 // parse a \Q...\E sequence:
1860 ++m_position; // skip the Q
1861 const charT* start = m_position;
1865 while((m_position != m_end)
1866 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1868 if(m_position == m_end)
1870 // a \Q...\E sequence may terminate with the end of the expression:
1874 if(++m_position == m_end) // skip the escape
1876 fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
1879 // check to see if it's a \E:
1880 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1883 end = m_position - 2;
1886 // otherwise go round again:
1889 // now add all the character between the two escapes as literals:
1893 this->append_literal(*start);
1898 #pragma warning(pop)
1902 template <class charT, class traits>
1903 bool basic_regex_parser<charT, traits>::parse_perl_extension()
1905 if(++m_position == m_end)
1907 // Rewind to start of (? sequence:
1909 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1910 fail(regex_constants::error_perl_extension, m_position - m_base);
1914 // treat comments as a special case, as these
1915 // are the only ones that don't start with a leading
1918 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
1920 while((m_position != m_end)
1921 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
1926 // backup some state, and prepare the way:
1929 std::ptrdiff_t jump_offset = 0;
1930 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
1931 pb->icase = this->flags() & regbase::icase;
1932 std::ptrdiff_t last_paren_start = this->getoffset(pb);
1933 // back up insertion point for alternations, and set new point:
1934 std::ptrdiff_t last_alt_point = m_alt_insert_point;
1935 this->m_pdata->m_data.align();
1936 m_alt_insert_point = this->m_pdata->m_data.size();
1937 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
1938 bool restore_flags = true;
1939 regex_constants::syntax_option_type old_flags = this->flags();
1940 bool old_case_change = m_has_case_change;
1941 m_has_case_change = false;
1943 int mark_reset = m_mark_reset;
1944 int max_mark = m_max_mark;
1946 m_max_mark = m_mark_count;
1949 // select the actual extension used:
1951 switch(this->m_traits.syntax_type(*m_position))
1953 case regex_constants::syntax_or:
1954 m_mark_reset = m_mark_count;
1956 case regex_constants::syntax_colon:
1958 // a non-capturing mark:
1960 pb->index = markid = 0;
1963 case regex_constants::syntax_digit:
1966 // a recursive subexpression:
1968 v = this->m_traits.toi(m_position, m_end, 10);
1969 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1971 // Rewind to start of (? sequence:
1973 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1974 fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
1978 pb->index = markid = 0;
1979 re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
1982 static_cast<re_case*>(
1983 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1984 )->icase = this->flags() & regbase::icase;
1987 case regex_constants::syntax_plus:
1989 // A forward-relative recursive subexpression:
1992 v = this->m_traits.toi(m_position, m_end, 10);
1993 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1995 // Rewind to start of (? sequence:
1997 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1998 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2002 goto insert_recursion;
2003 case regex_constants::syntax_dash:
2005 // Possibly a backward-relative recursive subexpression:
2008 v = this->m_traits.toi(m_position, m_end, 10);
2012 // Oops not a relative recursion at all, but a (?-imsx) group:
2013 goto option_group_jump;
2015 v = m_mark_count + 1 - v;
2018 // Rewind to start of (? sequence:
2020 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2021 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2024 goto insert_recursion;
2025 case regex_constants::syntax_equal:
2026 pb->index = markid = -1;
2028 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2029 this->m_pdata->m_data.align();
2030 m_alt_insert_point = this->m_pdata->m_data.size();
2032 case regex_constants::syntax_not:
2033 pb->index = markid = -2;
2035 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2036 this->m_pdata->m_data.align();
2037 m_alt_insert_point = this->m_pdata->m_data.size();
2039 case regex_constants::escape_type_left_word:
2041 // a lookbehind assertion:
2042 if(++m_position == m_end)
2044 // Rewind to start of (? sequence:
2046 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2047 fail(regex_constants::error_perl_extension, m_position - m_base);
2050 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
2051 if(t == regex_constants::syntax_not)
2052 pb->index = markid = -2;
2053 else if(t == regex_constants::syntax_equal)
2054 pb->index = markid = -1;
2057 // Probably a named capture which also starts (?< :
2060 goto named_capture_jump;
2063 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2064 this->append_state(syntax_element_backstep, sizeof(re_brace));
2065 this->m_pdata->m_data.align();
2066 m_alt_insert_point = this->m_pdata->m_data.size();
2069 case regex_constants::escape_type_right_word:
2071 // an independent sub-expression:
2073 pb->index = markid = -3;
2075 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2076 this->m_pdata->m_data.align();
2077 m_alt_insert_point = this->m_pdata->m_data.size();
2079 case regex_constants::syntax_open_mark:
2081 // a conditional expression:
2082 pb->index = markid = -4;
2083 if(++m_position == m_end)
2085 // Rewind to start of (? sequence:
2087 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2088 fail(regex_constants::error_perl_extension, m_position - m_base);
2091 v = this->m_traits.toi(m_position, m_end, 10);
2092 if(*m_position == charT('R'))
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);
2102 if(*m_position == charT('&'))
2104 const charT* base = ++m_position;
2105 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2107 if(m_position == m_end)
2109 // Rewind to start of (? sequence:
2111 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2112 fail(regex_constants::error_perl_extension, m_position - m_base);
2115 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
2119 v = -this->m_traits.toi(m_position, m_end, 10);
2121 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2122 br->index = v < 0 ? (v - 1) : 0;
2123 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2125 // Rewind to start of (? sequence:
2127 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2128 fail(regex_constants::error_perl_extension, m_position - m_base);
2131 if(++m_position == m_end)
2133 // Rewind to start of (? sequence:
2135 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2136 fail(regex_constants::error_perl_extension, m_position - m_base);
2140 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2142 const charT* base = ++m_position;
2143 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2145 if(m_position == m_end)
2147 // Rewind to start of (? sequence:
2149 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2150 fail(regex_constants::error_perl_extension, m_position - m_base);
2153 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2154 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2156 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2158 // Rewind to start of (? sequence:
2160 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2161 fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
2164 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2166 // Rewind to start of (? sequence:
2168 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2169 fail(regex_constants::error_perl_extension, m_position - m_base);
2172 if(++m_position == m_end)
2174 // Rewind to start of (? sequence:
2176 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2177 fail(regex_constants::error_perl_extension, m_position - m_base);
2181 else if(*m_position == charT('D'))
2183 const char* def = "DEFINE";
2184 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2185 ++m_position, ++def;
2186 if((m_position == m_end) || *def)
2188 // Rewind to start of (? sequence:
2190 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2191 fail(regex_constants::error_perl_extension, m_position - m_base);
2194 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2195 br->index = 9999; // special magic value!
2196 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2198 // Rewind to start of (? sequence:
2200 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2201 fail(regex_constants::error_perl_extension, m_position - m_base);
2204 if(++m_position == m_end)
2206 // Rewind to start of (? sequence:
2208 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2209 fail(regex_constants::error_perl_extension, m_position - m_base);
2215 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2217 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2219 // Rewind to start of (? sequence:
2221 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2222 fail(regex_constants::error_perl_extension, m_position - m_base);
2225 if(++m_position == m_end)
2227 // Rewind to start of (? sequence:
2229 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2230 fail(regex_constants::error_perl_extension, m_position - m_base);
2236 // verify that we have a lookahead or lookbehind assert:
2237 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2239 // Rewind to start of (? sequence:
2241 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2242 fail(regex_constants::error_perl_extension, m_position - m_base);
2245 if(++m_position == m_end)
2247 // Rewind to start of (? sequence:
2249 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2250 fail(regex_constants::error_perl_extension, m_position - m_base);
2253 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2255 if(++m_position == m_end)
2257 // Rewind to start of (? sequence:
2259 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2260 fail(regex_constants::error_perl_extension, m_position - m_base);
2263 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2264 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2266 // Rewind to start of (? sequence:
2268 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2269 fail(regex_constants::error_perl_extension, m_position - m_base);
2276 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2277 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
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);
2290 case regex_constants::syntax_close_mark:
2291 // Rewind to start of (? sequence:
2293 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2294 fail(regex_constants::error_perl_extension, m_position - m_base);
2296 case regex_constants::escape_type_end_buffer:
2298 name_delim = *m_position;
2301 if(0 == (this->flags() & regbase::nosubs))
2303 markid = ++m_mark_count;
2304 #ifndef BOOST_NO_STD_DISTANCE
2305 if(this->flags() & regbase::save_subexpression_location)
2306 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2308 if(this->flags() & regbase::save_subexpression_location)
2309 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2313 const charT* base = ++m_position;
2314 if(m_position == m_end)
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 while((m_position != m_end) && (*m_position != name_delim))
2324 if(m_position == m_end)
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 this->m_pdata->set_name(base, m_position, markid);
2337 if(*m_position == charT('R'))
2341 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2343 // Rewind to start of (? sequence:
2345 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2346 fail(regex_constants::error_perl_extension, m_position - m_base);
2349 goto insert_recursion;
2351 if(*m_position == charT('&'))
2354 const charT* base = m_position;
2355 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2357 if(m_position == m_end)
2359 // Rewind to start of (? sequence:
2361 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2362 fail(regex_constants::error_perl_extension, m_position - m_base);
2365 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2366 goto insert_recursion;
2368 if(*m_position == charT('P'))
2371 if(m_position == m_end)
2373 // Rewind to start of (? sequence:
2375 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2376 fail(regex_constants::error_perl_extension, m_position - m_base);
2379 if(*m_position == charT('>'))
2382 const charT* base = m_position;
2383 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2385 if(m_position == m_end)
2387 // Rewind to start of (? sequence:
2389 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2390 fail(regex_constants::error_perl_extension, m_position - m_base);
2393 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2394 goto insert_recursion;
2398 // lets assume that we have a (?imsx) group and try and parse it:
2401 regex_constants::syntax_option_type opts = parse_options();
2402 if(m_position == m_end)
2404 // Rewind to start of (? sequence:
2406 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2407 fail(regex_constants::error_perl_extension, m_position - m_base);
2410 // make a note of whether we have a case change:
2411 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2412 pb->index = markid = 0;
2413 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2415 // update flags and carry on as normal:
2417 restore_flags = false;
2418 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2420 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2422 // update flags and carry on until the matching ')' is found:
2428 // Rewind to start of (? sequence:
2430 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2431 fail(regex_constants::error_perl_extension, m_position - m_base);
2435 // finally append a case change state if we need it:
2436 if(m_has_case_change)
2438 static_cast<re_case*>(
2439 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2440 )->icase = opts & regbase::icase;
2445 // now recursively add more states, this will terminate when we get to a
2450 // Unwind alternatives:
2452 if(0 == unwind_alts(last_paren_start))
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, "Invalid alternation operators within (?...) block.");
2461 // we either have a ')' or we have run out of characters prematurely:
2463 if(m_position == m_end)
2465 // Rewind to start of (? sequence:
2467 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2468 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
2471 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2474 // restore the flags:
2478 // append a case change state if we need it:
2479 if(m_has_case_change)
2481 static_cast<re_case*>(
2482 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2483 )->icase = old_flags & regbase::icase;
2485 this->flags(old_flags);
2488 // set up the jump pointer if we have one:
2492 this->m_pdata->m_data.align();
2493 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2494 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2495 if(this->m_last_state == jmp)
2497 // Oops... we didn't have anything inside the assertion:
2498 // Rewind to start of (? sequence:
2500 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2501 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
2506 // verify that if this is conditional expression, that we do have
2507 // an alternative, if not add one:
2511 re_syntax_base* b = this->getaddress(expected_alt_point);
2512 // Make sure we have exactly one alternative following this state:
2513 if(b->type != syntax_element_alt)
2515 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2516 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2518 else if(this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2520 // Can't have seen more than one alternative:
2521 // Rewind to start of (? sequence:
2523 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2524 fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
2529 // We must *not* have seen an alternative inside a (DEFINE) block:
2530 b = this->getaddress(b->next.i, b);
2531 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2533 // Rewind to start of (? sequence:
2535 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2536 fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
2540 // check for invalid repetition of next state:
2541 b = this->getaddress(expected_alt_point);
2542 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2543 if((b->type != syntax_element_assert_backref)
2544 && (b->type != syntax_element_startmark))
2546 // Rewind to start of (? sequence:
2548 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2549 fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
2554 // append closing parenthesis state:
2556 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2558 pb->icase = this->flags() & regbase::icase;
2559 this->m_paren_start = last_paren_start;
2561 // restore the alternate insertion point:
2563 this->m_alt_insert_point = last_alt_point;
2565 // and the case change data:
2567 m_has_case_change = old_case_change;
2569 // And the mark_reset data:
2571 if(m_max_mark > m_mark_count)
2573 m_mark_count = m_max_mark;
2575 m_mark_reset = mark_reset;
2576 m_max_mark = max_mark;
2581 #ifndef BOOST_NO_STD_DISTANCE
2582 if(this->flags() & regbase::save_subexpression_location)
2583 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2585 if(this->flags() & regbase::save_subexpression_location)
2586 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2589 // allow backrefs to this mark:
2591 if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT)))
2592 this->m_backrefs |= 1u << (markid - 1);
2597 template <class charT, class traits>
2598 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2601 // parses an emacs style \sx or \Sx construct.
2603 if(++m_position == m_end)
2605 // Rewind to start of sequence:
2607 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
2608 fail(regex_constants::error_escape, m_position - m_base);
2611 basic_char_set<charT, traits> char_set;
2615 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2621 char_set.add_class(this->m_mask_space);
2624 char_set.add_class(this->m_word_mask);
2627 char_set.add_single(digraph<charT>(charT('$')));
2628 char_set.add_single(digraph<charT>(charT('&')));
2629 char_set.add_single(digraph<charT>(charT('*')));
2630 char_set.add_single(digraph<charT>(charT('+')));
2631 char_set.add_single(digraph<charT>(charT('-')));
2632 char_set.add_single(digraph<charT>(charT('_')));
2633 char_set.add_single(digraph<charT>(charT('<')));
2634 char_set.add_single(digraph<charT>(charT('>')));
2637 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2640 char_set.add_single(digraph<charT>(charT('(')));
2641 char_set.add_single(digraph<charT>(charT('[')));
2642 char_set.add_single(digraph<charT>(charT('{')));
2645 char_set.add_single(digraph<charT>(charT(')')));
2646 char_set.add_single(digraph<charT>(charT(']')));
2647 char_set.add_single(digraph<charT>(charT('}')));
2650 char_set.add_single(digraph<charT>(charT('"')));
2651 char_set.add_single(digraph<charT>(charT('\'')));
2652 char_set.add_single(digraph<charT>(charT('`')));
2655 char_set.add_single(digraph<charT>(charT('\'')));
2656 char_set.add_single(digraph<charT>(charT(',')));
2657 char_set.add_single(digraph<charT>(charT('#')));
2660 char_set.add_single(digraph<charT>(charT(';')));
2663 char_set.add_single(digraph<charT>(charT('\n')));
2664 char_set.add_single(digraph<charT>(charT('\f')));
2667 fail(regex_constants::error_ctype, m_position - m_base);
2670 if(0 == this->append_set(char_set))
2672 fail(regex_constants::error_ctype, m_position - m_base);
2679 template <class charT, class traits>
2680 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
2682 // we have a (?imsx-imsx) group, convert it into a set of flags:
2683 regex_constants::syntax_option_type f = this->flags();
2684 bool breakout = false;
2690 f |= regex_constants::mod_s;
2691 f &= ~regex_constants::no_mod_s;
2694 f &= ~regex_constants::no_mod_m;
2697 f |= regex_constants::icase;
2700 f |= regex_constants::mod_x;
2706 if(++m_position == m_end)
2708 // Rewind to start of (? sequence:
2710 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2711 fail(regex_constants::error_paren, m_position - m_base);
2719 if(*m_position == static_cast<charT>('-'))
2721 if(++m_position == m_end)
2723 // Rewind to start of (? sequence:
2725 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2726 fail(regex_constants::error_paren, m_position - m_base);
2734 f &= ~regex_constants::mod_s;
2735 f |= regex_constants::no_mod_s;
2738 f |= regex_constants::no_mod_m;
2741 f &= ~regex_constants::icase;
2744 f &= ~regex_constants::mod_x;
2750 if(++m_position == m_end)
2752 // Rewind to start of (? sequence:
2754 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2755 fail(regex_constants::error_paren, m_position - m_base);
2764 template <class charT, class traits>
2765 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
2768 // If we didn't actually add any states after the last
2769 // alternative then that's an error:
2771 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
2772 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
2775 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
2777 ((this->flags() & regbase::no_empty_expressions) == 0)
2781 fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
2785 // Fix up our alternatives:
2787 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
2790 // fix up the jump to point to the end of the states
2791 // that we've just added:
2793 std::ptrdiff_t jump_offset = m_alt_jumps.back();
2794 m_alt_jumps.pop_back();
2795 this->m_pdata->m_data.align();
2796 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2797 BOOST_ASSERT(jmp->type == syntax_element_jump);
2798 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
2804 #pragma warning(pop)
2807 } // namespace re_detail
2808 } // namespace boost
2811 #pragma warning(push)
2812 #pragma warning(disable: 4103)
2814 #ifdef BOOST_HAS_ABI_HEADERS
2815 # include BOOST_ABI_SUFFIX
2818 #pragma warning(pop)