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