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