summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/regex_executor.h
blob: b8e9266f910255db59078be4bc1b7592feb97d3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// class template regex -*- C++ -*-

// Copyright (C) 2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/**
 *  @file bits/regex_executor.h
 *  This is an internal header file, included by other library headers.
 *  Do not attempt to use it directly. @headername{regex}
 */

// FIXME convert comments to doxygen format.

// TODO Put _DFSExecutor and _BFSExecutor into one class. They are becoming
// much more similar. Also, make grouping seperated. The
// regex_constants::nosubs enables much more simpler execution.

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  template<typename, typename>
    class basic_regex;

  template<typename>
    class sub_match;

  template<typename, typename>
    class match_results;
_GLIBCXX_END_NAMESPACE_VERSION

namespace __detail
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   * @addtogroup regex-detail
   * @{
   */

  template<typename _BiIter, typename _Alloc,
    typename _CharT, typename _TraitsT>
    class _Executor
    {
    public:
      typedef basic_regex<_CharT, _TraitsT>           _RegexT;
      typedef match_results<_BiIter, _Alloc>          _ResultsT;
      typedef std::vector<sub_match<_BiIter>, _Alloc> _ResultsVec;
      typedef regex_constants::match_flag_type        _FlagT;
      typedef typename _TraitsT::char_class_type      _ClassT;

    public:
      _Executor(_BiIter         __begin,
		_BiIter         __end,
		_ResultsT&      __results,
		const _RegexT&  __re,
		_FlagT          __flags)
      : _M_begin(__begin),
      _M_end(__end),
      _M_results(__results),
      _M_re(__re),
      _M_flags(__flags)
      { }

      // Set matched when string exactly match the pattern.
      bool
      _M_match()
      {
	_M_match_mode = true;
	_M_init(_M_begin);
	return _M_main();
      }

      // Set matched when some prefix of the string matches the pattern.
      bool
      _M_search_from_first()
      {
	_M_match_mode = false;
	_M_init(_M_begin);
	return _M_main();
      }

      bool
      _M_search()
      {
	if (_M_flags & regex_constants::match_continuous)
	  return _M_search_from_first();
	auto __cur = _M_begin;
	do
	  {
	    _M_match_mode = false;
	    _M_init(__cur);
	    if (_M_main())
	      return true;
	  }
	// Continue when __cur == _M_end
	while (__cur++ != _M_end);
	return false;
      }

      bool
      _M_is_word(_CharT __ch) const
      {
	static const _CharT __s = 'w';
	return _M_re._M_traits.isctype
	  (__ch, _M_re._M_traits.lookup_classname(&__s, &__s+1));
      }

      bool
      _M_at_begin() const
      {
	return _M_current == _M_begin
	  && !(_M_flags & (regex_constants::match_not_bol
			   | regex_constants::match_prev_avail));
      }

      bool
      _M_at_end() const
      {
	return _M_current == _M_end
	  && !(_M_flags & regex_constants::match_not_eol);
      }

      bool
      _M_word_boundry(_State<_CharT, _TraitsT> __state) const;

      bool
      _M_lookahead(_State<_CharT, _TraitsT> __state) const;

    public:
      virtual void
      _M_init(_BiIter __cur) = 0;

      virtual void
      _M_set_start(_StateIdT __start) = 0;

      virtual bool
      _M_main() = 0;

      _BiIter         _M_current;
      const _BiIter   _M_begin;
      const _BiIter   _M_end;
      const _RegexT&  _M_re;
      _ResultsT&      _M_results;
      const _FlagT    _M_flags;
      bool            _M_match_mode;
    };

  // A _DFSExecutor perform a DFS on given NFA and input string. At the very
  // beginning the executor stands in the start state, then it try every
  // possible state transition in current state recursively. Some state
  // transitions consume input string, say, a single-char-matcher or a
  // back-reference matcher; some not, like assertion or other anchor nodes.
  // When the input is exhausted and the current state is an accepting state,
  // the whole executor return true.
  //
  // TODO: This approach is exponentially slow for certain input.
  //       Try to compile the NFA to a DFA.
  //
  // Time complexity: exponential
  // Space complexity: O(__end - __begin)
  template<typename _BiIter, typename _Alloc,
    typename _CharT, typename _TraitsT>
    class _DFSExecutor
    : public _Executor<_BiIter, _Alloc, _CharT, _TraitsT>
    {
    public:
      typedef _Executor<_BiIter, _Alloc, _CharT, _TraitsT> _BaseT;
      typedef _NFA<_CharT, _TraitsT>                       _NFAT;
      typedef typename _BaseT::_RegexT                     _RegexT;
      typedef typename _BaseT::_ResultsT                   _ResultsT;
      typedef typename _BaseT::_ResultsVec                 _ResultsVec;
      typedef typename _BaseT::_FlagT                      _FlagT;

    public:
      _DFSExecutor(_BiIter         __begin,
		   _BiIter         __end,
		   _ResultsT&      __results,
		   const _RegexT&  __re,
		   _FlagT          __flags)
      : _BaseT(__begin, __end, __results, __re, __flags),
      _M_nfa(*std::static_pointer_cast<_NFA<_CharT, _TraitsT>>
	     (__re._M_automaton)),
      _M_start_state(_M_nfa._M_start())
      { }

    private:
      void
      _M_init(_BiIter __cur)
      {
	_M_cur_results.resize(_M_nfa._M_sub_count() + 2);
	this->_M_current = __cur;
      }

      void
      _M_set_start(_StateIdT __start)
      { _M_start_state = __start; }

      bool
      _M_main()
      { return _M_dfs(this->_M_start_state); }

      bool
      _M_dfs(_StateIdT __start);

      // To record current solution.
      _ResultsVec     _M_cur_results;
      const _NFAT&    _M_nfa;
      _StateIdT       _M_start_state;
    };

  // Like the DFS approach, it try every possible state transition; Unlike DFS,
  // it uses a queue instead of a stack to store matching states. It's a BFS
  // approach.
  //
  // Russ Cox's article(http://swtch.com/~rsc/regexp/regexp1.html) explained
  // this algorithm clearly.
  //
  // Every entry of _M_covered saves the solution(grouping status) for every
  // matching head. When states transit, solutions will be compared and
  // deduplicated(based on which greedy mode we have).
  //
  // Time complexity: O((__end - __begin) * _M_nfa.size())
  // Space complexity: O(_M_nfa.size() * _M_nfa.mark_count())
  template<typename _BiIter, typename _Alloc,
    typename _CharT, typename _TraitsT>
    class _BFSExecutor
    : public _Executor<_BiIter, _Alloc, _CharT, _TraitsT>
    {
    public:
      typedef _Executor<_BiIter, _Alloc, _CharT, _TraitsT> _BaseT;
      typedef _NFA<_CharT, _TraitsT>                       _NFAT;
      typedef typename _BaseT::_RegexT                     _RegexT;
      typedef typename _BaseT::_ResultsT                   _ResultsT;
      typedef typename _BaseT::_ResultsVec                 _ResultsVec;
      typedef typename _BaseT::_FlagT                      _FlagT;
      // Here's a solution for greedy/ungreedy mode in BFS approach. We need to
      // carefully work out how to compare to conflict matching states.
      //
      // A matching state is a pair(where, when); `where` is a NFA node; `when`
      // is a _BiIter, indicating which char is the next to be matched. Two
      // matching states conflict if they have equivalent `where` and `when`.
      //
      // Now we need to drop one and keep another, because at most one of them
      // could be the final optimal solution. This behavior is affected by
      // greedy policy.
      //
      // The definition of `greedy`:
      // For the sequence of quantifiers in NFA sorted by there start position,
      // now maintain a vector in every matching state, with equal length to
      // quantifier seq, recording repeating times of every quantifier. Now to
      // compare two matching states, we just lexically compare these two
      // vectors. To win the compare(to survive), one matching state needs to
      // make its greedy quantifier count larger, and ungreedy quantifiers
      // count smaller.
      //
      // In the implementation, we recorded negtive counts for greedy
      // quantifiers and positive counts of ungreedy ones. Now the implicit
      // operator<() for lexicographical_compare will emit the answer.
      //
      // When two vectors equal, it means the `where`, `when` and quantifier
      // counts are identical, and indicates the same solution; so just return
      // false.
      struct _ResultsEntry
      : private _ResultsVec
      {
      public:
	_ResultsEntry(unsigned int __res_sz, unsigned int __sz)
	: _ResultsVec(__res_sz), _M_quant_keys(__sz)
	{ }

	void
	resize(unsigned int __n)
	{ _ResultsVec::resize(__n); }

	unsigned int
	size()
	{ return _ResultsVec::size(); }

	sub_match<_BiIter>&
	operator[](unsigned int __idx)
	{ return _ResultsVec::operator[](__idx); }

	bool
	operator<(const _ResultsEntry& __rhs) const
	{
	  _GLIBCXX_DEBUG_ASSERT(_M_quant_keys.size()
				== __rhs._M_quant_keys.size());
	  return lexicographical_compare(_M_quant_keys.begin(),
					 _M_quant_keys.end(),
					 __rhs._M_quant_keys.begin(),
					 __rhs._M_quant_keys.end());
	}

	void
	_M_inc(unsigned int __idx, bool __neg)
	{ _M_quant_keys[__idx] += __neg ? 1 : -1; }

	_ResultsVec
	_M_get()
	{ return *this; }

      public:
	std::vector<int> _M_quant_keys;
      };
      typedef std::unique_ptr<_ResultsEntry>               _ResultsPtr;

    public:
      _BFSExecutor(_BiIter         __begin,
		   _BiIter         __end,
		   _ResultsT&      __results,
		   const _RegexT&  __re,
		   _FlagT          __flags)
      : _BaseT(__begin, __end, __results, __re, __flags),
      _M_nfa(*std::static_pointer_cast<_NFA<_CharT, _TraitsT>>
	     (__re._M_automaton)),
      _M_start_state(_M_nfa._M_start())
      { }

    private:
      void
      _M_init(_BiIter __cur)
      {
	_GLIBCXX_DEBUG_ASSERT(this->_M_start_state != _S_invalid_state_id);
	this->_M_current = __cur;
	_M_covered.clear();
	_ResultsVec& __res(this->_M_results);
	_M_covered[this->_M_start_state] =
	  _ResultsPtr(new _ResultsEntry(__res.size(),
					_M_nfa._M_quant_count));
	_M_e_closure();
      }

      void
      _M_set_start(_StateIdT __start)
      { _M_start_state = __start; }

      bool
      _M_main();

      void
      _M_e_closure();

      void
      _M_move();

      bool
      _M_includes_some();

      std::map<_StateIdT, _ResultsPtr> _M_covered;
      // To record global optimal solution.
      _ResultsPtr                      _M_cur_results;
      const _NFAT&                     _M_nfa;
      _StateIdT                        _M_start_state;
    };

  template<typename _BiIter, typename _Alloc,
    typename _CharT, typename _TraitsT>
    std::unique_ptr<_Executor<_BiIter, _Alloc, _CharT, _TraitsT>>
    __get_executor(_BiIter __b,
		   _BiIter __e,
		   match_results<_BiIter, _Alloc>& __m,
		   const basic_regex<_CharT, _TraitsT>& __re,
		   regex_constants::match_flag_type __flags);

 //@} regex-detail
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace __detail
} // namespace std

#include <bits/regex_executor.tcc>