diff 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
line wrap: on
line diff
--- a/Implab/Formats/JSON/JSONGrammar.cs	Thu Feb 25 02:11:13 2016 +0300
+++ b/Implab/Formats/JSON/JSONGrammar.cs	Mon Feb 29 02:02:17 2016 +0300
@@ -1,8 +1,9 @@
 using System.Linq;
 using Implab.Automaton.RegularExpressions;
+using System;
 
 namespace Implab.Formats.JSON {
-    class JSONGrammar : Grammar<JSONGrammar> {
+    class JSONGrammar : Grammar<char,JSONGrammar.TokenType> {
         public enum TokenType {
             None,
             BeginObject,
@@ -28,8 +29,14 @@
             Exp
         }
 
-        readonly CDFADefinition m_jsonDFA;
-        readonly CDFADefinition m_stringDFA;
+        static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>();
+
+        public static JSONGrammar Instance {
+            get { return _instance.Value; }
+        }
+
+        readonly RegularCharDFADefinition<TokenType> m_jsonDFA;
+        readonly RegularCharDFADefinition<TokenType> m_stringDFA;
 
         public JSONGrammar() {
             DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x));
@@ -80,20 +87,29 @@
                 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar));
                     
 
-            m_jsonDFA = BuildDFA(jsonExpression);
-            m_stringDFA = BuildDFA(jsonStringExpression);
+            m_jsonDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet()); 
+            BuildDFA(jsonExpression, m_jsonDFA, m_jsonDFA.InputAlphabet);
+
+
+            m_stringDFA = new RegularCharDFADefinition<TokenType>(new CharAlphabet());
+            BuildDFA(jsonStringExpression, m_jsonDFA, m_jsonDFA.InputAlphabet);
         }
 
-        public CDFADefinition JsonDFA {
+        public RegularCharDFADefinition<TokenType> JsonDFA {
             get {
                 return m_jsonDFA;
             }
         }
 
-        public CDFADefinition JsonStringDFA {
+        public RegularDFADefinition<char,TokenType> JsonStringDFA {
             get {
                 return m_stringDFA;
             }
         }
+
+        Token<TokenType> SymbolRangeToken(char start, char stop) {
+            return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>());
+        }
+                
     }
 }