1*38fd1498Szrj // class template regex -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2013-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** 26*38fd1498Szrj * @file bits/regex_automaton.tcc 27*38fd1498Szrj * This is an internal header file, included by other library headers. 28*38fd1498Szrj * Do not attempt to use it directly. @headername{regex} 29*38fd1498Szrj */ 30*38fd1498Szrj 31*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 32*38fd1498Szrj { 33*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 34*38fd1498Szrj 35*38fd1498Szrj namespace __detail 36*38fd1498Szrj { 37*38fd1498Szrj #ifdef _GLIBCXX_DEBUG 38*38fd1498Szrj inline std::ostream& _M_print(std::ostream & ostr) const39*38fd1498Szrj _State_base::_M_print(std::ostream& ostr) const 40*38fd1498Szrj { 41*38fd1498Szrj switch (_M_opcode) 42*38fd1498Szrj { 43*38fd1498Szrj case _S_opcode_alternative: 44*38fd1498Szrj case _S_opcode_repeat: 45*38fd1498Szrj ostr << "alt next=" << _M_next << " alt=" << _M_alt; 46*38fd1498Szrj break; 47*38fd1498Szrj case _S_opcode_subexpr_begin: 48*38fd1498Szrj ostr << "subexpr begin next=" << _M_next << " index=" << _M_subexpr; 49*38fd1498Szrj break; 50*38fd1498Szrj case _S_opcode_subexpr_end: 51*38fd1498Szrj ostr << "subexpr end next=" << _M_next << " index=" << _M_subexpr; 52*38fd1498Szrj break; 53*38fd1498Szrj case _S_opcode_backref: 54*38fd1498Szrj ostr << "backref next=" << _M_next << " index=" << _M_backref_index; 55*38fd1498Szrj break; 56*38fd1498Szrj case _S_opcode_match: 57*38fd1498Szrj ostr << "match next=" << _M_next; 58*38fd1498Szrj break; 59*38fd1498Szrj case _S_opcode_accept: 60*38fd1498Szrj ostr << "accept next=" << _M_next; 61*38fd1498Szrj break; 62*38fd1498Szrj default: 63*38fd1498Szrj ostr << "unknown next=" << _M_next; 64*38fd1498Szrj break; 65*38fd1498Szrj } 66*38fd1498Szrj return ostr; 67*38fd1498Szrj } 68*38fd1498Szrj 69*38fd1498Szrj // Prints graphviz dot commands for state. 70*38fd1498Szrj inline std::ostream& _M_dot(std::ostream & __ostr,_StateIdT __id) const71*38fd1498Szrj _State_base::_M_dot(std::ostream& __ostr, _StateIdT __id) const 72*38fd1498Szrj { 73*38fd1498Szrj switch (_M_opcode) 74*38fd1498Szrj { 75*38fd1498Szrj case _S_opcode_alternative: 76*38fd1498Szrj case _S_opcode_repeat: 77*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nALT\"];\n" 78*38fd1498Szrj << __id << " -> " << _M_next 79*38fd1498Szrj << " [label=\"next\", tailport=\"s\"];\n" 80*38fd1498Szrj << __id << " -> " << _M_alt 81*38fd1498Szrj << " [label=\"alt\", tailport=\"n\"];\n"; 82*38fd1498Szrj break; 83*38fd1498Szrj case _S_opcode_backref: 84*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nBACKREF " 85*38fd1498Szrj << _M_subexpr << "\"];\n" 86*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"<match>\"];\n"; 87*38fd1498Szrj break; 88*38fd1498Szrj case _S_opcode_line_begin_assertion: 89*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nLINE_BEGIN \"];\n" 90*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 91*38fd1498Szrj break; 92*38fd1498Szrj case _S_opcode_line_end_assertion: 93*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nLINE_END \"];\n" 94*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 95*38fd1498Szrj break; 96*38fd1498Szrj case _S_opcode_word_boundary: 97*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nWORD_BOUNDRY " 98*38fd1498Szrj << _M_neg << "\"];\n" 99*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 100*38fd1498Szrj break; 101*38fd1498Szrj case _S_opcode_subexpr_lookahead: 102*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nLOOK_AHEAD\"];\n" 103*38fd1498Szrj << __id << " -> " << _M_next 104*38fd1498Szrj << " [label=\"epsilon\", tailport=\"s\"];\n" 105*38fd1498Szrj << __id << " -> " << _M_alt 106*38fd1498Szrj << " [label=\"<assert>\", tailport=\"n\"];\n"; 107*38fd1498Szrj break; 108*38fd1498Szrj case _S_opcode_subexpr_begin: 109*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nSBEGIN " 110*38fd1498Szrj << _M_subexpr << "\"];\n" 111*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 112*38fd1498Szrj break; 113*38fd1498Szrj case _S_opcode_subexpr_end: 114*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nSEND " 115*38fd1498Szrj << _M_subexpr << "\"];\n" 116*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"epsilon\"];\n"; 117*38fd1498Szrj break; 118*38fd1498Szrj case _S_opcode_dummy: 119*38fd1498Szrj break; 120*38fd1498Szrj case _S_opcode_match: 121*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nMATCH\"];\n" 122*38fd1498Szrj << __id << " -> " << _M_next << " [label=\"<match>\"];\n"; 123*38fd1498Szrj break; 124*38fd1498Szrj case _S_opcode_accept: 125*38fd1498Szrj __ostr << __id << " [label=\"" << __id << "\\nACC\"];\n" ; 126*38fd1498Szrj break; 127*38fd1498Szrj default: 128*38fd1498Szrj _GLIBCXX_DEBUG_ASSERT(false); 129*38fd1498Szrj break; 130*38fd1498Szrj } 131*38fd1498Szrj return __ostr; 132*38fd1498Szrj } 133*38fd1498Szrj 134*38fd1498Szrj template<typename _TraitsT> 135*38fd1498Szrj std::ostream& _M_dot(std::ostream & __ostr) const136*38fd1498Szrj _NFA<_TraitsT>::_M_dot(std::ostream& __ostr) const 137*38fd1498Szrj { 138*38fd1498Szrj __ostr << "digraph _Nfa {\n" 139*38fd1498Szrj " rankdir=LR;\n"; 140*38fd1498Szrj for (size_t __i = 0; __i < this->size(); ++__i) 141*38fd1498Szrj (*this)[__i]._M_dot(__ostr, __i); 142*38fd1498Szrj __ostr << "}\n"; 143*38fd1498Szrj return __ostr; 144*38fd1498Szrj } 145*38fd1498Szrj #endif 146*38fd1498Szrj 147*38fd1498Szrj template<typename _TraitsT> 148*38fd1498Szrj _StateIdT _M_insert_backref(size_t __index)149*38fd1498Szrj _NFA<_TraitsT>::_M_insert_backref(size_t __index) 150*38fd1498Szrj { 151*38fd1498Szrj if (this->_M_flags & regex_constants::__polynomial) 152*38fd1498Szrj __throw_regex_error(regex_constants::error_complexity, 153*38fd1498Szrj "Unexpected back-reference in polynomial mode."); 154*38fd1498Szrj // To figure out whether a backref is valid, a stack is used to store 155*38fd1498Szrj // unfinished sub-expressions. For example, when parsing 156*38fd1498Szrj // "(a(b)(c\\1(d)))" at '\\1', _M_subexpr_count is 3, indicating that 3 157*38fd1498Szrj // sub expressions are parsed or partially parsed(in the stack), aka, 158*38fd1498Szrj // "(a..", "(b)" and "(c.."). 159*38fd1498Szrj // _M_paren_stack is {1, 3}, for incomplete "(a.." and "(c..". At this 160*38fd1498Szrj // time, "\\2" is valid, but "\\1" and "\\3" are not. 161*38fd1498Szrj if (__index >= _M_subexpr_count) 162*38fd1498Szrj __throw_regex_error( 163*38fd1498Szrj regex_constants::error_backref, 164*38fd1498Szrj "Back-reference index exceeds current sub-expression count."); 165*38fd1498Szrj for (auto __it : this->_M_paren_stack) 166*38fd1498Szrj if (__index == __it) 167*38fd1498Szrj __throw_regex_error( 168*38fd1498Szrj regex_constants::error_backref, 169*38fd1498Szrj "Back-reference referred to an opened sub-expression."); 170*38fd1498Szrj this->_M_has_backref = true; 171*38fd1498Szrj _StateT __tmp(_S_opcode_backref); 172*38fd1498Szrj __tmp._M_backref_index = __index; 173*38fd1498Szrj return _M_insert_state(std::move(__tmp)); 174*38fd1498Szrj } 175*38fd1498Szrj 176*38fd1498Szrj template<typename _TraitsT> 177*38fd1498Szrj void _M_eliminate_dummy()178*38fd1498Szrj _NFA<_TraitsT>::_M_eliminate_dummy() 179*38fd1498Szrj { 180*38fd1498Szrj for (auto& __it : *this) 181*38fd1498Szrj { 182*38fd1498Szrj while (__it._M_next >= 0 && (*this)[__it._M_next]._M_opcode() 183*38fd1498Szrj == _S_opcode_dummy) 184*38fd1498Szrj __it._M_next = (*this)[__it._M_next]._M_next; 185*38fd1498Szrj if (__it._M_has_alt()) 186*38fd1498Szrj while (__it._M_alt >= 0 && (*this)[__it._M_alt]._M_opcode() 187*38fd1498Szrj == _S_opcode_dummy) 188*38fd1498Szrj __it._M_alt = (*this)[__it._M_alt]._M_next; 189*38fd1498Szrj } 190*38fd1498Szrj } 191*38fd1498Szrj 192*38fd1498Szrj // Just apply DFS on the sequence and re-link their links. 193*38fd1498Szrj template<typename _TraitsT> 194*38fd1498Szrj _StateSeq<_TraitsT> _M_clone()195*38fd1498Szrj _StateSeq<_TraitsT>::_M_clone() 196*38fd1498Szrj { 197*38fd1498Szrj std::map<_StateIdT, _StateIdT> __m; 198*38fd1498Szrj std::stack<_StateIdT> __stack; 199*38fd1498Szrj __stack.push(_M_start); 200*38fd1498Szrj while (!__stack.empty()) 201*38fd1498Szrj { 202*38fd1498Szrj auto __u = __stack.top(); 203*38fd1498Szrj __stack.pop(); 204*38fd1498Szrj auto __dup = _M_nfa[__u]; 205*38fd1498Szrj // _M_insert_state() never return -1 206*38fd1498Szrj auto __id = _M_nfa._M_insert_state(std::move(__dup)); 207*38fd1498Szrj __m[__u] = __id; 208*38fd1498Szrj if (__dup._M_has_alt()) 209*38fd1498Szrj if (__dup._M_alt != _S_invalid_state_id 210*38fd1498Szrj && __m.count(__dup._M_alt) == 0) 211*38fd1498Szrj __stack.push(__dup._M_alt); 212*38fd1498Szrj if (__u == _M_end) 213*38fd1498Szrj continue; 214*38fd1498Szrj if (__dup._M_next != _S_invalid_state_id 215*38fd1498Szrj && __m.count(__dup._M_next) == 0) 216*38fd1498Szrj __stack.push(__dup._M_next); 217*38fd1498Szrj } 218*38fd1498Szrj for (auto __it : __m) 219*38fd1498Szrj { 220*38fd1498Szrj auto __v = __it.second; 221*38fd1498Szrj auto& __ref = _M_nfa[__v]; 222*38fd1498Szrj if (__ref._M_next != _S_invalid_state_id) 223*38fd1498Szrj { 224*38fd1498Szrj __glibcxx_assert(__m.count(__ref._M_next) > 0); 225*38fd1498Szrj __ref._M_next = __m[__ref._M_next]; 226*38fd1498Szrj } 227*38fd1498Szrj if (__ref._M_has_alt()) 228*38fd1498Szrj if (__ref._M_alt != _S_invalid_state_id) 229*38fd1498Szrj { 230*38fd1498Szrj __glibcxx_assert(__m.count(__ref._M_alt) > 0); 231*38fd1498Szrj __ref._M_alt = __m[__ref._M_alt]; 232*38fd1498Szrj } 233*38fd1498Szrj } 234*38fd1498Szrj return _StateSeq(_M_nfa, __m[_M_start], __m[_M_end]); 235*38fd1498Szrj } 236*38fd1498Szrj } // namespace __detail 237*38fd1498Szrj 238*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 239*38fd1498Szrj } // namespace 240