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((0 == (this->flags() & regbase::main_option_type))
945 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
951 if(0 == this->m_last_state)
953 fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position), "Nothing to repeat.");
956 if(this->m_last_state->type == syntax_element_endmark)
958 // insert a repeat before the '(' matching the last ')':
959 insert_point = this->m_paren_start;
961 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
963 // the last state was a literal with more than one character, split it in two:
964 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
965 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
967 // now append new state:
968 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
970 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
971 insert_point = this->getoffset(this->m_last_state);
975 // repeat the last state whatever it was, need to add some error checking here:
976 switch(this->m_last_state->type)
978 case syntax_element_start_line:
979 case syntax_element_end_line:
980 case syntax_element_word_boundary:
981 case syntax_element_within_word:
982 case syntax_element_word_start:
983 case syntax_element_word_end:
984 case syntax_element_buffer_start:
985 case syntax_element_buffer_end:
986 case syntax_element_alt:
987 case syntax_element_soft_buffer_end:
988 case syntax_element_restart_continue:
989 case syntax_element_jump:
990 case syntax_element_startmark:
991 case syntax_element_backstep:
992 // can't legally repeat any of the above:
993 fail(regex_constants::error_badrepeat, m_position - m_base);
999 insert_point = this->getoffset(this->m_last_state);
1002 // OK we now know what to repeat, so insert the repeat around it:
1004 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
1007 rep->greedy = greedy;
1008 rep->leading = false;
1009 // store our repeater position for later:
1010 std::ptrdiff_t rep_off = this->getoffset(rep);
1011 // and append a back jump to the repeat:
1012 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
1013 jmp->alt.i = rep_off - this->getoffset(jmp);
1014 this->m_pdata->m_data.align();
1015 // now fill in the alt jump for the repeat:
1016 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
1017 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
1019 // If the repeat is pocessive then bracket the repeat with a (?>...)
1020 // independent sub-expression construct:
1024 if(m_position != m_end)
1027 // Check for illegal following quantifier, we have to do this here, because
1028 // the extra states we insert below circumvents are usual error checking :-(
1030 switch(this->m_traits.syntax_type(*m_position))
1032 case regex_constants::syntax_star:
1033 case regex_constants::syntax_plus:
1034 case regex_constants::syntax_question:
1035 fail(regex_constants::error_badrepeat, m_position - m_base);
1039 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
1041 pb->icase = this->flags() & regbase::icase;
1042 jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
1043 this->m_pdata->m_data.align();
1044 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
1045 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
1047 pb->icase = this->flags() & regbase::icase;
1052 template <class charT, class traits>
1053 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
1055 static const char* incomplete_message = "Missing } in quantified repetition.";
1057 // parse a repeat-range:
1059 std::size_t min, max;
1062 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1065 if(this->m_position == this->m_end)
1067 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1071 v = this->m_traits.toi(m_position, m_end, 10);
1073 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1077 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1080 else if(this->m_position == this->m_end)
1082 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1086 // see if we have a comma:
1087 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1089 // move on and error check:
1092 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1094 if(this->m_position == this->m_end)
1096 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1099 // get the value if any:
1100 v = this->m_traits.toi(m_position, m_end, 10);
1101 max = (v >= 0) ? v : (std::numeric_limits<std::size_t>::max)();
1105 // no comma, max = min:
1109 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1111 // OK now check trailing }:
1112 if(this->m_position == this->m_end)
1114 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1119 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1122 if(this->m_position == this->m_end)
1124 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1130 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1134 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1138 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1142 // finally go and add the repeat, unless error:
1146 // Backtrack to error location:
1148 while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
1150 fail(regex_constants::error_badbrace, m_position - m_base);
1153 return parse_repeat(min, max);
1156 template <class charT, class traits>
1157 bool basic_regex_parser<charT, traits>::parse_alt()
1160 // error check: if there have been no previous states,
1161 // or if the last state was a '(' then error:
1164 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1167 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1169 ((this->flags() & regbase::no_empty_expressions) == 0)
1173 fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression can start with the alternation operator |.");
1177 // Reset mark count if required:
1179 if(m_max_mark < m_mark_count)
1180 m_max_mark = m_mark_count;
1181 if(m_mark_reset >= 0)
1182 m_mark_count = m_mark_reset;
1186 // we need to append a trailing jump:
1188 re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump));
1189 std::ptrdiff_t jump_offset = this->getoffset(pj);
1191 // now insert the alternative:
1193 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1194 jump_offset += re_alt_size;
1195 this->m_pdata->m_data.align();
1196 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1198 // update m_alt_insert_point so that the next alternate gets
1199 // inserted at the start of the second of the two we've just created:
1201 this->m_alt_insert_point = this->m_pdata->m_data.size();
1203 // the start of this alternative must have a case changes state
1204 // if the current block has messed around with case changes:
1206 if(m_has_case_change)
1208 static_cast<re_case*>(
1209 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1210 )->icase = this->m_icase;
1213 // push the alternative onto our stack, a recursive
1214 // implementation here is easier to understand (and faster
1215 // as it happens), but causes all kinds of stack overflow problems
1216 // on programs with small stacks (COM+).
1218 m_alt_jumps.push_back(jump_offset);
1222 template <class charT, class traits>
1223 bool basic_regex_parser<charT, traits>::parse_set()
1225 static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1227 if(m_position == m_end)
1229 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1232 basic_char_set<charT, traits> char_set;
1234 const charT* base = m_position; // where the '[' was
1235 const charT* item_base = m_position; // where the '[' or '^' was
1237 while(m_position != m_end)
1239 switch(this->m_traits.syntax_type(*m_position))
1241 case regex_constants::syntax_caret:
1242 if(m_position == base)
1246 item_base = m_position;
1249 parse_set_literal(char_set);
1251 case regex_constants::syntax_close_set:
1252 if(m_position == item_base)
1254 parse_set_literal(char_set);
1260 if(0 == this->append_set(char_set))
1262 fail(regex_constants::error_ctype, m_position - m_base);
1267 case regex_constants::syntax_open_set:
1268 if(parse_inner_set(char_set))
1271 case regex_constants::syntax_escape:
1274 // look ahead and see if this is a character class shortcut
1278 if(this->m_traits.escape_syntax_type(*m_position)
1279 == regex_constants::escape_type_class)
1281 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1284 char_set.add_class(m);
1289 else if(this->m_traits.escape_syntax_type(*m_position)
1290 == regex_constants::escape_type_not_class)
1292 // negated character class:
1293 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1296 char_set.add_negated_class(m);
1301 // not a character class, just a regular escape:
1303 parse_set_literal(char_set);
1307 parse_set_literal(char_set);
1311 return m_position != m_end;
1314 template <class charT, class traits>
1315 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1317 static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1319 // we have either a character class [:name:]
1320 // a collating element [.name.]
1321 // or an equivalence class [=name=]
1323 if(m_end == ++m_position)
1325 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1328 switch(this->m_traits.syntax_type(*m_position))
1330 case regex_constants::syntax_dot:
1332 // a collating element is treated as a literal:
1335 parse_set_literal(char_set);
1337 case regex_constants::syntax_colon:
1339 // check that character classes are actually enabled:
1340 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1341 == (regbase::basic_syntax_group | regbase::no_char_classes))
1344 parse_set_literal(char_set);
1348 if(m_end == ++m_position)
1350 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1353 const charT* name_first = m_position;
1354 // skip at least one character, then find the matching ':]'
1355 if(m_end == ++m_position)
1357 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1360 while((m_position != m_end)
1361 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1363 const charT* name_last = m_position;
1364 if(m_end == m_position)
1366 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1369 if((m_end == ++m_position)
1370 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1372 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1376 // check for negated class:
1378 bool negated = false;
1379 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1384 typedef typename traits::char_class_type mask_type;
1385 mask_type m = this->m_traits.lookup_classname(name_first, name_last);
1388 if(char_set.empty() && (name_last - name_first == 1))
1390 // maybe a special case:
1392 if( (m_position != m_end)
1393 && (this->m_traits.syntax_type(*m_position)
1394 == regex_constants::syntax_close_set))
1396 if(this->m_traits.escape_syntax_type(*name_first)
1397 == regex_constants::escape_type_left_word)
1400 this->append_state(syntax_element_word_start);
1403 if(this->m_traits.escape_syntax_type(*name_first)
1404 == regex_constants::escape_type_right_word)
1407 this->append_state(syntax_element_word_end);
1412 fail(regex_constants::error_ctype, name_first - m_base);
1415 if(negated == false)
1416 char_set.add_class(m);
1418 char_set.add_negated_class(m);
1422 case regex_constants::syntax_equal:
1425 if(m_end == ++m_position)
1427 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1430 const charT* name_first = m_position;
1431 // skip at least one character, then find the matching '=]'
1432 if(m_end == ++m_position)
1434 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1437 while((m_position != m_end)
1438 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1440 const charT* name_last = m_position;
1441 if(m_end == m_position)
1443 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1446 if((m_end == ++m_position)
1447 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1449 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1452 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1453 if((0 == m.size()) || (m.size() > 2))
1455 fail(regex_constants::error_collate, name_first - m_base);
1464 char_set.add_equivalent(d);
1470 parse_set_literal(char_set);
1476 template <class charT, class traits>
1477 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1479 digraph<charT> start_range(get_next_set_literal(char_set));
1480 if(m_end == m_position)
1482 fail(regex_constants::error_brack, m_position - m_base);
1485 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1488 if(m_end == ++m_position)
1490 fail(regex_constants::error_brack, m_position - m_base);
1493 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1495 digraph<charT> end_range = get_next_set_literal(char_set);
1496 char_set.add_range(start_range, end_range);
1497 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1499 if(m_end == ++m_position)
1501 fail(regex_constants::error_brack, m_position - m_base);
1504 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1510 fail(regex_constants::error_range, m_position - m_base);
1517 char_set.add_single(start_range);
1520 template <class charT, class traits>
1521 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1523 digraph<charT> result;
1524 switch(this->m_traits.syntax_type(*m_position))
1526 case regex_constants::syntax_dash:
1527 if(!char_set.empty())
1529 // see if we are at the end of the set:
1530 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1532 fail(regex_constants::error_range, m_position - m_base);
1537 result.first = *m_position++;
1539 case regex_constants::syntax_escape:
1540 // check to see if escapes are supported first:
1541 if(this->flags() & regex_constants::no_escape_in_lists)
1543 result = *m_position++;
1547 result = unescape_character();
1549 case regex_constants::syntax_open_set:
1551 if(m_end == ++m_position)
1553 fail(regex_constants::error_collate, m_position - m_base);
1556 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1559 result.first = *m_position;
1563 if(m_end == ++m_position)
1565 fail(regex_constants::error_collate, m_position - m_base);
1568 const charT* name_first = m_position;
1569 // skip at least one character, then find the matching ':]'
1570 if(m_end == ++m_position)
1572 fail(regex_constants::error_collate, name_first - m_base);
1575 while((m_position != m_end)
1576 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1578 const charT* name_last = m_position;
1579 if(m_end == m_position)
1581 fail(regex_constants::error_collate, name_first - m_base);
1584 if((m_end == ++m_position)
1585 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1587 fail(regex_constants::error_collate, name_first - m_base);
1591 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1592 if(s.empty() || (s.size() > 2))
1594 fail(regex_constants::error_collate, name_first - m_base);
1597 result.first = s[0];
1599 result.second = s[1];
1605 result = *m_position++;
1611 // does a value fit in the specified charT type?
1613 template <class charT>
1614 bool valid_value(charT, int v, const mpl::true_&)
1616 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1618 template <class charT>
1619 bool valid_value(charT, int, const mpl::false_&)
1621 return true; // v will alsways fit in a charT
1623 template <class charT>
1624 bool valid_value(charT c, int v)
1626 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>());
1629 template <class charT, class traits>
1630 charT basic_regex_parser<charT, traits>::unescape_character()
1633 #pragma warning(push)
1634 #pragma warning(disable:4127)
1637 if(m_position == m_end)
1639 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
1642 switch(this->m_traits.escape_syntax_type(*m_position))
1644 case regex_constants::escape_type_control_a:
1645 result = charT('\a');
1647 case regex_constants::escape_type_e:
1650 case regex_constants::escape_type_control_f:
1651 result = charT('\f');
1653 case regex_constants::escape_type_control_n:
1654 result = charT('\n');
1656 case regex_constants::escape_type_control_r:
1657 result = charT('\r');
1659 case regex_constants::escape_type_control_t:
1660 result = charT('\t');
1662 case regex_constants::escape_type_control_v:
1663 result = charT('\v');
1665 case regex_constants::escape_type_word_assert:
1666 result = charT('\b');
1668 case regex_constants::escape_type_ascii_control:
1670 if(m_position == m_end)
1672 // Rewind to start of escape:
1674 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1675 fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
1678 result = static_cast<charT>(*m_position % 32);
1680 case regex_constants::escape_type_hex:
1682 if(m_position == m_end)
1684 // Rewind to start of escape:
1686 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1687 fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
1690 // maybe have \x{ddd}
1691 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1694 if(m_position == m_end)
1696 // Rewind to start of escape:
1698 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1699 fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
1702 int i = this->m_traits.toi(m_position, m_end, 16);
1703 if((m_position == m_end)
1705 || ((std::numeric_limits<charT>::is_specialized) && (i > (int)(std::numeric_limits<charT>::max)()))
1706 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1708 // Rewind to start of escape:
1710 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1711 fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
1719 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
1720 int i = this->m_traits.toi(m_position, m_position + len, 16);
1722 || !valid_value(charT(0), i))
1724 // Rewind to start of escape:
1726 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1727 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
1733 case regex_constants::syntax_digit:
1735 // an octal escape sequence, the first character must be a zero
1736 // followed by up to 3 octal digits:
1737 std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1738 const charT* bp = m_position;
1739 int val = this->m_traits.toi(bp, bp + 1, 8);
1742 // Rewind to start of escape:
1744 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1745 // Oops not an octal escape after all:
1746 fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
1749 val = this->m_traits.toi(m_position, m_position + len, 8);
1752 // Rewind to start of escape:
1754 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1755 fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
1758 return static_cast<charT>(val);
1760 case regex_constants::escape_type_named_char:
1763 if(m_position == m_end)
1765 // Rewind to start of escape:
1767 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1768 fail(regex_constants::error_escape, m_position - m_base);
1771 // maybe have \N{name}
1772 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1774 const charT* base = m_position;
1775 // skip forward until we find enclosing brace:
1776 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1778 if(m_position == m_end)
1780 // Rewind to start of escape:
1782 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1783 fail(regex_constants::error_escape, m_position - m_base);
1786 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
1789 // Rewind to start of escape:
1791 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1792 fail(regex_constants::error_collate, m_position - m_base);
1800 // fall through is a failure:
1801 // Rewind to start of escape:
1803 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1804 fail(regex_constants::error_escape, m_position - m_base);
1808 result = *m_position;
1814 #pragma warning(pop)
1818 template <class charT, class traits>
1819 bool basic_regex_parser<charT, traits>::parse_backref()
1821 BOOST_ASSERT(m_position != m_end);
1822 const charT* pc = m_position;
1823 int i = this->m_traits.toi(pc, pc + 1, 10);
1824 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1826 // not a backref at all but an octal escape sequence:
1827 charT c = unescape_character();
1828 this->append_literal(c);
1830 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1833 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1835 pb->icase = this->flags() & regbase::icase;
1839 // Rewind to start of escape:
1841 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1842 fail(regex_constants::error_backref, m_position - m_base);
1848 template <class charT, class traits>
1849 bool basic_regex_parser<charT, traits>::parse_QE()
1852 #pragma warning(push)
1853 #pragma warning(disable:4127)
1856 // parse a \Q...\E sequence:
1858 ++m_position; // skip the Q
1859 const charT* start = m_position;
1863 while((m_position != m_end)
1864 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1866 if(m_position == m_end)
1868 // a \Q...\E sequence may terminate with the end of the expression:
1872 if(++m_position == m_end) // skip the escape
1874 fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
1877 // check to see if it's a \E:
1878 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1881 end = m_position - 2;
1884 // otherwise go round again:
1887 // now add all the character between the two escapes as literals:
1891 this->append_literal(*start);
1896 #pragma warning(pop)
1900 template <class charT, class traits>
1901 bool basic_regex_parser<charT, traits>::parse_perl_extension()
1903 if(++m_position == m_end)
1905 // Rewind to start of (? sequence:
1907 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1908 fail(regex_constants::error_perl_extension, m_position - m_base);
1912 // treat comments as a special case, as these
1913 // are the only ones that don't start with a leading
1916 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
1918 while((m_position != m_end)
1919 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
1924 // backup some state, and prepare the way:
1927 std::ptrdiff_t jump_offset = 0;
1928 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
1929 pb->icase = this->flags() & regbase::icase;
1930 std::ptrdiff_t last_paren_start = this->getoffset(pb);
1931 // back up insertion point for alternations, and set new point:
1932 std::ptrdiff_t last_alt_point = m_alt_insert_point;
1933 this->m_pdata->m_data.align();
1934 m_alt_insert_point = this->m_pdata->m_data.size();
1935 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
1936 bool restore_flags = true;
1937 regex_constants::syntax_option_type old_flags = this->flags();
1938 bool old_case_change = m_has_case_change;
1939 m_has_case_change = false;
1941 int mark_reset = m_mark_reset;
1942 int max_mark = m_max_mark;
1944 m_max_mark = m_mark_count;
1947 // select the actual extension used:
1949 switch(this->m_traits.syntax_type(*m_position))
1951 case regex_constants::syntax_or:
1952 m_mark_reset = m_mark_count;
1954 case regex_constants::syntax_colon:
1956 // a non-capturing mark:
1958 pb->index = markid = 0;
1961 case regex_constants::syntax_digit:
1964 // a recursive subexpression:
1966 v = this->m_traits.toi(m_position, m_end, 10);
1967 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1969 // Rewind to start of (? sequence:
1971 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1972 fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
1976 pb->index = markid = 0;
1977 re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
1980 static_cast<re_case*>(
1981 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1982 )->icase = this->flags() & regbase::icase;
1985 case regex_constants::syntax_plus:
1987 // A forward-relative recursive subexpression:
1990 v = this->m_traits.toi(m_position, m_end, 10);
1991 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1993 // Rewind to start of (? sequence:
1995 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1996 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2000 goto insert_recursion;
2001 case regex_constants::syntax_dash:
2003 // Possibly a backward-relative recursive subexpression:
2006 v = this->m_traits.toi(m_position, m_end, 10);
2010 // Oops not a relative recursion at all, but a (?-imsx) group:
2011 goto option_group_jump;
2013 v = m_mark_count + 1 - v;
2016 // Rewind to start of (? sequence:
2018 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2019 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2022 goto insert_recursion;
2023 case regex_constants::syntax_equal:
2024 pb->index = markid = -1;
2026 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2027 this->m_pdata->m_data.align();
2028 m_alt_insert_point = this->m_pdata->m_data.size();
2030 case regex_constants::syntax_not:
2031 pb->index = markid = -2;
2033 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2034 this->m_pdata->m_data.align();
2035 m_alt_insert_point = this->m_pdata->m_data.size();
2037 case regex_constants::escape_type_left_word:
2039 // a lookbehind assertion:
2040 if(++m_position == m_end)
2042 // Rewind to start of (? sequence:
2044 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2045 fail(regex_constants::error_perl_extension, m_position - m_base);
2048 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
2049 if(t == regex_constants::syntax_not)
2050 pb->index = markid = -2;
2051 else if(t == regex_constants::syntax_equal)
2052 pb->index = markid = -1;
2055 // Probably a named capture which also starts (?< :
2058 goto named_capture_jump;
2061 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2062 this->append_state(syntax_element_backstep, sizeof(re_brace));
2063 this->m_pdata->m_data.align();
2064 m_alt_insert_point = this->m_pdata->m_data.size();
2067 case regex_constants::escape_type_right_word:
2069 // an independent sub-expression:
2071 pb->index = markid = -3;
2073 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2074 this->m_pdata->m_data.align();
2075 m_alt_insert_point = this->m_pdata->m_data.size();
2077 case regex_constants::syntax_open_mark:
2079 // a conditional expression:
2080 pb->index = markid = -4;
2081 if(++m_position == m_end)
2083 // Rewind to start of (? sequence:
2085 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2086 fail(regex_constants::error_perl_extension, m_position - m_base);
2089 v = this->m_traits.toi(m_position, m_end, 10);
2090 if(*m_position == charT('R'))
2092 if(++m_position == m_end)
2094 // Rewind to start of (? sequence:
2096 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2097 fail(regex_constants::error_perl_extension, m_position - m_base);
2100 if(*m_position == charT('&'))
2102 const charT* base = ++m_position;
2103 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2105 if(m_position == m_end)
2107 // Rewind to start of (? sequence:
2109 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2110 fail(regex_constants::error_perl_extension, m_position - m_base);
2113 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
2117 v = -this->m_traits.toi(m_position, m_end, 10);
2119 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2120 br->index = v < 0 ? (v - 1) : 0;
2121 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2123 // Rewind to start of (? sequence:
2125 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2126 fail(regex_constants::error_perl_extension, m_position - m_base);
2129 if(++m_position == m_end)
2131 // Rewind to start of (? sequence:
2133 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2134 fail(regex_constants::error_perl_extension, m_position - m_base);
2138 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2140 const charT* base = ++m_position;
2141 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2143 if(m_position == m_end)
2145 // Rewind to start of (? sequence:
2147 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2148 fail(regex_constants::error_perl_extension, m_position - m_base);
2151 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2152 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2154 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2156 // Rewind to start of (? sequence:
2158 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2159 fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
2162 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2164 // Rewind to start of (? sequence:
2166 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2167 fail(regex_constants::error_perl_extension, m_position - m_base);
2170 if(++m_position == m_end)
2172 // Rewind to start of (? sequence:
2174 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2175 fail(regex_constants::error_perl_extension, m_position - m_base);
2179 else if(*m_position == charT('D'))
2181 const char* def = "DEFINE";
2182 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2183 ++m_position, ++def;
2184 if((m_position == m_end) || *def)
2186 // Rewind to start of (? sequence:
2188 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2189 fail(regex_constants::error_perl_extension, m_position - m_base);
2192 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2193 br->index = 9999; // special magic value!
2194 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2196 // Rewind to start of (? sequence:
2198 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2199 fail(regex_constants::error_perl_extension, m_position - m_base);
2202 if(++m_position == m_end)
2204 // Rewind to start of (? sequence:
2206 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2207 fail(regex_constants::error_perl_extension, m_position - m_base);
2213 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2215 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2217 // Rewind to start of (? sequence:
2219 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2220 fail(regex_constants::error_perl_extension, m_position - m_base);
2223 if(++m_position == m_end)
2225 // Rewind to start of (? sequence:
2227 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2228 fail(regex_constants::error_perl_extension, m_position - m_base);
2234 // verify that we have a lookahead or lookbehind assert:
2235 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2237 // Rewind to start of (? sequence:
2239 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2240 fail(regex_constants::error_perl_extension, m_position - m_base);
2243 if(++m_position == m_end)
2245 // Rewind to start of (? sequence:
2247 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2248 fail(regex_constants::error_perl_extension, m_position - m_base);
2251 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2253 if(++m_position == m_end)
2255 // Rewind to start of (? sequence:
2257 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2258 fail(regex_constants::error_perl_extension, m_position - m_base);
2261 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2262 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2264 // Rewind to start of (? sequence:
2266 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2267 fail(regex_constants::error_perl_extension, m_position - m_base);
2274 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2275 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2277 // Rewind to start of (? sequence:
2279 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2280 fail(regex_constants::error_perl_extension, m_position - m_base);
2288 case regex_constants::syntax_close_mark:
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);
2294 case regex_constants::escape_type_end_buffer:
2296 name_delim = *m_position;
2299 if(0 == (this->flags() & regbase::nosubs))
2301 markid = ++m_mark_count;
2302 #ifndef BOOST_NO_STD_DISTANCE
2303 if(this->flags() & regbase::save_subexpression_location)
2304 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2306 if(this->flags() & regbase::save_subexpression_location)
2307 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2311 const charT* base = ++m_position;
2312 if(m_position == m_end)
2314 // Rewind to start of (? sequence:
2316 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2317 fail(regex_constants::error_perl_extension, m_position - m_base);
2320 while((m_position != m_end) && (*m_position != name_delim))
2322 if(m_position == m_end)
2324 // Rewind to start of (? sequence:
2326 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2327 fail(regex_constants::error_perl_extension, m_position - m_base);
2330 this->m_pdata->set_name(base, m_position, markid);
2335 if(*m_position == charT('R'))
2339 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2341 // Rewind to start of (? sequence:
2343 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2344 fail(regex_constants::error_perl_extension, m_position - m_base);
2347 goto insert_recursion;
2349 if(*m_position == charT('&'))
2352 const charT* base = m_position;
2353 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2355 if(m_position == m_end)
2357 // Rewind to start of (? sequence:
2359 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2360 fail(regex_constants::error_perl_extension, m_position - m_base);
2363 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2364 goto insert_recursion;
2366 if(*m_position == charT('P'))
2369 if(m_position == m_end)
2371 // Rewind to start of (? sequence:
2373 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2374 fail(regex_constants::error_perl_extension, m_position - m_base);
2377 if(*m_position == charT('>'))
2380 const charT* base = m_position;
2381 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2383 if(m_position == m_end)
2385 // Rewind to start of (? sequence:
2387 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2388 fail(regex_constants::error_perl_extension, m_position - m_base);
2391 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2392 goto insert_recursion;
2396 // lets assume that we have a (?imsx) group and try and parse it:
2399 regex_constants::syntax_option_type opts = parse_options();
2400 if(m_position == m_end)
2402 // Rewind to start of (? sequence:
2404 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2405 fail(regex_constants::error_perl_extension, m_position - m_base);
2408 // make a note of whether we have a case change:
2409 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2410 pb->index = markid = 0;
2411 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2413 // update flags and carry on as normal:
2415 restore_flags = false;
2416 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2418 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2420 // update flags and carry on until the matching ')' is found:
2426 // Rewind to start of (? sequence:
2428 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2429 fail(regex_constants::error_perl_extension, m_position - m_base);
2433 // finally append a case change state if we need it:
2434 if(m_has_case_change)
2436 static_cast<re_case*>(
2437 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2438 )->icase = opts & regbase::icase;
2443 // now recursively add more states, this will terminate when we get to a
2448 // Unwind alternatives:
2450 if(0 == unwind_alts(last_paren_start))
2452 // Rewind to start of (? sequence:
2454 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2455 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block.");
2459 // we either have a ')' or we have run out of characters prematurely:
2461 if(m_position == m_end)
2463 // Rewind to start of (? sequence:
2465 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2466 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
2469 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2472 // restore the flags:
2476 // append a case change state if we need it:
2477 if(m_has_case_change)
2479 static_cast<re_case*>(
2480 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2481 )->icase = old_flags & regbase::icase;
2483 this->flags(old_flags);
2486 // set up the jump pointer if we have one:
2490 this->m_pdata->m_data.align();
2491 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2492 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2493 if(this->m_last_state == jmp)
2495 // Oops... we didn't have anything inside the assertion:
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_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
2504 // verify that if this is conditional expression, that we do have
2505 // an alternative, if not add one:
2509 re_syntax_base* b = this->getaddress(expected_alt_point);
2510 // Make sure we have exactly one alternative following this state:
2511 if(b->type != syntax_element_alt)
2513 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2514 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2516 else if(this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2518 // Can't have seen more than one alternative:
2519 // Rewind to start of (? sequence:
2521 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2522 fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
2527 // We must *not* have seen an alternative inside a (DEFINE) block:
2528 b = this->getaddress(b->next.i, b);
2529 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2531 // Rewind to start of (? sequence:
2533 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2534 fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
2538 // check for invalid repetition of next state:
2539 b = this->getaddress(expected_alt_point);
2540 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2541 if((b->type != syntax_element_assert_backref)
2542 && (b->type != syntax_element_startmark))
2544 // Rewind to start of (? sequence:
2546 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2547 fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
2552 // append closing parenthesis state:
2554 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2556 pb->icase = this->flags() & regbase::icase;
2557 this->m_paren_start = last_paren_start;
2559 // restore the alternate insertion point:
2561 this->m_alt_insert_point = last_alt_point;
2563 // and the case change data:
2565 m_has_case_change = old_case_change;
2567 // And the mark_reset data:
2569 if(m_max_mark > m_mark_count)
2571 m_mark_count = m_max_mark;
2573 m_mark_reset = mark_reset;
2574 m_max_mark = max_mark;
2579 #ifndef BOOST_NO_STD_DISTANCE
2580 if(this->flags() & regbase::save_subexpression_location)
2581 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2583 if(this->flags() & regbase::save_subexpression_location)
2584 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2587 // allow backrefs to this mark:
2589 if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT)))
2590 this->m_backrefs |= 1u << (markid - 1);
2595 template <class charT, class traits>
2596 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2599 // parses an emacs style \sx or \Sx construct.
2601 if(++m_position == m_end)
2603 // Rewind to start of sequence:
2605 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
2606 fail(regex_constants::error_escape, m_position - m_base);
2609 basic_char_set<charT, traits> char_set;
2613 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2619 char_set.add_class(this->m_mask_space);
2622 char_set.add_class(this->m_word_mask);
2625 char_set.add_single(digraph<charT>(charT('$')));
2626 char_set.add_single(digraph<charT>(charT('&')));
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('>')));
2635 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2638 char_set.add_single(digraph<charT>(charT('(')));
2639 char_set.add_single(digraph<charT>(charT('[')));
2640 char_set.add_single(digraph<charT>(charT('{')));
2643 char_set.add_single(digraph<charT>(charT(')')));
2644 char_set.add_single(digraph<charT>(charT(']')));
2645 char_set.add_single(digraph<charT>(charT('}')));
2648 char_set.add_single(digraph<charT>(charT('"')));
2649 char_set.add_single(digraph<charT>(charT('\'')));
2650 char_set.add_single(digraph<charT>(charT('`')));
2653 char_set.add_single(digraph<charT>(charT('\'')));
2654 char_set.add_single(digraph<charT>(charT(',')));
2655 char_set.add_single(digraph<charT>(charT('#')));
2658 char_set.add_single(digraph<charT>(charT(';')));
2661 char_set.add_single(digraph<charT>(charT('\n')));
2662 char_set.add_single(digraph<charT>(charT('\f')));
2665 fail(regex_constants::error_ctype, m_position - m_base);
2668 if(0 == this->append_set(char_set))
2670 fail(regex_constants::error_ctype, m_position - m_base);
2677 template <class charT, class traits>
2678 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
2680 // we have a (?imsx-imsx) group, convert it into a set of flags:
2681 regex_constants::syntax_option_type f = this->flags();
2682 bool breakout = false;
2688 f |= regex_constants::mod_s;
2689 f &= ~regex_constants::no_mod_s;
2692 f &= ~regex_constants::no_mod_m;
2695 f |= regex_constants::icase;
2698 f |= regex_constants::mod_x;
2704 if(++m_position == m_end)
2706 // Rewind to start of (? sequence:
2708 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2709 fail(regex_constants::error_paren, m_position - m_base);
2717 if(*m_position == static_cast<charT>('-'))
2719 if(++m_position == m_end)
2721 // Rewind to start of (? sequence:
2723 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2724 fail(regex_constants::error_paren, m_position - m_base);
2732 f &= ~regex_constants::mod_s;
2733 f |= regex_constants::no_mod_s;
2736 f |= regex_constants::no_mod_m;
2739 f &= ~regex_constants::icase;
2742 f &= ~regex_constants::mod_x;
2748 if(++m_position == m_end)
2750 // Rewind to start of (? sequence:
2752 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2753 fail(regex_constants::error_paren, m_position - m_base);
2762 template <class charT, class traits>
2763 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
2766 // If we didn't actually add any states after the last
2767 // alternative then that's an error:
2769 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
2770 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
2773 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
2775 ((this->flags() & regbase::no_empty_expressions) == 0)
2779 fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
2783 // Fix up our alternatives:
2785 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
2788 // fix up the jump to point to the end of the states
2789 // that we've just added:
2791 std::ptrdiff_t jump_offset = m_alt_jumps.back();
2792 m_alt_jumps.pop_back();
2793 this->m_pdata->m_data.align();
2794 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2795 BOOST_ASSERT(jmp->type == syntax_element_jump);
2796 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
2802 #pragma warning(pop)
2805 } // namespace re_detail
2806 } // namespace boost
2809 #pragma warning(push)
2810 #pragma warning(disable: 4103)
2812 #ifdef BOOST_HAS_ABI_HEADERS
2813 # include BOOST_ABI_SUFFIX
2816 #pragma warning(pop)