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

sync
author cin
date Wed, 23 Mar 2016 19:52:08 +0300
parents a0ff6a0e9c44
children c32688129f14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
1 using System;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using System.Globalization;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
3 using Implab.Automaton;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
4 using System.Text;
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
5 using Implab.Components;
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
6 using System.IO;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
7
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
8 namespace Implab.Formats.JSON {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
9 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
10 /// Сканнер (лексер), разбивающий поток символов на токены JSON.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
11 /// </summary>
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
12 public class JSONScanner : Disposable {
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
13 readonly StringBuilder m_builder = new StringBuilder();
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
14
179
cin
parents: 177
diff changeset
15 readonly ScannerContext<JSONGrammar.TokenType> m_jsonContext = JSONGrammar.Instance.JsonExpression;
cin
parents: 177
diff changeset
16 readonly ScannerContext<JSONGrammar.TokenType> m_stringContext = JSONGrammar.Instance.JsonStringExpression;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
17
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
18
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
19 readonly TextScanner m_scanner;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 /// Создает новый экземпляр сканнера
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 /// </summary>
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
24 public JSONScanner(string text) {
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
25 Safe.ArgumentNotEmpty(text, "text");
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
26
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
27 m_scanner = new StringScanner(text);
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
28 }
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
29
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
30 public JSONScanner(TextReader reader, int bufferMax, int chunkSize) {
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
31 Safe.ArgumentNotNull(reader, "reader");
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
32
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
33 m_scanner = new ReaderScanner(reader, bufferMax, chunkSize);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 /// Читает следующий лексический элемент из входных данных.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 /// <param name="tokenValue">Возвращает значение прочитанного токена.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40 /// <param name="tokenType">Возвращает тип прочитанного токена.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 /// <returns><c>true</c> - чтение произведено успешно. <c>false</c> - достигнут конец входных данных</returns>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 /// <remarks>В случе если токен не распознается, возникает исключение. Значения токенов обрабатываются, т.е.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 /// в строках обрабатываются экранированные символы, числа становтся типа double.</remarks>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44 public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) {
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
45 JSONGrammar.TokenType[] tag;
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
46 if (m_jsonContext.Execute(m_scanner, out tag)) {
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
47 switch (tag[0]) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 case JSONGrammar.TokenType.StringBound:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 tokenValue = ReadString();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 tokenType = JsonTokenType.String;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 case JSONGrammar.TokenType.Number:
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
53 tokenValue = Double.Parse(m_scanner.GetTokenValue(), CultureInfo.InvariantCulture);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54 tokenType = JsonTokenType.Number;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56 default:
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
57 tokenType = (JsonTokenType)tag[0];
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
58 tokenValue = m_scanner.GetTokenValue();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 tokenValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 tokenType = JsonTokenType.None;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68 string ReadString() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 int pos = 0;
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
70 var buf = new char[6]; // the buffer for unescaping chars
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
71
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
72 JSONGrammar.TokenType[] tag;
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
73 m_builder.Clear();
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
74
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
75 while (m_stringContext.Execute(m_scanner, out tag)) {
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
76 switch (tag[0]) {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77 case JSONGrammar.TokenType.StringBound:
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
78 return m_builder.ToString();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
79 case JSONGrammar.TokenType.UnescapedChar:
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
80 m_scanner.CopyTokenTo(m_builder);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 break;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
82 case JSONGrammar.TokenType.EscapedUnicode: // \xXXXX - unicode escape sequence
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
83 m_scanner.CopyTokenTo(buf, 0);
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
84 m_builder.Append(StringTranslator.TranslateHexUnicode(buf, 2));
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 break;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
87 case JSONGrammar.TokenType.EscapedChar: // \t - escape sequence
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
88 m_scanner.CopyTokenTo(buf, 0);
0c3c69fe225b rewritten the text scanner
cin
parents: 165
diff changeset
89 m_builder.Append(StringTranslator.TranslateEscapedChar(buf[1]));
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 throw new ParserException("Unexpected end of data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 }
177
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
97
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
98 protected override void Dispose(bool disposing) {
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
99 if (disposing)
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
100 Safe.Dispose(m_scanner);
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
101 base.Dispose(disposing);
a0ff6a0e9c44 refactoring
cin
parents: 176
diff changeset
102 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
103 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
104 }