annotate Implab/JSON/JSONParser.cs @ 158:130781364799 v2

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