55
|
1 using Implab.Parsing;
|
|
2 using System;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Linq;
|
|
5 using System.Text;
|
|
6 using System.Threading.Tasks;
|
|
7
|
|
8 namespace Implab.JSON {
|
|
9 internal class JSONGrammar : Grammar<JSONGrammar> {
|
72
|
10 public enum TokenType : int {
|
55
|
11 None,
|
|
12 BeginObject,
|
|
13 EndObject,
|
|
14 BeginArray,
|
|
15 EndArray,
|
|
16 String,
|
|
17 Number,
|
|
18 Literal,
|
|
19 NameSeparator,
|
|
20 ValueSeparator,
|
|
21
|
|
22 StringBound,
|
|
23 EscapedChar,
|
|
24 UnescapedChar,
|
|
25 EscapedUnicode,
|
|
26
|
|
27 Minus,
|
|
28 Plus,
|
|
29 Sign,
|
|
30 Integer,
|
|
31 Dot,
|
|
32 Exp
|
|
33 }
|
|
34
|
|
35 readonly CDFADefinition m_jsonDFA;
|
|
36 readonly CDFADefinition m_stringDFA;
|
|
37
|
|
38 public JSONGrammar() {
|
|
39 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
|
|
40 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
|
|
41 var digit9 = SymbolRangeToken('1', '9');
|
|
42 var zero = SymbolToken('0');
|
|
43 var digit = zero.Or(digit9);
|
|
44 var dot = SymbolToken('.');
|
|
45 var minus = SymbolToken('-');
|
|
46 var sign = SymbolSetToken('-', '+');
|
|
47 var expSign = SymbolSetToken('e', 'E');
|
|
48 var letters = SymbolRangeToken('a', 'z');
|
|
49 var integer = zero.Or(digit9.Cat(digit.EClosure()));
|
|
50 var frac = dot.Cat(digit.Closure());
|
|
51 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure());
|
|
52 var quote = SymbolToken('"');
|
|
53 var backSlash = SymbolToken('\\');
|
|
54 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r');
|
|
55 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4));
|
|
56 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure();
|
|
57 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace);
|
|
58 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace);
|
|
59 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace);
|
|
60 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace);
|
|
61 var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace);
|
|
62 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace);
|
|
63
|
|
64 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional());
|
|
65 var literal = letters.Closure();
|
|
66 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x));
|
|
67
|
|
68 var jsonExpression =
|
|
69 number.Tag(TokenType.Number)
|
|
70 .Or(literal.Tag(TokenType.Literal))
|
|
71 .Or(quote.Tag(TokenType.StringBound))
|
|
72 .Or(beginObject.Tag(TokenType.BeginObject))
|
|
73 .Or(endObject.Tag(TokenType.EndObject))
|
|
74 .Or(beginArray.Tag(TokenType.BeginArray))
|
|
75 .Or(endArray.Tag(TokenType.EndArray))
|
|
76 .Or(nameSep.Tag(TokenType.NameSeparator))
|
|
77 .Or(valueSep.Tag(TokenType.ValueSeparator));
|
|
78
|
|
79
|
|
80 var jsonStringExpression =
|
|
81 quote.Tag(TokenType.StringBound)
|
|
82 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
|
|
83 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
|
|
84 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
|
72
|
85
|
55
|
86
|
|
87 m_jsonDFA = BuildDFA(jsonExpression);
|
|
88 m_stringDFA = BuildDFA(jsonStringExpression);
|
|
89 }
|
|
90
|
|
91 public CDFADefinition JsonDFA {
|
|
92 get {
|
|
93 return m_jsonDFA;
|
|
94 }
|
|
95 }
|
|
96
|
|
97 public CDFADefinition JsonStringDFA {
|
|
98 get {
|
|
99 return m_stringDFA;
|
|
100 }
|
|
101 }
|
|
102 }
|
|
103 }
|