annotate Implab/Formats/JSON/JSONGrammar.cs @ 178:d5c5db0335ee ref20160224

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