libstdc++
regex_scanner.h
Go to the documentation of this file.
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_scanner.h
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   /**
00038    * @addtogroup regex-detail
00039    * @{
00040    */
00041 
00042   struct _ScannerBase
00043   {
00044   public:
00045     /// Token types returned from the scanner.
00046     enum _TokenT
00047     {
00048       _S_token_anychar,
00049       _S_token_ord_char,
00050       _S_token_oct_num,
00051       _S_token_hex_num,
00052       _S_token_backref,
00053       _S_token_subexpr_begin,
00054       _S_token_subexpr_no_group_begin,
00055       _S_token_subexpr_lookahead_begin, // neg if _M_value[0] == 'n'
00056       _S_token_subexpr_end,
00057       _S_token_bracket_begin,
00058       _S_token_bracket_neg_begin,
00059       _S_token_bracket_end,
00060       _S_token_interval_begin,
00061       _S_token_interval_end,
00062       _S_token_quoted_class,
00063       _S_token_char_class_name,
00064       _S_token_collsymbol,
00065       _S_token_equiv_class_name,
00066       _S_token_opt,
00067       _S_token_or,
00068       _S_token_closure0,
00069       _S_token_closure1,
00070       _S_token_ungreedy,
00071       _S_token_line_begin,
00072       _S_token_line_end,
00073       _S_token_word_bound, // neg if _M_value[0] == 'n'
00074       _S_token_comma,
00075       _S_token_dup_count,
00076       _S_token_eof,
00077       _S_token_unknown
00078     };
00079 
00080   protected:
00081     typedef regex_constants::syntax_option_type _FlagT;
00082 
00083     enum _StateT
00084     {
00085       _S_state_normal,
00086       _S_state_in_brace,
00087       _S_state_in_bracket,
00088     };
00089 
00090   protected:
00091     _ScannerBase(_FlagT __flags)
00092     : _M_state(_S_state_normal),
00093     _M_flags(__flags),
00094     _M_escape_tbl(_M_is_ecma()
00095           ? _M_ecma_escape_tbl
00096           : _M_awk_escape_tbl),
00097     _M_spec_char(_M_is_ecma()
00098          ? _M_ecma_spec_char
00099          : _M_is_basic()
00100          ? _M_basic_spec_char
00101          : _M_extended_spec_char),
00102     _M_at_bracket_start(false)
00103     { }
00104 
00105   protected:
00106     const char*
00107     _M_find_escape(char __c)
00108     {
00109       auto __it = _M_escape_tbl;
00110       for (; __it->first != '\0'; ++__it)
00111     if (__it->first == __c)
00112       return &__it->second;
00113       return nullptr;
00114     }
00115 
00116     bool
00117     _M_is_ecma() const
00118     { return _M_flags & regex_constants::ECMAScript; }
00119 
00120     bool
00121     _M_is_basic() const
00122     { return _M_flags & (regex_constants::basic | regex_constants::grep); }
00123 
00124     bool
00125     _M_is_extended() const
00126     {
00127       return _M_flags & (regex_constants::extended
00128              | regex_constants::egrep
00129              | regex_constants::awk);
00130     }
00131 
00132     bool
00133     _M_is_grep() const
00134     { return _M_flags & (regex_constants::grep | regex_constants::egrep); }
00135 
00136     bool
00137     _M_is_awk() const
00138     { return _M_flags & regex_constants::awk; }
00139 
00140   protected:
00141     const std::pair<char, _TokenT> _M_token_tbl[9] =
00142       {
00143     {'^', _S_token_line_begin},
00144     {'$', _S_token_line_end},
00145     {'.', _S_token_anychar},
00146     {'*', _S_token_closure0},
00147     {'+', _S_token_closure1},
00148     {'?', _S_token_opt},
00149     {'|', _S_token_or},
00150     {'\n', _S_token_or}, // grep and egrep
00151     {'\0', _S_token_or},
00152       };
00153     const std::pair<char, char> _M_ecma_escape_tbl[8] =
00154       {
00155     {'0', '\0'},
00156     {'b', '\b'},
00157     {'f', '\f'},
00158     {'n', '\n'},
00159     {'r', '\r'},
00160     {'t', '\t'},
00161     {'v', '\v'},
00162     {'\0', '\0'},
00163       };
00164     const std::pair<char, char> _M_awk_escape_tbl[11] =
00165       {
00166     {'"', '"'},
00167     {'/', '/'},
00168     {'\\', '\\'},
00169     {'a', '\a'},
00170     {'b', '\b'},
00171     {'f', '\f'},
00172     {'n', '\n'},
00173     {'r', '\r'},
00174     {'t', '\t'},
00175     {'v', '\v'},
00176     {'\0', '\0'},
00177       };
00178     const char* _M_ecma_spec_char = "^$\\.*+?()[]{}|";
00179     const char* _M_basic_spec_char = ".[\\*^$";
00180     const char* _M_extended_spec_char = ".[\\()*+?{|^$";
00181 
00182     _StateT                       _M_state;
00183     _FlagT                        _M_flags;
00184     _TokenT                       _M_token;
00185     const std::pair<char, char>*  _M_escape_tbl;
00186     const char*                   _M_spec_char;
00187     bool                          _M_at_bracket_start;
00188   };
00189 
00190   /**
00191    * @brief Scans an input range for regex tokens.
00192    *
00193    * The %_Scanner class interprets the regular expression pattern in
00194    * the input range passed to its constructor as a sequence of parse
00195    * tokens passed to the regular expression compiler.  The sequence
00196    * of tokens provided depends on the flag settings passed to the
00197    * constructor: different regular expression grammars will interpret
00198    * the same input pattern in syntactically different ways.
00199    */
00200   template<typename _CharT>
00201     class _Scanner
00202     : public _ScannerBase
00203     {
00204     public:
00205       typedef const _CharT*                                       _IterT;
00206       typedef std::basic_string<_CharT>                           _StringT;
00207       typedef regex_constants::syntax_option_type                 _FlagT;
00208       typedef const std::ctype<_CharT>                            _CtypeT;
00209 
00210       _Scanner(_IterT __begin, _IterT __end,
00211            _FlagT __flags, std::locale __loc);
00212 
00213       void
00214       _M_advance();
00215 
00216       _TokenT
00217       _M_get_token() const
00218       { return _M_token; }
00219 
00220       const _StringT&
00221       _M_get_value() const
00222       { return _M_value; }
00223 
00224 #ifdef _GLIBCXX_DEBUG
00225       std::ostream&
00226       _M_print(std::ostream&);
00227 #endif
00228 
00229     private:
00230       void
00231       _M_scan_normal();
00232 
00233       void
00234       _M_scan_in_bracket();
00235 
00236       void
00237       _M_scan_in_brace();
00238 
00239       void
00240       _M_eat_escape_ecma();
00241 
00242       void
00243       _M_eat_escape_posix();
00244 
00245       void
00246       _M_eat_escape_awk();
00247 
00248       void
00249       _M_eat_class(char);
00250 
00251       _IterT                        _M_current;
00252       _IterT                        _M_end;
00253       _CtypeT&                      _M_ctype;
00254       _StringT                      _M_value;
00255       void (_Scanner::* _M_eat_escape)();
00256     };
00257 
00258  //@} regex-detail
00259 _GLIBCXX_END_NAMESPACE_VERSION
00260 } // namespace __detail
00261 } // namespace std
00262 
00263 #include <bits/regex_scanner.tcc>