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
194 #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS
196 // Augment error message with the regular expression text:
198 if(start_pos == position)
199 start_pos = (std::max)(static_cast<std::ptrdiff_t>(0), position - static_cast<std::ptrdiff_t>(10));
200 std::ptrdiff_t end_pos = (std::min)(position + static_cast<std::ptrdiff_t>(10), static_cast<std::ptrdiff_t>(m_end - m_base));
201 if(error_code != regex_constants::error_empty)
203 if((start_pos != 0) || (end_pos != (m_end - m_base)))
204 message += " The error occurred while parsing the regular expression fragment: '";
206 message += " The error occurred while parsing the regular expression: '";
207 if(start_pos != end_pos)
209 message += std::string(m_base + start_pos, m_base + position);
210 message += ">>>HERE>>>";
211 message += std::string(m_base + position, m_base + end_pos);
217 #ifndef BOOST_NO_EXCEPTIONS
218 if(0 == (this->flags() & regex_constants::no_except))
220 boost::regex_error e(message, error_code, position);
224 (void)position; // suppress warnings.
228 template <class charT, class traits>
229 bool basic_regex_parser<charT, traits>::parse_all()
232 while(result && (m_position != m_end))
234 result = (this->*m_parser_proc)();
240 #pragma warning(push)
241 #pragma warning(disable:4702)
243 template <class charT, class traits>
244 bool basic_regex_parser<charT, traits>::parse_basic()
246 switch(this->m_traits.syntax_type(*m_position))
248 case regex_constants::syntax_escape:
249 return parse_basic_escape();
250 case regex_constants::syntax_dot:
251 return parse_match_any();
252 case regex_constants::syntax_caret:
254 this->append_state(syntax_element_start_line);
256 case regex_constants::syntax_dollar:
258 this->append_state(syntax_element_end_line);
260 case regex_constants::syntax_star:
261 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line))
262 return parse_literal();
266 return parse_repeat();
268 case regex_constants::syntax_plus:
269 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
270 return parse_literal();
274 return parse_repeat(1);
276 case regex_constants::syntax_question:
277 if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex))
278 return parse_literal();
282 return parse_repeat(0, 1);
284 case regex_constants::syntax_open_set:
286 case regex_constants::syntax_newline:
287 if(this->flags() & regbase::newline_alt)
290 return parse_literal();
292 return parse_literal();
297 template <class charT, class traits>
298 bool basic_regex_parser<charT, traits>::parse_extended()
301 switch(this->m_traits.syntax_type(*m_position))
303 case regex_constants::syntax_open_mark:
304 return parse_open_paren();
305 case regex_constants::syntax_close_mark:
307 case regex_constants::syntax_escape:
308 return parse_extended_escape();
309 case regex_constants::syntax_dot:
310 return parse_match_any();
311 case regex_constants::syntax_caret:
314 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line));
316 case regex_constants::syntax_dollar:
319 (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line));
321 case regex_constants::syntax_star:
322 if(m_position == this->m_base)
324 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"*\" cannot start a regular expression.");
328 return parse_repeat();
329 case regex_constants::syntax_question:
330 if(m_position == this->m_base)
332 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"?\" cannot start a regular expression.");
336 return parse_repeat(0,1);
337 case regex_constants::syntax_plus:
338 if(m_position == this->m_base)
340 fail(regex_constants::error_badrepeat, 0, "The repeat operator \"+\" cannot start a regular expression.");
344 return parse_repeat(1);
345 case regex_constants::syntax_open_brace:
347 return parse_repeat_range(false);
348 case regex_constants::syntax_close_brace:
349 if((this->flags() & regbase::no_perl_ex) == regbase::no_perl_ex)
351 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
354 result = parse_literal();
356 case regex_constants::syntax_or:
358 case regex_constants::syntax_open_set:
360 case regex_constants::syntax_newline:
361 if(this->flags() & regbase::newline_alt)
364 return parse_literal();
365 case regex_constants::syntax_hash:
367 // If we have a mod_x flag set, then skip until
368 // we get to a newline character:
371 & (regbase::no_perl_ex|regbase::mod_x))
374 while((m_position != m_end) && !is_separator(*m_position++)){}
379 result = parse_literal();
388 template <class charT, class traits>
389 bool basic_regex_parser<charT, traits>::parse_literal()
391 // append this as a literal provided it's not a space character
392 // or the perl option regbase::mod_x is not set:
395 & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex))
397 || !this->m_traits.isctype(*m_position, this->m_mask_space))
398 this->append_literal(*m_position);
403 template <class charT, class traits>
404 bool basic_regex_parser<charT, traits>::parse_open_paren()
407 // skip the '(' and error check:
409 if(++m_position == m_end)
411 fail(regex_constants::error_paren, m_position - m_base);
415 // begin by checking for a perl-style (?...) extension:
418 ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0)
419 || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
422 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
423 return parse_perl_extension();
426 // update our mark count, and append the required state:
429 if(0 == (this->flags() & regbase::nosubs))
431 markid = ++m_mark_count;
432 #ifndef BOOST_NO_STD_DISTANCE
433 if(this->flags() & regbase::save_subexpression_location)
434 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 1, 0));
436 if(this->flags() & regbase::save_subexpression_location)
437 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 1, 0));
440 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
442 pb->icase = this->flags() & regbase::icase;
443 std::ptrdiff_t last_paren_start = this->getoffset(pb);
444 // back up insertion point for alternations, and set new point:
445 std::ptrdiff_t last_alt_point = m_alt_insert_point;
446 this->m_pdata->m_data.align();
447 m_alt_insert_point = this->m_pdata->m_data.size();
449 // back up the current flags in case we have a nested (?imsx) group:
451 regex_constants::syntax_option_type opts = this->flags();
452 bool old_case_change = m_has_case_change;
453 m_has_case_change = false; // no changes to this scope as yet...
455 // Back up branch reset data in case we have a nested (?|...)
457 int mark_reset = m_mark_reset;
460 // now recursively add more states, this will terminate when we get to a
465 // Unwind pushed alternatives:
467 if(0 == unwind_alts(last_paren_start))
472 if(m_has_case_change)
474 // the case has changed in one or more of the alternatives
475 // within the scoped (...) block: we have to add a state
476 // to reset the case sensitivity:
477 static_cast<re_case*>(
478 this->append_state(syntax_element_toggle_case, sizeof(re_case))
479 )->icase = opts & regbase::icase;
482 m_has_case_change = old_case_change;
484 // restore branch reset:
486 m_mark_reset = mark_reset;
488 // we either have a ')' or we have run out of characters prematurely:
490 if(m_position == m_end)
492 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
495 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
496 #ifndef BOOST_NO_STD_DISTANCE
497 if(markid && (this->flags() & regbase::save_subexpression_location))
498 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position);
500 if(markid && (this->flags() & regbase::save_subexpression_location))
501 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base);
505 // append closing parenthesis state:
507 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
509 pb->icase = this->flags() & regbase::icase;
510 this->m_paren_start = last_paren_start;
512 // restore the alternate insertion point:
514 this->m_alt_insert_point = last_alt_point;
516 // allow backrefs to this mark:
518 if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT))
519 this->m_backrefs |= 1u << (markid - 1);
524 template <class charT, class traits>
525 bool basic_regex_parser<charT, traits>::parse_basic_escape()
529 switch(this->m_traits.escape_syntax_type(*m_position))
531 case regex_constants::syntax_open_mark:
532 return parse_open_paren();
533 case regex_constants::syntax_close_mark:
535 case regex_constants::syntax_plus:
536 if(this->flags() & regex_constants::bk_plus_qm)
539 return parse_repeat(1);
542 return parse_literal();
543 case regex_constants::syntax_question:
544 if(this->flags() & regex_constants::bk_plus_qm)
547 return parse_repeat(0, 1);
550 return parse_literal();
551 case regex_constants::syntax_open_brace:
552 if(this->flags() & regbase::no_intervals)
553 return parse_literal();
555 return parse_repeat_range(true);
556 case regex_constants::syntax_close_brace:
557 if(this->flags() & regbase::no_intervals)
558 return parse_literal();
559 fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {.");
561 case regex_constants::syntax_or:
562 if(this->flags() & regbase::bk_vbar)
565 result = parse_literal();
567 case regex_constants::syntax_digit:
568 return parse_backref();
569 case regex_constants::escape_type_start_buffer:
570 if(this->flags() & regbase::emacs_ex)
573 this->append_state(syntax_element_buffer_start);
576 result = parse_literal();
578 case regex_constants::escape_type_end_buffer:
579 if(this->flags() & regbase::emacs_ex)
582 this->append_state(syntax_element_buffer_end);
585 result = parse_literal();
587 case regex_constants::escape_type_word_assert:
588 if(this->flags() & regbase::emacs_ex)
591 this->append_state(syntax_element_word_boundary);
594 result = parse_literal();
596 case regex_constants::escape_type_not_word_assert:
597 if(this->flags() & regbase::emacs_ex)
600 this->append_state(syntax_element_within_word);
603 result = parse_literal();
605 case regex_constants::escape_type_left_word:
606 if(this->flags() & regbase::emacs_ex)
609 this->append_state(syntax_element_word_start);
612 result = parse_literal();
614 case regex_constants::escape_type_right_word:
615 if(this->flags() & regbase::emacs_ex)
618 this->append_state(syntax_element_word_end);
621 result = parse_literal();
624 if(this->flags() & regbase::emacs_ex)
634 basic_char_set<charT, traits> char_set;
637 char_set.add_class(this->m_word_mask);
638 if(0 == this->append_set(char_set))
640 fail(regex_constants::error_ctype, m_position - m_base);
650 return add_emacs_code(negate);
653 // not supported yet:
654 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.");
660 result = parse_literal();
666 template <class charT, class traits>
667 bool basic_regex_parser<charT, traits>::parse_extended_escape()
670 if(m_position == m_end)
672 fail(regex_constants::error_escape, m_position - m_base, "Incomplete escape sequence found.");
675 bool negate = false; // in case this is a character class escape: \w \d etc
676 switch(this->m_traits.escape_syntax_type(*m_position))
678 case regex_constants::escape_type_not_class:
681 case regex_constants::escape_type_class:
683 escape_type_class_jump:
684 typedef typename traits::char_class_type m_type;
685 m_type m = this->m_traits.lookup_classname(m_position, m_position+1);
688 basic_char_set<charT, traits> char_set;
691 char_set.add_class(m);
692 if(0 == this->append_set(char_set))
694 fail(regex_constants::error_ctype, m_position - m_base);
701 // not a class, just a regular unknown escape:
703 this->append_literal(unescape_character());
706 case regex_constants::syntax_digit:
707 return parse_backref();
708 case regex_constants::escape_type_left_word:
710 this->append_state(syntax_element_word_start);
712 case regex_constants::escape_type_right_word:
714 this->append_state(syntax_element_word_end);
716 case regex_constants::escape_type_start_buffer:
718 this->append_state(syntax_element_buffer_start);
720 case regex_constants::escape_type_end_buffer:
722 this->append_state(syntax_element_buffer_end);
724 case regex_constants::escape_type_word_assert:
726 this->append_state(syntax_element_word_boundary);
728 case regex_constants::escape_type_not_word_assert:
730 this->append_state(syntax_element_within_word);
732 case regex_constants::escape_type_Z:
734 this->append_state(syntax_element_soft_buffer_end);
736 case regex_constants::escape_type_Q:
738 case regex_constants::escape_type_C:
739 return parse_match_any();
740 case regex_constants::escape_type_X:
742 this->append_state(syntax_element_combining);
744 case regex_constants::escape_type_G:
746 this->append_state(syntax_element_restart_continue);
748 case regex_constants::escape_type_not_property:
751 case regex_constants::escape_type_property:
755 if(m_position == m_end)
757 fail(regex_constants::error_escape, m_position - m_base, "Incomplete property escape found.");
760 // maybe have \p{ddd}
761 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
763 const charT* base = m_position;
764 // skip forward until we find enclosing brace:
765 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
767 if(m_position == m_end)
769 fail(regex_constants::error_escape, m_position - m_base, "Closing } missing from property escape sequence.");
772 m = this->m_traits.lookup_classname(++base, m_position++);
776 m = this->m_traits.lookup_classname(m_position, m_position+1);
781 basic_char_set<charT, traits> char_set;
784 char_set.add_class(m);
785 if(0 == this->append_set(char_set))
787 fail(regex_constants::error_ctype, m_position - m_base);
792 fail(regex_constants::error_ctype, m_position - m_base, "Escape sequence was neither a valid property nor a valid character class name.");
795 case regex_constants::escape_type_reset_start_mark:
796 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
798 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
800 pb->icase = this->flags() & regbase::icase;
801 this->m_pdata->m_data.align();
805 goto escape_type_class_jump;
806 case regex_constants::escape_type_line_ending:
807 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
809 const charT* e = get_escape_R_string<charT>();
810 const charT* old_position = m_position;
811 const charT* old_end = m_end;
812 const charT* old_base = m_base;
815 m_end = e + traits::length(e);
816 bool r = parse_all();
817 m_position = ++old_position;
822 goto escape_type_class_jump;
823 case regex_constants::escape_type_extended_backref:
824 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
826 bool have_brace = false;
827 bool negative = false;
828 static const char* incomplete_message = "Incomplete \\g escape found.";
829 if(++m_position == m_end)
831 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
834 // maybe have \g{ddd}
835 regex_constants::syntax_type syn = this->m_traits.syntax_type(*m_position);
836 regex_constants::syntax_type syn_end = 0;
837 if((syn == regex_constants::syntax_open_brace)
838 || (syn == regex_constants::escape_type_left_word)
839 || (syn == regex_constants::escape_type_end_buffer))
841 if(++m_position == m_end)
843 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
849 case regex_constants::syntax_open_brace:
850 syn_end = regex_constants::syntax_close_brace;
852 case regex_constants::escape_type_left_word:
853 syn_end = regex_constants::escape_type_right_word;
856 syn_end = regex_constants::escape_type_end_buffer;
860 negative = (*m_position == static_cast<charT>('-'));
861 if((negative) && (++m_position == m_end))
863 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
866 const charT* pc = m_position;
867 int i = this->m_traits.toi(pc, m_end, 10);
868 if((i < 0) && syn_end)
870 // Check for a named capture, get the leftmost one if there is more than one:
871 const charT* base = m_position;
872 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != syn_end))
876 i = hash_value_from_capture_name(base, m_position);
880 i = 1 + m_mark_count - i;
881 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)))))
884 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
886 pb->icase = this->flags() & regbase::icase;
890 fail(regex_constants::error_backref, m_position - m_base);
896 if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != syn_end))
898 fail(regex_constants::error_escape, m_position - m_base, incomplete_message);
905 goto escape_type_class_jump;
906 case regex_constants::escape_type_control_v:
907 if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
908 goto escape_type_class_jump;
911 this->append_literal(unescape_character());
917 template <class charT, class traits>
918 bool basic_regex_parser<charT, traits>::parse_match_any()
921 // we have a '.' that can match any character:
924 static_cast<re_dot*>(
925 this->append_state(syntax_element_wild, sizeof(re_dot))
926 )->mask = static_cast<unsigned char>(this->flags() & regbase::no_mod_s
927 ? re_detail::force_not_newline
928 : this->flags() & regbase::mod_s ?
929 re_detail::force_newline : re_detail::dont_care);
933 template <class charT, class traits>
934 bool basic_regex_parser<charT, traits>::parse_repeat(std::size_t low, std::size_t high)
937 bool pocessive = false;
938 std::size_t insert_point;
940 // when we get to here we may have a non-greedy ? mark still to come:
942 if((m_position != m_end)
944 (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex)))
945 || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex)))
949 // OK we have a perl or emacs regex, check for a '?':
950 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question)
955 // for perl regexes only check for pocessive ++ repeats.
956 if((m_position != m_end)
957 && (0 == (this->flags() & regbase::main_option_type))
958 && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus))
964 if(0 == this->m_last_state)
966 fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position), "Nothing to repeat.");
969 if(this->m_last_state->type == syntax_element_endmark)
971 // insert a repeat before the '(' matching the last ')':
972 insert_point = this->m_paren_start;
974 else if((this->m_last_state->type == syntax_element_literal) && (static_cast<re_literal*>(this->m_last_state)->length > 1))
976 // the last state was a literal with more than one character, split it in two:
977 re_literal* lit = static_cast<re_literal*>(this->m_last_state);
978 charT c = (static_cast<charT*>(static_cast<void*>(lit+1)))[lit->length - 1];
980 // now append new state:
981 lit = static_cast<re_literal*>(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
983 (static_cast<charT*>(static_cast<void*>(lit+1)))[0] = c;
984 insert_point = this->getoffset(this->m_last_state);
988 // repeat the last state whatever it was, need to add some error checking here:
989 switch(this->m_last_state->type)
991 case syntax_element_start_line:
992 case syntax_element_end_line:
993 case syntax_element_word_boundary:
994 case syntax_element_within_word:
995 case syntax_element_word_start:
996 case syntax_element_word_end:
997 case syntax_element_buffer_start:
998 case syntax_element_buffer_end:
999 case syntax_element_alt:
1000 case syntax_element_soft_buffer_end:
1001 case syntax_element_restart_continue:
1002 case syntax_element_jump:
1003 case syntax_element_startmark:
1004 case syntax_element_backstep:
1005 // can't legally repeat any of the above:
1006 fail(regex_constants::error_badrepeat, m_position - m_base);
1012 insert_point = this->getoffset(this->m_last_state);
1015 // OK we now know what to repeat, so insert the repeat around it:
1017 re_repeat* rep = static_cast<re_repeat*>(this->insert_state(insert_point, syntax_element_rep, re_repeater_size));
1020 rep->greedy = greedy;
1021 rep->leading = false;
1022 // store our repeater position for later:
1023 std::ptrdiff_t rep_off = this->getoffset(rep);
1024 // and append a back jump to the repeat:
1025 re_jump* jmp = static_cast<re_jump*>(this->append_state(syntax_element_jump, sizeof(re_jump)));
1026 jmp->alt.i = rep_off - this->getoffset(jmp);
1027 this->m_pdata->m_data.align();
1028 // now fill in the alt jump for the repeat:
1029 rep = static_cast<re_repeat*>(this->getaddress(rep_off));
1030 rep->alt.i = this->m_pdata->m_data.size() - rep_off;
1032 // If the repeat is pocessive then bracket the repeat with a (?>...)
1033 // independent sub-expression construct:
1037 if(m_position != m_end)
1040 // Check for illegal following quantifier, we have to do this here, because
1041 // the extra states we insert below circumvents our usual error checking :-(
1043 switch(this->m_traits.syntax_type(*m_position))
1045 case regex_constants::syntax_star:
1046 case regex_constants::syntax_plus:
1047 case regex_constants::syntax_question:
1048 case regex_constants::syntax_open_brace:
1049 fail(regex_constants::error_badrepeat, m_position - m_base);
1053 re_brace* pb = static_cast<re_brace*>(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace)));
1055 pb->icase = this->flags() & regbase::icase;
1056 jmp = static_cast<re_jump*>(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump)));
1057 this->m_pdata->m_data.align();
1058 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
1059 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
1061 pb->icase = this->flags() & regbase::icase;
1066 template <class charT, class traits>
1067 bool basic_regex_parser<charT, traits>::parse_repeat_range(bool isbasic)
1069 static const char* incomplete_message = "Missing } in quantified repetition.";
1071 // parse a repeat-range:
1073 std::size_t min, max;
1076 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1078 if(this->m_position == this->m_end)
1080 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1082 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1085 // Treat the opening '{' as a literal character, rewind to start of error:
1087 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1088 return parse_literal();
1091 v = this->m_traits.toi(m_position, m_end, 10);
1095 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1097 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1100 // Treat the opening '{' as a literal character, rewind to start of error:
1102 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1103 return parse_literal();
1105 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1107 if(this->m_position == this->m_end)
1109 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1111 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1114 // Treat the opening '{' as a literal character, rewind to start of error:
1116 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1117 return parse_literal();
1120 // see if we have a comma:
1121 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma)
1123 // move on and error check:
1126 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1128 if(this->m_position == this->m_end)
1130 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1132 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1135 // Treat the opening '{' as a literal character, rewind to start of error:
1137 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1138 return parse_literal();
1140 // get the value if any:
1141 v = this->m_traits.toi(m_position, m_end, 10);
1142 max = (v >= 0) ? (std::size_t)v : (std::numeric_limits<std::size_t>::max)();
1146 // no comma, max = min:
1150 while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space))
1152 // OK now check trailing }:
1153 if(this->m_position == this->m_end)
1155 if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex))
1157 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1160 // Treat the opening '{' as a literal character, rewind to start of error:
1162 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1163 return parse_literal();
1167 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape)
1170 if(this->m_position == this->m_end)
1172 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1178 fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message);
1182 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace)
1186 // Treat the opening '{' as a literal character, rewind to start of error:
1188 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position;
1189 return parse_literal();
1192 // finally go and add the repeat, unless error:
1196 // Backtrack to error location:
1198 while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position;
1200 fail(regex_constants::error_badbrace, m_position - m_base);
1203 return parse_repeat(min, max);
1206 template <class charT, class traits>
1207 bool basic_regex_parser<charT, traits>::parse_alt()
1210 // error check: if there have been no previous states,
1211 // or if the last state was a '(' then error:
1214 ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark))
1217 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
1219 ((this->flags() & regbase::no_empty_expressions) == 0)
1223 fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression cannot start with the alternation operator |.");
1227 // Reset mark count if required:
1229 if(m_max_mark < m_mark_count)
1230 m_max_mark = m_mark_count;
1231 if(m_mark_reset >= 0)
1232 m_mark_count = m_mark_reset;
1236 // we need to append a trailing jump:
1238 re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump));
1239 std::ptrdiff_t jump_offset = this->getoffset(pj);
1241 // now insert the alternative:
1243 re_alt* palt = static_cast<re_alt*>(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size));
1244 jump_offset += re_alt_size;
1245 this->m_pdata->m_data.align();
1246 palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt);
1248 // update m_alt_insert_point so that the next alternate gets
1249 // inserted at the start of the second of the two we've just created:
1251 this->m_alt_insert_point = this->m_pdata->m_data.size();
1253 // the start of this alternative must have a case changes state
1254 // if the current block has messed around with case changes:
1256 if(m_has_case_change)
1258 static_cast<re_case*>(
1259 this->append_state(syntax_element_toggle_case, sizeof(re_case))
1260 )->icase = this->m_icase;
1263 // push the alternative onto our stack, a recursive
1264 // implementation here is easier to understand (and faster
1265 // as it happens), but causes all kinds of stack overflow problems
1266 // on programs with small stacks (COM+).
1268 m_alt_jumps.push_back(jump_offset);
1272 template <class charT, class traits>
1273 bool basic_regex_parser<charT, traits>::parse_set()
1275 static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1277 if(m_position == m_end)
1279 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1282 basic_char_set<charT, traits> char_set;
1284 const charT* base = m_position; // where the '[' was
1285 const charT* item_base = m_position; // where the '[' or '^' was
1287 while(m_position != m_end)
1289 switch(this->m_traits.syntax_type(*m_position))
1291 case regex_constants::syntax_caret:
1292 if(m_position == base)
1296 item_base = m_position;
1299 parse_set_literal(char_set);
1301 case regex_constants::syntax_close_set:
1302 if(m_position == item_base)
1304 parse_set_literal(char_set);
1310 if(0 == this->append_set(char_set))
1312 fail(regex_constants::error_ctype, m_position - m_base);
1317 case regex_constants::syntax_open_set:
1318 if(parse_inner_set(char_set))
1321 case regex_constants::syntax_escape:
1324 // look ahead and see if this is a character class shortcut
1328 if(this->m_traits.escape_syntax_type(*m_position)
1329 == regex_constants::escape_type_class)
1331 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1334 char_set.add_class(m);
1339 else if(this->m_traits.escape_syntax_type(*m_position)
1340 == regex_constants::escape_type_not_class)
1342 // negated character class:
1343 char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1);
1346 char_set.add_negated_class(m);
1351 // not a character class, just a regular escape:
1353 parse_set_literal(char_set);
1357 parse_set_literal(char_set);
1361 return m_position != m_end;
1364 template <class charT, class traits>
1365 bool basic_regex_parser<charT, traits>::parse_inner_set(basic_char_set<charT, traits>& char_set)
1367 static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content.";
1369 // we have either a character class [:name:]
1370 // a collating element [.name.]
1371 // or an equivalence class [=name=]
1373 if(m_end == ++m_position)
1375 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1378 switch(this->m_traits.syntax_type(*m_position))
1380 case regex_constants::syntax_dot:
1382 // a collating element is treated as a literal:
1385 parse_set_literal(char_set);
1387 case regex_constants::syntax_colon:
1389 // check that character classes are actually enabled:
1390 if((this->flags() & (regbase::main_option_type | regbase::no_char_classes))
1391 == (regbase::basic_syntax_group | regbase::no_char_classes))
1394 parse_set_literal(char_set);
1398 if(m_end == ++m_position)
1400 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1403 const charT* name_first = m_position;
1404 // skip at least one character, then find the matching ':]'
1405 if(m_end == ++m_position)
1407 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1410 while((m_position != m_end)
1411 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon))
1413 const charT* name_last = m_position;
1414 if(m_end == m_position)
1416 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1419 if((m_end == ++m_position)
1420 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1422 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1426 // check for negated class:
1428 bool negated = false;
1429 if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret)
1434 typedef typename traits::char_class_type m_type;
1435 m_type m = this->m_traits.lookup_classname(name_first, name_last);
1438 if(char_set.empty() && (name_last - name_first == 1))
1440 // maybe a special case:
1442 if( (m_position != m_end)
1443 && (this->m_traits.syntax_type(*m_position)
1444 == regex_constants::syntax_close_set))
1446 if(this->m_traits.escape_syntax_type(*name_first)
1447 == regex_constants::escape_type_left_word)
1450 this->append_state(syntax_element_word_start);
1453 if(this->m_traits.escape_syntax_type(*name_first)
1454 == regex_constants::escape_type_right_word)
1457 this->append_state(syntax_element_word_end);
1462 fail(regex_constants::error_ctype, name_first - m_base);
1465 if(negated == false)
1466 char_set.add_class(m);
1468 char_set.add_negated_class(m);
1472 case regex_constants::syntax_equal:
1475 if(m_end == ++m_position)
1477 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1480 const charT* name_first = m_position;
1481 // skip at least one character, then find the matching '=]'
1482 if(m_end == ++m_position)
1484 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1487 while((m_position != m_end)
1488 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal))
1490 const charT* name_last = m_position;
1491 if(m_end == m_position)
1493 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1496 if((m_end == ++m_position)
1497 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1499 fail(regex_constants::error_brack, m_position - m_base, incomplete_message);
1502 string_type m = this->m_traits.lookup_collatename(name_first, name_last);
1503 if((0 == m.size()) || (m.size() > 2))
1505 fail(regex_constants::error_collate, name_first - m_base);
1514 char_set.add_equivalent(d);
1520 parse_set_literal(char_set);
1526 template <class charT, class traits>
1527 void basic_regex_parser<charT, traits>::parse_set_literal(basic_char_set<charT, traits>& char_set)
1529 digraph<charT> start_range(get_next_set_literal(char_set));
1530 if(m_end == m_position)
1532 fail(regex_constants::error_brack, m_position - m_base);
1535 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1538 if(m_end == ++m_position)
1540 fail(regex_constants::error_brack, m_position - m_base);
1543 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)
1545 digraph<charT> end_range = get_next_set_literal(char_set);
1546 char_set.add_range(start_range, end_range);
1547 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash)
1549 if(m_end == ++m_position)
1551 fail(regex_constants::error_brack, m_position - m_base);
1554 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set)
1560 fail(regex_constants::error_range, m_position - m_base);
1567 char_set.add_single(start_range);
1570 template <class charT, class traits>
1571 digraph<charT> basic_regex_parser<charT, traits>::get_next_set_literal(basic_char_set<charT, traits>& char_set)
1573 digraph<charT> result;
1574 switch(this->m_traits.syntax_type(*m_position))
1576 case regex_constants::syntax_dash:
1577 if(!char_set.empty())
1579 // see if we are at the end of the set:
1580 if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1582 fail(regex_constants::error_range, m_position - m_base);
1587 result.first = *m_position++;
1589 case regex_constants::syntax_escape:
1590 // check to see if escapes are supported first:
1591 if(this->flags() & regex_constants::no_escape_in_lists)
1593 result = *m_position++;
1597 result = unescape_character();
1599 case regex_constants::syntax_open_set:
1601 if(m_end == ++m_position)
1603 fail(regex_constants::error_collate, m_position - m_base);
1606 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)
1609 result.first = *m_position;
1613 if(m_end == ++m_position)
1615 fail(regex_constants::error_collate, m_position - m_base);
1618 const charT* name_first = m_position;
1619 // skip at least one character, then find the matching ':]'
1620 if(m_end == ++m_position)
1622 fail(regex_constants::error_collate, name_first - m_base);
1625 while((m_position != m_end)
1626 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot))
1628 const charT* name_last = m_position;
1629 if(m_end == m_position)
1631 fail(regex_constants::error_collate, name_first - m_base);
1634 if((m_end == ++m_position)
1635 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set))
1637 fail(regex_constants::error_collate, name_first - m_base);
1641 string_type s = this->m_traits.lookup_collatename(name_first, name_last);
1642 if(s.empty() || (s.size() > 2))
1644 fail(regex_constants::error_collate, name_first - m_base);
1647 result.first = s[0];
1649 result.second = s[1];
1655 result = *m_position++;
1661 // does a value fit in the specified charT type?
1663 template <class charT>
1664 bool valid_value(charT, int v, const mpl::true_&)
1666 return (v >> (sizeof(charT) * CHAR_BIT)) == 0;
1668 template <class charT>
1669 bool valid_value(charT, int, const mpl::false_&)
1671 return true; // v will alsways fit in a charT
1673 template <class charT>
1674 bool valid_value(charT c, int v)
1676 return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>());
1679 template <class charT, class traits>
1680 charT basic_regex_parser<charT, traits>::unescape_character()
1683 #pragma warning(push)
1684 #pragma warning(disable:4127)
1687 if(m_position == m_end)
1689 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely.");
1692 switch(this->m_traits.escape_syntax_type(*m_position))
1694 case regex_constants::escape_type_control_a:
1695 result = charT('\a');
1697 case regex_constants::escape_type_e:
1700 case regex_constants::escape_type_control_f:
1701 result = charT('\f');
1703 case regex_constants::escape_type_control_n:
1704 result = charT('\n');
1706 case regex_constants::escape_type_control_r:
1707 result = charT('\r');
1709 case regex_constants::escape_type_control_t:
1710 result = charT('\t');
1712 case regex_constants::escape_type_control_v:
1713 result = charT('\v');
1715 case regex_constants::escape_type_word_assert:
1716 result = charT('\b');
1718 case regex_constants::escape_type_ascii_control:
1720 if(m_position == m_end)
1722 // Rewind to start of escape:
1724 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1725 fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely.");
1728 result = static_cast<charT>(*m_position % 32);
1730 case regex_constants::escape_type_hex:
1732 if(m_position == m_end)
1734 // Rewind to start of escape:
1736 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1737 fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely.");
1740 // maybe have \x{ddd}
1741 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1744 if(m_position == m_end)
1746 // Rewind to start of escape:
1748 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1749 fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence.");
1752 int i = this->m_traits.toi(m_position, m_end, 16);
1753 if((m_position == m_end)
1755 || ((std::numeric_limits<charT>::is_specialized) && (i > (int)(std::numeric_limits<charT>::max)()))
1756 || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1758 // Rewind to start of escape:
1760 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1761 fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid.");
1769 std::ptrdiff_t len = (std::min)(static_cast<std::ptrdiff_t>(2), static_cast<std::ptrdiff_t>(m_end - m_position));
1770 int i = this->m_traits.toi(m_position, m_position + len, 16);
1772 || !valid_value(charT(0), i))
1774 // Rewind to start of escape:
1776 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1777 fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character.");
1783 case regex_constants::syntax_digit:
1785 // an octal escape sequence, the first character must be a zero
1786 // followed by up to 3 octal digits:
1787 std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast<std::ptrdiff_t>(4));
1788 const charT* bp = m_position;
1789 int val = this->m_traits.toi(bp, bp + 1, 8);
1792 // Rewind to start of escape:
1794 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1795 // Oops not an octal escape after all:
1796 fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence.");
1799 val = this->m_traits.toi(m_position, m_position + len, 8);
1802 // Rewind to start of escape:
1804 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1805 fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid.");
1808 return static_cast<charT>(val);
1810 case regex_constants::escape_type_named_char:
1813 if(m_position == m_end)
1815 // Rewind to start of escape:
1817 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1818 fail(regex_constants::error_escape, m_position - m_base);
1821 // maybe have \N{name}
1822 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace)
1824 const charT* base = m_position;
1825 // skip forward until we find enclosing brace:
1826 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace))
1828 if(m_position == m_end)
1830 // Rewind to start of escape:
1832 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1833 fail(regex_constants::error_escape, m_position - m_base);
1836 string_type s = this->m_traits.lookup_collatename(++base, m_position++);
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_collate, m_position - m_base);
1850 // fall through is a failure:
1851 // Rewind to start of escape:
1853 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1854 fail(regex_constants::error_escape, m_position - m_base);
1858 result = *m_position;
1864 #pragma warning(pop)
1868 template <class charT, class traits>
1869 bool basic_regex_parser<charT, traits>::parse_backref()
1871 BOOST_ASSERT(m_position != m_end);
1872 const charT* pc = m_position;
1873 int i = this->m_traits.toi(pc, pc + 1, 10);
1874 if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs)))
1876 // not a backref at all but an octal escape sequence:
1877 charT c = unescape_character();
1878 this->append_literal(c);
1880 else if((i > 0) && (this->m_backrefs & (1u << (i-1))))
1883 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_backref, sizeof(re_brace)));
1885 pb->icase = this->flags() & regbase::icase;
1889 // Rewind to start of escape:
1891 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
1892 fail(regex_constants::error_backref, m_position - m_base);
1898 template <class charT, class traits>
1899 bool basic_regex_parser<charT, traits>::parse_QE()
1902 #pragma warning(push)
1903 #pragma warning(disable:4127)
1906 // parse a \Q...\E sequence:
1908 ++m_position; // skip the Q
1909 const charT* start = m_position;
1913 while((m_position != m_end)
1914 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape))
1916 if(m_position == m_end)
1918 // a \Q...\E sequence may terminate with the end of the expression:
1922 if(++m_position == m_end) // skip the escape
1924 fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence.");
1927 // check to see if it's a \E:
1928 if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E)
1931 end = m_position - 2;
1934 // otherwise go round again:
1937 // now add all the character between the two escapes as literals:
1941 this->append_literal(*start);
1946 #pragma warning(pop)
1950 template <class charT, class traits>
1951 bool basic_regex_parser<charT, traits>::parse_perl_extension()
1953 if(++m_position == m_end)
1955 // Rewind to start of (? sequence:
1957 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
1958 fail(regex_constants::error_perl_extension, m_position - m_base);
1962 // treat comments as a special case, as these
1963 // are the only ones that don't start with a leading
1966 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash)
1968 while((m_position != m_end)
1969 && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark))
1974 // backup some state, and prepare the way:
1977 std::ptrdiff_t jump_offset = 0;
1978 re_brace* pb = static_cast<re_brace*>(this->append_state(syntax_element_startmark, sizeof(re_brace)));
1979 pb->icase = this->flags() & regbase::icase;
1980 std::ptrdiff_t last_paren_start = this->getoffset(pb);
1981 // back up insertion point for alternations, and set new point:
1982 std::ptrdiff_t last_alt_point = m_alt_insert_point;
1983 this->m_pdata->m_data.align();
1984 m_alt_insert_point = this->m_pdata->m_data.size();
1985 std::ptrdiff_t expected_alt_point = m_alt_insert_point;
1986 bool restore_flags = true;
1987 regex_constants::syntax_option_type old_flags = this->flags();
1988 bool old_case_change = m_has_case_change;
1989 m_has_case_change = false;
1991 int mark_reset = m_mark_reset;
1992 int max_mark = m_max_mark;
1994 m_max_mark = m_mark_count;
1997 // select the actual extension used:
1999 switch(this->m_traits.syntax_type(*m_position))
2001 case regex_constants::syntax_or:
2002 m_mark_reset = m_mark_count;
2004 case regex_constants::syntax_colon:
2006 // a non-capturing mark:
2008 pb->index = markid = 0;
2011 case regex_constants::syntax_digit:
2014 // a recursive subexpression:
2016 v = this->m_traits.toi(m_position, m_end, 10);
2017 if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2019 // Rewind to start of (? sequence:
2021 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2022 fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated.");
2026 pb->index = markid = 0;
2027 re_recurse* pr = static_cast<re_recurse*>(this->append_state(syntax_element_recurse, sizeof(re_recurse)));
2030 static_cast<re_case*>(
2031 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2032 )->icase = this->flags() & regbase::icase;
2035 case regex_constants::syntax_plus:
2037 // A forward-relative recursive subexpression:
2040 v = this->m_traits.toi(m_position, m_end, 10);
2041 if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2043 // Rewind to start of (? sequence:
2045 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2046 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2050 goto insert_recursion;
2051 case regex_constants::syntax_dash:
2053 // Possibly a backward-relative recursive subexpression:
2056 v = this->m_traits.toi(m_position, m_end, 10);
2060 // Oops not a relative recursion at all, but a (?-imsx) group:
2061 goto option_group_jump;
2063 v = m_mark_count + 1 - v;
2066 // Rewind to start of (? sequence:
2068 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2069 fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression.");
2072 goto insert_recursion;
2073 case regex_constants::syntax_equal:
2074 pb->index = markid = -1;
2076 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2077 this->m_pdata->m_data.align();
2078 m_alt_insert_point = this->m_pdata->m_data.size();
2080 case regex_constants::syntax_not:
2081 pb->index = markid = -2;
2083 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2084 this->m_pdata->m_data.align();
2085 m_alt_insert_point = this->m_pdata->m_data.size();
2087 case regex_constants::escape_type_left_word:
2089 // a lookbehind assertion:
2090 if(++m_position == m_end)
2092 // Rewind to start of (? sequence:
2094 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2095 fail(regex_constants::error_perl_extension, m_position - m_base);
2098 regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position);
2099 if(t == regex_constants::syntax_not)
2100 pb->index = markid = -2;
2101 else if(t == regex_constants::syntax_equal)
2102 pb->index = markid = -1;
2105 // Probably a named capture which also starts (?< :
2108 goto named_capture_jump;
2111 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2112 this->append_state(syntax_element_backstep, sizeof(re_brace));
2113 this->m_pdata->m_data.align();
2114 m_alt_insert_point = this->m_pdata->m_data.size();
2117 case regex_constants::escape_type_right_word:
2119 // an independent sub-expression:
2121 pb->index = markid = -3;
2123 jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump)));
2124 this->m_pdata->m_data.align();
2125 m_alt_insert_point = this->m_pdata->m_data.size();
2127 case regex_constants::syntax_open_mark:
2129 // a conditional expression:
2130 pb->index = markid = -4;
2131 if(++m_position == m_end)
2133 // Rewind to start of (? sequence:
2135 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2136 fail(regex_constants::error_perl_extension, m_position - m_base);
2139 v = this->m_traits.toi(m_position, m_end, 10);
2140 if(m_position == m_end)
2142 // Rewind to start of (? sequence:
2144 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2145 fail(regex_constants::error_perl_extension, m_position - m_base);
2148 if(*m_position == charT('R'))
2150 if(++m_position == m_end)
2152 // Rewind to start of (? sequence:
2154 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2155 fail(regex_constants::error_perl_extension, m_position - m_base);
2158 if(*m_position == charT('&'))
2160 const charT* base = ++m_position;
2161 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2163 if(m_position == m_end)
2165 // Rewind to start of (? sequence:
2167 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2168 fail(regex_constants::error_perl_extension, m_position - m_base);
2171 v = -static_cast<int>(hash_value_from_capture_name(base, m_position));
2175 v = -this->m_traits.toi(m_position, m_end, 10);
2177 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2178 br->index = v < 0 ? (v - 1) : 0;
2179 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2181 // Rewind to start of (? sequence:
2183 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2184 fail(regex_constants::error_perl_extension, m_position - m_base);
2187 if(++m_position == m_end)
2189 // Rewind to start of (? sequence:
2191 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2192 fail(regex_constants::error_perl_extension, m_position - m_base);
2196 else if((*m_position == charT('\'')) || (*m_position == charT('<')))
2198 const charT* base = ++m_position;
2199 while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\'')))
2201 if(m_position == m_end)
2203 // Rewind to start of (? sequence:
2205 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2206 fail(regex_constants::error_perl_extension, m_position - m_base);
2209 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2210 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2212 if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end))
2214 // Rewind to start of (? sequence:
2216 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2217 fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture.");
2220 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2222 // Rewind to start of (? sequence:
2224 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2225 fail(regex_constants::error_perl_extension, m_position - m_base);
2228 if(++m_position == m_end)
2230 // Rewind to start of (? sequence:
2232 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2233 fail(regex_constants::error_perl_extension, m_position - m_base);
2237 else if(*m_position == charT('D'))
2239 const char* def = "DEFINE";
2240 while(*def && (m_position != m_end) && (*m_position == charT(*def)))
2241 ++m_position, ++def;
2242 if((m_position == m_end) || *def)
2244 // Rewind to start of (? sequence:
2246 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2247 fail(regex_constants::error_perl_extension, m_position - m_base);
2250 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2251 br->index = 9999; // special magic value!
2252 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2254 // Rewind to start of (? sequence:
2256 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2257 fail(regex_constants::error_perl_extension, m_position - m_base);
2260 if(++m_position == m_end)
2262 // Rewind to start of (? sequence:
2264 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2265 fail(regex_constants::error_perl_extension, m_position - m_base);
2271 re_brace* br = static_cast<re_brace*>(this->append_state(syntax_element_assert_backref, sizeof(re_brace)));
2273 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2275 // Rewind to start of (? sequence:
2277 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2278 fail(regex_constants::error_perl_extension, m_position - m_base);
2281 if(++m_position == m_end)
2283 // Rewind to start of (? sequence:
2285 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2286 fail(regex_constants::error_perl_extension, m_position - m_base);
2292 // verify that we have a lookahead or lookbehind assert:
2293 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question)
2295 // Rewind to start of (? sequence:
2297 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2298 fail(regex_constants::error_perl_extension, m_position - m_base);
2301 if(++m_position == m_end)
2303 // Rewind to start of (? sequence:
2305 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2306 fail(regex_constants::error_perl_extension, m_position - m_base);
2309 if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word)
2311 if(++m_position == m_end)
2313 // Rewind to start of (? sequence:
2315 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2316 fail(regex_constants::error_perl_extension, m_position - m_base);
2319 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2320 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2322 // Rewind to start of (? sequence:
2324 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2325 fail(regex_constants::error_perl_extension, m_position - m_base);
2332 if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)
2333 && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not))
2335 // Rewind to start of (? sequence:
2337 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2338 fail(regex_constants::error_perl_extension, m_position - m_base);
2346 case regex_constants::syntax_close_mark:
2347 // Rewind to start of (? sequence:
2349 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2350 fail(regex_constants::error_perl_extension, m_position - m_base);
2352 case regex_constants::escape_type_end_buffer:
2354 name_delim = *m_position;
2357 if(0 == (this->flags() & regbase::nosubs))
2359 markid = ++m_mark_count;
2360 #ifndef BOOST_NO_STD_DISTANCE
2361 if(this->flags() & regbase::save_subexpression_location)
2362 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>(std::distance(m_base, m_position) - 2, 0));
2364 if(this->flags() & regbase::save_subexpression_location)
2365 this->m_pdata->m_subs.push_back(std::pair<std::size_t, std::size_t>((m_position - m_base) - 2, 0));
2369 const charT* base = ++m_position;
2370 if(m_position == m_end)
2372 // Rewind to start of (? sequence:
2374 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2375 fail(regex_constants::error_perl_extension, m_position - m_base);
2378 while((m_position != m_end) && (*m_position != name_delim))
2380 if(m_position == m_end)
2382 // Rewind to start of (? sequence:
2384 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2385 fail(regex_constants::error_perl_extension, m_position - m_base);
2388 this->m_pdata->set_name(base, m_position, markid);
2393 if(*m_position == charT('R'))
2397 if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)
2399 // Rewind to start of (? sequence:
2401 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2402 fail(regex_constants::error_perl_extension, m_position - m_base);
2405 goto insert_recursion;
2407 if(*m_position == charT('&'))
2410 const charT* base = m_position;
2411 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2413 if(m_position == m_end)
2415 // Rewind to start of (? sequence:
2417 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2418 fail(regex_constants::error_perl_extension, m_position - m_base);
2421 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2422 goto insert_recursion;
2424 if(*m_position == charT('P'))
2427 if(m_position == m_end)
2429 // Rewind to start of (? sequence:
2431 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2432 fail(regex_constants::error_perl_extension, m_position - m_base);
2435 if(*m_position == charT('>'))
2438 const charT* base = m_position;
2439 while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark))
2441 if(m_position == m_end)
2443 // Rewind to start of (? sequence:
2445 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2446 fail(regex_constants::error_perl_extension, m_position - m_base);
2449 v = static_cast<int>(hash_value_from_capture_name(base, m_position));
2450 goto insert_recursion;
2454 // lets assume that we have a (?imsx) group and try and parse it:
2457 regex_constants::syntax_option_type opts = parse_options();
2458 if(m_position == m_end)
2460 // Rewind to start of (? sequence:
2462 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2463 fail(regex_constants::error_perl_extension, m_position - m_base);
2466 // make a note of whether we have a case change:
2467 m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase));
2468 pb->index = markid = 0;
2469 if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark)
2471 // update flags and carry on as normal:
2473 restore_flags = false;
2474 old_case_change |= m_has_case_change; // defer end of scope by one ')'
2476 else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon)
2478 // update flags and carry on until the matching ')' is found:
2484 // Rewind to start of (? sequence:
2486 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2487 fail(regex_constants::error_perl_extension, m_position - m_base);
2491 // finally append a case change state if we need it:
2492 if(m_has_case_change)
2494 static_cast<re_case*>(
2495 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2496 )->icase = opts & regbase::icase;
2501 // now recursively add more states, this will terminate when we get to a
2506 // Unwind alternatives:
2508 if(0 == unwind_alts(last_paren_start))
2510 // Rewind to start of (? sequence:
2512 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2513 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block.");
2517 // we either have a ')' or we have run out of characters prematurely:
2519 if(m_position == m_end)
2521 // Rewind to start of (? sequence:
2523 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2524 this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end));
2527 BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark);
2530 // restore the flags:
2534 // append a case change state if we need it:
2535 if(m_has_case_change)
2537 static_cast<re_case*>(
2538 this->append_state(syntax_element_toggle_case, sizeof(re_case))
2539 )->icase = old_flags & regbase::icase;
2541 this->flags(old_flags);
2544 // set up the jump pointer if we have one:
2548 this->m_pdata->m_data.align();
2549 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2550 jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp);
2551 if((this->m_last_state == jmp) && (markid != -2))
2553 // Oops... we didn't have anything inside the assertion.
2554 // Note we don't get here for negated forward lookahead as (?!)
2555 // does have some uses.
2556 // Rewind to start of (? sequence:
2558 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2559 fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion.");
2564 // verify that if this is conditional expression, that we do have
2565 // an alternative, if not add one:
2569 re_syntax_base* b = this->getaddress(expected_alt_point);
2570 // Make sure we have exactly one alternative following this state:
2571 if(b->type != syntax_element_alt)
2573 re_alt* alt = static_cast<re_alt*>(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt)));
2574 alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt);
2576 else if(this->getaddress(static_cast<re_alt*>(b)->alt.i, b)->type == syntax_element_alt)
2578 // Can't have seen more than one alternative:
2579 // Rewind to start of (? sequence:
2581 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2582 fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression.");
2587 // We must *not* have seen an alternative inside a (DEFINE) block:
2588 b = this->getaddress(b->next.i, b);
2589 if((b->type == syntax_element_assert_backref) && (static_cast<re_brace*>(b)->index == 9999))
2591 // Rewind to start of (? sequence:
2593 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2594 fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block.");
2598 // check for invalid repetition of next state:
2599 b = this->getaddress(expected_alt_point);
2600 b = this->getaddress(static_cast<re_alt*>(b)->next.i, b);
2601 if((b->type != syntax_element_assert_backref)
2602 && (b->type != syntax_element_startmark))
2604 // Rewind to start of (? sequence:
2606 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2607 fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion.");
2612 // append closing parenthesis state:
2614 pb = static_cast<re_brace*>(this->append_state(syntax_element_endmark, sizeof(re_brace)));
2616 pb->icase = this->flags() & regbase::icase;
2617 this->m_paren_start = last_paren_start;
2619 // restore the alternate insertion point:
2621 this->m_alt_insert_point = last_alt_point;
2623 // and the case change data:
2625 m_has_case_change = old_case_change;
2627 // And the mark_reset data:
2629 if(m_max_mark > m_mark_count)
2631 m_mark_count = m_max_mark;
2633 m_mark_reset = mark_reset;
2634 m_max_mark = max_mark;
2639 #ifndef BOOST_NO_STD_DISTANCE
2640 if(this->flags() & regbase::save_subexpression_location)
2641 this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1;
2643 if(this->flags() & regbase::save_subexpression_location)
2644 this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1;
2647 // allow backrefs to this mark:
2649 if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT)))
2650 this->m_backrefs |= 1u << (markid - 1);
2655 template <class charT, class traits>
2656 bool basic_regex_parser<charT, traits>::add_emacs_code(bool negate)
2659 // parses an emacs style \sx or \Sx construct.
2661 if(++m_position == m_end)
2663 // Rewind to start of sequence:
2665 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position;
2666 fail(regex_constants::error_escape, m_position - m_base);
2669 basic_char_set<charT, traits> char_set;
2673 static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', };
2679 char_set.add_class(this->m_mask_space);
2682 char_set.add_class(this->m_word_mask);
2685 char_set.add_single(digraph<charT>(charT('$')));
2686 char_set.add_single(digraph<charT>(charT('&')));
2687 char_set.add_single(digraph<charT>(charT('*')));
2688 char_set.add_single(digraph<charT>(charT('+')));
2689 char_set.add_single(digraph<charT>(charT('-')));
2690 char_set.add_single(digraph<charT>(charT('_')));
2691 char_set.add_single(digraph<charT>(charT('<')));
2692 char_set.add_single(digraph<charT>(charT('>')));
2695 char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5));
2698 char_set.add_single(digraph<charT>(charT('(')));
2699 char_set.add_single(digraph<charT>(charT('[')));
2700 char_set.add_single(digraph<charT>(charT('{')));
2703 char_set.add_single(digraph<charT>(charT(')')));
2704 char_set.add_single(digraph<charT>(charT(']')));
2705 char_set.add_single(digraph<charT>(charT('}')));
2708 char_set.add_single(digraph<charT>(charT('"')));
2709 char_set.add_single(digraph<charT>(charT('\'')));
2710 char_set.add_single(digraph<charT>(charT('`')));
2713 char_set.add_single(digraph<charT>(charT('\'')));
2714 char_set.add_single(digraph<charT>(charT(',')));
2715 char_set.add_single(digraph<charT>(charT('#')));
2718 char_set.add_single(digraph<charT>(charT(';')));
2721 char_set.add_single(digraph<charT>(charT('\n')));
2722 char_set.add_single(digraph<charT>(charT('\f')));
2725 fail(regex_constants::error_ctype, m_position - m_base);
2728 if(0 == this->append_set(char_set))
2730 fail(regex_constants::error_ctype, m_position - m_base);
2737 template <class charT, class traits>
2738 regex_constants::syntax_option_type basic_regex_parser<charT, traits>::parse_options()
2740 // we have a (?imsx-imsx) group, convert it into a set of flags:
2741 regex_constants::syntax_option_type f = this->flags();
2742 bool breakout = false;
2748 f |= regex_constants::mod_s;
2749 f &= ~regex_constants::no_mod_s;
2752 f &= ~regex_constants::no_mod_m;
2755 f |= regex_constants::icase;
2758 f |= regex_constants::mod_x;
2764 if(++m_position == m_end)
2766 // Rewind to start of (? sequence:
2768 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2769 fail(regex_constants::error_paren, m_position - m_base);
2777 if(*m_position == static_cast<charT>('-'))
2779 if(++m_position == m_end)
2781 // Rewind to start of (? sequence:
2783 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2784 fail(regex_constants::error_paren, m_position - m_base);
2792 f &= ~regex_constants::mod_s;
2793 f |= regex_constants::no_mod_s;
2796 f |= regex_constants::no_mod_m;
2799 f &= ~regex_constants::icase;
2802 f &= ~regex_constants::mod_x;
2808 if(++m_position == m_end)
2810 // Rewind to start of (? sequence:
2812 while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position;
2813 fail(regex_constants::error_paren, m_position - m_base);
2822 template <class charT, class traits>
2823 bool basic_regex_parser<charT, traits>::unwind_alts(std::ptrdiff_t last_paren_start)
2826 // If we didn't actually add any states after the last
2827 // alternative then that's an error:
2829 if((this->m_alt_insert_point == static_cast<std::ptrdiff_t>(this->m_pdata->m_data.size()))
2830 && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)
2833 ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group)
2835 ((this->flags() & regbase::no_empty_expressions) == 0)
2839 fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |.");
2843 // Fix up our alternatives:
2845 while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start))
2848 // fix up the jump to point to the end of the states
2849 // that we've just added:
2851 std::ptrdiff_t jump_offset = m_alt_jumps.back();
2852 m_alt_jumps.pop_back();
2853 this->m_pdata->m_data.align();
2854 re_jump* jmp = static_cast<re_jump*>(this->getaddress(jump_offset));
2855 BOOST_ASSERT(jmp->type == syntax_element_jump);
2856 jmp->alt.i = this->m_pdata->m_data.size() - jump_offset;
2862 #pragma warning(pop)
2865 } // namespace re_detail
2866 } // namespace boost
2869 #pragma warning(push)
2870 #pragma warning(disable: 4103)
2872 #ifdef BOOST_HAS_ABI_HEADERS
2873 # include BOOST_ABI_SUFFIX
2876 #pragma warning(pop)