annotate Implab/Parsing/Token.cs @ 95:0141a165d032 v2

minor changes
author cin
date Wed, 29 Oct 2014 18:54:46 +0300
parents c0bf853aa04f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Globalization;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 namespace Implab.Parsing {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 public abstract class Token {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 public abstract void Accept(IVisitor visitor);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 public Token Extend() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 return new CatToken(this, new EndToken());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 public Token Tag<T>(T tag) where T : IConvertible {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 return new CatToken(this, new EndToken(tag.ToInt32(CultureInfo.InvariantCulture)));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 public Token Cat(Token right) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 return new CatToken(this, right);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 public Token Or(Token right) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 return new AltToken(this, right);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 public Token Optional() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 return Or(new EmptyToken());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 public Token EClosure() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 return new StarToken(this);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 public Token Closure() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 return new CatToken(this, new StarToken(this));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 public Token Repeat(int count) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 Token token = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 for (int i = 0; i < count; i++)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 token = token != null ? token.Cat(this) : this;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 return token ?? new EmptyToken();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 public Token Repeat(int min, int max) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50 if (min > max || min < 1)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 throw new ArgumentOutOfRangeException();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 var token = Repeat(min);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 for (int i = min; i < max; i++)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 token = token.Cat( this.Optional() );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 return token;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 public static Token New<T>(params T[] set) where T : struct, IConvertible {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 Safe.ArgumentNotNull(set, "set");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 Token token = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 foreach(var c in set.Distinct())
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 token = token == null ? new SymbolToken(c.ToInt32(CultureInfo.InvariantCulture)) : token.Or(new SymbolToken(c.ToInt32(CultureInfo.InvariantCulture)));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 return token;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 }