annotate Implab/Formats/JSON/JSONGrammar.cs @ 163:419aa51b04fd ref20160224

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