annotate Implab/Formats/JSON/JSONParser.cs @ 179:478ef706906a ref20160224

sync
author cin
date Wed, 23 Mar 2016 19:52:08 +0300
parents d5c5db0335ee
children c32688129f14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
1 using System;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using System.Diagnostics;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
3 using System.IO;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
4 using Implab.Automaton;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
5 using Implab.Automaton.RegularExpressions;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
6 using System.Linq;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
7 using Implab.Components;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
8
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
9 namespace Implab.Formats.JSON {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
10 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
11 /// internal
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
12 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
13 public struct JSONParserContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
14 public string memberName;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 public JSONElementContext elementContext;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19 /// Pull парсер JSON данных.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 /// <remarks>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 /// Следует отметить отдельную интерпретацию свойства <see cref="Level"/>,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 /// оно означает текущий уровень вложенности объектов, однако закрывающий
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24 /// элемент объекта и массива имеет уровень меньше, чем сам объект.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 /// <code>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26 /// { // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 /// "name" : "Peter", // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28 /// "address" : { // Level = 2
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
29 /// city : "Stern" // Level = 2
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30 /// } // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31 /// } // Level = 0
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
32 /// </code>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
33 /// </remarks>
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
34 public class JSONParser : Disposable {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36 enum MemberContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 MemberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 MemberValue
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
41 #region Parser rules
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
42 struct ParserContext {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
43 readonly int[,] m_dfa;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
44 int m_state;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
45
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
46 readonly JSONElementContext m_elementContext;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
47
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
48 public ParserContext(int[,] dfa, int state, JSONElementContext context) {
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
49 m_dfa = dfa;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
50 m_state = state;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
51 m_elementContext = context;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
52 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
53
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
54 public bool Move(JsonTokenType token) {
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
55 var next = m_dfa[m_state, token];
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
56 if (next == AutomatonConst.UNREACHABLE_STATE)
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
57 return false;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
58 m_state = next;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
59 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
60
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
61 public JSONElementContext ElementContext {
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
62 get { return m_elementContext; }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
63 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
64 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66 static JSONParser() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
69 var valueExpression = Token(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
70 var memberExpression = Token(JsonTokenType.String).Cat(Token(JsonTokenType.NameSeparator)).Cat(valueExpression);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 var objectExpression = memberExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 .Cat(
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
74 Token(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75 .Cat(memberExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
76 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
78 .Optional()
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
79 .Cat(Token(JsonTokenType.EndObject))
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
80 .End();
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
81
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 var arrayExpression = valueExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 .Cat(
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
84 Token(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 .Cat(valueExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
88 .Optional()
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
89 .Cat(Token(JsonTokenType.EndArray))
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
90 .End();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
92 var jsonExpression = valueExpression.End();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
94 _jsonDFA = CreateParserContext(jsonExpression, JSONElementContext.None);
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
95 _objectDFA = CreateParserContext(objectExpression, JSONElementContext.Object);
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
96 _arrayDFA = CreateParserContext(arrayExpression, JSONElementContext.Array);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
97 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
98
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
99 static Token Token(params JsonTokenType[] input) {
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
100 return Token.New( input.Select(t => (int)t).ToArray() );
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
101 }
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
102
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
103 static ParserContext CreateParserContext(Token expr, JSONElementContext context) {
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
104
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
105 var dfa = new DFATable();
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
106 var builder = new RegularExpressionVisitor(dfa);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
107 expr.Accept(builder);
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
108 builder.BuildDFA();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
109
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
110 return new ParserContext(dfa.CreateTransitionTable(), dfa.InitialState, context);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
111 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
112
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
113 #endregion
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
114
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
115 JSONScanner m_scanner;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
116 MemberContext m_memberContext;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
117
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
118 JSONElementType m_elementType;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
119 object m_elementValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
120
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
121 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
122 /// Создает новый парсер на основе строки, содержащей JSON
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
123 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
124 /// <param name="text"></param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
125 public JSONParser(string text)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
126 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
127 Safe.ArgumentNotEmpty(text, "text");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
128 m_scanner = new JSONScanner();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
129 m_scanner.Feed(text.ToCharArray());
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
130 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
131
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
132 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
133 /// Создает новый экземпляр парсера, на основе текстового потока.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
134 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
135 /// <param name="reader">Текстовый поток.</param>
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
136 public JSONParser(TextReader reader)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
137 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
138 Safe.ArgumentNotNull(reader, "reader");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
139 m_scanner = new JSONScanner();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
140 m_scanner.Feed(reader, dispose);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
141 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
142
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
143 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
144 /// Тип текущего элемента на котором стоит парсер.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
145 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
146 public JSONElementType ElementType {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
147 get { return m_elementType; }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
148 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
149
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
150 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
151 /// Имя элемента - имя свойства родительского контейнера. Для элементов массивов и корневого всегда
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
152 /// пустая строка.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
153 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
154 public string ElementName {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
155 get { return m_context.info.memberName; }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
156 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
157
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
158 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
159 /// Значение элемента. Только для элементов типа <see cref="JSONElementType.Value"/>, для остальных <c>null</c>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
160 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
161 public object ElementValue {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
162 get { return m_elementValue; }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
163 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
164
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
165 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
166 /// Читает слеюудущий объект из потока
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
167 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
168 /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
169 public bool Read() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
170 if (m_context.current == UNREACHEBLE_STATE)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
171 throw new InvalidOperationException("The parser is in invalid state");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
172 object tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
173 JsonTokenType tokenType;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
174 m_context.info.memberName = String.Empty;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
175 while (m_scanner.ReadToken(out tokenValue, out tokenType)) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
176 Move((int)tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
177 if (m_context.current == UNREACHEBLE_STATE)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
178 UnexpectedToken(tokenValue, tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
179 switch (tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
180 case JsonTokenType.BeginObject:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
181 Switch(
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
182 _objectDFA,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
183 INITIAL_STATE,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
184 new JSONParserContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
185 memberName = m_context.info.memberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
186 elementContext = JSONElementContext.Object
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
187 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
188 );
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
189 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
190 m_memberContext = MemberContext.MemberName;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
191 m_elementType = JSONElementType.BeginObject;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
192 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
193 case JsonTokenType.EndObject:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
194 Restore();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
195 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
196 m_elementType = JSONElementType.EndObject;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
197 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
198 case JsonTokenType.BeginArray:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
199 Switch(
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
200 _arrayDFA,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
201 INITIAL_STATE,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
202 new JSONParserContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
203 memberName = m_context.info.memberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
204 elementContext = JSONElementContext.Array
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
205 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
206 );
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
207 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
208 m_memberContext = MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
209 m_elementType = JSONElementType.BeginArray;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
210 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
211 case JsonTokenType.EndArray:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
212 Restore();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
213 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
214 m_elementType = JSONElementType.EndArray;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
215 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
216 case JsonTokenType.String:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
217 if (m_memberContext == MemberContext.MemberName) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
218 m_context.info.memberName = (string)tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
219 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
220 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
221 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
222 m_elementValue = tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
223 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
224 case JsonTokenType.Number:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
225 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
226 m_elementValue = tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
227 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
228 case JsonTokenType.Literal:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
229 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
230 m_elementValue = ParseLiteral((string)tokenValue);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
231 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
232 case JsonTokenType.NameSeparator:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
233 m_memberContext = MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
234 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
235 case JsonTokenType.ValueSeparator:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
236 m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
237 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
238 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
239 UnexpectedToken(tokenValue, tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
240 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
241 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
242 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
243 if (m_context.info.elementContext != JSONElementContext.None)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
244 throw new ParserException("Unexpedted end of data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
245 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
246 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
247
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
248 object ParseLiteral(string literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
249 switch (literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
250 case "null":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
251 return null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
252 case "false":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
253 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
254 case "true":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
255 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
256 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
257 UnexpectedToken(literal, JsonTokenType.Literal);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
258 return null; // avoid compliler error
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
259 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
260 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
261
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
262 void UnexpectedToken(object value, JsonTokenType tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
263 throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value));
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
264 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
265
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
266
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
267 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
268 /// Признак конца потока
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
269 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
270 public bool EOF {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
271 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
272 return m_scanner.EOF;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
273 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
274 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
275
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
276 protected override void Dispose(bool disposing) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
277 if (disposing) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
278 m_scanner.Dispose();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
279 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
280 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
281
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
282 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
283 /// Переходит в конец текущего объекта.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
284 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
285 public void SeekElementEnd() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
286 var level = Level - 1;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
287
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
288 Debug.Assert(level >= 0);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
289
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
290 while (Level != level)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
291 Read();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
292 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
293 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
294
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
295 }