/
Pattern.H
507 lines (389 loc) · 15 KB
/
Pattern.H
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/// Copyright 2013-2016 Henry G. Weller
/// Copyright 2007-2010 Philip L. Budne
// -----------------------------------------------------------------------------
// This file is part of
/// --- The PatMat Pattern Matcher
// -----------------------------------------------------------------------------
//
// PatMat is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
//
// Goofie 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.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, if you link this file with other files to produce an
// executable, this file does not by itself cause the resulting executable to
// be covered by the GNU General Public License. This exception does not
// however invalidate any other reasons why the executable file might be
// covered by the GNU Public License.
//
//
// PatMat was developed from the SPIPAT and GNAT.SPITBOL.PATTERNS package.
// GNAT was originally developed by the GNAT team at New York University.
// Extensive contributions were provided by Ada Core Technologies Inc.
// SPIPAT was developed by Philip L. Budne.
// -----------------------------------------------------------------------------
/// Title: Main Pattern class and user interface functions
/// Description:
// This package provides a complete set of operations for constructing
// patterns that can be used in the pattern matching operations provided.
// -----------------------------------------------------------------------------
#ifndef Pattern_H
#define Pattern_H
#include "CharacterSet.H"
#include <iostream>
#include <string>
// -----------------------------------------------------------------------------
namespace PatMat
{
// -----------------------------------------------------------------------------
/// Forward declarations
// -----------------------------------------------------------------------------
class Pattern;
class Pattern_;
class PatElmt_;
// -----------------------------------------------------------------------------
/// Typedefs
// -----------------------------------------------------------------------------
typedef char Character;
typedef unsigned int Natural;
typedef int Flags;
// -----------------------------------------------------------------------------
/// Internal Debugging
// -----------------------------------------------------------------------------
// Define to activate some built-in debugging traceback
//#define IDOUT(X) X
#define IDOUT(X)
// -----------------------------------------------------------------------------
/// StringGetter: string getter
// -----------------------------------------------------------------------------
//- String interface which returns a std::string. When this class is used as a
// formal parameter type in this package, it indicates a deferred pattern. The
// get function will be called when the pattern element is matched to
// obtain the currently referenced string value.
class StringGetter
{
public:
virtual ~StringGetter()
{}
virtual std::string get() const
{
Natural l;
const Character* p = get(l);
return std::string(p, l);
}
virtual const Character* get(Natural&) const = 0;
};
// -----------------------------------------------------------------------------
/// StringSetter: string setter
// -----------------------------------------------------------------------------
//- String interface which sets a std::string.
class StringSetter
{
public:
virtual ~StringSetter()
{}
virtual void set(const std::string&) = 0;
};
// -----------------------------------------------------------------------------
/// UnsignedGetter: Natural getter
// -----------------------------------------------------------------------------
//- Unsigned interface which returns an Natural.
// When this type is used as a formal parameter type in this package, it
// indicates a deferred pattern. The function will be called when the pattern
// element is matched to obtain the currently referenced Natural value.
class UnsignedGetter
{
public:
virtual ~UnsignedGetter()
{}
virtual Natural get() const = 0;
};
// -----------------------------------------------------------------------------
/// UnsignedSetter: Natural setter
// -----------------------------------------------------------------------------
//- Unsigned interface which sets an Natural.
class UnsignedSetter
{
public:
virtual ~UnsignedSetter()
{}
virtual void set(const Natural) = 0;
};
// -----------------------------------------------------------------------------
/// BoolGetter: boolean getter
// -----------------------------------------------------------------------------
//- Bool interface which returns a bool. When this class is used as a formal
// parameter type in this package, it indicates a deferred predicate
// pattern. The function will be called when the pattern element is matched and
// failure signalled if false is returned.
class BoolGetter
{
public:
virtual ~BoolGetter()
{}
virtual bool get() const = 0;
};
// -----------------------------------------------------------------------------
/// Match data
// -----------------------------------------------------------------------------
enum MatchRet
{
MATCH_UNITITIALIZED_PATTERN,
MATCH_LOGIC_ERROR,
MATCH_FAILURE,
MATCH_SUCCESS
};
const char* const MatchRetMessages[] =
{
"Uninitialized pattern",
"Internal logic error patterns",
"Match failure",
"Match success"
};
class MatchState
{
public:
// Output from match
Natural start_, stop_;
MatchRet ret_;
MatchState()
:
start_(0),
stop_(0),
ret_(MATCH_FAILURE)
{}
inline bool matched() const
{
return ret_ == MATCH_SUCCESS;
}
inline operator bool() const
{
return matched();
}
inline Natural start() const
{
return start_;
}
inline Natural stop() const
{
return stop_;
}
};
class MutableMatchState
:
public MatchState
{
public:
std::string& subject_;
MutableMatchState(const MatchState& ms, std::string& subject)
:
MatchState(ms),
subject_(subject)
{}
bool operator=(const std::string& replacement)
{
if (matched())
{
subject_.replace(start_, stop_ - start_, replacement);
return true;
}
return false;
}
bool operator=(const Character* replacement)
{
if (matched())
{
subject_.replace(start_, stop_ - start_, replacement);
return true;
}
return false;
}
};
// -----------------------------------------------------------------------------
/// Pattern: pattern object
// -----------------------------------------------------------------------------
class Pattern
{
// Private data
Pattern_* pat_;
// Private static functions
static Pattern inline orStrPat(const std::string&, const Pattern&);
static Pattern inline orPatStr(const Pattern&, const std::string&);
static Pattern inline orStrStr(const std::string&, const std::string&);
Pattern(Natural stackIndex, const PatElmt_* P);
public:
// Match flags
static const int debug = 1;
static const int anchor = 2;
static const int trace = 4;
// Constructors
Pattern();
Pattern(const Pattern&);
Pattern(const Character);
Pattern(const std::string&);
Pattern(const Character*);
Pattern(const Character* ptr, const Natural len);
Pattern(const StringGetter&);
// Destructor
~Pattern();
// Member operators
Pattern& operator=(const Pattern&);
// Alternation member operators
Pattern& operator|=(const std::string&);
Pattern& operator|=(const Character*);
Pattern& operator|=(const Character);
Pattern& operator|=(const Pattern&);
// Concatenation member operators
Pattern& operator&=(const std::string&);
Pattern& operator&=(const Character*);
Pattern& operator&=(const Character);
Pattern& operator&=(const Pattern&);
// Pattern construction functions not declared as methods
// to keep usage SNOBOL-like.
friend Pattern Abort();
// Alternation
friend Pattern operator|(const std::string& l, const Pattern& r);
friend Pattern operator|(const Character* l, const Pattern& r);
friend Pattern operator|(const Pattern& l, const std::string& r);
friend Pattern operator|(const Pattern& l, const Character* r);
friend Pattern operator|(const Pattern& l, const Pattern& r);
friend Pattern operator|(const Character l, const Pattern& r);
friend Pattern operator|(const Pattern& l, const Character r);
friend Pattern operator|(const std::string& l, const std::string& r);
friend Pattern operator|(const Character* l, const std::string& r);
friend Pattern operator|(const std::string& l, const Character* r);
friend Pattern Or(const Character* l, const Character* r);
friend Pattern operator|(const std::string& l, const Character r);
friend Pattern Or(const Character* l, const Character r);
friend Pattern operator|(const Character l, const std::string& r);
friend Pattern Or(const Character l, const Character* r);
friend Pattern Or(const Character l, const Character r);
friend Pattern Any(const Character);
friend Pattern Any(const CharacterSet&);
friend Pattern Any(const std::string&);
friend Pattern Any(const std::string*);
friend Pattern Any(const StringGetter&);
friend Pattern Arb();
friend Pattern Arbno(const Pattern&);
friend Pattern Arbno(const Character*);
friend Pattern Arbno(const std::string&);
friend Pattern Arbno(const Character);
// Assignment
friend Pattern operator*(const Pattern& p, std::string&);
friend Pattern operator*(const Pattern& p, StringSetter&);
friend Pattern operator%(const Pattern& p, std::string&);
friend Pattern operator%(const Pattern& p, StringSetter&);
friend Pattern Bal(const Character open, const Character close);
friend Pattern Break(const Character);
friend Pattern Break(const CharacterSet&);
friend Pattern Break(const std::string&);
friend Pattern Break(const std::string*);
friend Pattern Break(const StringGetter&);
friend Pattern BreakXMake(PatElmt_*);
friend Pattern BreakX(const Character);
friend Pattern BreakX(const CharacterSet&);
friend Pattern BreakX(const std::string&);
friend Pattern BreakX(const std::string*);
friend Pattern BreakX(const StringGetter&);
// Concatenation
friend Pattern operator&(const std::string& l, const Pattern& r);
friend Pattern operator&(const Character* l, const Pattern& r);
friend Pattern operator&(const Pattern& l, const std::string& r);
friend Pattern operator&(const Pattern& l, const Character* r);
friend Pattern operator&(const Pattern& l, const Pattern& r);
friend Pattern operator&(const Character l, const Pattern& r);
friend Pattern operator&(const Pattern& l, const Character r);
friend Pattern Defer(const Pattern&);
friend Pattern Defer(const std::string&);
friend Pattern Defer(const StringGetter&);
friend Pattern Defer(const BoolGetter&);
friend Pattern Fail();
friend Pattern Fence();
friend Pattern Fence(const Pattern&);
friend Pattern Len(const Natural);
friend Pattern Len(const UnsignedGetter&);
friend Pattern Len(const Natural*);
friend Pattern NotAny(const Character);
friend Pattern NotAny(const CharacterSet&);
friend Pattern NotAny(const std::string&);
friend Pattern NotAny(const std::string*);
friend Pattern NotAny(const StringGetter&);
friend Pattern NSpan(const Character);
friend Pattern NSpan(const CharacterSet&);
friend Pattern NSpan(const std::string&);
friend Pattern NSpan(const std::string*);
friend Pattern NSpan(const StringGetter&);
friend Pattern Pos(const Natural);
friend Pattern Pos(const UnsignedGetter&);
friend Pattern Pos(const Natural*);
friend Pattern Rem();
friend Pattern Rpos(const Natural);
friend Pattern Rpos(const UnsignedGetter&);
friend Pattern Rpos(const Natural*);
friend Pattern Rtab(const Natural);
friend Pattern Rtab(const UnsignedGetter&);
friend Pattern Rtab(const Natural*);
friend Pattern Setcur(Natural&);
friend Pattern Span(const Character);
friend Pattern Span(const CharacterSet&);
friend Pattern Span(const std::string&);
friend Pattern Span(const std::string*);
friend Pattern Span(const StringGetter&);
friend Pattern Succeed();
friend Pattern Tab(const Natural);
friend Pattern Tab(const UnsignedGetter&);
friend Pattern Tab(const Natural*);
friend Pattern assignOnmatch(const Pattern& p, std::string& var);
friend Pattern assignImmed(const Pattern& p, std::string& var);
// Matching
bool operator()
(
const Character* subject,
const Flags flags = 0
) const;
bool operator()
(
const std::string& subject,
const Flags flags = 0
) const;
MutableMatchState operator()
(
std::string& subject,
const Flags flags = 0
) const;
// Output
inline void debugMsg(const Character* fmt) const;
void dump(std::ostream&) const;
friend std::ostream& operator<<(std::ostream&, const Pattern&);
};
// -----------------------------------------------------------------------------
/// Sout: string setter which outputs the string
// -----------------------------------------------------------------------------
class Output
:
public StringSetter
{
public:
void set(const std::string& str)
{
std::cout << str;
}
};
extern Output output;
// ----------------------------------------------------------------------------
/// Pattern functions and operators
// ----------------------------------------------------------------------------
#include "PatternOperations.H"
// -----------------------------------------------------------------------------
} // End namespace PatMat
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
#endif // Pattern_H
// -----------------------------------------------------------------------------