libstdc++
|
00001 // class template regex -*- C++ -*- 00002 00003 // Copyright (C) 2013-2014 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** 00026 * @file bits/regex_automaton.tcc 00027 * This is an internal header file, included by other library headers. 00028 * Do not attempt to use it directly. @headername{regex} 00029 */ 00030 00031 namespace std _GLIBCXX_VISIBILITY(default) 00032 { 00033 namespace __detail 00034 { 00035 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00036 00037 #ifdef _GLIBCXX_DEBUG 00038 inline std::ostream& 00039 _State_base::_M_print(std::ostream& ostr) const 00040 { 00041 switch (_M_opcode) 00042 { 00043 case _S_opcode_alternative: 00044 ostr << "alt next=" << _M_next << " alt=" << _M_alt; 00045 break; 00046 case _S_opcode_subexpr_begin: 00047 ostr << "subexpr begin next=" << _M_next << " index=" << _M_subexpr; 00048 break; 00049 case _S_opcode_subexpr_end: 00050 ostr << "subexpr end next=" << _M_next << " index=" << _M_subexpr; 00051 break; 00052 case _S_opcode_backref: 00053 ostr << "backref next=" << _M_next << " index=" << _M_backref_index; 00054 break; 00055 case _S_opcode_match: 00056 ostr << "match next=" << _M_next; 00057 break; 00058 case _S_opcode_accept: 00059 ostr << "accept next=" << _M_next; 00060 break; 00061 default: 00062 ostr << "unknown next=" << _M_next; 00063 break; 00064 } 00065 return ostr; 00066 } 00067 00068 // Prints graphviz dot commands for state. 00069 inline std::ostream& 00070 _State_base::_M_dot(std::ostream& __ostr, _StateIdT __id) const 00071 { 00072 switch (_M_opcode) 00073 { 00074 case _S_opcode_alternative: 00075 __ostr << __id << " [label=\"" << __id << "\\nALT\"];\n" 00076 << __id << " -> " << _M_next 00077 << " [label=\"epsilon\", tailport=\"s\"];\n" 00078 << __id << " -> " << _M_alt 00079 << " [label=\"epsilon\", tailport=\"n\"];\n"; 00080 break; 00081 case _S_opcode_backref: 00082 __ostr << __id << " [label=\"" << __id << "\\nBACKREF " 00083 << _M_subexpr << "\"];\n" 00084 << __id << " -> " << _M_next << " [label=\"<match>\"];\n"; 00085 break; 00086 case _S_opcode_line_begin_assertion: 00087 __ostr << __id << " [label=\"" << __id << "\\nLINE_BEGIN \"];\n" 00088 << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 00089 break; 00090 case _S_opcode_line_end_assertion: 00091 __ostr << __id << " [label=\"" << __id << "\\nLINE_END \"];\n" 00092 << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 00093 break; 00094 case _S_opcode_word_boundary: 00095 __ostr << __id << " [label=\"" << __id << "\\nWORD_BOUNDRY " 00096 << _M_neg << "\"];\n" 00097 << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 00098 break; 00099 case _S_opcode_subexpr_lookahead: 00100 __ostr << __id << " [label=\"" << __id << "\\nLOOK_AHEAD\"];\n" 00101 << __id << " -> " << _M_next 00102 << " [label=\"epsilon\", tailport=\"s\"];\n" 00103 << __id << " -> " << _M_alt 00104 << " [label=\"<assert>\", tailport=\"n\"];\n"; 00105 break; 00106 case _S_opcode_subexpr_begin: 00107 __ostr << __id << " [label=\"" << __id << "\\nSBEGIN " 00108 << _M_subexpr << "\"];\n" 00109 << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 00110 break; 00111 case _S_opcode_subexpr_end: 00112 __ostr << __id << " [label=\"" << __id << "\\nSEND " 00113 << _M_subexpr << "\"];\n" 00114 << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 00115 break; 00116 case _S_opcode_dummy: 00117 break; 00118 case _S_opcode_match: 00119 __ostr << __id << " [label=\"" << __id << "\\nMATCH\"];\n" 00120 << __id << " -> " << _M_next << " [label=\"<match>\"];\n"; 00121 break; 00122 case _S_opcode_accept: 00123 __ostr << __id << " [label=\"" << __id << "\\nACC\"];\n" ; 00124 break; 00125 default: 00126 _GLIBCXX_DEBUG_ASSERT(false); 00127 break; 00128 } 00129 return __ostr; 00130 } 00131 00132 template<typename _TraitsT> 00133 std::ostream& 00134 _NFA<_TraitsT>::_M_dot(std::ostream& __ostr) const 00135 { 00136 __ostr << "digraph _Nfa {\n" 00137 " rankdir=LR;\n"; 00138 for (size_t __i = 0; __i < this->size(); ++__i) 00139 (*this)[__i]._M_dot(__ostr, __i); 00140 __ostr << "}\n"; 00141 return __ostr; 00142 } 00143 #endif 00144 00145 template<typename _TraitsT> 00146 _StateIdT 00147 _NFA<_TraitsT>::_M_insert_backref(size_t __index) 00148 { 00149 // To figure out whether a backref is valid, a stack is used to store 00150 // unfinished sub-expressions. For example, when parsing 00151 // "(a(b)(c\\1(d)))" at '\\1', _M_subexpr_count is 3, indicating that 3 00152 // sub expressions are parsed or partially parsed(in the stack), aka, 00153 // "(a..", "(b)" and "(c.."). 00154 // _M_paren_stack is {1, 3}, for incomplete "(a.." and "(c..". At this 00155 // time, "\\2" is valid, but "\\1" and "\\3" are not. 00156 if (__index >= _M_subexpr_count) 00157 __throw_regex_error(regex_constants::error_backref); 00158 for (auto __it : this->_M_paren_stack) 00159 if (__index == __it) 00160 __throw_regex_error(regex_constants::error_backref); 00161 this->_M_has_backref = true; 00162 _StateT __tmp(_S_opcode_backref); 00163 __tmp._M_backref_index = __index; 00164 return _M_insert_state(std::move(__tmp)); 00165 } 00166 00167 template<typename _TraitsT> 00168 void 00169 _NFA<_TraitsT>::_M_eliminate_dummy() 00170 { 00171 for (auto& __it : *this) 00172 { 00173 while (__it._M_next >= 0 && (*this)[__it._M_next]._M_opcode 00174 == _S_opcode_dummy) 00175 __it._M_next = (*this)[__it._M_next]._M_next; 00176 if (__it._M_opcode == _S_opcode_alternative 00177 || __it._M_opcode == _S_opcode_subexpr_lookahead) 00178 while (__it._M_alt >= 0 && (*this)[__it._M_alt]._M_opcode 00179 == _S_opcode_dummy) 00180 __it._M_alt = (*this)[__it._M_alt]._M_next; 00181 } 00182 } 00183 00184 // Just apply DFS on the sequence and re-link their links. 00185 template<typename _TraitsT> 00186 _StateSeq<_TraitsT> 00187 _StateSeq<_TraitsT>::_M_clone() 00188 { 00189 std::vector<_StateIdT> __m(_M_nfa.size(), -1); 00190 std::stack<_StateIdT> __stack; 00191 __stack.push(_M_start); 00192 while (!__stack.empty()) 00193 { 00194 auto __u = __stack.top(); 00195 __stack.pop(); 00196 auto __dup = _M_nfa[__u]; 00197 // _M_insert_state() never return -1 00198 auto __id = _M_nfa._M_insert_state(__dup); 00199 __m[__u] = __id; 00200 if (__dup._M_opcode == _S_opcode_alternative 00201 || __dup._M_opcode == _S_opcode_subexpr_lookahead) 00202 if (__dup._M_alt != _S_invalid_state_id && __m[__dup._M_alt] == -1) 00203 __stack.push(__dup._M_alt); 00204 if (__u == _M_end) 00205 continue; 00206 if (__dup._M_next != _S_invalid_state_id && __m[__dup._M_next] == -1) 00207 __stack.push(__dup._M_next); 00208 } 00209 for (auto __v : __m) 00210 { 00211 if (__v == -1) 00212 continue; 00213 auto& __ref = _M_nfa[__v]; 00214 if (__ref._M_next != _S_invalid_state_id) 00215 { 00216 _GLIBCXX_DEBUG_ASSERT(__m[__ref._M_next] != -1); 00217 __ref._M_next = __m[__ref._M_next]; 00218 } 00219 if (__ref._M_opcode == _S_opcode_alternative 00220 || __ref._M_opcode == _S_opcode_subexpr_lookahead) 00221 if (__ref._M_alt != _S_invalid_state_id) 00222 { 00223 _GLIBCXX_DEBUG_ASSERT(__m[__ref._M_alt] != -1); 00224 __ref._M_alt = __m[__ref._M_alt]; 00225 } 00226 } 00227 return _StateSeq(_M_nfa, __m[_M_start], __m[_M_end]); 00228 } 00229 00230 _GLIBCXX_END_NAMESPACE_VERSION 00231 } // namespace __detail 00232 } // namespace