diff Implab/Formats/JSON/JSONParser.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/JSONParser.cs	Thu Feb 25 02:11:13 2016 +0300
+++ b/Implab/Formats/JSON/JSONParser.cs	Mon Feb 29 02:02:17 2016 +0300
@@ -1,9 +1,12 @@
-using Implab.Parsing;
-using System;
+using System;
 using System.Diagnostics;
 using System.IO;
+using Implab.Automaton;
+using Implab.Automaton.RegularExpressions;
+using System.Linq;
+using Implab.Components;
 
-namespace Implab.JSON {
+namespace Implab.Formats.JSON {
     /// <summary>
     /// internal
     /// </summary>
@@ -28,53 +31,65 @@
     /// } // Level = 0
     /// </code>
     /// </remarks>
-    public class JSONParser : DFAutomaton<JSONParserContext>, IDisposable {
+    public class JSONParser : Disposable {
 
         enum MemberContext {
             MemberName,
             MemberValue
         }
 
+        struct ParserContext {
+            DFAStateDescriptior<object>
+        }
+
         static readonly EnumAlphabet<JsonTokenType> _alphabet = EnumAlphabet<JsonTokenType>.FullAlphabet;
-        static readonly DFAStateDescriptior[] _jsonDFA;
-        static readonly DFAStateDescriptior[] _objectDFA;
-        static readonly DFAStateDescriptior[] _arrayDFA;
+        static readonly DFAStateDescriptior<object>[] _jsonDFA;
+        static readonly int _jsonDFAInitialState;
+        static readonly DFAStateDescriptior<object>[] _objectDFA;
+        static readonly int _objectDFAInitialState;
+        static readonly DFAStateDescriptior<object>[] _arrayDFA;
+        static readonly int _arrayDFAInitialState;
 
         static JSONParser() {
 
 
-            var valueExpression = Token.New(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
-            var memberExpression = Token.New(JsonTokenType.String).Cat(Token.New(JsonTokenType.NameSeparator)).Cat(valueExpression);
+            var valueExpression = Token(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
+            var memberExpression = Token(JsonTokenType.String).Cat(Token(JsonTokenType.NameSeparator)).Cat(valueExpression);
 
             var objectExpression = memberExpression
                 .Cat(
-                    Token.New(JsonTokenType.ValueSeparator)
+                    Token(JsonTokenType.ValueSeparator)
                     .Cat(memberExpression)
                     .EClosure()
                 )
                 .Optional()
-                .Cat(Token.New(JsonTokenType.EndObject))
-                .Tag(0);
+                .Cat(Token(JsonTokenType.EndObject))
+                .Tag(null);
             var arrayExpression = valueExpression
                 .Cat(
-                    Token.New(JsonTokenType.ValueSeparator)
+                    Token(JsonTokenType.ValueSeparator)
                     .Cat(valueExpression)
                     .EClosure()
                 )
                 .Optional()
-                .Cat(Token.New(JsonTokenType.EndArray))
-                .Tag(0);
+                .Cat(Token(JsonTokenType.EndArray))
+                .Tag(null);
 
-            var jsonExpression = valueExpression.Tag(0);
+            var jsonExpression = valueExpression.Tag(null);
 
-            _jsonDFA = BuildDFA(jsonExpression).States;
-            _objectDFA = BuildDFA(objectExpression).States;
-            _arrayDFA = BuildDFA(arrayExpression).States;
+            _jsonDFA = CreateDFA(jsonExpression).GetTransitionTable();
+            _objectDFA = CreateDFA(objectExpression).GetTransitionTable();
+            _arrayDFA = CreateDFA(arrayExpression).GetTransitionTable();
         }
 
-        static EDFADefinition<JsonTokenType> BuildDFA(Token expr) {
-            var builder = new DFABuilder();
-            var dfa = new EDFADefinition<JsonTokenType>(_alphabet);
+        static Token<object> Token(params JsonTokenType[] input) {
+            return Token<object>.New(input.Select(t => _alphabet.Translate(t)).ToArray());
+        }
+
+        static RegularDFADefinition<JsonTokenType,object> CreateDFA(Token<object> expr) {
+            var builder = new RegularDFABuilder<object>();
+            var dfa = new RegularDFADefinition<JsonTokenType,object>(_alphabet);
+
             expr.Accept(builder);
 
             builder.BuildDFA(dfa);
@@ -243,25 +258,13 @@
             }
         }
 
-        protected virtual void Dispose(bool disposing) {
+        protected override void Dispose(bool disposing) {
             if (disposing) {
                 m_scanner.Dispose();
             }
         }
 
         /// <summary>
-        /// Освобождает парсер и связанный с ним сканнер.
-        /// </summary>
-        public void Dispose() {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        ~JSONParser() {
-            Dispose(false);
-        }
-
-        /// <summary>
         /// Переходит в конец текущего объекта.
         /// </summary>
         public void SeekElementEnd() {