libstdc++
regex_automaton.tcc
Go to the documentation of this file.
00001 // class template regex -*- C++ -*-
00002 
00003 // Copyright (C) 2013-2018 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 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00034 
00035 namespace __detail
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       case _S_opcode_repeat:
00045         ostr << "alt next=" << _M_next << " alt=" << _M_alt;
00046         break;
00047       case _S_opcode_subexpr_begin:
00048         ostr << "subexpr begin next=" << _M_next << " index=" << _M_subexpr;
00049         break;
00050       case _S_opcode_subexpr_end:
00051         ostr << "subexpr end next=" << _M_next << " index=" << _M_subexpr;
00052         break;
00053       case _S_opcode_backref:
00054         ostr << "backref next=" << _M_next << " index=" << _M_backref_index;
00055         break;
00056       case _S_opcode_match:
00057         ostr << "match next=" << _M_next;
00058         break;
00059       case _S_opcode_accept:
00060         ostr << "accept next=" << _M_next;
00061         break;
00062       default:
00063         ostr << "unknown next=" << _M_next;
00064         break;
00065     }
00066     return ostr;
00067   }
00068 
00069   // Prints graphviz dot commands for state.
00070   inline std::ostream&
00071   _State_base::_M_dot(std::ostream& __ostr, _StateIdT __id) const
00072   {
00073     switch (_M_opcode)
00074     {
00075       case _S_opcode_alternative:
00076       case _S_opcode_repeat:
00077         __ostr << __id << " [label=\"" << __id << "\\nALT\"];\n"
00078                << __id << " -> " << _M_next
00079                << " [label=\"next\", tailport=\"s\"];\n"
00080                << __id << " -> " << _M_alt
00081                << " [label=\"alt\", tailport=\"n\"];\n";
00082         break;
00083       case _S_opcode_backref:
00084         __ostr << __id << " [label=\"" << __id << "\\nBACKREF "
00085                << _M_subexpr << "\"];\n"
00086                << __id << " -> " << _M_next << " [label=\"<match>\"];\n";
00087         break;
00088       case _S_opcode_line_begin_assertion:
00089         __ostr << __id << " [label=\"" << __id << "\\nLINE_BEGIN \"];\n"
00090                << __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
00091         break;
00092       case _S_opcode_line_end_assertion:
00093         __ostr << __id << " [label=\"" << __id << "\\nLINE_END \"];\n"
00094                << __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
00095         break;
00096       case _S_opcode_word_boundary:
00097         __ostr << __id << " [label=\"" << __id << "\\nWORD_BOUNDRY "
00098                << _M_neg << "\"];\n"
00099                << __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
00100         break;
00101       case _S_opcode_subexpr_lookahead:
00102         __ostr << __id << " [label=\"" << __id << "\\nLOOK_AHEAD\"];\n"
00103                << __id << " -> " << _M_next
00104                << " [label=\"epsilon\", tailport=\"s\"];\n"
00105                << __id << " -> " << _M_alt
00106                << " [label=\"<assert>\", tailport=\"n\"];\n";
00107         break;
00108       case _S_opcode_subexpr_begin:
00109         __ostr << __id << " [label=\"" << __id << "\\nSBEGIN "
00110                << _M_subexpr << "\"];\n"
00111                << __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
00112         break;
00113       case _S_opcode_subexpr_end:
00114         __ostr << __id << " [label=\"" << __id << "\\nSEND "
00115                << _M_subexpr << "\"];\n"
00116                << __id << " -> " << _M_next << " [label=\"epsilon\"];\n";
00117         break;
00118       case _S_opcode_dummy:
00119         break;
00120       case _S_opcode_match:
00121         __ostr << __id << " [label=\"" << __id << "\\nMATCH\"];\n"
00122                << __id << " -> " << _M_next << " [label=\"<match>\"];\n";
00123         break;
00124       case _S_opcode_accept:
00125         __ostr << __id << " [label=\"" << __id << "\\nACC\"];\n" ;
00126         break;
00127       default:
00128         _GLIBCXX_DEBUG_ASSERT(false);
00129         break;
00130     }
00131     return __ostr;
00132   }
00133 
00134   template<typename _TraitsT>
00135     std::ostream&
00136     _NFA<_TraitsT>::_M_dot(std::ostream& __ostr) const
00137     {
00138       __ostr << "digraph _Nfa {\n"
00139                 "  rankdir=LR;\n";
00140       for (size_t __i = 0; __i < this->size(); ++__i)
00141         (*this)[__i]._M_dot(__ostr, __i);
00142       __ostr << "}\n";
00143       return __ostr;
00144     }
00145 #endif
00146 
00147   template<typename _TraitsT>
00148     _StateIdT
00149     _NFA<_TraitsT>::_M_insert_backref(size_t __index)
00150     {
00151       if (this->_M_flags & regex_constants::__polynomial)
00152         __throw_regex_error(regex_constants::error_complexity,
00153                             "Unexpected back-reference in polynomial mode.");
00154       // To figure out whether a backref is valid, a stack is used to store
00155       // unfinished sub-expressions. For example, when parsing
00156       // "(a(b)(c\\1(d)))" at '\\1', _M_subexpr_count is 3, indicating that 3
00157       // sub expressions are parsed or partially parsed(in the stack), aka,
00158       // "(a..", "(b)" and "(c..").
00159       // _M_paren_stack is {1, 3}, for incomplete "(a.." and "(c..". At this
00160       // time, "\\2" is valid, but "\\1" and "\\3" are not.
00161       if (__index >= _M_subexpr_count)
00162         __throw_regex_error(
00163           regex_constants::error_backref,
00164           "Back-reference index exceeds current sub-expression count.");
00165       for (auto __it : this->_M_paren_stack)
00166         if (__index == __it)
00167           __throw_regex_error(
00168             regex_constants::error_backref,
00169             "Back-reference referred to an opened sub-expression.");
00170       this->_M_has_backref = true;
00171       _StateT __tmp(_S_opcode_backref);
00172       __tmp._M_backref_index = __index;
00173       return _M_insert_state(std::move(__tmp));
00174     }
00175 
00176   template<typename _TraitsT>
00177     void
00178     _NFA<_TraitsT>::_M_eliminate_dummy()
00179     {
00180       for (auto& __it : *this)
00181         {
00182           while (__it._M_next >= 0 && (*this)[__it._M_next]._M_opcode()
00183                  == _S_opcode_dummy)
00184             __it._M_next = (*this)[__it._M_next]._M_next;
00185           if (__it._M_has_alt())
00186             while (__it._M_alt >= 0 && (*this)[__it._M_alt]._M_opcode()
00187                    == _S_opcode_dummy)
00188               __it._M_alt = (*this)[__it._M_alt]._M_next;
00189         }
00190     }
00191 
00192   // Just apply DFS on the sequence and re-link their links.
00193   template<typename _TraitsT>
00194     _StateSeq<_TraitsT>
00195     _StateSeq<_TraitsT>::_M_clone()
00196     {
00197       std::map<_StateIdT, _StateIdT> __m;
00198       std::stack<_StateIdT> __stack;
00199       __stack.push(_M_start);
00200       while (!__stack.empty())
00201         {
00202           auto __u = __stack.top();
00203           __stack.pop();
00204           auto __dup = _M_nfa[__u];
00205           // _M_insert_state() never return -1
00206           auto __id = _M_nfa._M_insert_state(std::move(__dup));
00207           __m[__u] = __id;
00208           if (__dup._M_has_alt())
00209             if (__dup._M_alt != _S_invalid_state_id
00210                 && __m.count(__dup._M_alt) == 0)
00211               __stack.push(__dup._M_alt);
00212           if (__u == _M_end)
00213             continue;
00214           if (__dup._M_next != _S_invalid_state_id
00215               && __m.count(__dup._M_next) == 0)
00216             __stack.push(__dup._M_next);
00217         }
00218       for (auto __it : __m)
00219         {
00220           auto __v = __it.second;
00221           auto& __ref = _M_nfa[__v];
00222           if (__ref._M_next != _S_invalid_state_id)
00223             {
00224               __glibcxx_assert(__m.count(__ref._M_next) > 0);
00225               __ref._M_next = __m[__ref._M_next];
00226             }
00227           if (__ref._M_has_alt())
00228             if (__ref._M_alt != _S_invalid_state_id)
00229               {
00230                 __glibcxx_assert(__m.count(__ref._M_alt) > 0);
00231                 __ref._M_alt = __m[__ref._M_alt];
00232               }
00233         }
00234       return _StateSeq(_M_nfa, __m[_M_start], __m[_M_end]);
00235     }
00236 } // namespace __detail
00237 
00238 _GLIBCXX_END_NAMESPACE_VERSION
00239 } // namespace