diff Implab/Automaton/RegularExpressions/RegularExpressionVisitorT.cs @ 192:f1da3afc3521 release v2.1

Слияние с v2
author cin
date Fri, 22 Apr 2016 13:10:34 +0300
parents d5c5db0335ee
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/Automaton/RegularExpressions/RegularExpressionVisitorT.cs	Fri Apr 22 13:10:34 2016 +0300
@@ -0,0 +1,37 @@
+using Implab;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+
+namespace Implab.Automaton.RegularExpressions {
+    /// <summary>
+    /// </summary>
+    public class RegularExpressionVisitor<TTag> : RegularExpressionVisitor {
+        readonly Dictionary<int, TTag> m_tags = new Dictionary<int, TTag>();
+
+        readonly ITaggedDFABuilder<TTag> m_builder;
+
+        public RegularExpressionVisitor(ITaggedDFABuilder<TTag> builder) : base(builder) {
+            m_builder = builder;
+        }
+
+        public override void Visit(EndToken token) {
+            base.Visit(token);
+            var tagged = token as EndToken<TTag>;
+            if (tagged != null)
+                m_tags.Add(Index, tagged.Tag);
+        }
+
+        protected override void MarkFinalState(HashSet<int> state) {
+            base.MarkFinalState(state);
+            m_builder.SetStateTag(Translate(state), GetStateTags(state));
+        }
+
+        TTag[] GetStateTags(IEnumerable<int> state) {
+            Debug.Assert(state != null);
+            return state.Where(m_tags.ContainsKey).Select(pos => m_tags[pos]).ToArray();
+        }
+
+    }
+}