Mercurial > pub > ImplabNet
comparison Implab/Formats/JSON/JSONGrammar.cs @ 178:d5c5db0335ee ref20160224
working on JSON parser
author | cin |
---|---|
date | Wed, 23 Mar 2016 19:51:45 +0300 |
parents | 0c3c69fe225b |
children | c32688129f14 |
comparison
equal
deleted
inserted
replaced
177:a0ff6a0e9c44 | 178:d5c5db0335ee |
---|---|
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 | 5 |
6 namespace Implab.Formats.JSON { | 6 namespace Implab.Formats.JSON { |
7 class JSONGrammar : Grammar<char,JSONGrammar.TokenType> { | 7 class JSONGrammar : Grammar<char> { |
8 public enum TokenType { | 8 public enum TokenType { |
9 None, | 9 None, |
10 BeginObject, | 10 BeginObject, |
11 EndObject, | 11 EndObject, |
12 BeginArray, | 12 BeginArray, |
27 | 27 |
28 public static JSONGrammar Instance { | 28 public static JSONGrammar Instance { |
29 get { return _instance.Value; } | 29 get { return _instance.Value; } |
30 } | 30 } |
31 | 31 |
32 readonly ScannerContext<TokenType> m_jsonDFA; | 32 readonly ScannerContext<TokenType> m_jsonExpression; |
33 readonly ScannerContext<TokenType> m_stringDFA; | 33 readonly ScannerContext<TokenType> m_stringExpression; |
34 | 34 |
35 public JSONGrammar() { | 35 public JSONGrammar() { |
36 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); | 36 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); |
37 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); | 37 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); |
38 var digit9 = SymbolRangeToken('1', '9'); | 38 var digit9 = SymbolRangeToken('1', '9'); |
79 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) | 79 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) |
80 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) | 80 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) |
81 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); | 81 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); |
82 | 82 |
83 | 83 |
84 m_jsonDFA = BuildScannerContext(jsonExpression); | 84 m_jsonExpression = BuildScannerContext<TokenType>(jsonExpression); |
85 m_stringDFA = BuildScannerContext(jsonStringExpression); | 85 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression); |
86 | |
87 | |
86 } | 88 } |
87 | 89 |
88 public ScannerContext<TokenType> JsonDFA { | 90 public ScannerContext<TokenType> JsonExpression { |
89 get { | 91 get { |
90 return m_jsonDFA; | 92 return m_jsonExpression; |
91 } | 93 } |
92 } | 94 } |
93 | 95 |
94 public ScannerContext<TokenType> JsonStringDFA { | 96 public ScannerContext<TokenType> JsonStringExpression { |
95 get { | 97 get { |
96 return m_stringDFA; | 98 return m_stringExpression; |
97 } | 99 } |
98 } | 100 } |
99 | 101 |
100 Token<TokenType> SymbolRangeToken(char start, char stop) { | 102 Token SymbolRangeToken(char start, char stop) { |
101 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); | 103 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); |
102 } | 104 } |
103 | 105 |
104 protected override IAlphabetBuilder<char> CreateAlphabet() { | 106 protected override IAlphabetBuilder<char> CreateAlphabet() { |
105 return new CharAlphabet(); | 107 return new CharAlphabet(); |