annotate Implab/Formats/JSON/JSONParser.cs @ 170:181119ef3b39 ref20160224

DFA refactoring, rx based dfa.
author cin
date Fri, 04 Mar 2016 01:56:31 +0300
parents e227e78d72e4
children 92d5278d1b10
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
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
41 struct ParserContext {
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
42 DFAStateDescriptior<object>
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
43 }
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
44
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 static readonly EnumAlphabet<JsonTokenType> _alphabet = EnumAlphabet<JsonTokenType>.FullAlphabet;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
46 static readonly DFAStateDescriptior<object>[] _jsonDFA;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
47 static readonly int _jsonDFAInitialState;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
48 static readonly DFAStateDescriptior<object>[] _objectDFA;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
49 static readonly int _objectDFAInitialState;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
50 static readonly DFAStateDescriptior<object>[] _arrayDFA;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
51 static readonly int _arrayDFAInitialState;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 static JSONParser() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
56 var valueExpression = Token(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
57 var memberExpression = Token(JsonTokenType.String).Cat(Token(JsonTokenType.NameSeparator)).Cat(valueExpression);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59 var objectExpression = memberExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 .Cat(
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
61 Token(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 .Cat(memberExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 .Optional()
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
66 .Cat(Token(JsonTokenType.EndObject))
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
67 .Tag(null);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68 var arrayExpression = valueExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 .Cat(
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
70 Token(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 .Cat(valueExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 .Optional()
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
75 .Cat(Token(JsonTokenType.EndArray))
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
76 .Tag(null);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
78 var jsonExpression = valueExpression.Tag(null);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
79
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
80 _jsonDFA = CreateDFA(jsonExpression).GetTransitionTable();
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
81 _objectDFA = CreateDFA(objectExpression).GetTransitionTable();
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
82 _arrayDFA = CreateDFA(arrayExpression).GetTransitionTable();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
85 static Token<object> Token(params JsonTokenType[] input) {
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
86 return Token<object>.New(input.Select(t => _alphabet.Translate(t)).ToArray());
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
87 }
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
88
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
89 static RegularDFADefinition<JsonTokenType,object> CreateDFA(Token<object> expr) {
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
90 var builder = new RegularDFABuilder<object>();
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
91 var dfa = new RegularDFADefinition<JsonTokenType,object>(_alphabet);
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
92
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 expr.Accept(builder);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 builder.BuildDFA(dfa);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 return dfa;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
97 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
98
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
99 JSONScanner m_scanner;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
100 MemberContext m_memberContext;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
101
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
102 JSONElementType m_elementType;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
103 object m_elementValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
104
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
105 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
106 /// Создает новый парсер на основе строки, содержащей JSON
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
107 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
108 /// <param name="text"></param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
109 public JSONParser(string text)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
110 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
111 Safe.ArgumentNotEmpty(text, "text");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
112 m_scanner = new JSONScanner();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
113 m_scanner.Feed(text.ToCharArray());
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
114 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
115
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
116 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
117 /// Создает новый экземпляр парсера, на основе текстового потока.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
118 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
119 /// <param name="reader">Текстовый поток.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
120 /// <param name="dispose">Признак того, что парсер должен конролировать время жизни входного потока.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
121 public JSONParser(TextReader reader, bool dispose)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
122 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
123 Safe.ArgumentNotNull(reader, "reader");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
124 m_scanner = new JSONScanner();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
125 m_scanner.Feed(reader, dispose);
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 /// Тип текущего элемента на котором стоит парсер.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
130 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
131 public JSONElementType ElementType {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
132 get { return m_elementType; }
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 /// пустая строка.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
138 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
139 public string ElementName {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
140 get { return m_context.info.memberName; }
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 /// Значение элемента. Только для элементов типа <see cref="JSONElementType.Value"/>, для остальных <c>null</c>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
145 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
146 public object ElementValue {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
147 get { return m_elementValue; }
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 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
153 /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
154 public bool Read() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
155 if (m_context.current == UNREACHEBLE_STATE)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
156 throw new InvalidOperationException("The parser is in invalid state");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
157 object tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
158 JsonTokenType tokenType;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
159 m_context.info.memberName = String.Empty;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
160 while (m_scanner.ReadToken(out tokenValue, out tokenType)) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
161 Move((int)tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
162 if (m_context.current == UNREACHEBLE_STATE)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
163 UnexpectedToken(tokenValue, tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
164 switch (tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
165 case JsonTokenType.BeginObject:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
166 Switch(
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
167 _objectDFA,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
168 INITIAL_STATE,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
169 new JSONParserContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
170 memberName = m_context.info.memberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
171 elementContext = JSONElementContext.Object
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
172 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
173 );
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
174 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
175 m_memberContext = MemberContext.MemberName;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
176 m_elementType = JSONElementType.BeginObject;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
177 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
178 case JsonTokenType.EndObject:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
179 Restore();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
180 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
181 m_elementType = JSONElementType.EndObject;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
182 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
183 case JsonTokenType.BeginArray:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
184 Switch(
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
185 _arrayDFA,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
186 INITIAL_STATE,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
187 new JSONParserContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
188 memberName = m_context.info.memberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
189 elementContext = JSONElementContext.Array
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
190 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
191 );
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
192 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
193 m_memberContext = MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
194 m_elementType = JSONElementType.BeginArray;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
195 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
196 case JsonTokenType.EndArray:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
197 Restore();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
198 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
199 m_elementType = JSONElementType.EndArray;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
200 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
201 case JsonTokenType.String:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
202 if (m_memberContext == MemberContext.MemberName) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
203 m_context.info.memberName = (string)tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
204 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
205 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
206 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
207 m_elementValue = tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
208 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
209 case JsonTokenType.Number:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
210 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
211 m_elementValue = tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
212 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
213 case JsonTokenType.Literal:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
214 m_elementType = JSONElementType.Value;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
215 m_elementValue = ParseLiteral((string)tokenValue);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
216 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
217 case JsonTokenType.NameSeparator:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
218 m_memberContext = MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
219 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
220 case JsonTokenType.ValueSeparator:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
221 m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
222 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
223 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
224 UnexpectedToken(tokenValue, tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
225 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
226 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
227 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
228 if (m_context.info.elementContext != JSONElementContext.None)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
229 throw new ParserException("Unexpedted end of data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
230 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
231 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
232
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
233 object ParseLiteral(string literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
234 switch (literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
235 case "null":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
236 return null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
237 case "false":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
238 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
239 case "true":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
240 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
241 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
242 UnexpectedToken(literal, JsonTokenType.Literal);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
243 return null; // avoid compliler error
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
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
247 void UnexpectedToken(object value, JsonTokenType tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
248 throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value));
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 bool EOF {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
256 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
257 return m_scanner.EOF;
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
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
261 protected override void Dispose(bool disposing) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
262 if (disposing) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
263 m_scanner.Dispose();
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 void SeekElementEnd() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
271 var level = Level - 1;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
272
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
273 Debug.Assert(level >= 0);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
274
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
275 while (Level != level)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
276 Read();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
277 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
278 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
279
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
280 }