annotate Implab/JSON/JSONParser.cs @ 55:c0bf853aa04f

Added initial JSON support +JSONParser +JSONWriter
author cin
date Sun, 15 Jun 2014 19:39:11 +0400
parents
children 7759c80cad95
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using Implab.Parsing;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Diagnostics;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 namespace Implab.JSON {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// internal
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 public struct JSONParserContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 public string memberName;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 public JSONElementContext elementContext;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 /// Pull парсер JSON данных.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 public class JSONParser : DFAutomaton<JSONParserContext> {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 enum MemberContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 MemberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 MemberValue
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 static readonly EnumAlphabet<JsonTokenType> _alphabet = EnumAlphabet<JsonTokenType>.FullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 static readonly DFAStateDescriptior[] _jsonDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 static readonly DFAStateDescriptior[] _objectDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 static readonly DFAStateDescriptior[] _arrayDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 static JSONParser() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 var jsonExpression = Token.New(JsonTokenType.BeginObject, JsonTokenType.BeginArray).Tag(0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 var valueExpression = Token.New(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 var memberExpression = Token.New(JsonTokenType.String).Cat(Token.New(JsonTokenType.NameSeparator)).Cat(valueExpression);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 var objectExpression = memberExpression
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 .Cat(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 Token.New(JsonTokenType.ValueSeparator)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 .Cat(memberExpression)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 .EClosure()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 .Optional()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 .Cat(Token.New(JsonTokenType.EndObject))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47 .Tag(0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 var arrayExpression = valueExpression
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 .Cat(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50 Token.New(JsonTokenType.ValueSeparator)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 .Cat(valueExpression)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 .EClosure()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 .Optional()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 .Cat(Token.New(JsonTokenType.EndArray))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 .Tag(0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 _jsonDFA = BuildDFA(jsonExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 _objectDFA = BuildDFA(objectExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 _arrayDFA = BuildDFA(arrayExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 static EDFADefinition<JsonTokenType> BuildDFA(Token expr) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 var builder = new DFABuilder();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 var dfa = new EDFADefinition<JsonTokenType>(_alphabet);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 expr.Accept(builder);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68 builder.BuildDFA(dfa);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 return dfa;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
71
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
72 JSONScanner m_scanner;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
73 MemberContext m_memberContext;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
74
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75 JSONElementType m_elementType;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 object m_elementValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78 public JSONParser(string text)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty } ) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80 Safe.ArgumentNotEmpty(text, "text");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 m_scanner = new JSONScanner();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82 m_scanner.Feed(text.ToCharArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85 public JSONElementType ElementType {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 get { return m_elementType; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89 public string ElementName {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
90 get { return m_context.info.memberName; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
91 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
92
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
93 public object ElementValue {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
94 get { return m_elementValue; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
95 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
96
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
97 public bool Read() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
98 if (m_context.current == UNREACHEBLE_STATE)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
99 throw new InvalidOperationException("The parser is in invalid state");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
100 object tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
101 JsonTokenType tokenType;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
102 m_context.info.memberName = String.Empty;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
103 while (m_scanner.ReadToken(out tokenValue, out tokenType)) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
104 Move((int)tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
105 if (m_context.current == UNREACHEBLE_STATE)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
106 UnexpectedToken(tokenValue, tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
107 switch (tokenType) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
108 case JsonTokenType.BeginObject:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
109 Switch(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
110 _objectDFA,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
111 INITIAL_STATE,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
112 new JSONParserContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
113 memberName = m_context.info.memberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
114 elementContext = JSONElementContext.Object
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
115 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
116 );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
117 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
118 m_memberContext = MemberContext.MemberName;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
119 m_elementType = JSONElementType.BeginObject;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
120 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
121 case JsonTokenType.EndObject:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
122 Restore();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
123 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
124 m_elementType = JSONElementType.EndObject;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
125 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
126 case JsonTokenType.BeginArray:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
127 Switch(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
128 _arrayDFA,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
129 INITIAL_STATE,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
130 new JSONParserContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
131 memberName = m_context.info.memberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
132 elementContext = JSONElementContext.Array
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
133 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
134 );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
135 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
136 m_memberContext = MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
137 m_elementType = JSONElementType.BeginArray;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
138 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
139 case JsonTokenType.EndArray:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
140 Restore();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
141 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
142 m_elementType = JSONElementType.EndArray;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
143 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
144 case JsonTokenType.String:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
145 if (m_memberContext == MemberContext.MemberName) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
146 m_context.info.memberName = (string)tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
147 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
148 } else {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
149 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
150 m_elementValue = tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
151 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
152 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
153 case JsonTokenType.Number:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
154 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
155 m_elementValue = tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
156 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
157 case JsonTokenType.Literal:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
158 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
159 m_elementValue = ParseLiteral((string)tokenValue);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
160 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
161 case JsonTokenType.NameSeparator:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
162 m_memberContext = MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
163 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
164 case JsonTokenType.ValueSeparator:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
165 m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
166 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
167 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
168 UnexpectedToken(tokenValue, tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
169 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
170 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
171 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
172 if (m_context.info.elementContext != JSONElementContext.None)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
173 throw new ParserException("Unexpedted end of data");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
174 return false;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
175 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
176
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
177 object ParseLiteral(string literal) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
178 switch (literal) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
179 case "null":
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
180 return null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
181 case "false" :
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
182 return false;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
183 case "true":
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
184 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
185 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
186 UnexpectedToken(literal, JsonTokenType.Literal);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
187 return null; // avoid compliler error
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
188 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
189 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
190
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
191 void UnexpectedToken(object value, JsonTokenType tokenType) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
192 throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
193 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
194
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
195 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
196
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
197 }