annotate Implab/Formats/JSON/JSONScanner.cs @ 176:0c3c69fe225b ref20160224

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