comparison Implab/JSON/JSONGrammar.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents c0bf853aa04f
children 97fbbf816844
comparison
equal deleted inserted replaced
71:1714fd8678ef 72:d67b95eddaf4
5 using System.Text; 5 using System.Text;
6 using System.Threading.Tasks; 6 using System.Threading.Tasks;
7 7
8 namespace Implab.JSON { 8 namespace Implab.JSON {
9 internal class JSONGrammar : Grammar<JSONGrammar> { 9 internal class JSONGrammar : Grammar<JSONGrammar> {
10 public enum TokenType : int{ 10 public enum TokenType : int {
11 None, 11 None,
12 BeginObject, 12 BeginObject,
13 EndObject, 13 EndObject,
14 BeginArray, 14 BeginArray,
15 EndArray, 15 EndArray,
51 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure()); 51 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure());
52 var quote = SymbolToken('"'); 52 var quote = SymbolToken('"');
53 var backSlash = SymbolToken('\\'); 53 var backSlash = SymbolToken('\\');
54 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r'); 54 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r');
55 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4)); 55 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4));
56 var escape = backSlash.Cat(specialEscapeChars.Or(unicodeEspace));
57 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure(); 56 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure();
58 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace); 57 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace);
59 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace); 58 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace);
60 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace); 59 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace);
61 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace); 60 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace);
63 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace); 62 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace);
64 63
65 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional()); 64 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional());
66 var literal = letters.Closure(); 65 var literal = letters.Closure();
67 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x)); 66 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x));
68 var character = unescaped.Or(escape);
69 var str = quote.Cat(character.EClosure()).Cat(quote);
70
71 67
72 var jsonExpression = 68 var jsonExpression =
73 number.Tag(TokenType.Number) 69 number.Tag(TokenType.Number)
74 .Or(literal.Tag(TokenType.Literal)) 70 .Or(literal.Tag(TokenType.Literal))
75 .Or(quote.Tag(TokenType.StringBound)) 71 .Or(quote.Tag(TokenType.StringBound))
84 var jsonStringExpression = 80 var jsonStringExpression =
85 quote.Tag(TokenType.StringBound) 81 quote.Tag(TokenType.StringBound)
86 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) 82 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
87 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) 83 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
88 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); 84 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
89 85
90 var jsonNumberExpression =
91 minus.Tag(TokenType.Minus)
92 .Or(SymbolToken('+').Tag(TokenType.Plus))
93 .Or(digit.Closure().Tag(TokenType.Integer))
94 .Or(dot.Tag(TokenType.Dot))
95 .Or(expSign.Tag(TokenType.Exp));
96 86
97 m_jsonDFA = BuildDFA(jsonExpression); 87 m_jsonDFA = BuildDFA(jsonExpression);
98 m_stringDFA = BuildDFA(jsonStringExpression); 88 m_stringDFA = BuildDFA(jsonStringExpression);
99 } 89 }
100 90