comparison Implab/Formats/JSON/JSONGrammar.cs @ 180:c32688129f14 ref20160224

refactoring complete, JSONParser rewritten
author cin
date Thu, 24 Mar 2016 02:30:46 +0300
parents d5c5db0335ee
children b2b6a6640aa3
comparison
equal deleted inserted replaced
179:478ef706906a 180:c32688129f14
1 using System.Linq; 1 using System.Linq;
2 using Implab.Automaton.RegularExpressions; 2 using Implab.Automaton.RegularExpressions;
3 using System; 3 using System;
4 using Implab.Automaton; 4 using Implab.Automaton;
5 using Implab.Components;
5 6
6 namespace Implab.Formats.JSON { 7 namespace Implab.Formats.JSON {
7 class JSONGrammar : Grammar<char> { 8 class JSONGrammar : Grammar<char> {
8 public enum TokenType { 9 public enum TokenType {
9 None, 10 None,
21 EscapedChar, 22 EscapedChar,
22 UnescapedChar, 23 UnescapedChar,
23 EscapedUnicode 24 EscapedUnicode
24 } 25 }
25 26
26 static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>(); 27 static LazyAndWeak<JSONGrammar> _instance = new LazyAndWeak<JSONGrammar>(() => new JSONGrammar());
27 28
28 public static JSONGrammar Instance { 29 public static JSONGrammar Instance {
29 get { return _instance.Value; } 30 get { return _instance.Value; }
30 } 31 }
31 32
32 readonly ScannerContext<TokenType> m_jsonExpression; 33 readonly ScannerContext<TokenType> m_jsonExpression;
33 readonly ScannerContext<TokenType> m_stringExpression; 34 readonly ScannerContext<TokenType> m_stringExpression;
35 readonly CharAlphabet m_defaultAlphabet = new CharAlphabet();
34 36
35 public JSONGrammar() { 37 public JSONGrammar() {
36 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); 38 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
37 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); 39 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
38 var digit9 = SymbolRangeToken('1', '9'); 40 var digit9 = SymbolRangeToken('1', '9');
85 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression); 87 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression);
86 88
87 89
88 } 90 }
89 91
92 protected override IAlphabetBuilder<char> AlphabetBuilder {
93 get {
94 return m_defaultAlphabet;
95 }
96 }
97
90 public ScannerContext<TokenType> JsonExpression { 98 public ScannerContext<TokenType> JsonExpression {
91 get { 99 get {
92 return m_jsonExpression; 100 return m_jsonExpression;
93 } 101 }
94 } 102 }
101 109
102 Token SymbolRangeToken(char start, char stop) { 110 Token SymbolRangeToken(char start, char stop) {
103 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); 111 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
104 } 112 }
105 113
106 protected override IAlphabetBuilder<char> CreateAlphabet() { 114 protected override IndexedAlphabetBase<char> CreateAlphabet() {
107 return new CharAlphabet(); 115 return new CharAlphabet();
108 } 116 }
109 117
110 } 118 }
111 } 119 }