comparison Implab/Formats/JSON/JSONGrammar.cs @ 165:e227e78d72e4 ref20160224

DFA refactoring
author cin
date Mon, 29 Feb 2016 02:02:17 +0300
parents 419aa51b04fd
children 92d5278d1b10
comparison
equal deleted inserted replaced
164:ec35731ae299 165:e227e78d72e4
1 using System.Linq; 1 using System.Linq;
2 using Implab.Automaton.RegularExpressions; 2 using Implab.Automaton.RegularExpressions;
3 using System;
3 4
4 namespace Implab.Formats.JSON { 5 namespace Implab.Formats.JSON {
5 class JSONGrammar : Grammar<JSONGrammar> { 6 class JSONGrammar : Grammar<char,JSONGrammar.TokenType> {
6 public enum TokenType { 7 public enum TokenType {
7 None, 8 None,
8 BeginObject, 9 BeginObject,
9 EndObject, 10 EndObject,
10 BeginArray, 11 BeginArray,
26 Integer, 27 Integer,
27 Dot, 28 Dot,
28 Exp 29 Exp
29 } 30 }
30 31
31 readonly CDFADefinition m_jsonDFA; 32 static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>();
32 readonly CDFADefinition m_stringDFA; 33
34 public static JSONGrammar Instance {
35 get { return _instance.Value; }
36 }
37
38 readonly RegularCharDFADefinition<TokenType> m_jsonDFA;
39 readonly RegularCharDFADefinition<TokenType> m_stringDFA;
33 40
34 public JSONGrammar() { 41 public JSONGrammar() {
35 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); 42 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
36 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); 43 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9'));
37 var digit9 = SymbolRangeToken('1', '9'); 44 var digit9 = SymbolRangeToken('1', '9');
78 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) 85 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
79 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) 86 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
80 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); 87 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
81 88
82 89
83 m_jsonDFA = BuildDFA(jsonExpression); 90 m_jsonDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet());
84 m_stringDFA = BuildDFA(jsonStringExpression); 91 BuildDFA(jsonExpression, m_jsonDFA, m_jsonDFA.InputAlphabet);
92
93
94 m_stringDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet());
95 BuildDFA(jsonStringExpression, m_jsonDFA, m_jsonDFA.InputAlphabet);
85 } 96 }
86 97
87 public CDFADefinition JsonDFA { 98 public RegularCharDFADefinition<TokenType> JsonDFA {
88 get { 99 get {
89 return m_jsonDFA; 100 return m_jsonDFA;
90 } 101 }
91 } 102 }
92 103
93 public CDFADefinition JsonStringDFA { 104 public RegularDFADefinition<char,TokenType> JsonStringDFA {
94 get { 105 get {
95 return m_stringDFA; 106 return m_stringDFA;
96 } 107 }
97 } 108 }
109
110 Token<TokenType> SymbolRangeToken(char start, char stop) {
111 return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
112 }
113
98 } 114 }
99 } 115 }