annotate Implab/JSON/JSONParser.cs @ 138:f75cfa58e3d4 v2

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