comparison Implab/Automaton/RegularExpressions/Token.cs @ 177:a0ff6a0e9c44 ref20160224

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