163
|
1 using System.Linq;
|
|
2 using Implab.Automaton.RegularExpressions;
|
165
|
3 using System;
|
172
|
4 using Implab.Automaton;
|
180
|
5 using Implab.Components;
|
163
|
6
|
|
7 namespace Implab.Formats.JSON {
|
178
|
8 class JSONGrammar : Grammar<char> {
|
163
|
9 public enum TokenType {
|
|
10 None,
|
|
11 BeginObject,
|
|
12 EndObject,
|
|
13 BeginArray,
|
|
14 EndArray,
|
|
15 String,
|
|
16 Number,
|
|
17 Literal,
|
|
18 NameSeparator,
|
|
19 ValueSeparator,
|
183
|
20 Whitespace,
|
163
|
21
|
|
22 StringBound,
|
|
23 EscapedChar,
|
|
24 UnescapedChar,
|
176
|
25 EscapedUnicode
|
163
|
26 }
|
|
27
|
180
|
28 static LazyAndWeak<JSONGrammar> _instance = new LazyAndWeak<JSONGrammar>(() => new JSONGrammar());
|
165
|
29
|
|
30 public static JSONGrammar Instance {
|
|
31 get { return _instance.Value; }
|
|
32 }
|
|
33
|
178
|
34 readonly ScannerContext<TokenType> m_jsonExpression;
|
|
35 readonly ScannerContext<TokenType> m_stringExpression;
|
180
|
36 readonly CharAlphabet m_defaultAlphabet = new CharAlphabet();
|
163
|
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))
|
183
|
77 .Or(valueSep.Tag(TokenType.ValueSeparator))
|
|
78 .Or(SymbolSetToken('\n', '\r', '\t', ' ').Closure().Tag(TokenType.Whitespace));
|
163
|
79
|
|
80
|
|
81 var jsonStringExpression =
|
|
82 quote.Tag(TokenType.StringBound)
|
|
83 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
|
|
84 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
|
|
85 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
|
|
86
|
|
87
|
178
|
88 m_jsonExpression = BuildScannerContext<TokenType>(jsonExpression);
|
|
89 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression);
|
|
90
|
|
91
|
163
|
92 }
|
|
93
|
180
|
94 protected override IAlphabetBuilder<char> AlphabetBuilder {
|
|
95 get {
|
|
96 return m_defaultAlphabet;
|
|
97 }
|
|
98 }
|
|
99
|
178
|
100 public ScannerContext<TokenType> JsonExpression {
|
163
|
101 get {
|
178
|
102 return m_jsonExpression;
|
163
|
103 }
|
|
104 }
|
|
105
|
178
|
106 public ScannerContext<TokenType> JsonStringExpression {
|
163
|
107 get {
|
178
|
108 return m_stringExpression;
|
163
|
109 }
|
|
110 }
|
165
|
111
|
178
|
112 Token SymbolRangeToken(char start, char stop) {
|
182
|
113 return SymbolToken(Enumerable.Range(start, stop - start + 1).Select(x => (char)x));
|
165
|
114 }
|
172
|
115
|
180
|
116 protected override IndexedAlphabetBase<char> CreateAlphabet() {
|
172
|
117 return new CharAlphabet();
|
|
118 }
|
165
|
119
|
163
|
120 }
|
|
121 }
|