1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * @file bits/regex_nfa.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 namespace std _GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
37 // Base class for, um, automata. Could be an NFA or a DFA. Your choice.
41 typedef unsigned int _SizeT;
48 _M_sub_count() const = 0;
52 _M_dot(std::ostream& __ostr) const = 0;
56 // Generic shared pointer to an automaton.
57 typedef std::shared_ptr<_Automaton> _AutomatonPtr;
59 // Operation codes that define the type of transitions within the base NFA
60 // that represents the regular expression.
63 _S_opcode_unknown = 0,
64 _S_opcode_alternative = 1,
65 _S_opcode_subexpr_begin = 4,
66 _S_opcode_subexpr_end = 5,
67 _S_opcode_match = 100,
68 _S_opcode_accept = 255
71 // Provides a generic facade for a templated match_results.
74 virtual void _M_set_pos(int __i, int __j, const _PatternCursor& __p) = 0;
75 virtual void _M_set_matched(int __i, bool __is_matched) = 0;
78 // Tags current state (for subexpr begin/end).
79 typedef std::function<void (const _PatternCursor&, _Results&)> _Tagger;
81 template<typename _FwdIterT, typename _TraitsT>
90 operator()(const _PatternCursor& __pc, _Results& __r)
91 { __r._M_set_pos(_M_index, 0, __pc); }
96 template<typename _FwdIterT, typename _TraitsT>
105 operator()(const _PatternCursor& __pc, _Results& __r)
106 { __r._M_set_pos(_M_index, 1, __pc); }
111 // Indicates if current state matches cursor current.
112 typedef std::function<bool (const _PatternCursor&)> _Matcher;
114 // Matches any character
116 _AnyMatcher(const _PatternCursor&)
119 // Matches a single character
120 template<typename _InIterT, typename _TraitsT>
123 typedef typename _TraitsT::char_type char_type;
126 _CharMatcher(char_type __c, const _TraitsT& __t = _TraitsT())
127 : _M_traits(__t), _M_c(_M_traits.translate(__c))
131 operator()(const _PatternCursor& __pc) const
133 typedef const _SpecializedCursor<_InIterT>& _CursorT;
134 _CursorT __c = static_cast<_CursorT>(__pc);
135 return _M_traits.translate(__c._M_current()) == _M_c;
138 const _TraitsT& _M_traits;
142 // Matches a character range (bracket expression)
143 template<typename _InIterT, typename _TraitsT>
146 typedef typename _TraitsT::char_type _CharT;
147 typedef std::basic_string<_CharT> _StringT;
150 _RangeMatcher(bool __is_non_matching, const _TraitsT& __t = _TraitsT())
151 : _M_traits(__t), _M_is_non_matching(__is_non_matching)
155 operator()(const _PatternCursor& __pc) const
157 typedef const _SpecializedCursor<_InIterT>& _CursorT;
158 _CursorT __c = static_cast<_CursorT>(__pc);
163 _M_add_char(_CharT __c)
167 _M_add_collating_element(const _StringT& __s)
171 _M_add_equivalence_class(const _StringT& __s)
175 _M_add_character_class(const _StringT& __s)
182 const _TraitsT& _M_traits;
183 bool _M_is_non_matching;
186 // Identifies a state in the NFA.
187 typedef int _StateIdT;
189 // The special case in which a state identifier is not an index.
190 static const _StateIdT _S_invalid_state_id = -1;
193 // An individual state in an NFA
195 // In this case a "state" is an entry in the NFA definition coupled with its
196 // outgoing transition(s). All states have a single outgoing transition,
197 // except for accepting states (which have no outgoing transitions) and alt
198 // states, which have two outgoing transitions.
202 typedef int _OpcodeT;
204 _OpcodeT _M_opcode; // type of outgoing transition
205 _StateIdT _M_next; // outgoing transition
206 _StateIdT _M_alt; // for _S_opcode_alternative
207 unsigned int _M_subexpr; // for _S_opcode_subexpr_*
208 _Tagger _M_tagger; // for _S_opcode_subexpr_*
209 _Matcher _M_matches; // for _S_opcode_match
211 explicit _State(_OpcodeT __opcode)
212 : _M_opcode(__opcode), _M_next(_S_invalid_state_id)
215 _State(const _Matcher& __m)
216 : _M_opcode(_S_opcode_match), _M_next(_S_invalid_state_id), _M_matches(__m)
219 _State(_OpcodeT __opcode, unsigned int __s, const _Tagger& __t)
220 : _M_opcode(__opcode), _M_next(_S_invalid_state_id), _M_subexpr(__s),
224 _State(_StateIdT __next, _StateIdT __alt)
225 : _M_opcode(_S_opcode_alternative), _M_next(__next), _M_alt(__alt)
228 #ifdef _GLIBCXX_DEBUG
230 _M_print(std::ostream& ostr) const;
232 // Prints graphviz dot commands for state.
234 _M_dot(std::ostream& __ostr, _StateIdT __id) const;
239 // The Grep Matcher works on sets of states. Here are sets of states.
240 typedef std::set<_StateIdT> _StateSet;
242 // A collection of all states making up an NFA
244 // An NFA is a 4-tuple M = (K, S, s, F), where
245 // K is a finite set of states,
246 // S is the alphabet of the NFA,
247 // s is the initial state,
248 // F is a set of final (accepting) states.
250 // This NFA class is templated on S, a type that will hold values of the
251 // underlying alphabet (without regard to semantics of that alphabet). The
252 // other elements of the tuple are generated during construction of the NFA
253 // and are available through accessor member functions.
256 : public _Automaton, public std::vector<_State>
259 typedef _State _StateT;
260 typedef unsigned int _SizeT;
261 typedef regex_constants::syntax_option_type _FlagT;
265 : _M_flags(__f), _M_start_state(0), _M_subexpr_count(0)
277 { return _M_start_state; }
280 _M_final_states() const
281 { return _M_accepting_states; }
285 { return _M_subexpr_count; }
290 this->push_back(_StateT(_S_opcode_accept));
291 _M_accepting_states.insert(this->size()-1);
292 return this->size()-1;
296 _M_insert_alt(_StateIdT __next, _StateIdT __alt)
298 this->push_back(_StateT(__next, __alt));
299 return this->size()-1;
303 _M_insert_matcher(_Matcher __m)
305 this->push_back(_StateT(__m));
306 return this->size()-1;
310 _M_insert_subexpr_begin(const _Tagger& __t)
312 this->push_back(_StateT(_S_opcode_subexpr_begin, _M_subexpr_count++, __t));
313 return this->size()-1;
317 _M_insert_subexpr_end(unsigned int __i, const _Tagger& __t)
319 this->push_back(_StateT(_S_opcode_subexpr_end, __i, __t));
320 return this->size()-1;
323 #ifdef _GLIBCXX_DEBUG
325 _M_dot(std::ostream& __ostr) const;
330 _StateIdT _M_start_state;
331 _StateSet _M_accepting_states;
332 _SizeT _M_subexpr_count;
335 // Describes a sequence of one or more %_State, its current start and end(s).
337 // This structure contains fragments of an NFA during construction.
341 // Constructs a single-node sequence
342 _StateSeq(_Nfa& __ss, _StateIdT __s, _StateIdT __e = _S_invalid_state_id)
343 : _M_nfa(__ss), _M_start(__s), _M_end1(__s), _M_end2(__e)
345 // Constructs a split sequence from two other sequencces
346 _StateSeq(const _StateSeq& __e1, const _StateSeq& __e2)
347 : _M_nfa(__e1._M_nfa),
348 _M_start(_M_nfa._M_insert_alt(__e1._M_start, __e2._M_start)),
349 _M_end1(__e1._M_end1), _M_end2(__e2._M_end1)
352 // Constructs a split sequence from a single sequence
353 _StateSeq(const _StateSeq& __e, _StateIdT __id)
354 : _M_nfa(__e._M_nfa),
355 _M_start(_M_nfa._M_insert_alt(__id, __e._M_start)),
356 _M_end1(__id), _M_end2(__e._M_end1)
359 // Constructs a copy of a %_StateSeq
360 _StateSeq(const _StateSeq& __rhs)
361 : _M_nfa(__rhs._M_nfa), _M_start(__rhs._M_start),
362 _M_end1(__rhs._M_end1), _M_end2(__rhs._M_end2)
366 _StateSeq& operator=(const _StateSeq& __rhs);
372 // Extends a sequence by one.
374 _M_push_back(_StateIdT __id);
376 // Extends and maybe joins a sequence.
378 _M_append(_StateIdT __id);
381 _M_append(_StateSeq& __rhs);
383 // Clones an entire sequence.
395 _GLIBCXX_END_NAMESPACE_VERSION
396 } // namespace __regex
399 #include <bits/regex_nfa.tcc>