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

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