163
|
1 using System.Linq;
|
|
2 using Implab.Automaton.RegularExpressions;
|
165
|
3 using System;
|
172
|
4 using Implab.Automaton;
|
163
|
5
|
|
6 namespace Implab.Formats.JSON {
|
178
|
7 class JSONGrammar : Grammar<char> {
|
163
|
8 public enum TokenType {
|
|
9 None,
|
|
10 BeginObject,
|
|
11 EndObject,
|
|
12 BeginArray,
|
|
13 EndArray,
|
|
14 String,
|
|
15 Number,
|
|
16 Literal,
|
|
17 NameSeparator,
|
|
18 ValueSeparator,
|
|
19
|
|
20 StringBound,
|
|
21 EscapedChar,
|
|
22 UnescapedChar,
|
176
|
23 EscapedUnicode
|
163
|
24 }
|
|
25
|
165
|
26 static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>();
|
|
27
|
|
28 public static JSONGrammar Instance {
|
|
29 get { return _instance.Value; }
|
|
30 }
|
|
31
|
178
|
32 readonly ScannerContext<TokenType> m_jsonExpression;
|
|
33 readonly ScannerContext<TokenType> m_stringExpression;
|
163
|
34
|
|
35 public JSONGrammar() {
|
|
36 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
|
|
37 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
|
|
38 var digit9 = SymbolRangeToken('1', '9');
|
|
39 var zero = SymbolToken('0');
|
|
40 var digit = zero.Or(digit9);
|
|
41 var dot = SymbolToken('.');
|
|
42 var minus = SymbolToken('-');
|
|
43 var sign = SymbolSetToken('-', '+');
|
|
44 var expSign = SymbolSetToken('e', 'E');
|
|
45 var letters = SymbolRangeToken('a', 'z');
|
|
46 var integer = zero.Or(digit9.Cat(digit.EClosure()));
|
|
47 var frac = dot.Cat(digit.Closure());
|
|
48 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure());
|
|
49 var quote = SymbolToken('"');
|
|
50 var backSlash = SymbolToken('\\');
|
|
51 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r');
|
|
52 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4));
|
|
53 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure();
|
|
54 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace);
|
|
55 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace);
|
|
56 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace);
|
|
57 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace);
|
|
58 var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace);
|
|
59 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace);
|
|
60
|
|
61 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional());
|
|
62 var literal = letters.Closure();
|
|
63 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x));
|
|
64
|
|
65 var jsonExpression =
|
|
66 number.Tag(TokenType.Number)
|
|
67 .Or(literal.Tag(TokenType.Literal))
|
|
68 .Or(quote.Tag(TokenType.StringBound))
|
|
69 .Or(beginObject.Tag(TokenType.BeginObject))
|
|
70 .Or(endObject.Tag(TokenType.EndObject))
|
|
71 .Or(beginArray.Tag(TokenType.BeginArray))
|
|
72 .Or(endArray.Tag(TokenType.EndArray))
|
|
73 .Or(nameSep.Tag(TokenType.NameSeparator))
|
|
74 .Or(valueSep.Tag(TokenType.ValueSeparator));
|
|
75
|
|
76
|
|
77 var jsonStringExpression =
|
|
78 quote.Tag(TokenType.StringBound)
|
|
79 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
|
|
80 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
|
|
81 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
|
|
82
|
|
83
|
178
|
84 m_jsonExpression = BuildScannerContext<TokenType>(jsonExpression);
|
|
85 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression);
|
|
86
|
|
87
|
163
|
88 }
|
|
89
|
178
|
90 public ScannerContext<TokenType> JsonExpression {
|
163
|
91 get {
|
178
|
92 return m_jsonExpression;
|
163
|
93 }
|
|
94 }
|
|
95
|
178
|
96 public ScannerContext<TokenType> JsonStringExpression {
|
163
|
97 get {
|
178
|
98 return m_stringExpression;
|
163
|
99 }
|
|
100 }
|
165
|
101
|
178
|
102 Token SymbolRangeToken(char start, char stop) {
|
165
|
103 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
|
|
104 }
|
172
|
105
|
|
106 protected override IAlphabetBuilder<char> CreateAlphabet() {
|
|
107 return new CharAlphabet();
|
|
108 }
|
165
|
109
|
163
|
110 }
|
|
111 }
|