annotate Implab/Formats/JSON/JSONScanner.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.Collections.Generic;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
4 using System.Globalization;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
5 using System.Linq;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
6 using System.Text;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
7 using System.Threading.Tasks;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
8
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
9 namespace Implab.JSON {
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>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
13 public class JSONScanner : Scanner {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
14 char[] m_stringBuffer;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 DFAStateDescriptior[] m_stringDFA;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 int[] m_stringAlphabet;
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 /// Создает новый экземпляр сканнера
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 public JSONScanner()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 : base(JSONGrammar.Instance.JsonDFA.States, JSONGrammar.Instance.JsonDFA.Alphabet.GetTranslationMap()) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 m_stringBuffer = new char[1024];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24 var dfa = JSONGrammar.Instance.JsonStringDFA;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 m_stringAlphabet = dfa.Alphabet.GetTranslationMap();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26 m_stringDFA = dfa.States;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
29 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30 /// Читает следующий лексический элемент из входных данных.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
32 /// <param name="tokenValue">Возвращает значение прочитанного токена.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
33 /// <param name="tokenType">Возвращает тип прочитанного токена.</param>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34 /// <returns><c>true</c> - чтение произведено успешно. <c>false</c> - достигнут конец входных данных</returns>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35 /// <remarks>В случе если токен не распознается, возникает исключение. Значения токенов обрабатываются, т.е.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36 /// в строках обрабатываются экранированные символы, числа становтся типа double.</remarks>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38 if (ReadTokenInternal()) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 switch ((JSONGrammar.TokenType)m_currentState.tag[0]) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40 case JSONGrammar.TokenType.StringBound:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41 tokenValue = ReadString();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 tokenType = JsonTokenType.String;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44 case JSONGrammar.TokenType.Number:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 tokenValue = Double.Parse(new String(m_buffer, m_tokenOffset, m_tokenLen), CultureInfo.InvariantCulture);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46 tokenType = JsonTokenType.Number;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 tokenType = (JsonTokenType)m_currentState.tag[0];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 tokenValue = new String(m_buffer, m_tokenOffset, m_tokenLen);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55 tokenValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56 tokenType = JsonTokenType.None;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
57 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
58 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
59
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
60 string ReadString() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61 int pos = 0;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
62 Switch(m_stringDFA, m_stringAlphabet);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
63 while (ReadTokenInternal()) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
64 switch ((JSONGrammar.TokenType)m_currentState.tag[0]) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
65 case JSONGrammar.TokenType.StringBound:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
66 Restore();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67 return new String(m_stringBuffer, 0, pos);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
68 case JSONGrammar.TokenType.UnescapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
69 EnsureStringBufferSize(pos + m_tokenLen);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
70 Array.Copy(m_buffer, m_tokenOffset, m_stringBuffer, pos, m_tokenLen);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 pos += m_tokenLen;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
73 case JSONGrammar.TokenType.EscapedUnicode:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 EnsureStringBufferSize(pos + 1);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75 m_stringBuffer[pos] = StringTranslator.TranslateHexUnicode(m_buffer, m_tokenOffset + 2);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
76 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
78 case JSONGrammar.TokenType.EscapedChar:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
79 EnsureStringBufferSize(pos + 1);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
80 m_stringBuffer[pos] = StringTranslator.TranslateEscapedChar(m_buffer[m_tokenOffset + 1]);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 pos++;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
83 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
88
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
89 throw new ParserException("Unexpected end of data");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
91
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92 void EnsureStringBufferSize(int size) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
93 if (size > m_stringBuffer.Length) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
94 var newBuffer = new char[size];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
95 m_stringBuffer.CopyTo(newBuffer, 0);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 m_stringBuffer = newBuffer;
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 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
100 }