Mercurial > pub > ImplabNet
comparison Implab/Automaton/RegularExpressions/RegularExpressionVisitorT.cs @ 178:d5c5db0335ee ref20160224
working on JSON parser
author | cin |
---|---|
date | Wed, 23 Mar 2016 19:51:45 +0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
177:a0ff6a0e9c44 | 178:d5c5db0335ee |
---|---|
1 using Implab; | |
2 using System; | |
3 using System.Collections.Generic; | |
4 using System.Diagnostics; | |
5 using System.Linq; | |
6 | |
7 namespace Implab.Automaton.RegularExpressions { | |
8 /// <summary> | |
9 /// </summary> | |
10 public class RegularExpressionVisitor<TTag> : RegularExpressionVisitor { | |
11 readonly Dictionary<int, TTag> m_tags = new Dictionary<int, TTag>(); | |
12 | |
13 readonly ITaggedDFABuilder<TTag> m_builder; | |
14 | |
15 public RegularExpressionVisitor(ITaggedDFABuilder<TTag> builder) : base(builder) { | |
16 m_builder = builder; | |
17 } | |
18 | |
19 public override void Visit(EndToken token) { | |
20 base.Visit(token); | |
21 var tagged = token as EndToken<TTag>; | |
22 if (tagged != null) | |
23 m_tags.Add(Index, tagged.Tag); | |
24 } | |
25 | |
26 protected override void MarkFinalState(HashSet<int> state) { | |
27 base.MarkFinalState(state); | |
28 m_builder.SetStateTag(Translate(state), GetStateTags(state)); | |
29 } | |
30 | |
31 TTag[] GetStateTags(IEnumerable<int> state) { | |
32 Debug.Assert(state != null); | |
33 return state.Where(m_tags.ContainsKey).Select(pos => m_tags[pos]).ToArray(); | |
34 } | |
35 | |
36 } | |
37 } |