Chris@16: /* Chris@16: * Chris@16: * Copyright (c) 2004 Chris@16: * John Maddock Chris@16: * Chris@16: * Use, modification and distribution are subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying file Chris@16: * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: */ Chris@16: Chris@16: /* Chris@16: * LOCATION: see http://www.boost.org for most recent version. Chris@16: * FILE basic_regex_parser.cpp Chris@16: * VERSION see Chris@16: * DESCRIPTION: Declares template class basic_regex_parser. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP Chris@16: #define BOOST_REGEX_V4_BASIC_REGEX_PARSER_HPP Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4103) Chris@16: #endif Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_PREFIX Chris@16: #endif Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: namespace boost{ Chris@16: namespace re_detail{ Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4244 4800) Chris@16: #endif Chris@16: Chris@16: template Chris@16: class basic_regex_parser : public basic_regex_creator Chris@16: { Chris@16: public: Chris@16: basic_regex_parser(regex_data* data); Chris@16: void parse(const charT* p1, const charT* p2, unsigned flags); Chris@16: void fail(regex_constants::error_type error_code, std::ptrdiff_t position); Chris@16: void fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos); Chris@16: void fail(regex_constants::error_type error_code, std::ptrdiff_t position, const std::string& message) Chris@16: { Chris@16: fail(error_code, position, message, position); Chris@16: } Chris@16: Chris@16: bool parse_all(); Chris@16: bool parse_basic(); Chris@16: bool parse_extended(); Chris@16: bool parse_literal(); Chris@16: bool parse_open_paren(); Chris@16: bool parse_basic_escape(); Chris@16: bool parse_extended_escape(); Chris@16: bool parse_match_any(); Chris@16: bool parse_repeat(std::size_t low = 0, std::size_t high = (std::numeric_limits::max)()); Chris@16: bool parse_repeat_range(bool isbasic); Chris@16: bool parse_alt(); Chris@16: bool parse_set(); Chris@16: bool parse_backref(); Chris@16: void parse_set_literal(basic_char_set& char_set); Chris@16: bool parse_inner_set(basic_char_set& char_set); Chris@16: bool parse_QE(); Chris@16: bool parse_perl_extension(); Chris@16: bool add_emacs_code(bool negate); Chris@16: bool unwind_alts(std::ptrdiff_t last_paren_start); Chris@16: digraph get_next_set_literal(basic_char_set& char_set); Chris@16: charT unescape_character(); Chris@16: regex_constants::syntax_option_type parse_options(); Chris@16: Chris@16: private: Chris@16: typedef bool (basic_regex_parser::*parser_proc_type)(); Chris@16: typedef typename traits::string_type string_type; Chris@16: typedef typename traits::char_class_type char_class_type; Chris@16: parser_proc_type m_parser_proc; // the main parser to use Chris@16: const charT* m_base; // the start of the string being parsed Chris@16: const charT* m_end; // the end of the string being parsed Chris@16: const charT* m_position; // our current parser position Chris@16: unsigned m_mark_count; // how many sub-expressions we have Chris@16: int m_mark_reset; // used to indicate that we're inside a (?|...) block. Chris@16: unsigned m_max_mark; // largest mark count seen inside a (?|...) block. Chris@16: std::ptrdiff_t m_paren_start; // where the last seen ')' began (where repeats are inserted). Chris@16: std::ptrdiff_t m_alt_insert_point; // where to insert the next alternative Chris@16: bool m_has_case_change; // true if somewhere in the current block the case has changed Chris@16: #if defined(BOOST_MSVC) && defined(_M_IX86) Chris@16: // This is an ugly warning suppression workaround (for warnings *inside* std::vector Chris@16: // that can not otherwise be suppressed)... Chris@16: BOOST_STATIC_ASSERT(sizeof(long) >= sizeof(void*)); Chris@16: std::vector m_alt_jumps; // list of alternative in the current scope. Chris@16: #else Chris@16: std::vector m_alt_jumps; // list of alternative in the current scope. Chris@16: #endif Chris@16: Chris@16: basic_regex_parser& operator=(const basic_regex_parser&); Chris@16: basic_regex_parser(const basic_regex_parser&); Chris@16: }; Chris@16: Chris@16: template Chris@16: basic_regex_parser::basic_regex_parser(regex_data* data) Chris@16: : basic_regex_creator(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) Chris@16: { Chris@16: } Chris@16: Chris@16: template Chris@16: void basic_regex_parser::parse(const charT* p1, const charT* p2, unsigned l_flags) Chris@16: { Chris@16: // pass l_flags on to base class: Chris@16: this->init(l_flags); Chris@16: // set up pointers: Chris@16: m_position = m_base = p1; Chris@16: m_end = p2; Chris@16: // empty strings are errors: Chris@16: if((p1 == p2) && Chris@16: ( Chris@16: ((l_flags & regbase::main_option_type) != regbase::perl_syntax_group) Chris@16: || (l_flags & regbase::no_empty_expressions) Chris@16: ) Chris@16: ) Chris@16: { Chris@16: fail(regex_constants::error_empty, 0); Chris@16: return; Chris@16: } Chris@16: // select which parser to use: Chris@16: switch(l_flags & regbase::main_option_type) Chris@16: { Chris@16: case regbase::perl_syntax_group: Chris@16: { Chris@16: m_parser_proc = &basic_regex_parser::parse_extended; Chris@16: // Chris@16: // Add a leading paren with index zero to give recursions a target: Chris@16: // Chris@16: re_brace* br = static_cast(this->append_state(syntax_element_startmark, sizeof(re_brace))); Chris@16: br->index = 0; Chris@16: br->icase = this->flags() & regbase::icase; Chris@16: break; Chris@16: } Chris@16: case regbase::basic_syntax_group: Chris@16: m_parser_proc = &basic_regex_parser::parse_basic; Chris@16: break; Chris@16: case regbase::literal: Chris@16: m_parser_proc = &basic_regex_parser::parse_literal; Chris@16: break; Chris@16: default: Chris@16: // Ooops, someone has managed to set more than one of the main option flags, Chris@16: // so this must be an error: Chris@16: fail(regex_constants::error_unknown, 0, "An invalid combination of regular expression syntax flags was used."); Chris@16: return; Chris@16: } Chris@16: Chris@16: // parse all our characters: Chris@16: bool result = parse_all(); Chris@16: // Chris@16: // Unwind our alternatives: Chris@16: // Chris@16: unwind_alts(-1); Chris@16: // reset l_flags as a global scope (?imsx) may have altered them: Chris@16: this->flags(l_flags); Chris@16: // if we haven't gobbled up all the characters then we must Chris@16: // have had an unexpected ')' : Chris@16: if(!result) Chris@16: { Chris@16: fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_position), "Found a closing ) with no corresponding openening parenthesis."); Chris@16: return; Chris@16: } Chris@16: // if an error has been set then give up now: Chris@16: if(this->m_pdata->m_status) Chris@16: return; Chris@16: // fill in our sub-expression count: Chris@16: this->m_pdata->m_mark_count = 1 + m_mark_count; Chris@16: this->finalize(p1, p2); Chris@16: } Chris@16: Chris@16: template Chris@16: void basic_regex_parser::fail(regex_constants::error_type error_code, std::ptrdiff_t position) Chris@16: { Chris@16: // get the error message: Chris@16: std::string message = this->m_pdata->m_ptraits->error_string(error_code); Chris@16: fail(error_code, position, message); Chris@16: } Chris@16: Chris@16: template Chris@16: void basic_regex_parser::fail(regex_constants::error_type error_code, std::ptrdiff_t position, std::string message, std::ptrdiff_t start_pos) Chris@16: { Chris@16: if(0 == this->m_pdata->m_status) // update the error code if not already set Chris@16: this->m_pdata->m_status = error_code; Chris@16: m_position = m_end; // don't bother parsing anything else Chris@16: Chris@16: #ifndef BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS Chris@16: // Chris@16: // Augment error message with the regular expression text: Chris@16: // Chris@16: if(start_pos == position) Chris@16: start_pos = (std::max)(static_cast(0), position - static_cast(10)); Chris@16: std::ptrdiff_t end_pos = (std::min)(position + static_cast(10), static_cast(m_end - m_base)); Chris@16: if(error_code != regex_constants::error_empty) Chris@16: { Chris@16: if((start_pos != 0) || (end_pos != (m_end - m_base))) Chris@16: message += " The error occurred while parsing the regular expression fragment: '"; Chris@16: else Chris@16: message += " The error occurred while parsing the regular expression: '"; Chris@16: if(start_pos != end_pos) Chris@16: { Chris@16: message += std::string(m_base + start_pos, m_base + position); Chris@16: message += ">>>HERE>>>"; Chris@16: message += std::string(m_base + position, m_base + end_pos); Chris@16: } Chris@16: message += "'."; Chris@16: } Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_NO_EXCEPTIONS Chris@16: if(0 == (this->flags() & regex_constants::no_except)) Chris@16: { Chris@16: boost::regex_error e(message, error_code, position); Chris@16: e.raise(); Chris@16: } Chris@16: #else Chris@16: (void)position; // suppress warnings. Chris@16: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_all() Chris@16: { Chris@16: bool result = true; Chris@16: while(result && (m_position != m_end)) Chris@16: { Chris@16: result = (this->*m_parser_proc)(); Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4702) Chris@16: #endif Chris@16: template Chris@16: bool basic_regex_parser::parse_basic() Chris@16: { Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_escape: Chris@16: return parse_basic_escape(); Chris@16: case regex_constants::syntax_dot: Chris@16: return parse_match_any(); Chris@16: case regex_constants::syntax_caret: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_start_line); Chris@16: break; Chris@16: case regex_constants::syntax_dollar: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_end_line); Chris@16: break; Chris@16: case regex_constants::syntax_star: Chris@16: if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line)) Chris@16: return parse_literal(); Chris@16: else Chris@16: { Chris@16: ++m_position; Chris@16: return parse_repeat(); Chris@16: } Chris@16: case regex_constants::syntax_plus: Chris@16: if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex)) Chris@16: return parse_literal(); Chris@16: else Chris@16: { Chris@16: ++m_position; Chris@16: return parse_repeat(1); Chris@16: } Chris@16: case regex_constants::syntax_question: Chris@16: if(!(this->m_last_state) || (this->m_last_state->type == syntax_element_start_line) || !(this->flags() & regbase::emacs_ex)) Chris@16: return parse_literal(); Chris@16: else Chris@16: { Chris@16: ++m_position; Chris@16: return parse_repeat(0, 1); Chris@16: } Chris@16: case regex_constants::syntax_open_set: Chris@16: return parse_set(); Chris@16: case regex_constants::syntax_newline: Chris@16: if(this->flags() & regbase::newline_alt) Chris@16: return parse_alt(); Chris@16: else Chris@16: return parse_literal(); Chris@16: default: Chris@16: return parse_literal(); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_extended() Chris@16: { Chris@16: bool result = true; Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_open_mark: Chris@16: return parse_open_paren(); Chris@16: case regex_constants::syntax_close_mark: Chris@16: return false; Chris@16: case regex_constants::syntax_escape: Chris@16: return parse_extended_escape(); Chris@16: case regex_constants::syntax_dot: Chris@16: return parse_match_any(); Chris@16: case regex_constants::syntax_caret: Chris@16: ++m_position; Chris@16: this->append_state( Chris@16: (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_start : syntax_element_start_line)); Chris@16: break; Chris@16: case regex_constants::syntax_dollar: Chris@16: ++m_position; Chris@16: this->append_state( Chris@16: (this->flags() & regex_constants::no_mod_m ? syntax_element_buffer_end : syntax_element_end_line)); Chris@16: break; Chris@16: case regex_constants::syntax_star: Chris@16: if(m_position == this->m_base) Chris@16: { Chris@16: fail(regex_constants::error_badrepeat, 0, "The repeat operator \"*\" cannot start a regular expression."); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return parse_repeat(); Chris@16: case regex_constants::syntax_question: Chris@16: if(m_position == this->m_base) Chris@16: { Chris@16: fail(regex_constants::error_badrepeat, 0, "The repeat operator \"?\" cannot start a regular expression."); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return parse_repeat(0,1); Chris@16: case regex_constants::syntax_plus: Chris@16: if(m_position == this->m_base) Chris@16: { Chris@16: fail(regex_constants::error_badrepeat, 0, "The repeat operator \"+\" cannot start a regular expression."); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return parse_repeat(1); Chris@16: case regex_constants::syntax_open_brace: Chris@16: ++m_position; Chris@16: return parse_repeat_range(false); Chris@16: case regex_constants::syntax_close_brace: Chris@101: if((this->flags() & regbase::no_perl_ex) == regbase::no_perl_ex) Chris@101: { Chris@101: fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {."); Chris@101: return false; Chris@101: } Chris@101: result = parse_literal(); Chris@101: break; Chris@16: case regex_constants::syntax_or: Chris@16: return parse_alt(); Chris@16: case regex_constants::syntax_open_set: Chris@16: return parse_set(); Chris@16: case regex_constants::syntax_newline: Chris@16: if(this->flags() & regbase::newline_alt) Chris@16: return parse_alt(); Chris@16: else Chris@16: return parse_literal(); Chris@16: case regex_constants::syntax_hash: Chris@16: // Chris@16: // If we have a mod_x flag set, then skip until Chris@16: // we get to a newline character: Chris@16: // Chris@16: if((this->flags() Chris@16: & (regbase::no_perl_ex|regbase::mod_x)) Chris@16: == regbase::mod_x) Chris@16: { Chris@16: while((m_position != m_end) && !is_separator(*m_position++)){} Chris@16: return true; Chris@16: } Chris@16: BOOST_FALLTHROUGH; Chris@16: default: Chris@16: result = parse_literal(); Chris@16: break; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_literal() Chris@16: { Chris@16: // append this as a literal provided it's not a space character Chris@16: // or the perl option regbase::mod_x is not set: Chris@16: if( Chris@16: ((this->flags() Chris@16: & (regbase::main_option_type|regbase::mod_x|regbase::no_perl_ex)) Chris@16: != regbase::mod_x) Chris@16: || !this->m_traits.isctype(*m_position, this->m_mask_space)) Chris@16: this->append_literal(*m_position); Chris@16: ++m_position; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_open_paren() Chris@16: { Chris@16: // Chris@16: // skip the '(' and error check: Chris@16: // Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_paren, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // begin by checking for a perl-style (?...) extension: Chris@16: // Chris@16: if( Chris@16: ((this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) == 0) Chris@16: || ((this->flags() & (regbase::main_option_type | regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex)) Chris@16: ) Chris@16: { Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question) Chris@16: return parse_perl_extension(); Chris@16: } Chris@16: // Chris@16: // update our mark count, and append the required state: Chris@16: // Chris@16: unsigned markid = 0; Chris@16: if(0 == (this->flags() & regbase::nosubs)) Chris@16: { Chris@16: markid = ++m_mark_count; Chris@16: #ifndef BOOST_NO_STD_DISTANCE Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.push_back(std::pair(std::distance(m_base, m_position) - 1, 0)); Chris@16: #else Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.push_back(std::pair((m_position - m_base) - 1, 0)); Chris@16: #endif Chris@16: } Chris@16: re_brace* pb = static_cast(this->append_state(syntax_element_startmark, sizeof(re_brace))); Chris@16: pb->index = markid; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: std::ptrdiff_t last_paren_start = this->getoffset(pb); Chris@16: // back up insertion point for alternations, and set new point: Chris@16: std::ptrdiff_t last_alt_point = m_alt_insert_point; Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: // Chris@16: // back up the current flags in case we have a nested (?imsx) group: Chris@16: // Chris@16: regex_constants::syntax_option_type opts = this->flags(); Chris@16: bool old_case_change = m_has_case_change; Chris@16: m_has_case_change = false; // no changes to this scope as yet... Chris@16: // Chris@16: // Back up branch reset data in case we have a nested (?|...) Chris@16: // Chris@16: int mark_reset = m_mark_reset; Chris@16: m_mark_reset = -1; Chris@16: // Chris@16: // now recursively add more states, this will terminate when we get to a Chris@16: // matching ')' : Chris@16: // Chris@16: parse_all(); Chris@16: // Chris@16: // Unwind pushed alternatives: Chris@16: // Chris@16: if(0 == unwind_alts(last_paren_start)) Chris@16: return false; Chris@16: // Chris@16: // restore flags: Chris@16: // Chris@16: if(m_has_case_change) Chris@16: { Chris@16: // the case has changed in one or more of the alternatives Chris@16: // within the scoped (...) block: we have to add a state Chris@16: // to reset the case sensitivity: Chris@16: static_cast( Chris@16: this->append_state(syntax_element_toggle_case, sizeof(re_case)) Chris@16: )->icase = opts & regbase::icase; Chris@16: } Chris@16: this->flags(opts); Chris@16: m_has_case_change = old_case_change; Chris@16: // Chris@16: // restore branch reset: Chris@16: // Chris@16: m_mark_reset = mark_reset; Chris@16: // Chris@16: // we either have a ')' or we have run out of characters prematurely: Chris@16: // Chris@16: if(m_position == m_end) Chris@16: { Chris@16: this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end)); Chris@16: return false; Chris@16: } Chris@16: BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark); Chris@16: #ifndef BOOST_NO_STD_DISTANCE Chris@16: if(markid && (this->flags() & regbase::save_subexpression_location)) Chris@16: this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position); Chris@16: #else Chris@16: if(markid && (this->flags() & regbase::save_subexpression_location)) Chris@16: this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base); Chris@16: #endif Chris@16: ++m_position; Chris@16: // Chris@16: // append closing parenthesis state: Chris@16: // Chris@16: pb = static_cast(this->append_state(syntax_element_endmark, sizeof(re_brace))); Chris@16: pb->index = markid; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: this->m_paren_start = last_paren_start; Chris@16: // Chris@16: // restore the alternate insertion point: Chris@16: // Chris@16: this->m_alt_insert_point = last_alt_point; Chris@16: // Chris@16: // allow backrefs to this mark: Chris@16: // Chris@16: if((markid > 0) && (markid < sizeof(unsigned) * CHAR_BIT)) Chris@16: this->m_backrefs |= 1u << (markid - 1); Chris@16: Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_basic_escape() Chris@16: { Chris@16: ++m_position; Chris@16: bool result = true; Chris@16: switch(this->m_traits.escape_syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_open_mark: Chris@16: return parse_open_paren(); Chris@16: case regex_constants::syntax_close_mark: Chris@16: return false; Chris@16: case regex_constants::syntax_plus: Chris@16: if(this->flags() & regex_constants::bk_plus_qm) Chris@16: { Chris@16: ++m_position; Chris@16: return parse_repeat(1); Chris@16: } Chris@16: else Chris@16: return parse_literal(); Chris@16: case regex_constants::syntax_question: Chris@16: if(this->flags() & regex_constants::bk_plus_qm) Chris@16: { Chris@16: ++m_position; Chris@16: return parse_repeat(0, 1); Chris@16: } Chris@16: else Chris@16: return parse_literal(); Chris@16: case regex_constants::syntax_open_brace: Chris@16: if(this->flags() & regbase::no_intervals) Chris@16: return parse_literal(); Chris@16: ++m_position; Chris@16: return parse_repeat_range(true); Chris@16: case regex_constants::syntax_close_brace: Chris@16: if(this->flags() & regbase::no_intervals) Chris@16: return parse_literal(); Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, "Found a closing repetition operator } with no corresponding {."); Chris@16: return false; Chris@16: case regex_constants::syntax_or: Chris@16: if(this->flags() & regbase::bk_vbar) Chris@16: return parse_alt(); Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::syntax_digit: Chris@16: return parse_backref(); Chris@16: case regex_constants::escape_type_start_buffer: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_buffer_start); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::escape_type_end_buffer: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_buffer_end); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::escape_type_word_assert: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_boundary); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::escape_type_not_word_assert: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_within_word); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::escape_type_left_word: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_start); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: case regex_constants::escape_type_right_word: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_end); Chris@16: } Chris@16: else Chris@16: result = parse_literal(); Chris@16: break; Chris@16: default: Chris@16: if(this->flags() & regbase::emacs_ex) Chris@16: { Chris@16: bool negate = true; Chris@16: switch(*m_position) Chris@16: { Chris@16: case 'w': Chris@16: negate = false; Chris@16: BOOST_FALLTHROUGH; Chris@16: case 'W': Chris@16: { Chris@16: basic_char_set char_set; Chris@16: if(negate) Chris@16: char_set.negate(); Chris@16: char_set.add_class(this->m_word_mask); Chris@16: if(0 == this->append_set(char_set)) Chris@16: { Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return true; Chris@16: } Chris@16: case 's': Chris@16: negate = false; Chris@16: BOOST_FALLTHROUGH; Chris@16: case 'S': Chris@16: return add_emacs_code(negate); Chris@16: case 'c': Chris@16: case 'C': Chris@16: // not supported yet: Chris@16: 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."); Chris@16: return false; Chris@16: default: Chris@16: break; Chris@16: } Chris@16: } Chris@16: result = parse_literal(); Chris@16: break; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_extended_escape() Chris@16: { Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Incomplete escape sequence found."); Chris@16: return false; Chris@16: } Chris@16: bool negate = false; // in case this is a character class escape: \w \d etc Chris@16: switch(this->m_traits.escape_syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::escape_type_not_class: Chris@16: negate = true; Chris@16: BOOST_FALLTHROUGH; Chris@16: case regex_constants::escape_type_class: Chris@16: { Chris@16: escape_type_class_jump: Chris@16: typedef typename traits::char_class_type m_type; Chris@16: m_type m = this->m_traits.lookup_classname(m_position, m_position+1); Chris@16: if(m != 0) Chris@16: { Chris@16: basic_char_set char_set; Chris@16: if(negate) Chris@16: char_set.negate(); Chris@16: char_set.add_class(m); Chris@16: if(0 == this->append_set(char_set)) Chris@16: { Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return true; Chris@16: } Chris@16: // Chris@16: // not a class, just a regular unknown escape: Chris@16: // Chris@16: this->append_literal(unescape_character()); Chris@16: break; Chris@16: } Chris@16: case regex_constants::syntax_digit: Chris@16: return parse_backref(); Chris@16: case regex_constants::escape_type_left_word: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_start); Chris@16: break; Chris@16: case regex_constants::escape_type_right_word: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_end); Chris@16: break; Chris@16: case regex_constants::escape_type_start_buffer: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_buffer_start); Chris@16: break; Chris@16: case regex_constants::escape_type_end_buffer: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_buffer_end); Chris@16: break; Chris@16: case regex_constants::escape_type_word_assert: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_boundary); Chris@16: break; Chris@16: case regex_constants::escape_type_not_word_assert: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_within_word); Chris@16: break; Chris@16: case regex_constants::escape_type_Z: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_soft_buffer_end); Chris@16: break; Chris@16: case regex_constants::escape_type_Q: Chris@16: return parse_QE(); Chris@16: case regex_constants::escape_type_C: Chris@16: return parse_match_any(); Chris@16: case regex_constants::escape_type_X: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_combining); Chris@16: break; Chris@16: case regex_constants::escape_type_G: Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_restart_continue); Chris@16: break; Chris@16: case regex_constants::escape_type_not_property: Chris@16: negate = true; Chris@16: BOOST_FALLTHROUGH; Chris@16: case regex_constants::escape_type_property: Chris@16: { Chris@16: ++m_position; Chris@16: char_class_type m; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Incomplete property escape found."); Chris@16: return false; Chris@16: } Chris@16: // maybe have \p{ddd} Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace) Chris@16: { Chris@16: const charT* base = m_position; Chris@16: // skip forward until we find enclosing brace: Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Closing } missing from property escape sequence."); Chris@16: return false; Chris@16: } Chris@16: m = this->m_traits.lookup_classname(++base, m_position++); Chris@16: } Chris@16: else Chris@16: { Chris@16: m = this->m_traits.lookup_classname(m_position, m_position+1); Chris@16: ++m_position; Chris@16: } Chris@16: if(m != 0) Chris@16: { Chris@16: basic_char_set char_set; Chris@16: if(negate) Chris@16: char_set.negate(); Chris@16: char_set.add_class(m); Chris@16: if(0 == this->append_set(char_set)) Chris@16: { Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: fail(regex_constants::error_ctype, m_position - m_base, "Escape sequence was neither a valid property nor a valid character class name."); Chris@16: return false; Chris@16: } Chris@16: case regex_constants::escape_type_reset_start_mark: Chris@16: if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex))) Chris@16: { Chris@16: re_brace* pb = static_cast(this->append_state(syntax_element_startmark, sizeof(re_brace))); Chris@16: pb->index = -5; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: this->m_pdata->m_data.align(); Chris@16: ++m_position; Chris@16: return true; Chris@16: } Chris@16: goto escape_type_class_jump; Chris@16: case regex_constants::escape_type_line_ending: Chris@16: if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex))) Chris@16: { Chris@16: const charT* e = get_escape_R_string(); Chris@16: const charT* old_position = m_position; Chris@16: const charT* old_end = m_end; Chris@16: const charT* old_base = m_base; Chris@16: m_position = e; Chris@16: m_base = e; Chris@16: m_end = e + traits::length(e); Chris@16: bool r = parse_all(); Chris@16: m_position = ++old_position; Chris@16: m_end = old_end; Chris@16: m_base = old_base; Chris@16: return r; Chris@16: } Chris@16: goto escape_type_class_jump; Chris@16: case regex_constants::escape_type_extended_backref: Chris@16: if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex))) Chris@16: { Chris@16: bool have_brace = false; Chris@16: bool negative = false; Chris@16: static const char* incomplete_message = "Incomplete \\g escape found."; Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // maybe have \g{ddd} Chris@16: regex_constants::syntax_type syn = this->m_traits.syntax_type(*m_position); Chris@16: regex_constants::syntax_type syn_end = 0; Chris@16: if((syn == regex_constants::syntax_open_brace) Chris@16: || (syn == regex_constants::escape_type_left_word) Chris@16: || (syn == regex_constants::escape_type_end_buffer)) Chris@16: { Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: have_brace = true; Chris@16: switch(syn) Chris@16: { Chris@16: case regex_constants::syntax_open_brace: Chris@16: syn_end = regex_constants::syntax_close_brace; Chris@16: break; Chris@16: case regex_constants::escape_type_left_word: Chris@16: syn_end = regex_constants::escape_type_right_word; Chris@16: break; Chris@16: default: Chris@16: syn_end = regex_constants::escape_type_end_buffer; Chris@16: break; Chris@16: } Chris@16: } Chris@16: negative = (*m_position == static_cast('-')); Chris@16: if((negative) && (++m_position == m_end)) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: const charT* pc = m_position; Chris@16: int i = this->m_traits.toi(pc, m_end, 10); Chris@16: if((i < 0) && syn_end) Chris@16: { Chris@16: // Check for a named capture, get the leftmost one if there is more than one: Chris@16: const charT* base = m_position; Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != syn_end)) Chris@16: { Chris@16: ++m_position; Chris@16: } Chris@16: i = hash_value_from_capture_name(base, m_position); Chris@16: pc = m_position; Chris@16: } Chris@16: if(negative) Chris@16: i = 1 + m_mark_count - i; Chris@16: 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))))) Chris@16: { Chris@16: m_position = pc; Chris@16: re_brace* pb = static_cast(this->append_state(syntax_element_backref, sizeof(re_brace))); Chris@16: pb->index = i; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: } Chris@16: else Chris@16: { Chris@16: fail(regex_constants::error_backref, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: m_position = pc; Chris@16: if(have_brace) Chris@16: { Chris@16: if((m_position == m_end) || (this->m_traits.syntax_type(*m_position) != syn_end)) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: goto escape_type_class_jump; Chris@16: case regex_constants::escape_type_control_v: Chris@16: if(0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex))) Chris@16: goto escape_type_class_jump; Chris@16: BOOST_FALLTHROUGH; Chris@16: default: Chris@16: this->append_literal(unescape_character()); Chris@16: break; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_match_any() Chris@16: { Chris@16: // Chris@16: // we have a '.' that can match any character: Chris@16: // Chris@16: ++m_position; Chris@16: static_cast( Chris@16: this->append_state(syntax_element_wild, sizeof(re_dot)) Chris@16: )->mask = static_cast(this->flags() & regbase::no_mod_s Chris@16: ? re_detail::force_not_newline Chris@16: : this->flags() & regbase::mod_s ? Chris@16: re_detail::force_newline : re_detail::dont_care); Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_repeat(std::size_t low, std::size_t high) Chris@16: { Chris@16: bool greedy = true; Chris@16: bool pocessive = false; Chris@16: std::size_t insert_point; Chris@16: // Chris@16: // when we get to here we may have a non-greedy ? mark still to come: Chris@16: // Chris@16: if((m_position != m_end) Chris@16: && ( Chris@16: (0 == (this->flags() & (regbase::main_option_type | regbase::no_perl_ex))) Chris@16: || ((regbase::basic_syntax_group|regbase::emacs_ex) == (this->flags() & (regbase::main_option_type | regbase::emacs_ex))) Chris@16: ) Chris@16: ) Chris@16: { Chris@16: // OK we have a perl or emacs regex, check for a '?': Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_question) Chris@16: { Chris@16: greedy = false; Chris@16: ++m_position; Chris@16: } Chris@16: // for perl regexes only check for pocessive ++ repeats. Chris@16: if((m_position != m_end) Chris@16: && (0 == (this->flags() & regbase::main_option_type)) Chris@16: && (this->m_traits.syntax_type(*m_position) == regex_constants::syntax_plus)) Chris@16: { Chris@16: pocessive = true; Chris@16: ++m_position; Chris@16: } Chris@16: } Chris@16: if(0 == this->m_last_state) Chris@16: { Chris@16: fail(regex_constants::error_badrepeat, ::boost::re_detail::distance(m_base, m_position), "Nothing to repeat."); Chris@16: return false; Chris@16: } Chris@16: if(this->m_last_state->type == syntax_element_endmark) Chris@16: { Chris@16: // insert a repeat before the '(' matching the last ')': Chris@16: insert_point = this->m_paren_start; Chris@16: } Chris@16: else if((this->m_last_state->type == syntax_element_literal) && (static_cast(this->m_last_state)->length > 1)) Chris@16: { Chris@16: // the last state was a literal with more than one character, split it in two: Chris@16: re_literal* lit = static_cast(this->m_last_state); Chris@16: charT c = (static_cast(static_cast(lit+1)))[lit->length - 1]; Chris@101: lit->length -= 1; Chris@16: // now append new state: Chris@16: lit = static_cast(this->append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT))); Chris@16: lit->length = 1; Chris@16: (static_cast(static_cast(lit+1)))[0] = c; Chris@16: insert_point = this->getoffset(this->m_last_state); Chris@16: } Chris@16: else Chris@16: { Chris@16: // repeat the last state whatever it was, need to add some error checking here: Chris@16: switch(this->m_last_state->type) Chris@16: { Chris@16: case syntax_element_start_line: Chris@16: case syntax_element_end_line: Chris@16: case syntax_element_word_boundary: Chris@16: case syntax_element_within_word: Chris@16: case syntax_element_word_start: Chris@16: case syntax_element_word_end: Chris@16: case syntax_element_buffer_start: Chris@16: case syntax_element_buffer_end: Chris@16: case syntax_element_alt: Chris@16: case syntax_element_soft_buffer_end: Chris@16: case syntax_element_restart_continue: Chris@16: case syntax_element_jump: Chris@16: case syntax_element_startmark: Chris@16: case syntax_element_backstep: Chris@16: // can't legally repeat any of the above: Chris@16: fail(regex_constants::error_badrepeat, m_position - m_base); Chris@16: return false; Chris@16: default: Chris@16: // do nothing... Chris@16: break; Chris@16: } Chris@16: insert_point = this->getoffset(this->m_last_state); Chris@16: } Chris@16: // Chris@16: // OK we now know what to repeat, so insert the repeat around it: Chris@16: // Chris@16: re_repeat* rep = static_cast(this->insert_state(insert_point, syntax_element_rep, re_repeater_size)); Chris@16: rep->min = low; Chris@16: rep->max = high; Chris@16: rep->greedy = greedy; Chris@16: rep->leading = false; Chris@16: // store our repeater position for later: Chris@16: std::ptrdiff_t rep_off = this->getoffset(rep); Chris@16: // and append a back jump to the repeat: Chris@16: re_jump* jmp = static_cast(this->append_state(syntax_element_jump, sizeof(re_jump))); Chris@16: jmp->alt.i = rep_off - this->getoffset(jmp); Chris@16: this->m_pdata->m_data.align(); Chris@16: // now fill in the alt jump for the repeat: Chris@16: rep = static_cast(this->getaddress(rep_off)); Chris@16: rep->alt.i = this->m_pdata->m_data.size() - rep_off; Chris@16: // Chris@16: // If the repeat is pocessive then bracket the repeat with a (?>...) Chris@16: // independent sub-expression construct: Chris@16: // Chris@16: if(pocessive) Chris@16: { Chris@16: if(m_position != m_end) Chris@16: { Chris@16: // Chris@16: // Check for illegal following quantifier, we have to do this here, because Chris@16: // the extra states we insert below circumvents our usual error checking :-( Chris@16: // Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_star: Chris@16: case regex_constants::syntax_plus: Chris@16: case regex_constants::syntax_question: Chris@16: case regex_constants::syntax_open_brace: Chris@16: fail(regex_constants::error_badrepeat, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: re_brace* pb = static_cast(this->insert_state(insert_point, syntax_element_startmark, sizeof(re_brace))); Chris@16: pb->index = -3; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: jmp = static_cast(this->insert_state(insert_point + sizeof(re_brace), syntax_element_jump, sizeof(re_jump))); Chris@16: this->m_pdata->m_data.align(); Chris@16: jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp); Chris@16: pb = static_cast(this->append_state(syntax_element_endmark, sizeof(re_brace))); Chris@16: pb->index = -3; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_repeat_range(bool isbasic) Chris@16: { Chris@16: static const char* incomplete_message = "Missing } in quantified repetition."; Chris@16: // Chris@16: // parse a repeat-range: Chris@16: // Chris@16: std::size_t min, max; Chris@16: int v; Chris@16: // skip whitespace: Chris@16: while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space)) Chris@16: ++m_position; Chris@16: if(this->m_position == this->m_end) Chris@16: { Chris@16: if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: // get min: Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: // skip whitespace: Chris@16: if(v < 0) Chris@16: { Chris@16: if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space)) Chris@16: ++m_position; Chris@16: if(this->m_position == this->m_end) Chris@16: { Chris@16: if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: min = v; Chris@16: // see if we have a comma: Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_comma) Chris@16: { Chris@16: // move on and error check: Chris@16: ++m_position; Chris@16: // skip whitespace: Chris@16: while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space)) Chris@16: ++m_position; Chris@16: if(this->m_position == this->m_end) Chris@16: { Chris@16: if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: // get the value if any: Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: max = (v >= 0) ? (std::size_t)v : (std::numeric_limits::max)(); Chris@16: } Chris@16: else Chris@16: { Chris@16: // no comma, max = min: Chris@16: max = min; Chris@16: } Chris@16: // skip whitespace: Chris@16: while((m_position != m_end) && this->m_traits.isctype(*m_position, this->m_mask_space)) Chris@16: ++m_position; Chris@16: // OK now check trailing }: Chris@16: if(this->m_position == this->m_end) Chris@16: { Chris@16: if(this->flags() & (regbase::main_option_type | regbase::no_perl_ex)) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: if(isbasic) Chris@16: { Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_escape) Chris@16: { Chris@16: ++m_position; Chris@16: if(this->m_position == this->m_end) Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: fail(regex_constants::error_brace, this->m_position - this->m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_brace) Chris@16: ++m_position; Chris@16: else Chris@16: { Chris@16: // Treat the opening '{' as a literal character, rewind to start of error: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_brace) --m_position; Chris@16: return parse_literal(); Chris@16: } Chris@16: // Chris@16: // finally go and add the repeat, unless error: Chris@16: // Chris@16: if(min > max) Chris@16: { Chris@16: // Backtrack to error location: Chris@16: m_position -= 2; Chris@16: while(this->m_traits.isctype(*m_position, this->m_word_mask)) --m_position; Chris@16: ++m_position; Chris@16: fail(regex_constants::error_badbrace, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: return parse_repeat(min, max); Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_alt() Chris@16: { Chris@16: // Chris@16: // error check: if there have been no previous states, Chris@16: // or if the last state was a '(' then error: Chris@16: // Chris@16: if( Chris@16: ((this->m_last_state == 0) || (this->m_last_state->type == syntax_element_startmark)) Chris@16: && Chris@16: !( Chris@16: ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) Chris@16: && Chris@16: ((this->flags() & regbase::no_empty_expressions) == 0) Chris@16: ) Chris@16: ) Chris@16: { Chris@101: fail(regex_constants::error_empty, this->m_position - this->m_base, "A regular expression cannot start with the alternation operator |."); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // Reset mark count if required: Chris@16: // Chris@16: if(m_max_mark < m_mark_count) Chris@16: m_max_mark = m_mark_count; Chris@16: if(m_mark_reset >= 0) Chris@16: m_mark_count = m_mark_reset; Chris@16: Chris@16: ++m_position; Chris@16: // Chris@16: // we need to append a trailing jump: Chris@16: // Chris@16: re_syntax_base* pj = this->append_state(re_detail::syntax_element_jump, sizeof(re_jump)); Chris@16: std::ptrdiff_t jump_offset = this->getoffset(pj); Chris@16: // Chris@16: // now insert the alternative: Chris@16: // Chris@16: re_alt* palt = static_cast(this->insert_state(this->m_alt_insert_point, syntax_element_alt, re_alt_size)); Chris@16: jump_offset += re_alt_size; Chris@16: this->m_pdata->m_data.align(); Chris@16: palt->alt.i = this->m_pdata->m_data.size() - this->getoffset(palt); Chris@16: // Chris@16: // update m_alt_insert_point so that the next alternate gets Chris@16: // inserted at the start of the second of the two we've just created: Chris@16: // Chris@16: this->m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: // Chris@16: // the start of this alternative must have a case changes state Chris@16: // if the current block has messed around with case changes: Chris@16: // Chris@16: if(m_has_case_change) Chris@16: { Chris@16: static_cast( Chris@16: this->append_state(syntax_element_toggle_case, sizeof(re_case)) Chris@16: )->icase = this->m_icase; Chris@16: } Chris@16: // Chris@16: // push the alternative onto our stack, a recursive Chris@16: // implementation here is easier to understand (and faster Chris@16: // as it happens), but causes all kinds of stack overflow problems Chris@16: // on programs with small stacks (COM+). Chris@16: // Chris@16: m_alt_jumps.push_back(jump_offset); Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_set() Chris@16: { Chris@16: static const char* incomplete_message = "Character set declaration starting with [ terminated prematurely - either no ] was found or the set had no content."; Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: basic_char_set char_set; Chris@16: Chris@16: const charT* base = m_position; // where the '[' was Chris@16: const charT* item_base = m_position; // where the '[' or '^' was Chris@16: Chris@16: while(m_position != m_end) Chris@16: { Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_caret: Chris@16: if(m_position == base) Chris@16: { Chris@16: char_set.negate(); Chris@16: ++m_position; Chris@16: item_base = m_position; Chris@16: } Chris@16: else Chris@16: parse_set_literal(char_set); Chris@16: break; Chris@16: case regex_constants::syntax_close_set: Chris@16: if(m_position == item_base) Chris@16: { Chris@16: parse_set_literal(char_set); Chris@16: break; Chris@16: } Chris@16: else Chris@16: { Chris@16: ++m_position; Chris@16: if(0 == this->append_set(char_set)) Chris@16: { Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: return true; Chris@16: case regex_constants::syntax_open_set: Chris@16: if(parse_inner_set(char_set)) Chris@16: break; Chris@16: return true; Chris@16: case regex_constants::syntax_escape: Chris@16: { Chris@16: // Chris@16: // look ahead and see if this is a character class shortcut Chris@16: // \d \w \s etc... Chris@16: // Chris@16: ++m_position; Chris@16: if(this->m_traits.escape_syntax_type(*m_position) Chris@16: == regex_constants::escape_type_class) Chris@16: { Chris@16: char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1); Chris@16: if(m != 0) Chris@16: { Chris@16: char_set.add_class(m); Chris@16: ++m_position; Chris@16: break; Chris@16: } Chris@16: } Chris@16: else if(this->m_traits.escape_syntax_type(*m_position) Chris@16: == regex_constants::escape_type_not_class) Chris@16: { Chris@16: // negated character class: Chris@16: char_class_type m = this->m_traits.lookup_classname(m_position, m_position+1); Chris@16: if(m != 0) Chris@16: { Chris@16: char_set.add_negated_class(m); Chris@16: ++m_position; Chris@16: break; Chris@16: } Chris@16: } Chris@16: // not a character class, just a regular escape: Chris@16: --m_position; Chris@16: parse_set_literal(char_set); Chris@16: break; Chris@16: } Chris@16: default: Chris@16: parse_set_literal(char_set); Chris@16: break; Chris@16: } Chris@16: } Chris@16: return m_position != m_end; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_inner_set(basic_char_set& char_set) Chris@16: { Chris@16: static const char* incomplete_message = "Character class declaration starting with [ terminated prematurely - either no ] was found or the set had no content."; Chris@16: // Chris@16: // we have either a character class [:name:] Chris@16: // a collating element [.name.] Chris@16: // or an equivalence class [=name=] Chris@16: // Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_dot: Chris@16: // Chris@16: // a collating element is treated as a literal: Chris@16: // Chris@16: --m_position; Chris@16: parse_set_literal(char_set); Chris@16: return true; Chris@16: case regex_constants::syntax_colon: Chris@16: { Chris@16: // check that character classes are actually enabled: Chris@16: if((this->flags() & (regbase::main_option_type | regbase::no_char_classes)) Chris@16: == (regbase::basic_syntax_group | regbase::no_char_classes)) Chris@16: { Chris@16: --m_position; Chris@16: parse_set_literal(char_set); Chris@16: return true; Chris@16: } Chris@16: // skip the ':' Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: const charT* name_first = m_position; Chris@16: // skip at least one character, then find the matching ':]' Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: while((m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_colon)) Chris@16: ++m_position; Chris@16: const charT* name_last = m_position; Chris@16: if(m_end == m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: if((m_end == ++m_position) Chris@16: || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // check for negated class: Chris@16: // Chris@16: bool negated = false; Chris@16: if(this->m_traits.syntax_type(*name_first) == regex_constants::syntax_caret) Chris@16: { Chris@16: ++name_first; Chris@16: negated = true; Chris@16: } Chris@16: typedef typename traits::char_class_type m_type; Chris@16: m_type m = this->m_traits.lookup_classname(name_first, name_last); Chris@16: if(m == 0) Chris@16: { Chris@16: if(char_set.empty() && (name_last - name_first == 1)) Chris@16: { Chris@16: // maybe a special case: Chris@16: ++m_position; Chris@16: if( (m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position) Chris@16: == regex_constants::syntax_close_set)) Chris@16: { Chris@16: if(this->m_traits.escape_syntax_type(*name_first) Chris@16: == regex_constants::escape_type_left_word) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_start); Chris@16: return false; Chris@16: } Chris@16: if(this->m_traits.escape_syntax_type(*name_first) Chris@16: == regex_constants::escape_type_right_word) Chris@16: { Chris@16: ++m_position; Chris@16: this->append_state(syntax_element_word_end); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: } Chris@16: fail(regex_constants::error_ctype, name_first - m_base); Chris@16: return false; Chris@16: } Chris@16: if(negated == false) Chris@16: char_set.add_class(m); Chris@16: else Chris@16: char_set.add_negated_class(m); Chris@16: ++m_position; Chris@16: break; Chris@16: } Chris@16: case regex_constants::syntax_equal: Chris@16: { Chris@16: // skip the '=' Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: const charT* name_first = m_position; Chris@16: // skip at least one character, then find the matching '=]' Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: while((m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal)) Chris@16: ++m_position; Chris@16: const charT* name_last = m_position; Chris@16: if(m_end == m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: if((m_end == ++m_position) Chris@16: || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base, incomplete_message); Chris@16: return false; Chris@16: } Chris@16: string_type m = this->m_traits.lookup_collatename(name_first, name_last); Chris@16: if((0 == m.size()) || (m.size() > 2)) Chris@16: { Chris@16: fail(regex_constants::error_collate, name_first - m_base); Chris@16: return false; Chris@16: } Chris@16: digraph d; Chris@16: d.first = m[0]; Chris@16: if(m.size() > 1) Chris@16: d.second = m[1]; Chris@16: else Chris@16: d.second = 0; Chris@16: char_set.add_equivalent(d); Chris@16: ++m_position; Chris@16: break; Chris@16: } Chris@16: default: Chris@16: --m_position; Chris@16: parse_set_literal(char_set); Chris@16: break; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: void basic_regex_parser::parse_set_literal(basic_char_set& char_set) Chris@16: { Chris@16: digraph start_range(get_next_set_literal(char_set)); Chris@16: if(m_end == m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base); Chris@16: return; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash) Chris@16: { Chris@16: // we have a range: Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base); Chris@16: return; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set) Chris@16: { Chris@16: digraph end_range = get_next_set_literal(char_set); Chris@16: char_set.add_range(start_range, end_range); Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_dash) Chris@16: { Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_brack, m_position - m_base); Chris@16: return; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_set) Chris@16: { Chris@16: // trailing - : Chris@16: --m_position; Chris@16: return; Chris@16: } Chris@16: fail(regex_constants::error_range, m_position - m_base); Chris@16: return; Chris@16: } Chris@16: return; Chris@16: } Chris@16: --m_position; Chris@16: } Chris@16: char_set.add_single(start_range); Chris@16: } Chris@16: Chris@16: template Chris@16: digraph basic_regex_parser::get_next_set_literal(basic_char_set& char_set) Chris@16: { Chris@16: digraph result; Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_dash: Chris@16: if(!char_set.empty()) Chris@16: { Chris@16: // see if we are at the end of the set: Chris@16: if((++m_position == m_end) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)) Chris@16: { Chris@16: fail(regex_constants::error_range, m_position - m_base); Chris@16: return result; Chris@16: } Chris@16: --m_position; Chris@16: } Chris@16: result.first = *m_position++; Chris@16: return result; Chris@16: case regex_constants::syntax_escape: Chris@16: // check to see if escapes are supported first: Chris@16: if(this->flags() & regex_constants::no_escape_in_lists) Chris@16: { Chris@16: result = *m_position++; Chris@16: break; Chris@16: } Chris@16: ++m_position; Chris@16: result = unescape_character(); Chris@16: break; Chris@16: case regex_constants::syntax_open_set: Chris@16: { Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_collate, m_position - m_base); Chris@16: return result; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot) Chris@16: { Chris@16: --m_position; Chris@16: result.first = *m_position; Chris@16: ++m_position; Chris@16: return result; Chris@16: } Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_collate, m_position - m_base); Chris@16: return result; Chris@16: } Chris@16: const charT* name_first = m_position; Chris@16: // skip at least one character, then find the matching ':]' Chris@16: if(m_end == ++m_position) Chris@16: { Chris@16: fail(regex_constants::error_collate, name_first - m_base); Chris@16: return result; Chris@16: } Chris@16: while((m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_dot)) Chris@16: ++m_position; Chris@16: const charT* name_last = m_position; Chris@16: if(m_end == m_position) Chris@16: { Chris@16: fail(regex_constants::error_collate, name_first - m_base); Chris@16: return result; Chris@16: } Chris@16: if((m_end == ++m_position) Chris@16: || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_set)) Chris@16: { Chris@16: fail(regex_constants::error_collate, name_first - m_base); Chris@16: return result; Chris@16: } Chris@16: ++m_position; Chris@16: string_type s = this->m_traits.lookup_collatename(name_first, name_last); Chris@16: if(s.empty() || (s.size() > 2)) Chris@16: { Chris@16: fail(regex_constants::error_collate, name_first - m_base); Chris@16: return result; Chris@16: } Chris@16: result.first = s[0]; Chris@16: if(s.size() > 1) Chris@16: result.second = s[1]; Chris@16: else Chris@16: result.second = 0; Chris@16: return result; Chris@16: } Chris@16: default: Chris@16: result = *m_position++; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: // Chris@16: // does a value fit in the specified charT type? Chris@16: // Chris@16: template Chris@16: bool valid_value(charT, int v, const mpl::true_&) Chris@16: { Chris@16: return (v >> (sizeof(charT) * CHAR_BIT)) == 0; Chris@16: } Chris@16: template Chris@16: bool valid_value(charT, int, const mpl::false_&) Chris@16: { Chris@16: return true; // v will alsways fit in a charT Chris@16: } Chris@16: template Chris@16: bool valid_value(charT c, int v) Chris@16: { Chris@16: return valid_value(c, v, mpl::bool_<(sizeof(charT) < sizeof(int))>()); Chris@16: } Chris@16: Chris@16: template Chris@16: charT basic_regex_parser::unescape_character() Chris@16: { Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4127) Chris@16: #endif Chris@16: charT result(0); Chris@16: if(m_position == m_end) Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Escape sequence terminated prematurely."); Chris@16: return false; Chris@16: } Chris@16: switch(this->m_traits.escape_syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::escape_type_control_a: Chris@16: result = charT('\a'); Chris@16: break; Chris@16: case regex_constants::escape_type_e: Chris@16: result = charT(27); Chris@16: break; Chris@16: case regex_constants::escape_type_control_f: Chris@16: result = charT('\f'); Chris@16: break; Chris@16: case regex_constants::escape_type_control_n: Chris@16: result = charT('\n'); Chris@16: break; Chris@16: case regex_constants::escape_type_control_r: Chris@16: result = charT('\r'); Chris@16: break; Chris@16: case regex_constants::escape_type_control_t: Chris@16: result = charT('\t'); Chris@16: break; Chris@16: case regex_constants::escape_type_control_v: Chris@16: result = charT('\v'); Chris@16: break; Chris@16: case regex_constants::escape_type_word_assert: Chris@16: result = charT('\b'); Chris@16: break; Chris@16: case regex_constants::escape_type_ascii_control: Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base, "ASCII escape sequence terminated prematurely."); Chris@16: return result; Chris@16: } Chris@16: result = static_cast(*m_position % 32); Chris@16: break; Chris@16: case regex_constants::escape_type_hex: Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Hexadecimal escape sequence terminated prematurely."); Chris@16: return result; Chris@16: } Chris@16: // maybe have \x{ddd} Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace) Chris@16: { Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Missing } in hexadecimal escape sequence."); Chris@16: return result; Chris@16: } Chris@16: int i = this->m_traits.toi(m_position, m_end, 16); Chris@16: if((m_position == m_end) Chris@16: || (i < 0) Chris@16: || ((std::numeric_limits::is_specialized) && (i > (int)(std::numeric_limits::max)())) Chris@16: || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace)) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_badbrace, m_position - m_base, "Hexadecimal escape sequence was invalid."); Chris@16: return result; Chris@16: } Chris@16: ++m_position; Chris@16: result = charT(i); Chris@16: } Chris@16: else Chris@16: { Chris@16: std::ptrdiff_t len = (std::min)(static_cast(2), static_cast(m_end - m_position)); Chris@16: int i = this->m_traits.toi(m_position, m_position + len, 16); Chris@16: if((i < 0) Chris@16: || !valid_value(charT(0), i)) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Escape sequence did not encode a valid character."); Chris@16: return result; Chris@16: } Chris@16: result = charT(i); Chris@16: } Chris@16: return result; Chris@16: case regex_constants::syntax_digit: Chris@16: { Chris@16: // an octal escape sequence, the first character must be a zero Chris@16: // followed by up to 3 octal digits: Chris@16: std::ptrdiff_t len = (std::min)(::boost::re_detail::distance(m_position, m_end), static_cast(4)); Chris@16: const charT* bp = m_position; Chris@16: int val = this->m_traits.toi(bp, bp + 1, 8); Chris@16: if(val != 0) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: // Oops not an octal escape after all: Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Invalid octal escape sequence."); Chris@16: return result; Chris@16: } Chris@16: val = this->m_traits.toi(m_position, m_position + len, 8); Chris@16: if(val < 0) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Octal escape sequence is invalid."); Chris@16: return result; Chris@16: } Chris@16: return static_cast(val); Chris@16: } Chris@16: case regex_constants::escape_type_named_char: Chris@16: { Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: // maybe have \N{name} Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_open_brace) Chris@16: { Chris@16: const charT* base = m_position; Chris@16: // skip forward until we find enclosing brace: Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_brace)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: string_type s = this->m_traits.lookup_collatename(++base, m_position++); Chris@16: if(s.empty()) Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_collate, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(s.size() == 1) Chris@16: { Chris@16: return s[0]; Chris@16: } Chris@16: } Chris@16: // fall through is a failure: Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: default: Chris@16: result = *m_position; Chris@16: break; Chris@16: } Chris@16: ++m_position; Chris@16: return result; Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_backref() Chris@16: { Chris@16: BOOST_ASSERT(m_position != m_end); Chris@16: const charT* pc = m_position; Chris@16: int i = this->m_traits.toi(pc, pc + 1, 10); Chris@16: if((i == 0) || (((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) && (this->flags() & regbase::no_bk_refs))) Chris@16: { Chris@16: // not a backref at all but an octal escape sequence: Chris@16: charT c = unescape_character(); Chris@16: this->append_literal(c); Chris@16: } Chris@16: else if((i > 0) && (this->m_backrefs & (1u << (i-1)))) Chris@16: { Chris@16: m_position = pc; Chris@16: re_brace* pb = static_cast(this->append_state(syntax_element_backref, sizeof(re_brace))); Chris@16: pb->index = i; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Rewind to start of escape: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_backref, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_QE() Chris@16: { Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4127) Chris@16: #endif Chris@16: // Chris@16: // parse a \Q...\E sequence: Chris@16: // Chris@16: ++m_position; // skip the Q Chris@16: const charT* start = m_position; Chris@16: const charT* end; Chris@16: do Chris@16: { Chris@16: while((m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // a \Q...\E sequence may terminate with the end of the expression: Chris@16: end = m_position; Chris@16: break; Chris@16: } Chris@16: if(++m_position == m_end) // skip the escape Chris@16: { Chris@16: fail(regex_constants::error_escape, m_position - m_base, "Unterminated \\Q...\\E sequence."); Chris@16: return false; Chris@16: } Chris@16: // check to see if it's a \E: Chris@16: if(this->m_traits.escape_syntax_type(*m_position) == regex_constants::escape_type_E) Chris@16: { Chris@16: ++m_position; Chris@16: end = m_position - 2; Chris@16: break; Chris@16: } Chris@16: // otherwise go round again: Chris@16: }while(true); Chris@16: // Chris@16: // now add all the character between the two escapes as literals: Chris@16: // Chris@16: while(start != end) Chris@16: { Chris@16: this->append_literal(*start); Chris@16: ++start; Chris@16: } Chris@16: return true; Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::parse_perl_extension() Chris@16: { Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // treat comments as a special case, as these Chris@16: // are the only ones that don't start with a leading Chris@16: // startmark state: Chris@16: // Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_hash) Chris@16: { Chris@16: while((m_position != m_end) Chris@16: && (this->m_traits.syntax_type(*m_position++) != regex_constants::syntax_close_mark)) Chris@16: {} Chris@16: return true; Chris@16: } Chris@16: // Chris@16: // backup some state, and prepare the way: Chris@16: // Chris@16: int markid = 0; Chris@16: std::ptrdiff_t jump_offset = 0; Chris@16: re_brace* pb = static_cast(this->append_state(syntax_element_startmark, sizeof(re_brace))); Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: std::ptrdiff_t last_paren_start = this->getoffset(pb); Chris@16: // back up insertion point for alternations, and set new point: Chris@16: std::ptrdiff_t last_alt_point = m_alt_insert_point; Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: std::ptrdiff_t expected_alt_point = m_alt_insert_point; Chris@16: bool restore_flags = true; Chris@16: regex_constants::syntax_option_type old_flags = this->flags(); Chris@16: bool old_case_change = m_has_case_change; Chris@16: m_has_case_change = false; Chris@16: charT name_delim; Chris@16: int mark_reset = m_mark_reset; Chris@16: int max_mark = m_max_mark; Chris@16: m_mark_reset = -1; Chris@16: m_max_mark = m_mark_count; Chris@16: int v; Chris@16: // Chris@16: // select the actual extension used: Chris@16: // Chris@16: switch(this->m_traits.syntax_type(*m_position)) Chris@16: { Chris@16: case regex_constants::syntax_or: Chris@16: m_mark_reset = m_mark_count; Chris@16: BOOST_FALLTHROUGH; Chris@16: case regex_constants::syntax_colon: Chris@16: // Chris@16: // a non-capturing mark: Chris@16: // Chris@16: pb->index = markid = 0; Chris@16: ++m_position; Chris@16: break; Chris@16: case regex_constants::syntax_digit: Chris@16: { Chris@16: // Chris@16: // a recursive subexpression: Chris@16: // Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: if((v < 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "The recursive sub-expression refers to an invalid marking group, or is unterminated."); Chris@16: return false; Chris@16: } Chris@16: insert_recursion: Chris@16: pb->index = markid = 0; Chris@16: re_recurse* pr = static_cast(this->append_state(syntax_element_recurse, sizeof(re_recurse))); Chris@16: pr->alt.i = v; Chris@16: pr->state_id = 0; Chris@16: static_cast( Chris@16: this->append_state(syntax_element_toggle_case, sizeof(re_case)) Chris@16: )->icase = this->flags() & regbase::icase; Chris@16: break; Chris@16: } Chris@16: case regex_constants::syntax_plus: Chris@16: // Chris@16: // A forward-relative recursive subexpression: Chris@16: // Chris@16: ++m_position; Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: if((v <= 0) || (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression."); Chris@16: return false; Chris@16: } Chris@16: v += m_mark_count; Chris@16: goto insert_recursion; Chris@16: case regex_constants::syntax_dash: Chris@16: // Chris@16: // Possibly a backward-relative recursive subexpression: Chris@16: // Chris@16: ++m_position; Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: if(v <= 0) Chris@16: { Chris@16: --m_position; Chris@16: // Oops not a relative recursion at all, but a (?-imsx) group: Chris@16: goto option_group_jump; Chris@16: } Chris@16: v = m_mark_count + 1 - v; Chris@16: if(v <= 0) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "An invalid or unterminated recursive sub-expression."); Chris@16: return false; Chris@16: } Chris@16: goto insert_recursion; Chris@16: case regex_constants::syntax_equal: Chris@16: pb->index = markid = -1; Chris@16: ++m_position; Chris@16: jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump))); Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: break; Chris@16: case regex_constants::syntax_not: Chris@16: pb->index = markid = -2; Chris@16: ++m_position; Chris@16: jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump))); Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: break; Chris@16: case regex_constants::escape_type_left_word: Chris@16: { Chris@16: // a lookbehind assertion: Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: regex_constants::syntax_type t = this->m_traits.syntax_type(*m_position); Chris@16: if(t == regex_constants::syntax_not) Chris@16: pb->index = markid = -2; Chris@16: else if(t == regex_constants::syntax_equal) Chris@16: pb->index = markid = -1; Chris@16: else Chris@16: { Chris@16: // Probably a named capture which also starts (?< : Chris@16: name_delim = '>'; Chris@16: --m_position; Chris@16: goto named_capture_jump; Chris@16: } Chris@16: ++m_position; Chris@16: jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump))); Chris@16: this->append_state(syntax_element_backstep, sizeof(re_brace)); Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: break; Chris@16: } Chris@16: case regex_constants::escape_type_right_word: Chris@16: // Chris@16: // an independent sub-expression: Chris@16: // Chris@16: pb->index = markid = -3; Chris@16: ++m_position; Chris@16: jump_offset = this->getoffset(this->append_state(syntax_element_jump, sizeof(re_jump))); Chris@16: this->m_pdata->m_data.align(); Chris@16: m_alt_insert_point = this->m_pdata->m_data.size(); Chris@16: break; Chris@16: case regex_constants::syntax_open_mark: Chris@16: { Chris@16: // a conditional expression: Chris@16: pb->index = markid = -4; Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: v = this->m_traits.toi(m_position, m_end, 10); Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(*m_position == charT('R')) Chris@16: { Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(*m_position == charT('&')) Chris@16: { Chris@16: const charT* base = ++m_position; Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: v = -static_cast(hash_value_from_capture_name(base, m_position)); Chris@16: } Chris@16: else Chris@16: { Chris@16: v = -this->m_traits.toi(m_position, m_end, 10); Chris@16: } Chris@16: re_brace* br = static_cast(this->append_state(syntax_element_assert_backref, sizeof(re_brace))); Chris@16: br->index = v < 0 ? (v - 1) : 0; Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: else if((*m_position == charT('\'')) || (*m_position == charT('<'))) Chris@16: { Chris@16: const charT* base = ++m_position; Chris@16: while((m_position != m_end) && (*m_position != charT('>')) && (*m_position != charT('\''))) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: v = static_cast(hash_value_from_capture_name(base, m_position)); Chris@16: re_brace* br = static_cast(this->append_state(syntax_element_assert_backref, sizeof(re_brace))); Chris@16: br->index = v; Chris@16: if(((*m_position != charT('>')) && (*m_position != charT('\''))) || (++m_position == m_end)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "Unterminated named capture."); Chris@16: return false; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: else if(*m_position == charT('D')) Chris@16: { Chris@16: const char* def = "DEFINE"; Chris@16: while(*def && (m_position != m_end) && (*m_position == charT(*def))) Chris@16: ++m_position, ++def; Chris@16: if((m_position == m_end) || *def) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: re_brace* br = static_cast(this->append_state(syntax_element_assert_backref, sizeof(re_brace))); Chris@16: br->index = 9999; // special magic value! Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: else if(v > 0) Chris@16: { Chris@16: re_brace* br = static_cast(this->append_state(syntax_element_assert_backref, sizeof(re_brace))); Chris@16: br->index = v; Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // verify that we have a lookahead or lookbehind assert: Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_question) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::escape_type_left_word) Chris@16: { Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: m_position -= 3; Chris@16: } Chris@16: else Chris@16: { Chris@16: if((this->m_traits.syntax_type(*m_position) != regex_constants::syntax_equal) Chris@16: && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_not)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: m_position -= 2; Chris@16: } Chris@16: } Chris@16: break; Chris@16: } Chris@16: case regex_constants::syntax_close_mark: Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: case regex_constants::escape_type_end_buffer: Chris@16: { Chris@16: name_delim = *m_position; Chris@16: named_capture_jump: Chris@16: markid = 0; Chris@16: if(0 == (this->flags() & regbase::nosubs)) Chris@16: { Chris@16: markid = ++m_mark_count; Chris@16: #ifndef BOOST_NO_STD_DISTANCE Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.push_back(std::pair(std::distance(m_base, m_position) - 2, 0)); Chris@16: #else Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.push_back(std::pair((m_position - m_base) - 2, 0)); Chris@16: #endif Chris@16: } Chris@16: pb->index = markid; Chris@16: const charT* base = ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: while((m_position != m_end) && (*m_position != name_delim)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: this->m_pdata->set_name(base, m_position, markid); Chris@16: ++m_position; Chris@16: break; Chris@16: } Chris@16: default: Chris@16: if(*m_position == charT('R')) Chris@16: { Chris@16: ++m_position; Chris@16: v = 0; Chris@16: if(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: goto insert_recursion; Chris@16: } Chris@16: if(*m_position == charT('&')) Chris@16: { Chris@16: ++m_position; Chris@16: const charT* base = m_position; Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: v = static_cast(hash_value_from_capture_name(base, m_position)); Chris@16: goto insert_recursion; Chris@16: } Chris@16: if(*m_position == charT('P')) Chris@16: { Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(*m_position == charT('>')) Chris@16: { Chris@16: ++m_position; Chris@16: const charT* base = m_position; Chris@16: while((m_position != m_end) && (this->m_traits.syntax_type(*m_position) != regex_constants::syntax_close_mark)) Chris@16: ++m_position; Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: v = static_cast(hash_value_from_capture_name(base, m_position)); Chris@16: goto insert_recursion; Chris@16: } Chris@16: } Chris@16: // Chris@16: // lets assume that we have a (?imsx) group and try and parse it: Chris@16: // Chris@16: option_group_jump: Chris@16: regex_constants::syntax_option_type opts = parse_options(); Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: // make a note of whether we have a case change: Chris@16: m_has_case_change = ((opts & regbase::icase) != (this->flags() & regbase::icase)); Chris@16: pb->index = markid = 0; Chris@16: if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark) Chris@16: { Chris@16: // update flags and carry on as normal: Chris@16: this->flags(opts); Chris@16: restore_flags = false; Chris@16: old_case_change |= m_has_case_change; // defer end of scope by one ')' Chris@16: } Chris@16: else if(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_colon) Chris@16: { Chris@16: // update flags and carry on until the matching ')' is found: Chris@16: this->flags(opts); Chris@16: ++m_position; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: Chris@16: // finally append a case change state if we need it: Chris@16: if(m_has_case_change) Chris@16: { Chris@16: static_cast( Chris@16: this->append_state(syntax_element_toggle_case, sizeof(re_case)) Chris@16: )->icase = opts & regbase::icase; Chris@16: } Chris@16: Chris@16: } Chris@16: // Chris@16: // now recursively add more states, this will terminate when we get to a Chris@16: // matching ')' : Chris@16: // Chris@16: parse_all(); Chris@16: // Chris@16: // Unwind alternatives: Chris@16: // Chris@16: if(0 == unwind_alts(last_paren_start)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid alternation operators within (?...) block."); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // we either have a ')' or we have run out of characters prematurely: Chris@16: // Chris@16: if(m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: this->fail(regex_constants::error_paren, ::boost::re_detail::distance(m_base, m_end)); Chris@16: return false; Chris@16: } Chris@16: BOOST_ASSERT(this->m_traits.syntax_type(*m_position) == regex_constants::syntax_close_mark); Chris@16: ++m_position; Chris@16: // Chris@16: // restore the flags: Chris@16: // Chris@16: if(restore_flags) Chris@16: { Chris@16: // append a case change state if we need it: Chris@16: if(m_has_case_change) Chris@16: { Chris@16: static_cast( Chris@16: this->append_state(syntax_element_toggle_case, sizeof(re_case)) Chris@16: )->icase = old_flags & regbase::icase; Chris@16: } Chris@16: this->flags(old_flags); Chris@16: } Chris@16: // Chris@16: // set up the jump pointer if we have one: Chris@16: // Chris@16: if(jump_offset) Chris@16: { Chris@16: this->m_pdata->m_data.align(); Chris@16: re_jump* jmp = static_cast(this->getaddress(jump_offset)); Chris@16: jmp->alt.i = this->m_pdata->m_data.size() - this->getoffset(jmp); Chris@16: if((this->m_last_state == jmp) && (markid != -2)) Chris@16: { Chris@16: // Oops... we didn't have anything inside the assertion. Chris@16: // Note we don't get here for negated forward lookahead as (?!) Chris@16: // does have some uses. Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_perl_extension, m_position - m_base, "Invalid or empty zero width assertion."); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: // Chris@16: // verify that if this is conditional expression, that we do have Chris@16: // an alternative, if not add one: Chris@16: // Chris@16: if(markid == -4) Chris@16: { Chris@16: re_syntax_base* b = this->getaddress(expected_alt_point); Chris@16: // Make sure we have exactly one alternative following this state: Chris@16: if(b->type != syntax_element_alt) Chris@16: { Chris@16: re_alt* alt = static_cast(this->insert_state(expected_alt_point, syntax_element_alt, sizeof(re_alt))); Chris@16: alt->alt.i = this->m_pdata->m_data.size() - this->getoffset(alt); Chris@16: } Chris@16: else if(this->getaddress(static_cast(b)->alt.i, b)->type == syntax_element_alt) Chris@16: { Chris@16: // Can't have seen more than one alternative: Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_bad_pattern, m_position - m_base, "More than one alternation operator | was encountered inside a conditional expression."); Chris@16: return false; Chris@16: } Chris@16: else Chris@16: { Chris@16: // We must *not* have seen an alternative inside a (DEFINE) block: Chris@16: b = this->getaddress(b->next.i, b); Chris@16: if((b->type == syntax_element_assert_backref) && (static_cast(b)->index == 9999)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_bad_pattern, m_position - m_base, "Alternation operators are not allowed inside a DEFINE block."); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: // check for invalid repetition of next state: Chris@16: b = this->getaddress(expected_alt_point); Chris@16: b = this->getaddress(static_cast(b)->next.i, b); Chris@16: if((b->type != syntax_element_assert_backref) Chris@16: && (b->type != syntax_element_startmark)) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_badrepeat, m_position - m_base, "A repetition operator cannot be applied to a zero-width assertion."); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: // Chris@16: // append closing parenthesis state: Chris@16: // Chris@16: pb = static_cast(this->append_state(syntax_element_endmark, sizeof(re_brace))); Chris@16: pb->index = markid; Chris@16: pb->icase = this->flags() & regbase::icase; Chris@16: this->m_paren_start = last_paren_start; Chris@16: // Chris@16: // restore the alternate insertion point: Chris@16: // Chris@16: this->m_alt_insert_point = last_alt_point; Chris@16: // Chris@16: // and the case change data: Chris@16: // Chris@16: m_has_case_change = old_case_change; Chris@16: // Chris@16: // And the mark_reset data: Chris@16: // Chris@16: if(m_max_mark > m_mark_count) Chris@16: { Chris@16: m_mark_count = m_max_mark; Chris@16: } Chris@16: m_mark_reset = mark_reset; Chris@16: m_max_mark = max_mark; Chris@16: Chris@16: Chris@16: if(markid > 0) Chris@16: { Chris@16: #ifndef BOOST_NO_STD_DISTANCE Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.at(markid - 1).second = std::distance(m_base, m_position) - 1; Chris@16: #else Chris@16: if(this->flags() & regbase::save_subexpression_location) Chris@16: this->m_pdata->m_subs.at(markid - 1).second = (m_position - m_base) - 1; Chris@16: #endif Chris@16: // Chris@16: // allow backrefs to this mark: Chris@16: // Chris@16: if((markid > 0) && (markid < (int)(sizeof(unsigned) * CHAR_BIT))) Chris@16: this->m_backrefs |= 1u << (markid - 1); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::add_emacs_code(bool negate) Chris@16: { Chris@16: // Chris@16: // parses an emacs style \sx or \Sx construct. Chris@16: // Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_escape) --m_position; Chris@16: fail(regex_constants::error_escape, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: basic_char_set char_set; Chris@16: if(negate) Chris@16: char_set.negate(); Chris@16: Chris@16: static const charT s_punct[5] = { 'p', 'u', 'n', 'c', 't', }; Chris@16: Chris@16: switch(*m_position) Chris@16: { Chris@16: case 's': Chris@16: case ' ': Chris@16: char_set.add_class(this->m_mask_space); Chris@16: break; Chris@16: case 'w': Chris@16: char_set.add_class(this->m_word_mask); Chris@16: break; Chris@16: case '_': Chris@16: char_set.add_single(digraph(charT('$'))); Chris@16: char_set.add_single(digraph(charT('&'))); Chris@16: char_set.add_single(digraph(charT('*'))); Chris@16: char_set.add_single(digraph(charT('+'))); Chris@16: char_set.add_single(digraph(charT('-'))); Chris@16: char_set.add_single(digraph(charT('_'))); Chris@16: char_set.add_single(digraph(charT('<'))); Chris@16: char_set.add_single(digraph(charT('>'))); Chris@16: break; Chris@16: case '.': Chris@16: char_set.add_class(this->m_traits.lookup_classname(s_punct, s_punct+5)); Chris@16: break; Chris@16: case '(': Chris@16: char_set.add_single(digraph(charT('('))); Chris@16: char_set.add_single(digraph(charT('['))); Chris@16: char_set.add_single(digraph(charT('{'))); Chris@16: break; Chris@16: case ')': Chris@16: char_set.add_single(digraph(charT(')'))); Chris@16: char_set.add_single(digraph(charT(']'))); Chris@16: char_set.add_single(digraph(charT('}'))); Chris@16: break; Chris@16: case '"': Chris@16: char_set.add_single(digraph(charT('"'))); Chris@16: char_set.add_single(digraph(charT('\''))); Chris@16: char_set.add_single(digraph(charT('`'))); Chris@16: break; Chris@16: case '\'': Chris@16: char_set.add_single(digraph(charT('\''))); Chris@16: char_set.add_single(digraph(charT(','))); Chris@16: char_set.add_single(digraph(charT('#'))); Chris@16: break; Chris@16: case '<': Chris@16: char_set.add_single(digraph(charT(';'))); Chris@16: break; Chris@16: case '>': Chris@16: char_set.add_single(digraph(charT('\n'))); Chris@16: char_set.add_single(digraph(charT('\f'))); Chris@16: break; Chris@16: default: Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: if(0 == this->append_set(char_set)) Chris@16: { Chris@16: fail(regex_constants::error_ctype, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: ++m_position; Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: regex_constants::syntax_option_type basic_regex_parser::parse_options() Chris@16: { Chris@16: // we have a (?imsx-imsx) group, convert it into a set of flags: Chris@16: regex_constants::syntax_option_type f = this->flags(); Chris@16: bool breakout = false; Chris@16: do Chris@16: { Chris@16: switch(*m_position) Chris@16: { Chris@16: case 's': Chris@16: f |= regex_constants::mod_s; Chris@16: f &= ~regex_constants::no_mod_s; Chris@16: break; Chris@16: case 'm': Chris@16: f &= ~regex_constants::no_mod_m; Chris@16: break; Chris@16: case 'i': Chris@16: f |= regex_constants::icase; Chris@16: break; Chris@16: case 'x': Chris@16: f |= regex_constants::mod_x; Chris@16: break; Chris@16: default: Chris@16: breakout = true; Chris@16: continue; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_paren, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: while(!breakout); Chris@16: Chris@16: breakout = false; Chris@16: Chris@16: if(*m_position == static_cast('-')) Chris@16: { Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_paren, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: do Chris@16: { Chris@16: switch(*m_position) Chris@16: { Chris@16: case 's': Chris@16: f &= ~regex_constants::mod_s; Chris@16: f |= regex_constants::no_mod_s; Chris@16: break; Chris@16: case 'm': Chris@16: f |= regex_constants::no_mod_m; Chris@16: break; Chris@16: case 'i': Chris@16: f &= ~regex_constants::icase; Chris@16: break; Chris@16: case 'x': Chris@16: f &= ~regex_constants::mod_x; Chris@16: break; Chris@16: default: Chris@16: breakout = true; Chris@16: continue; Chris@16: } Chris@16: if(++m_position == m_end) Chris@16: { Chris@16: // Rewind to start of (? sequence: Chris@16: --m_position; Chris@16: while(this->m_traits.syntax_type(*m_position) != regex_constants::syntax_open_mark) --m_position; Chris@16: fail(regex_constants::error_paren, m_position - m_base); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: while(!breakout); Chris@16: } Chris@16: return f; Chris@16: } Chris@16: Chris@16: template Chris@16: bool basic_regex_parser::unwind_alts(std::ptrdiff_t last_paren_start) Chris@16: { Chris@16: // Chris@16: // If we didn't actually add any states after the last Chris@16: // alternative then that's an error: Chris@16: // Chris@16: if((this->m_alt_insert_point == static_cast(this->m_pdata->m_data.size())) Chris@16: && m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start) Chris@16: && Chris@16: !( Chris@16: ((this->flags() & regbase::main_option_type) == regbase::perl_syntax_group) Chris@16: && Chris@16: ((this->flags() & regbase::no_empty_expressions) == 0) Chris@16: ) Chris@16: ) Chris@16: { Chris@16: fail(regex_constants::error_empty, this->m_position - this->m_base, "Can't terminate a sub-expression with an alternation operator |."); Chris@16: return false; Chris@16: } Chris@16: // Chris@16: // Fix up our alternatives: Chris@16: // Chris@16: while(m_alt_jumps.size() && (m_alt_jumps.back() > last_paren_start)) Chris@16: { Chris@16: // Chris@16: // fix up the jump to point to the end of the states Chris@16: // that we've just added: Chris@16: // Chris@16: std::ptrdiff_t jump_offset = m_alt_jumps.back(); Chris@16: m_alt_jumps.pop_back(); Chris@16: this->m_pdata->m_data.align(); Chris@16: re_jump* jmp = static_cast(this->getaddress(jump_offset)); Chris@16: BOOST_ASSERT(jmp->type == syntax_element_jump); Chris@16: jmp->alt.i = this->m_pdata->m_data.size() - jump_offset; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: } // namespace re_detail Chris@16: } // namespace boost Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable: 4103) Chris@16: #endif Chris@16: #ifdef BOOST_HAS_ABI_HEADERS Chris@16: # include BOOST_ABI_SUFFIX Chris@16: #endif Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif