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