162
|
1 using Implab;
|
|
2 using System;
|
|
3 using System.Linq;
|
|
4
|
|
5 namespace Implab.Automaton.RegularExpressions {
|
177
|
6 public abstract class Token {
|
|
7 public abstract void Accept(IVisitor visitor);
|
162
|
8
|
178
|
9 public Token End() {
|
177
|
10 return Cat(new EndToken());
|
162
|
11 }
|
|
12
|
177
|
13 public Token Tag<TTag>(TTag tag) {
|
162
|
14 return Cat(new EndToken<TTag>(tag));
|
|
15 }
|
|
16
|
177
|
17 public Token Cat(Token right) {
|
|
18 return new CatToken(this, right);
|
162
|
19 }
|
|
20
|
177
|
21 public Token Or(Token right) {
|
|
22 return new AltToken(this, right);
|
162
|
23 }
|
|
24
|
177
|
25 public Token Optional() {
|
|
26 return Or(new EmptyToken());
|
162
|
27 }
|
|
28
|
177
|
29 public Token EClosure() {
|
|
30 return new StarToken(this);
|
162
|
31 }
|
|
32
|
177
|
33 public Token Closure() {
|
|
34 return Cat(new StarToken(this));
|
162
|
35 }
|
|
36
|
177
|
37 public Token Repeat(int count) {
|
|
38 Token token = null;
|
162
|
39
|
|
40 for (int i = 0; i < count; i++)
|
|
41 token = token != null ? token.Cat(this) : this;
|
177
|
42 return token ?? new EmptyToken();
|
162
|
43 }
|
|
44
|
177
|
45 public Token Repeat(int min, int max) {
|
162
|
46 if (min > max || min < 1)
|
|
47 throw new ArgumentOutOfRangeException();
|
|
48 var token = Repeat(min);
|
|
49
|
|
50 for (int i = min; i < max; i++)
|
165
|
51 token = token.Cat( Optional() );
|
162
|
52 return token;
|
|
53 }
|
|
54
|
177
|
55 public static Token New(params int[] set) {
|
162
|
56 Safe.ArgumentNotNull(set, "set");
|
177
|
57 Token token = null;
|
162
|
58 foreach(var c in set.Distinct())
|
177
|
59 token = token == null ? new SymbolToken(c) : token.Or(new SymbolToken(c));
|
162
|
60 return token;
|
|
61 }
|
|
62 }
|
|
63 }
|