comparison Implab/Formats/JSON/JSONGrammar.cs @ 172:92d5278d1b10 ref20160224

Working on text scanner
author cin
date Mon, 14 Mar 2016 01:19:38 +0300
parents e227e78d72e4
children 0c3c69fe225b
comparison
equal deleted inserted replaced
171:0f70905b4652 172:92d5278d1b10
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 5
5 namespace Implab.Formats.JSON { 6 namespace Implab.Formats.JSON {
6 class JSONGrammar : Grammar<char,JSONGrammar.TokenType> { 7 class JSONGrammar : Grammar<char,JSONGrammar.TokenType> {
7 public enum TokenType { 8 public enum TokenType {
8 None, 9 None,
33 34
34 public static JSONGrammar Instance { 35 public static JSONGrammar Instance {
35 get { return _instance.Value; } 36 get { return _instance.Value; }
36 } 37 }
37 38
38 readonly RegularCharDFADefinition<TokenType> m_jsonDFA; 39 readonly RegularDFA<char, TokenType> m_jsonDFA;
39 readonly RegularCharDFADefinition<TokenType> m_stringDFA; 40 readonly RegularDFA<char, TokenType> m_stringDFA;
40 41
41 public JSONGrammar() { 42 public JSONGrammar() {
42 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); 43 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
43 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); 44 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
44 var digit9 = SymbolRangeToken('1', '9'); 45 var digit9 = SymbolRangeToken('1', '9');
85 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) 86 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
86 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) 87 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
87 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); 88 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
88 89
89 90
90 m_jsonDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet()); 91 m_jsonDFA = BuildDFA(jsonExpression);
91 BuildDFA(jsonExpression, m_jsonDFA, m_jsonDFA.InputAlphabet); 92 m_stringDFA = BuildDFA(jsonStringExpression);
92
93
94 m_stringDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet());
95 BuildDFA(jsonStringExpression, m_jsonDFA, m_jsonDFA.InputAlphabet);
96 } 93 }
97 94
98 public RegularCharDFADefinition<TokenType> JsonDFA { 95 public RegularDFA<char, TokenType> JsonDFA {
99 get { 96 get {
100 return m_jsonDFA; 97 return m_jsonDFA;
101 } 98 }
102 } 99 }
103 100
104 public RegularDFADefinition<char,TokenType> JsonStringDFA { 101 public RegularDFA<char,TokenType> JsonStringDFA {
105 get { 102 get {
106 return m_stringDFA; 103 return m_stringDFA;
107 } 104 }
108 } 105 }
109 106
110 Token<TokenType> SymbolRangeToken(char start, char stop) { 107 Token<TokenType> SymbolRangeToken(char start, char stop) {
111 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); 108 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
112 } 109 }
110
111 protected override IAlphabetBuilder<char> CreateAlphabet() {
112 return new CharAlphabet();
113 }
113 114
114 } 115 }
115 } 116 }