forked from MartinNowak/dranges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typepattern.d
757 lines (693 loc) · 23.7 KB
/
typepattern.d
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**
This module implements a basic pattern matching engine for type tuples. Think regexes, but for types:
"one or more int, followed by a string or a double, and then anything but a float".
Pattern expressions are built by aliasing the basic building blocks provided here and tested with the Match!(Pattern, Tuple...)
template. A match result is a struct exposing three fields:
$(UL
$(LI hasMatched: a boolean that tells us whether or not the typetuple matched the pattern)
$(LI Result: a typetuple holding the match result. If hasMatched is false, it will be empty.)
$(LI Post: the rest of the input typetuple, the part that was not consumed by the pattern.)
)
Usage:
----
// Let's define some basic patterns first:
alias OneOrMore!(int) Ints;
alias Either!(int, double) IOrD;
alias _ Any; // Predefined '_' pattern, matching any type.
alias And!(_,_) Two; // Matches any two types in succession. Will fail if passed 0 or 1 type.
// A more complicated example:
alias Successively!(
int,
None!(int,double,string),
_,
Repeat!(int, 2, 4),
Not!int,
End) Pattern;
// To match, use Match!(Pattern, TypeTuple):
alias Match!(Ints, int, int, int, double) Res1;
alias Match!(IOrD, double, int, int, double) Res2;
alias Match!(Any, double, int, int, double) Res3;
alias Match!(Two, double, int, int, double) Res4;
alias Match!(Pattern, int, float, string, int,int,int, string) Res5;
// All is done at compile-time, of course.
pragma(msg, Res1.Result.stringof, " // ", Res1.Post.stringof);
pragma(msg, Res2.Result.stringof, " // ", Res2.Post.stringof);
pragma(msg, Res3.Result.stringof, " // ", Res3.Post.stringof);
pragma(msg, Res4.Result.stringof, " // ", Res4.Post.stringof);
pragma(msg, Res5.Result.stringof, " // ", Res5.Post.stringof);
----
Features:
$(UL
$(LI Any type is its own pattern. 'int' matches an int and so on.)
$(LI Patterns can be tested in succession (And) or in alternation (Or). Predefined compositions like Successively and Either are there to help you.).
$(LI You can Repeat patterns min times, up to max times. There are predefined ZeroOrMore, OneOrMore, Option (aka ZeroOrOne) for your convenience.)
$(LI Patterns can (and will) backtrack to find a match.)
$(LI All patterns are tested only from the beginning of the tuple. In regex terms, it's as if they all begin with '^'. Use UnAnchored!Pattern to
make a pattern consume input until it finds something.)
$(LI You can easily extend the module by defining your own patterns: just define a Match(T...) template inside a struct that alias itself to a correct
match result.)
)
TODO:
$(UL
$(LI Maybe extend that to all template parameters, and not only types.)
$(LI No capture is implemented yet. That would mean passing an entire state around, to remember previous captures to give them to
a BackReference!(ref number) template. It's doable, but a bit to much for me right now.)
$(LI No named captures either...)
)
*/
module dranges.typepattern;
import std.typetuple;
import std.typecons;
import dranges.templates,
dranges.typetuple;
struct NoBacktrack {}
template MatchResult(bool successOrFailure, Types...)
{
struct Next(Rest...)
{
enum bool hasMatched = successOrFailure;
alias TypeTuple!(Types) Result;
alias TypeTuple!(Rest) Post;
}
}
template Failure(Rest...)
{
enum bool hasMatched = false;
alias TypeTuple!() Result;
alias TypeTuple!(Rest) Post;
}
template Success(Types...)
{
struct Next(Rest...)
{
enum bool hasMatched = true;
alias TypeTuple!(Types) Result;
alias TypeTuple!(Rest) Post;
}
}
template isMatchResult(T)
{
static if (T.hasMatched && is(T.Result) && is(T.Post))
enum bool isMatchResult = true;
else
enum bool isMatchResult = false;
}
/// The match engine, driving the entire pattern evaluation.
template Match(Pattern, Types...)
{
static if (is(Pattern.Match!Types))// && isMatchResult!(Pattern.Match!Types))
alias Pattern.Match!Types Match;
else static if (Types.length > 0 && is(Pattern == Types[0])) // Pattern is a normal type
alias Success!(Types[0]).Next!(Types[1..$]) Match;
else
alias Failure!(Types) Match;
}
template Match(alias Pattern, Types...)
{
// pragma(msg, "Testing ", Pattern.stringof, " with ", Types.stringof);
static if (is(Pattern.Match!Types))// && isMatchResult!(Pattern.Match!Types))
alias Pattern.Match!Types Match;
else
alias Failure!(Types) Match;
}
/**
Returns a tuple of all matches of a pattern in an input tuple. To distinguish them from one another, the matches are wrapped in Tuples.
Usage:
----
alias AllMatches!(Pattern).In!(InputTuple) AM;
----
Example:
----
// finds all couples where a type is followed by an int.
alias AllMatches!(And!(_,int)).In!( int,double,int,string,int*,int,int ) Ints;
assert(is(AM == TypeTuple!(
Tuple!(double,int),
Tuple!(int*,int),
Tuple!(int,int)
)));
----
*/
template AllMatches(Pattern)
{
template In(T...)
{
static if (Match!(Pattern, T).hasMatched)
alias TypeTuple!(
Tuple!(Match!(Pattern, T).Result),
// AllMatches!(Pattern, Match!(Pattern, T).Post) // ?
AllMatches!(Pattern).In!(T[1..$])
) In;
else static if (T.length)
alias AllMatches!(Pattern).In!(T[1..$]) In;
else
alias TypeTuple!() In;
}
}
/**
Replaces the first instance of $(M Pattern) with $(M WithThis) in an input typetuple.
Example:
----
alias Replace!(And!(_,int), float).In!(double,int, string,string,int) Rep;
assert(is( Rep == TypeTuple!(float, string, string, int)));
----
*/
template Replace(Pattern, WithThis)
{
template In(T...)
{
static if (Match!(Pattern, T).hasMatched)
alias TypeTuple!(
WithThis,
Match!(Pattern, T).Post
) In;
else static if (T.length)
alias TypeTuple!(
T[0],
Replace!(Pattern, WithThis).In!(T[1..$])
) In;
else
alias TypeTuple!() In;
}
}
/**
Replaces every instance of $(M Pattern) with $(M WithThis) in an input typetuple.
Example:
----
alias ReplaceAll!(And!(_,int), float).In!(double,int, string,string,int) Rep;
assert(is( Rep == TypeTuple!(float, string, float)));
----
*/
template ReplaceAll(Pattern, WithThis)
{
template In(T...)
{
static if (Match!(Pattern, T).hasMatched)
alias TypeTuple!(
WithThis,
ReplaceAll!(Pattern, WithThis).In!(Match!(Pattern, T).Post)
) In;
else static if (T.length)
alias TypeTuple!(
T[0],
ReplaceAll!(Pattern, WithThis).In!(T[1..$])
) In;
else
alias TypeTuple!() In;
}
}
/**
A cousin of $(M Replace): finds the first match of $(M Pattern) in an input typetuple and replaces it with $(M WithThis!(Match)).
Example:
----
// transforms the first integral type with its unsigned equivalent
alias Transform!(If!(isIntegral), Unsigned).In!( int,double,float,byte,ulong,string ) T;
assert(is( T == TypeTuple!(uint,double,float,byte,ulong,string)));
----
*/
template Transform(Pattern, alias WithThis)
{
template In(T...)
{
static if (Match!(Pattern, T).hasMatched)
alias TypeTuple!(
WithThis!(Match!(Pattern, T).Result),
Match!(Pattern, T).Post
) In;
else static if (T.length)
alias TypeTuple!(
T[0],
Transform!(Pattern, WithThis).In!(T[1..$])
) In;
else
alias TypeTuple!() In;
}
}
/**
A cousin of $(M ReplaceAll): finds all successive matches of $(M Pattern) in an input typetuple and replaces them with (M WithThis!(Match)).
Example:
----
// transforms all integral types with their unsigned equivalent
alias TransformAll!(If!(isIntegral), Unsigned).In!( int,double,float,byte,ulong,string ) T;
assert(is( T == TypeTuple!(uint,double,float,ubyte,ulong,string)));
----
*/
template TransformAll(Pattern, alias WithThis)
{
template In(T...)
{
static if (Match!(Pattern, T).hasMatched)
alias TypeTuple!(
WithThis!(Match!(Pattern, T).Result),
TransformAll!(Pattern, WithThis).In!(Match!(Pattern, T).Post)
) In;
else static if (T.length)
alias TypeTuple!(
T[0],
TransformAll!(Pattern, WithThis).In!(T[1..$])
) In;
else
alias TypeTuple!() In;
}
}
/**
Another take on pattern-matching. It takes couples of a pattern and a function (maybe a templated function)
as template parameters. When given a value as input, it will test the value type against all patterns
in succession. When a pattern matches, it will return the associated function's return.
Example:
----
alias match!(
OneOrMore!int, fun1,
And!(double, int), fun2,
_, fun3,
) matcher;
----
See_Also: $(M dranges.patternmatch), $(M dranges.functional.eitherFun).
*/
template match(Pattern, alias action, Rest...)
{
auto match(T...)(T t)
{
static if (Match!(Pattern, T).hasMatched)
{
Match!(Pattern, T).Result extract;
foreach(i, Type; Match!(Pattern, T).Result)
{
extract[i] = t[i];
}
return action(extract);
}
else static if (Rest.length)
{
alias .match!(Rest) matchTheRest;
return matchTheRest(t);
}
else
{
static assert(false, "Cannot match input of type " ~ T.stringof);
}
}
}
/// Matches any type. Think '.' for regexes. Does not match the empty typetuple.
struct _
{
template Match(T...)
{
static if (T.length > 0)
alias Success!(T[0]).Next!(T[1..$]) Match;
else
alias Failure!(T) Match;
}
}
/// Matches the end of a tuple (aka, the empty typetuple). Think '$' for regexes.
struct End
{
template Match(T...)
{
static if (T.length == 0)
alias Success!().Next!(T) Match;
else
alias Failure!(T) Match;
}
}
/**
It matches if $(M Pattern1) or $(M Pattern2) matches. More precisely, it tests $(M Pattern1).
If it matches, $(M Or) returns the match result.
If it fails, $(M Or) will try the second pattern and return its match result (which may be a failure).
If it tested only the first pattern, it can backtrack when asked to, by testing the second pattern.
*/
struct Or(Pattern1, Pattern2)
{
template Match(T...)
{
static if (.Match!(Pattern1, T).hasMatched)
alias .Match!(Pattern1, T) Match;
else static if (.Match!(Pattern2, T).hasMatched)
alias .Match!(Pattern2, T) Match;
else
alias Failure!(T) Match;
}
template Backtrack()
{
static if (.Match!(Pattern1, T).hasMatched)
alias Pattern2 Backtrack;
else
alias NoBacktrack Backtrack;
}
}
/**
Will match if and only if $(M Pattern1) matches and $(M Pattern2) matches after $(M Pattern1).
It's the basic building block for all type patterns, since it threads them in succession.
When you need to test P1 and then P2 and then P3, it's $(M And) you need.
If the second pattern fails, it will ask the first pattern to backtrack (if possible) and test the second pattern again.
See_Also: $(M Successively).
*/
struct And(Pattern1, Pattern2)
{
template Match(T...)
{
static if (.Match!(Pattern1, T).hasMatched)
{
static if (.Match!(Pattern2, .Match!(Pattern1, T).Post).hasMatched)
alias Success!(.Match!(Pattern1, T).Result, .Match!(Pattern2, .Match!(Pattern1, T).Post).Result).Next!(.Match!(Pattern2, .Match!(Pattern1, T).Post).Post) Match;
else static if (is(Pattern1.Backtrack!()) && !is(Pattern1.Backtrack!() == NoBacktrack))
alias And!(Pattern1.Backtrack!(), Pattern2).Match!(T) Match;
else
alias Failure!(T) Match;
}
else
alias Failure!(T) Match;
}
template Backtrack()
{
static if (is(Pattern2.Backtrack!()) && !is(Pattern2.Backtrack!() == NoBacktrack))
alias And!(Pattern1, Pattern2.Backtrack!()) Backtrack;
else static if (is(Pattern1.Backtrack!()) && !is(Pattern1.Backtrack!() == NoBacktrack))
alias And!(Pattern1.Backtrack!(), Pattern2) Backtrack;
else
alias NoBacktrack Backtrack;
}
}
/**
Convenience template, equivalent to $(M And!(P1, And!(P2, And!(P3, ...)))). It will try to match all the Pi:
if one fails, the entire template fails. It's composed of $(M And)s, so it will backtrack internally
as much as necessary to match all patterns.
*/
template Successively(Patterns...) if(Patterns.length > 0)
{
static if (Patterns.length > 1)
alias And!(Patterns[0], Successively!(Patterns[1..$])) Successively;
else
alias Patterns[0] Successively;
}
/**
Convenience template, equivalent to $(M Or!(P1, Or!(P2, Or!(...)))). It will try $(M P1), and stop there if it matches.
Else, it will test $(M P2) and so on. If forced to backtrack, it will continue with the yet untested patterns.
*/
template Either(Patterns...) if(Patterns.length > 0)
{
static if (Patterns.length > 1)
alias Or!(Patterns[0], Either!(Patterns[1..$])) Either;
else
alias Patterns[0] Either;
}
/**
Greedily matches $(M Pattern) at least $(M min) times up to (included) $(M max) times. If forced to backtrack, it will try one match less.
*/
template Repeat(Pattern, int min, int max) if (min >= 0 && max >= 0 && min <= max)
{
static if (min > 0)
alias Successively!(TypeNuple!(Pattern, min), RepeatImpl!(Pattern, max-min)) Repeat;
else
alias RepeatImpl!(Pattern, max) Repeat;
}
template Repeat(Pattern, int max) if (max >= 0)
{
alias Repeat!(Pattern, 0, max) Repeat;
}
struct RepeatImpl(Pattern, int max) //if (max >= 0)
{
template Match(T...)
{
static if (.Match!(Pattern, T).hasMatched)
// {
static if (max > 0)
alias And!(Pattern, RepeatImpl!(Pattern, max-1)).Match!(T) Match;
else
alias Success!().Next!(T) Match;
// }
else
alias Success!().Next!(T) Match;
}
template Backtrack()
{
static if (max > 0)
alias RepeatImpl!(Pattern, max-1) Backtrack;
else
alias NoBacktrack Backtrack;
}
}
/**
Repeat $(M Pattern min) times. If forced to backtrack, it will try one more match, up to $(M max).
*/
template LazyRepeat(Pattern, int min, int max) if (min <= max)
{
static if (min > 0)
alias Successively!(TypeNuple!(Pattern, min), .LazyRepeat!(Pattern, max-min)) LazyRepeat;
else
alias .LazyRepeat!(Pattern, max) LazyRepeat;
}
struct LazyRepeat(Pattern, int max)
{
template Match(T...)
{
alias TypeTuple!() Match;
}
template Backtrack()
{
static if (max > 0)
alias And!(Pattern, LazyRepeat!(Pattern, max-1)) Backtrack;
else
alias NoBacktrack Backtrack;
}
}
/// Tests for 0 or more $(M Pattern). I stopped at 128 max due to template instantiation limits.
template ZeroOrMore(Pattern)
{
alias Repeat!(Pattern, 128) ZeroOrMore;
}
/// Tests for 1 or more $(M Pattern). I stopped at 128 max due to template instantiation limits.
template OneOrMore(Pattern)
{
alias Repeat!(Pattern, 1, 128) OneOrMore;
}
/// 0 or 1 $(M Pattern)
template Option(Pattern)
{
alias Repeat!(Pattern, 0, 1) Option;
}
/// Invert the pattern.
struct Not(Pattern)
{
template Match(T...)
{
static if (.Match!(Pattern, T).hasMatched)
alias Failure!(T) Match;
else
alias .Match!(_, T) Match; // What should it do?
}
}
/// Matches only iff none of the $(M Patterns) match.
template None(Patterns...)
{
alias Not!(Either!Patterns) None;
}
/**
$(M isA) is a very powerful pattern: it matches if the input type is an instance of $(M templ) (a templated type) with the inner types matching $(M Patterns):
----
alias isA!(Tuple, _, _) Tuple2; // matches any Tuple!(_,_): a Tuple with two types
alias isA!(Tuple, OneOrMore!_) Tuple1; // matches any Tuple with one or more type inside.
// So, it matchesTuple!(int, string, double) but not Tuple!()
alias isA!(Tuple, int, ZeroOrMore!_) Tuple3; // matches Tuple!(int, double) or Tuple!(int, double, string) or even Tuple!(int)
----
*/
struct isA(alias templ, Patterns...)
{
template Match(T...)
{
static if ( T.length > 0
&& isInstanceOf!(templ, T[0]) // is T[0] a templ?
&& .Match!(Successively!Patterns, TemplateParametersTypeTuple!(T[0])).hasMatched // is T[0] type params the good ones?
&& .Match!(Successively!Patterns, TemplateParametersTypeTuple!(T[0])).Post.length == 0 // is there nothing left to consume?
)
alias Success!(T[0]).Next!(T[1..$]) Match;
else
alias Failure!(T) Match;
}
}
/**
$(M Inside) is like $(M isA), but the match result is the inner types, *not* the global type:
----
alias isA!(Tuple, _, _) Tuple2; // matches Tuple!(int, string), produces Tuple!(int, string)
alias Inside!(Tuple, _, _) Inside2; // matches Tuple!(int, string), but produces (int, string)
----
*/
struct Inside(alias templ, Patterns...)
{
template Match(T...)
{
static if ( T.length > 0
&& isInstanceOf!(templ, T[0]) // is T[0] a templ?
&& .Match!(Successively!Patterns, TemplateParametersTypeTuple!(T[0])).hasMatched // are T[0]'s type params the good ones?
&& .Match!(Successively!Patterns, TemplateParametersTypeTuple!(T[0])).Post.length == 0 // is there nothing left to consume?
)
alias Success!(.Match!(Successively!Patterns, TemplateParametersTypeTuple!(T[0])).Result).Next!(T[1..$]) Match;
else
alias Failure!(T) Match;
}
}
/**
Matches if $(M T[0]) verifies the condition (ie if $(M condition!(T[0])) is statically true)
----
alias If!(isIntegral) Integer; // will match int, byte, short, long, etc.
----
*/
struct If(alias condition)
{
template Match(T...)
{
static if (T.length && condition!(T[0]))
alias Success!(T[0]).Next!(T[1..$]) Match;
else
alias Failure!(T) Match;
}
}
/**
An extended version of the previous $(M If). If $(M T[0]) verifies the condition (ie if $(M condition!(T[0])) is statically true),
it'll use the $(M Then) pattern on $(M T). Else, it'll use the $(M Else) pattern.
*/
struct If(alias condition, Then, Else)
{
template Match(T...)
{
static if (T.length && condition!(T[0]))
alias .Match!(Then, T) Match;
else
alias .Match!(Else, T) Match;
}
}
/**
Variation on the same theme: $(M Then) is here a template that will be applied on the $(M If) match result.
That is, if $(M condition!(T[0])), returns $(M Then!(T[0])), else return $(M Else).
*/
struct If(alias condition, alias Then, Else)
{
template Match(T...)
{
static if (T.length && condition!(T[0]))
alias Success!(Then!(T[0])).Next!(T[1..$]) Match;
else
alias .Match!(Else, T) Match;
}
}
/**
The same, with $(M Else) being an alias.
*/
struct If(alias condition, Then, alias Else)
{
template Match(T...)
{
static if (T.length && condition!(T[0]))
alias .Match!(Then, T) Match;
else
alias Success!(Else!(T[0])).Next!(T[1..$]) Match;
}
}
/**
The same, with $(M Then) and $(M Else) being aliases.
*/
struct If(alias condition, alias Then, alias Else)
{
template Match(T...)
{
static if (T.length && condition!(T[0]))
alias Success!(Then!(T[0])).Next!(T[1..$]) Match;
else
alias Success!(Else!(T[0])).Next!(T[1..$]) Match;
}
}
/**
Transforms a pattern such that, if it matches, it puts back its match in the input tuple, for the next pattern
to consume.
See_Also: $(M LookAhead).
*/
struct ReleaseMatch(Pattern)
{
template Match(T...)
{
static if (.Match!(Pattern, T).hasMatched)
alias Success!().Next!(.Match!(Pattern, T).Result, .Match!(Pattern, T).Post) Match;
else
alias Failure!(T) Match;
}
}
/**
Transforms a pattern such that, if it matches, it discards its match: the match result is the empty typetuple.
See_Also: $(M Left), $(M Right), $(M LeftMost), $(M RightMost).
*/
struct DiscardMatch(Pattern)
{
template Match(T...)
{
static if (.Match!(Pattern, T).hasMatched)
alias Success!().Next!(.Match!(Pattern, T).Post) Match;
else
alias Failure!(T) Match;
}
}
/**
Matches iff $(M Pattern1) and then $(M Pattern2) match, but returns only $(M Pattern1)'s match result.
*/
template Left(Pattern1, Pattern2)
{
alias And!(Pattern1, DiscardMatch!Pattern2) Left;
}
/**
Matches iff $(M Pattern1) and then $(M Pattern2) match, but returns only $(M Pattern2)'s match result.
*/
template Right(Pattern1, Pattern2)
{
alias And!(DiscardMatch!Pattern1, Pattern2) Right;
}
/**
Matches iff all $(M Patterns) match, but returns only the first (leftmost) pattern match result.
*/
template LeftMost(Patterns...)
{
alias Successively!(Patterns[0], staticMap!(DiscardMatch, Patterns[1..$])) LeftMost;
}
/**
Matches iff all $(M Patterns) match, but returns only the last (rightmost) pattern match result.
*/
template RightMost(Patterns...)
{
alias Successively!(staticMap!(DiscardMatch, Patterns[0..$-1]), Patterns[$-1]) RightMost;
}
/**
Matches iff all $(M Patterns) match, returns the leftmost pattern match result, without consuming the rest.
*/
template LookAhead(Patterns...)
{
alias Successively!(Patterns[0], staticMap!(ReleaseMatch, Patterns[1..$])) LookAhead;
}
/**
Tests the first pattern. If it matches, it releases its match and then tests the second pattern in the same manner. All
patterns will be tested in this way, getting them to match on the same input (more or less). Only if all of them match
simultaneously will $(M Simultaneously) returns a match: the last pattern's match. Not to be confused with $(M And) or $(M Successively)
that test for matches while consuming the first patterns matches.
----
// S matches if there are two types in a row and the first one is an int. Returns 'int'.
alias Simultaneously!(And!(_,_), int) S;
----
Note: I'm not sure this is interesting, as there is frequently a way to express the same pattern in another way.
I'm just playing along with $(M ReleaseMatch), really.
*/
template Simultaneously(Patterns...)
{
alias Successively!(staticMap!(ReleaseMatch, Patterns[0..$-1]), Patterns[$-1]) Simultaneously;
}
/**
Transforms a pattern so that it'll consume input until it finds a match.
Only if it reaches the end of the typetuple without matching does it return $(M NoMatch).
*/
struct UnAnchored(Pattern)
{
template Match(T...)
{
static if (.Match!(Pattern,T).hasMatched)
alias .Match!(Pattern, T) Match;
else static if (T.length)
alias UnAnchored!(Pattern).Match!(T[1..$]) Match;
else
alias Failure!(T) Match;
}
}