annotate Implab/Automaton/RegularExpressions/Token.cs @ 173:ecfece82ca11 ref20160224

Working on text scanner
author cin
date Tue, 15 Mar 2016 02:11:06 +0300
parents e227e78d72e4
children a0ff6a0e9c44
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 {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6 public abstract class Token<TTag> {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7 public abstract void Accept(IVisitor<TTag> visitor);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 public Token<TTag> Extend() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 return Cat(new EndToken<TTag>());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12
165
e227e78d72e4 DFA refactoring
cin
parents: 162
diff changeset
13 public Token<TTag> Tag(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
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 public Token<TTag> Cat(Token<TTag> right) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18 return new CatToken<TTag>(this, right);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 public Token<TTag> Or(Token<TTag> right) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22 return new AltToken<TTag>(this, right);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 public Token<TTag> Optional() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26 return Or(new EmptyToken<TTag>());
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29 public Token<TTag> EClosure() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 return new StarToken<TTag>(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33 public Token<TTag> Closure() {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 return Cat(new StarToken<TTag>(this));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 public Token<TTag> Repeat(int count) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38 Token<TTag> token = null;
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;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 return token ?? new EmptyToken<TTag>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 public Token<TTag> Repeat(int min, int max) {
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
165
e227e78d72e4 DFA refactoring
cin
parents: 162
diff changeset
55 public static Token<TTag> New(params int[] set) {
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 Safe.ArgumentNotNull(set, "set");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 Token<TTag> token = null;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58 foreach(var c in set.Distinct())
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 token = token == null ? new SymbolToken<TTag>(c) : token.Or(new SymbolToken<TTag>(c));
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 }