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 {
|
165
|
7 class JSONGrammar : Grammar<char,JSONGrammar.TokenType> {
|
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,
|
|
23 EscapedUnicode,
|
|
24
|
|
25 Minus,
|
|
26 Plus,
|
|
27 Sign,
|
|
28 Integer,
|
|
29 Dot,
|
|
30 Exp
|
|
31 }
|
|
32
|
165
|
33 static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>();
|
|
34
|
|
35 public static JSONGrammar Instance {
|
|
36 get { return _instance.Value; }
|
|
37 }
|
|
38
|
172
|
39 readonly RegularDFA<char, TokenType> m_jsonDFA;
|
|
40 readonly RegularDFA<char, TokenType> m_stringDFA;
|
163
|
41
|
|
42 public JSONGrammar() {
|
|
43 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
|
|
44 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
|
|
45 var digit9 = SymbolRangeToken('1', '9');
|
|
46 var zero = SymbolToken('0');
|
|
47 var digit = zero.Or(digit9);
|
|
48 var dot = SymbolToken('.');
|
|
49 var minus = SymbolToken('-');
|
|
50 var sign = SymbolSetToken('-', '+');
|
|
51 var expSign = SymbolSetToken('e', 'E');
|
|
52 var letters = SymbolRangeToken('a', 'z');
|
|
53 var integer = zero.Or(digit9.Cat(digit.EClosure()));
|
|
54 var frac = dot.Cat(digit.Closure());
|
|
55 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure());
|
|
56 var quote = SymbolToken('"');
|
|
57 var backSlash = SymbolToken('\\');
|
|
58 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r');
|
|
59 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4));
|
|
60 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure();
|
|
61 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace);
|
|
62 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace);
|
|
63 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace);
|
|
64 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace);
|
|
65 var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace);
|
|
66 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace);
|
|
67
|
|
68 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional());
|
|
69 var literal = letters.Closure();
|
|
70 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x));
|
|
71
|
|
72 var jsonExpression =
|
|
73 number.Tag(TokenType.Number)
|
|
74 .Or(literal.Tag(TokenType.Literal))
|
|
75 .Or(quote.Tag(TokenType.StringBound))
|
|
76 .Or(beginObject.Tag(TokenType.BeginObject))
|
|
77 .Or(endObject.Tag(TokenType.EndObject))
|
|
78 .Or(beginArray.Tag(TokenType.BeginArray))
|
|
79 .Or(endArray.Tag(TokenType.EndArray))
|
|
80 .Or(nameSep.Tag(TokenType.NameSeparator))
|
|
81 .Or(valueSep.Tag(TokenType.ValueSeparator));
|
|
82
|
|
83
|
|
84 var jsonStringExpression =
|
|
85 quote.Tag(TokenType.StringBound)
|
|
86 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
|
|
87 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
|
|
88 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
|
|
89
|
|
90
|
172
|
91 m_jsonDFA = BuildDFA(jsonExpression);
|
|
92 m_stringDFA = BuildDFA(jsonStringExpression);
|
163
|
93 }
|
|
94
|
172
|
95 public RegularDFA<char, TokenType> JsonDFA {
|
163
|
96 get {
|
|
97 return m_jsonDFA;
|
|
98 }
|
|
99 }
|
|
100
|
172
|
101 public RegularDFA<char,TokenType> JsonStringDFA {
|
163
|
102 get {
|
|
103 return m_stringDFA;
|
|
104 }
|
|
105 }
|
165
|
106
|
|
107 Token<TokenType> SymbolRangeToken(char start, char stop) {
|
|
108 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
|
|
109 }
|
172
|
110
|
|
111 protected override IAlphabetBuilder<char> CreateAlphabet() {
|
|
112 return new CharAlphabet();
|
|
113 }
|
165
|
114
|
163
|
115 }
|
|
116 }
|