annotate Implab/JSON/JSONScanner.cs @ 59:21611344d366

code cleanup
author cin
date Wed, 18 Jun 2014 03:54:02 +0400
parents c0bf853aa04f
children 10c7337d29e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab.Parsing;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Globalization;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 namespace Implab.JSON {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 /// Сканнер, разбивающий поток символов на токены JSON.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 public class JSONScanner : Scanner {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 char[] m_stringBuffer;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 DFAStateDescriptior[] m_stringDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 int[] m_stringAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 public JSONScanner()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 : base(JSONGrammar.Instance.JsonDFA) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 m_stringBuffer = new char[1024];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 var dfa = JSONGrammar.Instance.JsonStringDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 m_stringAlphabet = dfa.Alphabet.GetTranslationMap();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 m_stringDFA = dfa.States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 if (ReadTokenInternal()) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28 switch ((JSONGrammar.TokenType)m_currentState.tag[0]) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 case JSONGrammar.TokenType.StringBound:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 tokenValue = ReadString();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 tokenType = JsonTokenType.String;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 case JSONGrammar.TokenType.Number:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 tokenValue = Double.Parse(new String(m_buffer, m_tokenOffset, m_tokenLen), CultureInfo.InvariantCulture);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 tokenType = JsonTokenType.Number;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 tokenType = (JsonTokenType)m_currentState.tag[0];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 tokenValue = new String(m_buffer, m_tokenOffset, m_tokenLen);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 tokenValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 tokenType = JsonTokenType.None;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 return false;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 string ReadString() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50 int pos = 0;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 Switch(m_stringDFA, m_stringAlphabet);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 while (ReadTokenInternal()) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53 switch ((JSONGrammar.TokenType)m_currentState.tag[0]) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 case JSONGrammar.TokenType.StringBound:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 Restore();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 return new String(m_stringBuffer, 0, pos);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57 case JSONGrammar.TokenType.UnescapedChar:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 EnsureStringBufferSize(pos + m_tokenLen);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 Array.Copy(m_buffer, m_tokenOffset, m_stringBuffer, pos, m_tokenLen);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 pos += m_tokenLen;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 case JSONGrammar.TokenType.EscapedUnicode:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 EnsureStringBufferSize(pos + 1);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 m_stringBuffer[pos] = StringTranslator.TranslateHexUnicode(m_buffer, m_tokenOffset + 2);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 pos++;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 case JSONGrammar.TokenType.EscapedChar:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68 EnsureStringBufferSize(pos + 1);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 m_stringBuffer[pos] = StringTranslator.TranslateEscapedChar(m_buffer[m_tokenOffset + 1]);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 pos++;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
71 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
72 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
73 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
74 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78 throw new ParserException("Unexpected end of data");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 void EnsureStringBufferSize(int size) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82 if (size > m_stringBuffer.Length) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83 var newBuffer = new char[size];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84 m_stringBuffer.CopyTo(newBuffer, 0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85 m_stringBuffer = newBuffer;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89 }