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);
51 bool parse_extended();
53 bool parse_open_paren();
54 bool parse_basic_escape();
55 bool parse_extended_escape();
56 bool parse_match_any();
57 bool parse_repeat(std::size_t low = 0, std::size_t high = (std::numeric_limits<std::size_t>::max)());
58 bool parse_repeat_range(bool isbasic);
62 void parse_set_literal(basic_char_set<charT, traits>& char_set);
63 bool parse_inner_set(basic_char_set<charT, traits>& char_set);
65 bool parse_perl_extension();
66 bool add_emacs_code(bool negate);
67 bool unwind_alts(std::ptrdiff_t last_paren_start);
68 digraph<charT> get_next_set_literal(basic_char_set<charT, traits>& char_set);
69 charT unescape_character();
70 regex_constants::syntax_option_type parse_options();
73 typedef bool (basic_regex_parser::*parser_proc_type)();
74 typedef typename traits::string_type string_type;
75 typedef typename traits::char_class_type char_class_type;
76 parser_proc_type m_parser_proc; // the main parser to use
77 const charT* m_base; // the start of the string being parsed
78 const charT* m_end; // the end of the string being parsed
79 const charT* m_position; // our current parser position
80 unsigned m_mark_count; // how many sub-expressions we have
81 int m_mark_reset; // used to indicate that we're inside a (?|...) block.
82 unsigned m_max_mark; // largest mark count seen inside a (?|...) block.
83 std::ptrdiff_t m_paren_start; // where the last seen ')' began (where repeats are inserted).
84 std::ptrdiff_t m_alt_insert_point; // where to insert the next alternative
85 bool m_has_case_change; // true if somewhere in the current block the case has changed
86 #if defined(BOOST_MSVC) && defined(_M_IX86)
87 // This is an ugly warning suppression workaround (for warnings *inside* std::vector
88 // that can not otherwise be suppressed)...
89 BOOST_STATIC_ASSERT(sizeof(long) >= sizeof(void*));
90 std::vector<long> m_alt_jumps; // list of alternative in the current scope.
92 std::vector<std::ptrdiff_t> m_alt_jumps; // list of alternative in the current scope.
95 basic_regex_parser& operator=(const basic_regex_parser&);
96 basic_regex_parser(const basic_regex_parser&);
99 template <class charT, class traits>
100 basic_regex_parser<charT, traits>::basic_regex_parser(regex_data<charT, traits>* data)
101 : 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)
105 template <class charT, class traits>
106 void basic_regex_parser<charT, traits>::parse(const charT* p1, const charT* p2, unsigned l_flags)
108 // pass l_flags on to base class:
111 m_position = m_base = p1;
113 // empty strings are errors:
116 ((l_flags & regbase::main_option_type) != regbase::perl_syntax_group)
117 || (l_flags & regbase::no_empty_expressions)
121 fail(regex_constants::error_empty, 0);
124 // select which parser to use:
125 switch(l_flags & regbase::main_option_type)
127 case regbase::perl_syntax_group:
129 m_parser_proc = &basic_regex_parser<charT, traits>::parse_extended;
131 // Add a leading paren with index zero to give recursions a target:
133 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
135 br->icase = this->flags() & regbase::icase;
138 case regbase::basic_syntax_group:
139 m_parser_proc = &basic_regex_parser<charT, traits>::parse_basic;
141 case regbase::literal:
142 m_parser_proc = &basic_regex_parser<charT, traits>::parse_literal;
146 // parse all our characters:
147 bool result = parse_all();
149 // Unwind our alternatives:
152 // reset l_flags as a global scope (?imsx) may have altered them:
153 this->flags(l_flags);
154 // if we haven't gobbled up all the characters then we must
155 // have had an unexpected ')' :
158 fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_position));
161 // if an error has been set then give up now:
162 if(this->m_pdata->m_status)
164 // fill in our sub-expression count:
165 this->m_pdata->m_mark_count = 1 + m_mark_count;
166 this->finalize(p1, p2);
169 template <class charT, class traits>
170 void basic_regex_parser<charT, traits>::fail(regex_constants::error_type error_code, std::ptrdiff_t position)
172 if(0 == this->m_pdata->m_status) // update the error code if not already set
173 this->m_pdata->m_status = error_code;
174 m_position = m_end; // don't bother parsing anything else
175 // get the error message:
176 std::string message = this->m_pdata->m_ptraits->error_string(error_code);
177 // and raise the exception, this will do nothing if exceptions are disabled:
178 #ifndef BOOST_NO_EXCEPTIONS
179 if(0 == (this->flags() & regex_constants::no_except))
181 boost::regex_error e(message, error_code, position);
185 (void)position; // suppress warnings.
189 template <class charT, class traits>
190 bool basic_regex_parser<charT, traits>::parse_all()
193 while(result && (m_position != m_end))
195 result = (this->*m_parser_proc)();
201 #pragma warning(push)
202 #pragma warning(disable:4702)
204 template <class charT, class traits>
205 bool basic_regex_parser<charT, traits>::parse_basic()
207 switch(this->m_traits.syntax_type(*m_position))
209 case regex_constants::syntax_escape:
210 return parse_basic_escape();
211 case regex_constants::syntax_dot:
212 return parse_match_any();
213 case regex_constants::syntax_caret:
215 this->append_state(syntax_element_start_line);
217 case regex_constants::syntax_dollar:
219 this->append_state(syntax_element_end_line);
221 case regex_constants::syntax_star:
222 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line))
223 return parse_literal();
227 return parse_repeat();
229 case regex_constants::syntax_plus:
230 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
231 return parse_literal();
235 return parse_repeat(1);
237 case regex_constants::syntax_question:
238 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
239 return parse_literal();
243 return parse_repeat(0, 1);
245 case regex_constants::syntax_open_set:
247 case regex_constants::syntax_newline:
248 if(this->flags() & regbase::newline_alt)
251 return parse_literal();
253 return parse_literal();
258 template <class charT, class traits>
259 bool basic_regex_parser<charT, traits>::parse_extended()
262 switch(this->m_traits.syntax_type(*m_position))
264 case regex_constants::syntax_open_mark:
265 return parse_open_paren();
266 case regex_constants::syntax_close_mark:
268 case regex_constants::syntax_escape:
269 return parse_extended_escape();
270 case regex_constants::syntax_dot:
271 return parse_match_any();
272 case regex_constants::syntax_caret:
275 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line));
277 case regex_constants::syntax_dollar:
280 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line));
282 case regex_constants::syntax_star:
283 if(m_position == this->m_base)
285 fail(regex_constants::error_badrepeat, 0);
289 return parse_repeat();
290 case regex_constants::syntax_question:
291 if(m_position == this->m_base)
293 fail(regex_constants::error_badrepeat, 0);
297 return parse_repeat(0,1);
298 case regex_constants::syntax_plus:
299 if(m_position == this->m_base)
301 fail(regex_constants::error_badrepeat, 0);
305 return parse_repeat(1);
306 case regex_constants::syntax_open_brace:
308 return parse_repeat_range(false);
309 case regex_constants::syntax_close_brace:
310 fail(regex_constants::error_brace, this->m_position - this->m_end);
312 case regex_constants::syntax_or:
314 case regex_constants::syntax_open_set:
316 case regex_constants::syntax_newline:
317 if(this->flags() & regbase::newline_alt)
320 return parse_literal();
321 case regex_constants::syntax_hash:
323 // If we have a mod_x flag set, then skip until
324 // we get to a newline character:
327 & (regbase::no_perl_ex|regbase::mod_x))
330 while((m_position != m_end) && !is_separator(*m_position++)){}
333 // Otherwise fall through:
335 result = parse_literal();
344 template <class charT, class traits>
345 bool basic_regex_parser<charT, traits>::parse_literal()
347 // append this as a literal provided it's not a space character
348 // or the perl option regbase::mod_x is not set:
351 & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex))
353 || !this->m_traits.isctype(*m_position, this->m_mask_space))
354 this->append_literal(*m_position);
359 template <class charT, class traits>
360 bool basic_regex_parser<charT, traits>::parse_open_paren()
363 // skip the '(' and error check:
365 if(++m_position == m_end)
367 fail(regex_constants::error_paren, m_position - m_base);
371 // begin by checking for a perl-style (?...) extension:
374 ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0)
375 || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
378 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
379 return parse_perl_extension();
382 // update our mark count, and append the required state:
385 if(0 == (this->flags() & regbase::nosubs))
387 markid = ++m_mark_count;
388 #ifndef BOOST_NO_STD_DISTANCE
389 if(this->flags() & regbase::save_subexpression_location)
390 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 1, 0));
392 if(this->flags() & regbase::save_subexpression_location)
393 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 1, 0));
396 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
398 pb->icase = this->flags() & regbase::icase;
399 std::ptrdiff_t last_paren_start = this->getoffset(pb);
400 // back up insertion point for alternations, and set new point:
401 std::ptrdiff_t last_alt_point = m_alt_insert_point;
402 this->m_pdata->m_data.align();
403 m_alt_insert_point = this->m_pdata->m_data.size();
405 // back up the current flags in case we have a nested (?imsx) group:
407 regex_constants::syntax_option_type opts = this->flags();
408 bool old_case_change = m_has_case_change;
409 m_has_case_change = false; // no changes to this scope as yet...
411 // Back up branch reset data in case we have a nested (?|...)
413 int mark_reset = m_mark_reset;
416 // now recursively add more states, this will terminate when we get to a
421 // Unwind pushed alternatives:
423 if(0 == unwind_alts(last_paren_start))
428 if(m_has_case_change)
430 // the case has changed in one or more of the alternatives
431 // within the scoped (...) block: we have to add a state
432 // to reset the case sensitivity:
433 static_cast<re_case*>(
434 this->append_state(syntax_element_toggle_case, sizeof(re_case))
435 )->icase = opts & regbase::icase;
438 m_has_case_change = old_case_change;
440 // restore branch reset:
442 m_mark_reset = mark_reset;
444 // we either have a ')' or we have run out of characters prematurely:
446 if(m_position == m_end)
448 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
451 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
452 #ifndef BOOST_NO_STD_DISTANCE
453 if(markid && (this->flags() & regbase::save_subexpression_location))
454 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position);
456 if(markid && (this->flags() & regbase::save_subexpression_location))
457 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base);
461 // append closing parenthesis state:
463 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
465 pb->icase = this->flags() & regbase::icase;
466 this->m_paren_start = last_paren_start;
468 // restore the alternate insertion point:
470 this->m_alt_insert_point = last_alt_point;
472 // allow backrefs to this mark:
474 if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT))
475 this->m_backrefs |= 1u << (markid - 1);
480 template <class charT, class traits>
481 bool basic_regex_parser<charT, traits>::parse_basic_escape()
485 switch(this->m_traits.escape_syntax_type(*m_position))
487 case regex_constants::syntax_open_mark:
488 return parse_open_paren();
489 case regex_constants::syntax_close_mark:
491 case regex_constants::syntax_plus:
492 if(this->flags() & regex_constants::bk_plus_qm)
495 return parse_repeat(1);
498 return parse_literal();
499 case regex_constants::syntax_question:
500 if(this->flags() & regex_constants::bk_plus_qm)
503 return parse_repeat(0, 1);
506 return parse_literal();
507 case regex_constants::syntax_open_brace:
508 if(this->flags() & regbase::no_intervals)
509 return parse_literal();
511 return parse_repeat_range(true);
512 case regex_constants::syntax_close_brace:
513 if(this->flags() & regbase::no_intervals)
514 return parse_literal();
515 fail(regex_constants::error_brace, this->m_position - this->m_base);
517 case regex_constants::syntax_or:
518 if(this->flags() & regbase::bk_vbar)
521 result = parse_literal();
523 case regex_constants::syntax_digit:
524 return parse_backref();
525 case regex_constants::escape_type_start_buffer:
526 if(this->flags() & regbase::emacs_ex)
529 this->append_state(syntax_element_buffer_start);
532 result = parse_literal();
534 case regex_constants::escape_type_end_buffer:
535 if(this->flags() & regbase::emacs_ex)
538 this->append_state(syntax_element_buffer_end);
541 result = parse_literal();
543 case regex_constants::escape_type_word_assert:
544 if(this->flags() & regbase::emacs_ex)
547 this->append_state(syntax_element_word_boundary);
550 result = parse_literal();
552 case regex_constants::escape_type_not_word_assert:
553 if(this->flags() & regbase::emacs_ex)
556 this->append_state(syntax_element_within_word);
559 result = parse_literal();
561 case regex_constants::escape_type_left_word:
562 if(this->flags() & regbase::emacs_ex)
565 this->append_state(syntax_element_word_start);
568 result = parse_literal();
570 case regex_constants::escape_type_right_word:
571 if(this->flags() & regbase::emacs_ex)
574 this->append_state(syntax_element_word_end);
577 result = parse_literal();
580 if(this->flags() & regbase::emacs_ex)
590 basic_char_set<charT, traits> char_set;
593 char_set.add_class(this->m_word_mask);
594 if(0 == this->append_set(char_set))
596 fail(regex_constants::error_ctype, m_position - m_base);
606 return add_emacs_code(negate);
609 // not supported yet:
610 fail(regex_constants::error_escape, m_position - m_base);
616 result = parse_literal();
622 template <class charT, class traits>
623 bool basic_regex_parser<charT, traits>::parse_extended_escape()
626 bool negate = false; // in case this is a character class escape: \w \d etc
627 switch(this->m_traits.escape_syntax_type(*m_position))
629 case regex_constants::escape_type_not_class:
632 case regex_constants::escape_type_class:
634 escape_type_class_jump:
635 typedef typename traits::char_class_type mask_type;
636 mask_type m = this->m_traits.lookup_classname(m_position, m_position+1);
639 basic_char_set<charT, traits> char_set;
642 char_set.add_class(m);
643 if(0 == this->append_set(char_set))
645 fail(regex_constants::error_ctype, m_position - m_base);
652 // not a class, just a regular unknown escape:
654 this->append_literal(unescape_character());
657 case regex_constants::syntax_digit:
658 return parse_backref();
659 case regex_constants::escape_type_left_word:
661 this->append_state(syntax_element_word_start);
663 case regex_constants::escape_type_right_word:
665 this->append_state(syntax_element_word_end);
667 case regex_constants::escape_type_start_buffer:
669 this->append_state(syntax_element_buffer_start);
671 case regex_constants::escape_type_end_buffer:
673 this->append_state(syntax_element_buffer_end);
675 case regex_constants::escape_type_word_assert:
677 this->append_state(syntax_element_word_boundary);
679 case regex_constants::escape_type_not_word_assert:
681 this->append_state(syntax_element_within_word);
683 case regex_constants::escape_type_Z:
685 this->append_state(syntax_element_soft_buffer_end);
687 case regex_constants::escape_type_Q:
689 case regex_constants::escape_type_C:
690 return parse_match_any();
691 case regex_constants::escape_type_X:
693 this->append_state(syntax_element_combining);
695 case regex_constants::escape_type_G:
697 this->append_state(syntax_element_restart_continue);
699 case regex_constants::escape_type_not_property:
702 case regex_constants::escape_type_property:
706 if(m_position == m_end)
708 fail(regex_constants::error_escape, m_position - m_base);
711 // maybe have \p{ddd}
712 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
714 const charT* base = m_position;
715 // skip forward until we find enclosing brace:
716 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
718 if(m_position == m_end)
720 fail(regex_constants::error_escape, m_position - m_base);
723 m = this->m_traits.lookup_classname(++base, m_position++);
727 m = this->m_traits.lookup_classname(m_position, m_position+1);
732 basic_char_set<charT, traits> char_set;
735 char_set.add_class(m);
736 if(0 == this->append_set(char_set))
738 fail(regex_constants::error_ctype, m_position - m_base);
743 fail(regex_constants::error_ctype, m_position - m_base);
746 case regex_constants::escape_type_reset_start_mark:
747 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
749 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
751 pb->icase = this->flags() & regbase::icase;
752 this->m_pdata->m_data.align();
756 goto escape_type_class_jump;
757 case regex_constants::escape_type_line_ending:
758 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
760 const charT* e = get_escape_R_string<charT>();
761 const charT* old_position = m_position;
762 const charT* old_end = m_end;
763 const charT* old_base = m_base;
766 m_end = e + traits::length(e);
767 bool r = parse_all();
768 m_position = ++old_position;
773 goto escape_type_class_jump;
774 case regex_constants::escape_type_extended_backref:
775 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
777 bool have_brace = false;
778 bool negative = false;
779 if(++m_position == m_end)
781 fail(regex_constants::error_escape, m_position - m_base);
784 // maybe have \g{ddd}
785 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
787 if(++m_position == m_end)
789 fail(regex_constants::error_escape, m_position - m_base);
794 negative = (*m_position == static_cast<charT>('-'));
795 if((negative) && (++m_position == m_end))
797 fail(regex_constants::error_escape, m_position - m_base);
800 const charT* pc = m_position;
801 int i = this->m_traits.toi(pc, m_end, 10);
804 // Check for a named capture:
805 const charT* base = m_position;
806 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
808 i = this->m_pdata->get_id(base, m_position);
812 i = 1 + m_mark_count - i;
813 if((i > 0) && (this->m_backrefs & (1u << (i-1))))
816 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
818 pb->icase = this->flags() & regbase::icase;
822 fail(regex_constants::error_backref, m_position - m_end);
828 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
830 fail(regex_constants::error_escape, m_position - m_base);
837 goto escape_type_class_jump;
838 case regex_constants::escape_type_control_v:
839 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
840 goto escape_type_class_jump;
843 this->append_literal(unescape_character());
849 template <class charT, class traits>
850 bool basic_regex_parser<charT, traits>::parse_match_any()
853 // we have a '.' that can match any character:
856 static_cast<re_dot*>(
857 this->append_state(syntax_element_wild, sizeof(re_dot))
858 )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
859 ? re_detail::force_not_newline
860 : this->flags() & regbase::mod_s ?
861 re_detail::force_newline : re_detail::dont_care);
865 template <class charT, class traits>
866 bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
869 bool pocessive = false;
870 std::size_t insert_point;
872 // when we get to here we may have a non-greedy ? mark still to come:
874 if((m_position != m_end)
876 (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
877 || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
881 // OK we have a perl or emacs regex, check for a '?':
882 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
887 // for perl regexes only check for pocessive ++ repeats.
888 if((0 == (this->flags() & regbase::main_option_type))
889 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
895 if(0 == this->m_last_state)
897 fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position));
900 if(this->m_last_state->type == syntax_element_endmark)
902 // insert a repeat before the '(' matching the last ')':
903 insert_point = this->m_paren_start;
905 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
907 // the last state was a literal with more than one character, split it in two:
908 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
909 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
911 // now append new state:
912 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
914 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
915 insert_point = this->getoffset(this->m_last_state);
919 // repeat the last state whatever it was, need to add some error checking here:
920 switch(this->m_last_state->type)
922 case syntax_element_start_line:
923 case syntax_element_end_line:
924 case syntax_element_word_boundary:
925 case syntax_element_within_word:
926 case syntax_element_word_start:
927 case syntax_element_word_end:
928 case syntax_element_buffer_start:
929 case syntax_element_buffer_end:
930 case syntax_element_alt:
931 case syntax_element_soft_buffer_end:
932 case syntax_element_restart_continue:
933 case syntax_element_jump:
934 case syntax_element_startmark:
935 case syntax_element_backstep:
936 // can't legally repeat any of the above:
937 fail(regex_constants::error_badrepeat, m_position - m_base);
943 insert_point = this->getoffset(this->m_last_state);
946 // OK we now know what to repeat, so insert the repeat around it:
948 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
951 rep->greedy = greedy;
952 rep->leading = false;
953 // store our repeater position for later:
954 std::ptrdiff_t rep_off = this->getoffset(rep);
955 // and append a back jump to the repeat:
956 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
957 jmp->alt.i = rep_off - this->getoffset(jmp);
958 this->m_pdata->m_data.align();
959 // now fill in the alt jump for the repeat:
960 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
961 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
963 // If the repeat is pocessive then bracket the repeat with a (?>...)
964 // independent sub-expression construct:
968 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
970 pb->icase = this->flags() & regbase::icase;
971 re_jump* jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
972 this->m_pdata->m_data.align();
973 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
974 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
976 pb->icase = this->flags() & regbase::icase;
981 template <class charT, class traits>
982 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
985 // parse a repeat-range:
987 std::size_t min, max;
990 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
993 if(this->m_position == this->m_end)
995 fail(regex_constants::error_brace, this->m_position - this->m_base);
999 v = this->m_traits.toi(m_position, m_end, 10);
1001 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1005 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1008 else if(this->m_position == this->m_end)
1010 fail(regex_constants::error_brace, this->m_position - this->m_base);
1014 // see if we have a comma:
1015 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1017 // move on and error check:
1020 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1022 if(this->m_position == this->m_end)
1024 fail(regex_constants::error_brace, this->m_position - this->m_base);
1027 // get the value if any:
1028 v = this->m_traits.toi(m_position, m_end, 10);
1029 max = (v >= 0) ? v : (std::numeric_limits<std::size_t>::max)();
1033 // no comma, max = min:
1037 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1039 // OK now check trailing }:
1040 if(this->m_position == this->m_end)
1042 fail(regex_constants::error_brace, this->m_position - this->m_base);
1047 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1050 if(this->m_position == this->m_end)
1052 fail(regex_constants::error_brace, this->m_position - this->m_base);
1058 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1062 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1066 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1070 // finally go and add the repeat, unless error:
1074 fail(regex_constants::error_badbrace, this->m_position - this->m_base);
1077 return parse_repeat(min, max);
1080 template <class charT, class traits>
1081 bool basic_regex_parser<charT, traits>::parse_alt()
1084 // error check: if there have been no previous states,
1085 // or if the last state was a '(' then error:
1088 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1091 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1093 ((this->flags() & regbase::no_empty_expressions) == 0)
1097 fail(regex_constants::error_empty, this->m_position - this->m_base);
1101 // Reset mark count if required:
1103 if(m_max_mark < m_mark_count)
1104 m_max_mark = m_mark_count;
1105 if(m_mark_reset >= 0)
1106 m_mark_count = m_mark_reset;
1110 // we need to append a trailing jump:
1112 re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump));
1113 std::ptrdiff_t jump_offset = this->getoffset(pj);
1115 // now insert the alternative:
1117 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1118 jump_offset += re_alt_size;
1119 this->m_pdata->m_data.align();
1120 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1122 // update m_alt_insert_point so that the next alternate gets
1123 // inserted at the start of the second of the two we've just created:
1125 this->m_alt_insert_point = this->m_pdata->m_data.size();
1127 // the start of this alternative must have a case changes state
1128 // if the current block has messed around with case changes:
1130 if(m_has_case_change)
1132 static_cast<re_case*>(
1133 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1134 )->icase = this->m_icase;
1137 // push the alternative onto our stack, a recursive
1138 // implementation here is easier to understand (and faster
1139 // as it happens), but causes all kinds of stack overflow problems
1140 // on programs with small stacks (COM+).
1142 m_alt_jumps.push_back(jump_offset);
1146 template <class charT, class traits>
1147 bool basic_regex_parser<charT, traits>::parse_set()
1150 if(m_position == m_end)
1152 fail(regex_constants::error_brack, m_position - m_base);
1155 basic_char_set<charT, traits> char_set;
1157 const charT* base = m_position; // where the '[' was
1158 const charT* item_base = m_position; // where the '[' or '^' was
1160 while(m_position != m_end)
1162 switch(this->m_traits.syntax_type(*m_position))
1164 case regex_constants::syntax_caret:
1165 if(m_position == base)
1169 item_base = m_position;
1172 parse_set_literal(char_set);
1174 case regex_constants::syntax_close_set:
1175 if(m_position == item_base)
1177 parse_set_literal(char_set);
1183 if(0 == this->append_set(char_set))
1185 fail(regex_constants::error_range, m_position - m_base);
1190 case regex_constants::syntax_open_set:
1191 if(parse_inner_set(char_set))
1194 case regex_constants::syntax_escape:
1197 // look ahead and see if this is a character class shortcut
1201 if(this->m_traits.escape_syntax_type(*m_position)
1202 == regex_constants::escape_type_class)
1204 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1207 char_set.add_class(m);
1212 else if(this->m_traits.escape_syntax_type(*m_position)
1213 == regex_constants::escape_type_not_class)
1215 // negated character class:
1216 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1219 char_set.add_negated_class(m);
1224 // not a character class, just a regular escape:
1226 parse_set_literal(char_set);
1230 parse_set_literal(char_set);
1234 return m_position != m_end;
1237 template <class charT, class traits>
1238 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1241 // we have either a character class [:name:]
1242 // a collating element [.name.]
1243 // or an equivalence class [=name=]
1245 if(m_end == ++m_position)
1247 fail(regex_constants::error_brack, m_position - m_base);
1250 switch(this->m_traits.syntax_type(*m_position))
1252 case regex_constants::syntax_dot:
1254 // a collating element is treated as a literal:
1257 parse_set_literal(char_set);
1259 case regex_constants::syntax_colon:
1261 // check that character classes are actually enabled:
1262 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1263 == (regbase::basic_syntax_group | regbase::no_char_classes))
1266 parse_set_literal(char_set);
1270 if(m_end == ++m_position)
1272 fail(regex_constants::error_brack, m_position - m_base);
1275 const charT* name_first = m_position;
1276 // skip at least one character, then find the matching ':]'
1277 if(m_end == ++m_position)
1279 fail(regex_constants::error_brack, m_position - m_base);
1282 while((m_position != m_end)
1283 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1285 const charT* name_last = m_position;
1286 if(m_end == m_position)
1288 fail(regex_constants::error_brack, m_position - m_base);
1291 if((m_end == ++m_position)
1292 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1294 fail(regex_constants::error_brack, m_position - m_base);
1298 // check for negated class:
1300 bool negated = false;
1301 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1306 typedef typename traits::char_class_type mask_type;
1307 mask_type m = this->m_traits.lookup_classname(name_first, name_last);
1310 if(char_set.empty() && (name_last - name_first == 1))
1312 // maybe a special case:
1314 if( (m_position != m_end)
1315 && (this->m_traits.syntax_type(*m_position)
1316 == regex_constants::syntax_close_set))
1318 if(this->m_traits.escape_syntax_type(*name_first)
1319 == regex_constants::escape_type_left_word)
1322 this->append_state(syntax_element_word_start);
1325 if(this->m_traits.escape_syntax_type(*name_first)
1326 == regex_constants::escape_type_right_word)
1329 this->append_state(syntax_element_word_end);
1334 fail(regex_constants::error_ctype, name_first - m_base);
1337 if(negated == false)
1338 char_set.add_class(m);
1340 char_set.add_negated_class(m);
1344 case regex_constants::syntax_equal:
1347 if(m_end == ++m_position)
1349 fail(regex_constants::error_brack, m_position - m_base);
1352 const charT* name_first = m_position;
1353 // skip at least one character, then find the matching '=]'
1354 if(m_end == ++m_position)
1356 fail(regex_constants::error_brack, m_position - m_base);
1359 while((m_position != m_end)
1360 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1362 const charT* name_last = m_position;
1363 if(m_end == m_position)
1365 fail(regex_constants::error_brack, m_position - m_base);
1368 if((m_end == ++m_position)
1369 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1371 fail(regex_constants::error_brack, m_position - m_base);
1374 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1375 if((0 == m.size()) || (m.size() > 2))
1377 fail(regex_constants::error_collate, name_first - m_base);
1386 char_set.add_equivalent(d);
1392 parse_set_literal(char_set);
1398 template <class charT, class traits>
1399 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1401 digraph<charT> start_range(get_next_set_literal(char_set));
1402 if(m_end == m_position)
1404 fail(regex_constants::error_brack, m_position - m_base);
1407 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1410 if(m_end == ++m_position)
1412 fail(regex_constants::error_brack, m_position - m_base);
1415 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1417 digraph<charT> end_range = get_next_set_literal(char_set);
1418 char_set.add_range(start_range, end_range);
1419 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1421 if(m_end == ++m_position)
1423 fail(regex_constants::error_brack, m_position - m_base);
1426 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1432 fail(regex_constants::error_range, m_position - m_base);
1439 char_set.add_single(start_range);
1442 template <class charT, class traits>
1443 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1445 digraph<charT> result;
1446 switch(this->m_traits.syntax_type(*m_position))
1448 case regex_constants::syntax_dash:
1449 if(!char_set.empty())
1451 // see if we are at the end of the set:
1452 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1454 fail(regex_constants::error_range, m_position - m_base);
1459 result.first = *m_position++;
1461 case regex_constants::syntax_escape:
1462 // check to see if escapes are supported first:
1463 if(this->flags() & regex_constants::no_escape_in_lists)
1465 result = *m_position++;
1469 result = unescape_character();
1471 case regex_constants::syntax_open_set:
1473 if(m_end == ++m_position)
1475 fail(regex_constants::error_collate, m_position - m_base);
1478 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1481 result.first = *m_position;
1485 if(m_end == ++m_position)
1487 fail(regex_constants::error_collate, m_position - m_base);
1490 const charT* name_first = m_position;
1491 // skip at least one character, then find the matching ':]'
1492 if(m_end == ++m_position)
1494 fail(regex_constants::error_collate, name_first - m_base);
1497 while((m_position != m_end)
1498 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1500 const charT* name_last = m_position;
1501 if(m_end == m_position)
1503 fail(regex_constants::error_collate, name_first - m_base);
1506 if((m_end == ++m_position)
1507 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1509 fail(regex_constants::error_collate, name_first - m_base);
1513 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1514 if(s.empty() || (s.size() > 2))
1516 fail(regex_constants::error_collate, name_first - m_base);
1519 result.first = s[0];
1521 result.second = s[1];
1527 result = *m_position++;
1533 // does a value fit in the specified charT type?
1535 template <class charT>
1536 bool valid_value(charT, int v, const mpl::true_&)
1538 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1540 template <class charT>
1541 bool valid_value(charT, int, const mpl::false_&)
1543 return true; // v will alsways fit in a charT
1545 template <class charT>
1546 bool valid_value(charT c, int v)
1548 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>());
1551 template <class charT, class traits>
1552 charT basic_regex_parser<charT, traits>::unescape_character()
1555 #pragma warning(push)
1556 #pragma warning(disable:4127)
1559 if(m_position == m_end)
1561 fail(regex_constants::error_escape, m_position - m_base);
1564 switch(this->m_traits.escape_syntax_type(*m_position))
1566 case regex_constants::escape_type_control_a:
1567 result = charT('\a');
1569 case regex_constants::escape_type_e:
1572 case regex_constants::escape_type_control_f:
1573 result = charT('\f');
1575 case regex_constants::escape_type_control_n:
1576 result = charT('\n');
1578 case regex_constants::escape_type_control_r:
1579 result = charT('\r');
1581 case regex_constants::escape_type_control_t:
1582 result = charT('\t');
1584 case regex_constants::escape_type_control_v:
1585 result = charT('\v');
1587 case regex_constants::escape_type_word_assert:
1588 result = charT('\b');
1590 case regex_constants::escape_type_ascii_control:
1592 if(m_position == m_end)
1594 fail(regex_constants::error_escape, m_position - m_base);
1598 if((*m_position < charT('@'))
1599 || (*m_position > charT(125)) )
1601 fail(regex_constants::error_escape, m_position - m_base);
1605 result = static_cast<charT>(*m_position % 32);
1607 case regex_constants::escape_type_hex:
1609 if(m_position == m_end)
1611 fail(regex_constants::error_escape, m_position - m_base);
1614 // maybe have \x{ddd}
1615 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1618 if(m_position == m_end)
1620 fail(regex_constants::error_escape, m_position - m_base);
1623 int i = this->m_traits.toi(m_position, m_end, 16);
1624 if((m_position == m_end)
1626 || ((std::numeric_limits<charT>::is_specialized) && (i > (int)(std::numeric_limits<charT>::max)()))
1627 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1629 fail(regex_constants::error_badbrace, m_position - m_base);
1637 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), m_end - m_position);
1638 int i = this->m_traits.toi(m_position, m_position + len, 16);
1640 || !valid_value(charT(0), i))
1642 fail(regex_constants::error_escape, m_position - m_base);
1648 case regex_constants::syntax_digit:
1650 // an octal escape sequence, the first character must be a zero
1651 // followed by up to 3 octal digits:
1652 std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1653 const charT* bp = m_position;
1654 int val = this->m_traits.toi(bp, bp + 1, 8);
1657 // Oops not an octal escape after all:
1658 fail(regex_constants::error_escape, m_position - m_base);
1661 val = this->m_traits.toi(m_position, m_position + len, 8);
1664 fail(regex_constants::error_escape, m_position - m_base);
1667 return static_cast<charT>(val);
1669 case regex_constants::escape_type_named_char:
1672 if(m_position == m_end)
1674 fail(regex_constants::error_escape, m_position - m_base);
1677 // maybe have \N{name}
1678 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1680 const charT* base = m_position;
1681 // skip forward until we find enclosing brace:
1682 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1684 if(m_position == m_end)
1686 fail(regex_constants::error_escape, m_position - m_base);
1689 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
1692 fail(regex_constants::error_collate, m_position - m_base);
1700 // fall through is a failure:
1701 fail(regex_constants::error_escape, m_position - m_base);
1705 result = *m_position;
1711 #pragma warning(pop)
1715 template <class charT, class traits>
1716 bool basic_regex_parser<charT, traits>::parse_backref()
1718 BOOST_ASSERT(m_position != m_end);
1719 const charT* pc = m_position;
1720 int i = this->m_traits.toi(pc, pc + 1, 10);
1721 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1723 // not a backref at all but an octal escape sequence:
1724 charT c = unescape_character();
1725 this->append_literal(c);
1727 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1730 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1732 pb->icase = this->flags() & regbase::icase;
1736 fail(regex_constants::error_backref, m_position - m_end);
1742 template <class charT, class traits>
1743 bool basic_regex_parser<charT, traits>::parse_QE()
1746 #pragma warning(push)
1747 #pragma warning(disable:4127)
1750 // parse a \Q...\E sequence:
1752 ++m_position; // skip the Q
1753 const charT* start = m_position;
1757 while((m_position != m_end)
1758 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1760 if(m_position == m_end)
1762 // a \Q...\E sequence may terminate with the end of the expression:
1766 if(++m_position == m_end) // skip the escape
1768 fail(regex_constants::error_escape, m_position - m_base);
1771 // check to see if it's a \E:
1772 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1775 end = m_position - 2;
1778 // otherwise go round again:
1781 // now add all the character between the two escapes as literals:
1785 this->append_literal(*start);
1790 #pragma warning(pop)
1794 template <class charT, class traits>
1795 bool basic_regex_parser<charT, traits>::parse_perl_extension()
1797 if(++m_position == m_end)
1799 fail(regex_constants::error_badrepeat, m_position - m_base);
1803 // treat comments as a special case, as these
1804 // are the only ones that don't start with a leading
1807 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
1809 while((m_position != m_end)
1810 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
1815 // backup some state, and prepare the way:
1818 std::ptrdiff_t jump_offset = 0;
1819 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
1820 pb->icase = this->flags() & regbase::icase;
1821 std::ptrdiff_t last_paren_start = this->getoffset(pb);
1822 // back up insertion point for alternations, and set new point:
1823 std::ptrdiff_t last_alt_point = m_alt_insert_point;
1824 this->m_pdata->m_data.align();
1825 m_alt_insert_point = this->m_pdata->m_data.size();
1826 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
1827 bool restore_flags = true;
1828 regex_constants::syntax_option_type old_flags = this->flags();
1829 bool old_case_change = m_has_case_change;
1830 m_has_case_change = false;
1832 int mark_reset = m_mark_reset;
1836 // select the actual extension used:
1838 switch(this->m_traits.syntax_type(*m_position))
1840 case regex_constants::syntax_or:
1841 m_mark_reset = m_mark_count;
1843 case regex_constants::syntax_colon:
1845 // a non-capturing mark:
1847 pb->index = markid = 0;
1850 case regex_constants::syntax_digit:
1853 // a recursive subexpression:
1855 v = this->m_traits.toi(m_position, m_end, 10);
1856 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1858 fail(regex_constants::error_backref, m_position - m_base);
1862 pb->index = markid = 0;
1863 static_cast<re_jump*>(this->append_state(syntax_element_recurse, sizeof(re_jump)))->alt.i = v;
1864 static_cast<re_case*>(
1865 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1866 )->icase = this->flags() & regbase::icase;
1869 case regex_constants::syntax_plus:
1871 // A forward-relative recursive subexpression:
1874 v = this->m_traits.toi(m_position, m_end, 10);
1875 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1877 fail(regex_constants::error_backref, m_position - m_base);
1881 goto insert_recursion;
1882 case regex_constants::syntax_dash:
1884 // Possibly a backward-relative recursive subexpression:
1887 v = this->m_traits.toi(m_position, m_end, 10);
1891 // Oops not a relative recursion at all, but a (?-imsx) group:
1892 goto option_group_jump;
1894 v = m_mark_count + 1 - v;
1897 fail(regex_constants::error_backref, m_position - m_base);
1900 goto insert_recursion;
1901 case regex_constants::syntax_equal:
1902 pb->index = markid = -1;
1904 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1905 this->m_pdata->m_data.align();
1906 m_alt_insert_point = this->m_pdata->m_data.size();
1908 case regex_constants::syntax_not:
1909 pb->index = markid = -2;
1911 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1912 this->m_pdata->m_data.align();
1913 m_alt_insert_point = this->m_pdata->m_data.size();
1915 case regex_constants::escape_type_left_word:
1917 // a lookbehind assertion:
1918 if(++m_position == m_end)
1920 fail(regex_constants::error_badrepeat, m_position - m_base);
1923 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
1924 if(t == regex_constants::syntax_not)
1925 pb->index = markid = -2;
1926 else if(t == regex_constants::syntax_equal)
1927 pb->index = markid = -1;
1930 // Probably a named capture which also starts (?< :
1933 goto named_capture_jump;
1936 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1937 this->append_state(syntax_element_backstep, sizeof(re_brace));
1938 this->m_pdata->m_data.align();
1939 m_alt_insert_point = this->m_pdata->m_data.size();
1942 case regex_constants::escape_type_right_word:
1944 // an independent sub-expression:
1946 pb->index = markid = -3;
1948 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
1949 this->m_pdata->m_data.align();
1950 m_alt_insert_point = this->m_pdata->m_data.size();
1952 case regex_constants::syntax_open_mark:
1954 // a conditional expression:
1955 pb->index = markid = -4;
1956 if(++m_position == m_end)
1958 fail(regex_constants::error_badrepeat, m_position - m_base);
1961 int v = this->m_traits.toi(m_position, m_end, 10);
1962 if(*m_position == charT('R'))
1964 if(++m_position == m_end)
1966 fail(regex_constants::error_badrepeat, m_position - m_base);
1969 if(*m_position == charT('&'))
1971 const charT* base = ++m_position;
1972 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
1974 if(m_position == m_end)
1976 fail(regex_constants::error_badrepeat, m_position - m_base);
1979 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
1983 v = -this->m_traits.toi(m_position, m_end, 10);
1985 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
1986 br->index = v < 0 ? (v - 1) : 0;
1987 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
1989 fail(regex_constants::error_badrepeat, m_position - m_base);
1992 if(++m_position == m_end)
1994 fail(regex_constants::error_badrepeat, m_position - m_base);
1998 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2000 const charT* base = ++m_position;
2001 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2003 if(m_position == m_end)
2005 fail(regex_constants::error_badrepeat, m_position - m_base);
2008 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2009 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2011 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2013 fail(regex_constants::error_badrepeat, m_position - m_base);
2016 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2018 fail(regex_constants::error_badrepeat, m_position - m_base);
2021 if(++m_position == m_end)
2023 fail(regex_constants::error_badrepeat, m_position - m_base);
2027 else if(*m_position == charT('D'))
2029 const char* def = "DEFINE";
2030 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2031 ++m_position, ++def;
2032 if((m_position == m_end) || *def)
2034 fail(regex_constants::error_badrepeat, m_position - m_base);
2037 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2038 br->index = 9999; // special magic value!
2039 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2041 fail(regex_constants::error_badrepeat, m_position - m_base);
2044 if(++m_position == m_end)
2046 fail(regex_constants::error_badrepeat, m_position - m_base);
2052 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2054 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2056 fail(regex_constants::error_badrepeat, m_position - m_base);
2059 if(++m_position == m_end)
2061 fail(regex_constants::error_badrepeat, m_position - m_base);
2067 // verify that we have a lookahead or lookbehind assert:
2068 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2070 fail(regex_constants::error_badrepeat, m_position - m_base);
2073 if(++m_position == m_end)
2075 fail(regex_constants::error_badrepeat, m_position - m_base);
2078 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2080 if(++m_position == m_end)
2082 fail(regex_constants::error_badrepeat, m_position - m_base);
2085 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2086 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2088 fail(regex_constants::error_badrepeat, m_position - m_base);
2095 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2096 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2098 fail(regex_constants::error_paren, m_position - m_base);
2106 case regex_constants::syntax_close_mark:
2107 fail(regex_constants::error_badrepeat, m_position - m_base);
2109 case regex_constants::escape_type_end_buffer:
2111 name_delim = *m_position;
2114 if(0 == (this->flags() & regbase::nosubs))
2116 markid = ++m_mark_count;
2117 #ifndef BOOST_NO_STD_DISTANCE
2118 if(this->flags() & regbase::save_subexpression_location)
2119 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2121 if(this->flags() & regbase::save_subexpression_location)
2122 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2126 const charT* base = ++m_position;
2127 if(m_position == m_end)
2129 fail(regex_constants::error_paren, m_position - m_base);
2132 while((m_position != m_end) && (*m_position != name_delim))
2134 if(m_position == m_end)
2136 fail(regex_constants::error_paren, m_position - m_base);
2139 this->m_pdata->set_name(base, m_position, markid);
2144 if(*m_position == charT('R'))
2148 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2150 fail(regex_constants::error_backref, m_position - m_base);
2153 goto insert_recursion;
2155 if(*m_position == charT('&'))
2158 const charT* base = m_position;
2159 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2161 if(m_position == m_end)
2163 fail(regex_constants::error_backref, m_position - m_base);
2166 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2167 goto insert_recursion;
2169 if(*m_position == charT('P'))
2172 if(m_position == m_end)
2174 fail(regex_constants::error_backref, m_position - m_base);
2177 if(*m_position == charT('>'))
2180 const charT* base = m_position;
2181 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2183 if(m_position == m_end)
2185 fail(regex_constants::error_backref, m_position - m_base);
2188 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2189 goto insert_recursion;
2193 // lets assume that we have a (?imsx) group and try and parse it:
2196 regex_constants::syntax_option_type opts = parse_options();
2197 if(m_position == m_end)
2199 // make a note of whether we have a case change:
2200 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2201 pb->index = markid = 0;
2202 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2204 // update flags and carry on as normal:
2206 restore_flags = false;
2207 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2209 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2211 // update flags and carry on until the matching ')' is found:
2217 fail(regex_constants::error_badrepeat, m_position - m_base);
2221 // finally append a case change state if we need it:
2222 if(m_has_case_change)
2224 static_cast<re_case*>(
2225 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2226 )->icase = opts & regbase::icase;
2231 // now recursively add more states, this will terminate when we get to a
2236 // Unwind alternatives:
2238 if(0 == unwind_alts(last_paren_start))
2241 // we either have a ')' or we have run out of characters prematurely:
2243 if(m_position == m_end)
2245 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
2248 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2251 // restore the flags:
2255 // append a case change state if we need it:
2256 if(m_has_case_change)
2258 static_cast<re_case*>(
2259 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2260 )->icase = old_flags & regbase::icase;
2262 this->flags(old_flags);
2265 // set up the jump pointer if we have one:
2269 this->m_pdata->m_data.align();
2270 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2271 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2272 if(this->m_last_state == jmp)
2274 // Oops... we didn't have anything inside the assertion:
2275 fail(regex_constants::error_empty, m_position - m_base);
2280 // verify that if this is conditional expression, that we do have
2281 // an alternative, if not add one:
2285 re_syntax_base* b = this->getaddress(expected_alt_point);
2286 // Make sure we have exactly one alternative following this state:
2287 if(b->type != syntax_element_alt)
2289 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2290 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2292 else if(this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2294 // Can't have seen more than one alternative:
2295 fail(regex_constants::error_bad_pattern, m_position - m_base);
2300 // We must *not* have seen an alternative inside a (DEFINE) block:
2301 b = this->getaddress(b->next.i, b);
2302 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2304 fail(regex_constants::error_bad_pattern, m_position - m_base);
2308 // check for invalid repetition of next state:
2309 b = this->getaddress(expected_alt_point);
2310 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2311 if((b->type != syntax_element_assert_backref)
2312 && (b->type != syntax_element_startmark))
2314 fail(regex_constants::error_badrepeat, m_position - m_base);
2319 // append closing parenthesis state:
2321 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2323 pb->icase = this->flags() & regbase::icase;
2324 this->m_paren_start = last_paren_start;
2326 // restore the alternate insertion point:
2328 this->m_alt_insert_point = last_alt_point;
2330 // and the case change data:
2332 m_has_case_change = old_case_change;
2334 // And the mark_reset data:
2336 if(m_max_mark > m_mark_count)
2338 m_mark_count = m_max_mark;
2340 m_mark_reset = mark_reset;
2345 #ifndef BOOST_NO_STD_DISTANCE
2346 if(this->flags() & regbase::save_subexpression_location)
2347 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2349 if(this->flags() & regbase::save_subexpression_location)
2350 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2353 // allow backrefs to this mark:
2355 if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT)))
2356 this->m_backrefs |= 1u << (markid - 1);
2361 template <class charT, class traits>
2362 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2365 // parses an emacs style \sx or \Sx construct.
2367 if(++m_position == m_end)
2369 fail(regex_constants::error_escape, m_position - m_base);
2372 basic_char_set<charT, traits> char_set;
2376 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2382 char_set.add_class(this->m_mask_space);
2385 char_set.add_class(this->m_word_mask);
2388 char_set.add_single(digraph<charT>(charT('$')));
2389 char_set.add_single(digraph<charT>(charT('&')));
2390 char_set.add_single(digraph<charT>(charT('*')));
2391 char_set.add_single(digraph<charT>(charT('+')));
2392 char_set.add_single(digraph<charT>(charT('-')));
2393 char_set.add_single(digraph<charT>(charT('_')));
2394 char_set.add_single(digraph<charT>(charT('<')));
2395 char_set.add_single(digraph<charT>(charT('>')));
2398 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2401 char_set.add_single(digraph<charT>(charT('(')));
2402 char_set.add_single(digraph<charT>(charT('[')));
2403 char_set.add_single(digraph<charT>(charT('{')));
2406 char_set.add_single(digraph<charT>(charT(')')));
2407 char_set.add_single(digraph<charT>(charT(']')));
2408 char_set.add_single(digraph<charT>(charT('}')));
2411 char_set.add_single(digraph<charT>(charT('"')));
2412 char_set.add_single(digraph<charT>(charT('\'')));
2413 char_set.add_single(digraph<charT>(charT('`')));
2416 char_set.add_single(digraph<charT>(charT('\'')));
2417 char_set.add_single(digraph<charT>(charT(',')));
2418 char_set.add_single(digraph<charT>(charT('#')));
2421 char_set.add_single(digraph<charT>(charT(';')));
2424 char_set.add_single(digraph<charT>(charT('\n')));
2425 char_set.add_single(digraph<charT>(charT('\f')));
2428 fail(regex_constants::error_ctype, m_position - m_base);
2431 if(0 == this->append_set(char_set))
2433 fail(regex_constants::error_ctype, m_position - m_base);
2440 template <class charT, class traits>
2441 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
2443 // we have a (?imsx-imsx) group, convert it into a set of flags:
2444 regex_constants::syntax_option_type f = this->flags();
2445 bool breakout = false;
2451 f |= regex_constants::mod_s;
2452 f &= ~regex_constants::no_mod_s;
2455 f &= ~regex_constants::no_mod_m;
2458 f |= regex_constants::icase;
2461 f |= regex_constants::mod_x;
2467 if(++m_position == m_end)
2469 fail(regex_constants::error_paren, m_position - m_base);
2475 if(*m_position == static_cast<charT>('-'))
2477 if(++m_position == m_end)
2479 fail(regex_constants::error_paren, m_position - m_base);
2487 f &= ~regex_constants::mod_s;
2488 f |= regex_constants::no_mod_s;
2491 f |= regex_constants::no_mod_m;
2494 f &= ~regex_constants::icase;
2497 f &= ~regex_constants::mod_x;
2503 if(++m_position == m_end)
2505 fail(regex_constants::error_paren, m_position - m_base);
2514 template <class charT, class traits>
2515 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
2518 // If we didn't actually add any states after the last
2519 // alternative then that's an error:
2521 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
2522 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
2525 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
2527 ((this->flags() & regbase::no_empty_expressions) == 0)
2531 fail(regex_constants::error_empty, this->m_position - this->m_base);
2535 // Fix up our alternatives:
2537 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
2540 // fix up the jump to point to the end of the states
2541 // that we've just added:
2543 std::ptrdiff_t jump_offset = m_alt_jumps.back();
2544 m_alt_jumps.pop_back();
2545 this->m_pdata->m_data.align();
2546 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2547 BOOST_ASSERT(jmp->type == syntax_element_jump);
2548 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
2554 #pragma warning(pop)
2557 } // namespace re_detail
2558 } // namespace boost
2561 #pragma warning(push)
2562 #pragma warning(disable: 4103)
2564 #ifdef BOOST_HAS_ABI_HEADERS
2565 # include BOOST_ABI_SUFFIX
2568 #pragma warning(pop)