comparison Implab/Formats/Json/JsonGrammar.cs @ 236:302ca905c19e v2

JsonReader optimizations
author cin
date Tue, 21 Nov 2017 14:57:58 +0300
parents 3e26338eb977
children
comparison
equal deleted inserted replaced
235:b49969a7043c 236:302ca905c19e
29 29
30 public static JsonGrammar Instance { 30 public static JsonGrammar Instance {
31 get { return _instance.Value; } 31 get { return _instance.Value; }
32 } 32 }
33 33
34 readonly InputScanner<TokenType> m_jsonExpression; 34 readonly FastInputScanner<TokenType> m_jsonExpression;
35 readonly InputScanner<TokenType> m_stringExpression; 35 readonly FastInputScanner<TokenType> m_stringExpression;
36 readonly CharAlphabet m_defaultAlphabet = new CharAlphabet(); 36 readonly CharAlphabet m_defaultAlphabet = new CharAlphabet();
37 37
38 public CharAlphabet DefaultAlphabet { get { return m_defaultAlphabet; } } 38 public CharAlphabet DefaultAlphabet { get { return m_defaultAlphabet; } }
39 39
40 public JsonGrammar() { 40 public JsonGrammar() {
85 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) 85 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar))
86 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) 86 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode))
87 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); 87 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
88 88
89 89
90 m_jsonExpression = BuildScanner(jsonExpression); 90 m_jsonExpression = BuildFastScanner(jsonExpression);
91 m_stringExpression = BuildScanner(jsonStringExpression); 91 m_stringExpression = BuildFastScanner(jsonStringExpression);
92 } 92 }
93 93
94 public static InputScanner<TokenType> CreateJsonExpressionScanner() { 94 public static FastInputScanner<TokenType> CreateJsonExpressionScanner() {
95 return Instance.m_jsonExpression.Clone(); 95 return Instance.m_jsonExpression.Clone();
96 } 96 }
97 97
98 public static InputScanner<TokenType> CreateStringExpressionScanner() { 98 public static FastInputScanner<TokenType> CreateStringExpressionScanner() {
99 return Instance.m_stringExpression.Clone(); 99 return Instance.m_stringExpression.Clone();
100 } 100 }
101 101
102 protected override IAlphabetBuilder<char> AlphabetBuilder { 102 protected override IAlphabetBuilder<char> AlphabetBuilder {
103 get { 103 get {
107 107
108 Token SymbolRangeToken(char start, char stop) { 108 Token SymbolRangeToken(char start, char stop) {
109 return SymbolToken(Enumerable.Range(start, stop - start + 1).Select(x => (char)x)); 109 return SymbolToken(Enumerable.Range(start, stop - start + 1).Select(x => (char)x));
110 } 110 }
111 111
112 public InputScanner<TokenType> BuildScanner(Token regexp) { 112 public FastInputScanner<TokenType> BuildFastScanner(Token regexp) {
113 var dfa = new RegularDFA<char, TokenType>(AlphabetBuilder); 113 var dfa = new RegularDFA<char, TokenType>(AlphabetBuilder);
114 114
115 var visitor = new RegularExpressionVisitor<TokenType>(dfa); 115 var visitor = new RegularExpressionVisitor<TokenType>(dfa);
116 regexp.Accept(visitor); 116 regexp.Accept(visitor);
117 visitor.BuildDFA(); 117 visitor.BuildDFA();
120 throw new ApplicationException("The specified language contains empty token"); 120 throw new ApplicationException("The specified language contains empty token");
121 121
122 var ab = new CharAlphabet(); 122 var ab = new CharAlphabet();
123 var optimal = dfa.Optimize(ab); 123 var optimal = dfa.Optimize(ab);
124 124
125 return new InputScanner<TokenType>( 125 return new FastInputScanner<TokenType>(
126 optimal.CreateTransitionTable(), 126 optimal.CreateTransitionTable(),
127 optimal.CreateFinalStateTable(), 127 optimal.CreateFinalStateTable(),
128 NormalizeTags(optimal.CreateTagTable()), 128 NormalizeTags(optimal.CreateTagTable()),
129 optimal.InitialState, 129 optimal.InitialState,
130 ab.CreateCharMap() 130 ab.GetTranslationMap()
131 ); 131 );
132 } 132 }
133 133
134 static TokenType[] NormalizeTags(TokenType[][] tags) { 134 static TokenType[] NormalizeTags(TokenType[][] tags) {
135 var result = new TokenType[tags.Length]; 135 var result = new TokenType[tags.Length];