annotate Implab/Automaton/RegularExpressions/Token.cs @ 188:3071220371f8 ref20160224

Updated VS project
author cin
date Fri, 22 Apr 2016 12:59:31 +0300
parents d5c5db0335ee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using Implab;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Linq;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 namespace Implab.Automaton.RegularExpressions {
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
6 public abstract class Token {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
7 public abstract void Accept(IVisitor visitor);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8
178
d5c5db0335ee working on JSON parser
cin
parents: 177
diff changeset
9 public Token End() {
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
10 return Cat(new EndToken());
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
13 public Token Tag<TTag>(TTag tag) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 return Cat(new EndToken<TTag>(tag));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
17 public Token Cat(Token right) {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
18 return new CatToken(this, right);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
21 public Token Or(Token right) {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
22 return new AltToken(this, right);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
25 public Token Optional() {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
26 return Or(new EmptyToken());
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
29 public Token EClosure() {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
30 return new StarToken(this);
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
33 public Token Closure() {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
34 return Cat(new StarToken(this));
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
37 public Token Repeat(int count) {
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
38 Token token = null;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 for (int i = 0; i < count; i++)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 token = token != null ? token.Cat(this) : this;
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
42 return token ?? new EmptyToken();
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
45 public Token Repeat(int min, int max) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 if (min > max || min < 1)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47 throw new ArgumentOutOfRangeException();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 var token = Repeat(min);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50 for (int i = min; i < max; i++)
165
e227e78d72e4 DFA refactoring
cin
parents: 162
diff changeset
51 token = token.Cat( Optional() );
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 return token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
55 public static Token New(params int[] set) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 Safe.ArgumentNotNull(set, "set");
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
57 Token token = null;
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 foreach(var c in set.Distinct())
177
a0ff6a0e9c44 refactoring
cin
parents: 165
diff changeset
59 token = token == null ? new SymbolToken(c) : token.Or(new SymbolToken(c));
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 return token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 }