@@ -0,0 +1,9 | |||||
|
1 | ||||
|
2 | namespace Implab.Automaton { | |||
|
3 | public static class DFAConst { | |||
|
4 | public const int UNREACHABLE_STATE = -1; | |||
|
5 | ||||
|
6 | public const int UNCLASSIFIED_INPUT = 0; | |||
|
7 | } | |||
|
8 | } | |||
|
9 |
@@ -0,0 +1,37 | |||||
|
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 | } |
@@ -0,0 +1,44 | |||||
|
1 | using System; | |||
|
2 | using System.Threading; | |||
|
3 | ||||
|
4 | namespace Implab.Components { | |||
|
5 | public class LazyAndWeak<T> where T : class { | |||
|
6 | ||||
|
7 | readonly Func<T> m_factory; | |||
|
8 | readonly object m_lock; | |||
|
9 | WeakReference m_reference; | |||
|
10 | ||||
|
11 | ||||
|
12 | public LazyAndWeak(Func<T> factory, bool useLock) { | |||
|
13 | Safe.ArgumentNotNull(factory, "factory"); | |||
|
14 | m_factory = factory; | |||
|
15 | m_lock = useLock ? new object() : null; | |||
|
16 | } | |||
|
17 | ||||
|
18 | public LazyAndWeak(Func<T> factory) : this(factory, false) { | |||
|
19 | } | |||
|
20 | ||||
|
21 | public T Value { | |||
|
22 | get { | |||
|
23 | while (true) { | |||
|
24 | var weak = m_reference; | |||
|
25 | T value; | |||
|
26 | if (weak != null) { | |||
|
27 | value = weak.Target as T; | |||
|
28 | if (value != null) | |||
|
29 | return value; | |||
|
30 | } | |||
|
31 | ||||
|
32 | if (m_lock == null) { | |||
|
33 | value = m_factory(); | |||
|
34 | ||||
|
35 | if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak) | |||
|
36 | return value; | |||
|
37 | } else { | |||
|
38 | } | |||
|
39 | } | |||
|
40 | } | |||
|
41 | } | |||
|
42 | } | |||
|
43 | } | |||
|
44 |
@@ -1,313 +1,313 | |||||
1 | using Implab; |
|
1 | using Implab; | |
2 | using System; |
|
2 | using System; | |
3 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
4 | using System.Linq; |
|
4 | using System.Linq; | |
5 |
|
5 | |||
6 | namespace Implab.Automaton { |
|
6 | namespace Implab.Automaton { | |
7 | public class DFATable : IDFATableBuilder { |
|
7 | public class DFATable : IDFATableBuilder { | |
8 | int m_stateCount; |
|
8 | int m_stateCount; | |
9 | int m_symbolCount; |
|
9 | int m_symbolCount; | |
10 | int m_initialState; |
|
10 | int m_initialState; | |
11 |
|
11 | |||
12 | readonly HashSet<int> m_finalStates = new HashSet<int>(); |
|
12 | readonly HashSet<int> m_finalStates = new HashSet<int>(); | |
13 | readonly HashSet<AutomatonTransition> m_transitions = new HashSet<AutomatonTransition>(); |
|
13 | readonly HashSet<AutomatonTransition> m_transitions = new HashSet<AutomatonTransition>(); | |
14 |
|
14 | |||
15 |
|
15 | |||
16 | #region IDFADefinition implementation |
|
16 | #region IDFADefinition implementation | |
17 |
|
17 | |||
18 | public bool IsFinalState(int s) { |
|
18 | public bool IsFinalState(int s) { | |
19 | Safe.ArgumentInRange(s, 0, m_stateCount, "s"); |
|
19 | Safe.ArgumentInRange(s, 0, m_stateCount, "s"); | |
20 |
|
20 | |||
21 | return m_finalStates.Contains(s); |
|
21 | return m_finalStates.Contains(s); | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | public IEnumerable<int> FinalStates { |
|
24 | public IEnumerable<int> FinalStates { | |
25 | get { |
|
25 | get { | |
26 | return m_finalStates; |
|
26 | return m_finalStates; | |
27 | } |
|
27 | } | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | public int StateCount { |
|
30 | public int StateCount { | |
31 | get { return m_stateCount; } |
|
31 | get { return m_stateCount; } | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
34 | public int AlphabetSize { |
|
34 | public int AlphabetSize { | |
35 | get { return m_symbolCount; } |
|
35 | get { return m_symbolCount; } | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | public int InitialState { |
|
38 | public int InitialState { | |
39 | get { return m_initialState; } |
|
39 | get { return m_initialState; } | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | #endregion |
|
42 | #endregion | |
43 |
|
43 | |||
44 | public void SetInitialState(int s) { |
|
44 | public void SetInitialState(int s) { | |
45 | Safe.ArgumentAssert(s >= 0, "s"); |
|
45 | Safe.ArgumentAssert(s >= 0, "s"); | |
46 | m_initialState = s; |
|
46 | m_initialState = s; | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | public void MarkFinalState(int state) { |
|
49 | public void MarkFinalState(int state) { | |
50 | m_finalStates.Add(state); |
|
50 | m_finalStates.Add(state); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | public void Add(AutomatonTransition item) { |
|
53 | public void Add(AutomatonTransition item) { | |
54 | Safe.ArgumentAssert(item.s1 >= 0, "item"); |
|
54 | Safe.ArgumentAssert(item.s1 >= 0, "item"); | |
55 | Safe.ArgumentAssert(item.s2 >= 0, "item"); |
|
55 | Safe.ArgumentAssert(item.s2 >= 0, "item"); | |
56 | Safe.ArgumentAssert(item.edge >= 0, "item"); |
|
56 | Safe.ArgumentAssert(item.edge >= 0, "item"); | |
57 |
|
57 | |||
58 | m_stateCount = Math.Max(m_stateCount, Math.Max(item.s1, item.s2) + 1); |
|
58 | m_stateCount = Math.Max(m_stateCount, Math.Max(item.s1, item.s2) + 1); | |
59 | m_symbolCount = Math.Max(m_symbolCount, item.edge); |
|
59 | m_symbolCount = Math.Max(m_symbolCount, item.edge); | |
60 |
|
60 | |||
61 | m_transitions.Add(item); |
|
61 | m_transitions.Add(item); | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | public void Clear() { |
|
64 | public void Clear() { | |
65 | m_stateCount = 0; |
|
65 | m_stateCount = 0; | |
66 | m_symbolCount = 0; |
|
66 | m_symbolCount = 0; | |
67 | m_finalStates.Clear(); |
|
67 | m_finalStates.Clear(); | |
68 | m_transitions.Clear(); |
|
68 | m_transitions.Clear(); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | public bool Contains(AutomatonTransition item) { |
|
71 | public bool Contains(AutomatonTransition item) { | |
72 | return m_transitions.Contains(item); |
|
72 | return m_transitions.Contains(item); | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | public void CopyTo(AutomatonTransition[] array, int arrayIndex) { |
|
75 | public void CopyTo(AutomatonTransition[] array, int arrayIndex) { | |
76 | m_transitions.CopyTo(array, arrayIndex); |
|
76 | m_transitions.CopyTo(array, arrayIndex); | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | public bool Remove(AutomatonTransition item) { |
|
79 | public bool Remove(AutomatonTransition item) { | |
80 | m_transitions.Remove(item); |
|
80 | m_transitions.Remove(item); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | public int Count { |
|
83 | public int Count { | |
84 | get { |
|
84 | get { | |
85 | return m_transitions.Count; |
|
85 | return m_transitions.Count; | |
86 | } |
|
86 | } | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | public bool IsReadOnly { |
|
89 | public bool IsReadOnly { | |
90 | get { |
|
90 | get { | |
91 | return false; |
|
91 | return false; | |
92 | } |
|
92 | } | |
93 | } |
|
93 | } | |
94 |
|
94 | |||
95 | public IEnumerator<AutomatonTransition> GetEnumerator() { |
|
95 | public IEnumerator<AutomatonTransition> GetEnumerator() { | |
96 | return m_transitions.GetEnumerator(); |
|
96 | return m_transitions.GetEnumerator(); | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { |
|
99 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { | |
100 | return GetEnumerator(); |
|
100 | return GetEnumerator(); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | public int[,] CreateTransitionTable() { |
|
103 | public int[,] CreateTransitionTable() { | |
104 | var table = new int[StateCount,AlphabetSize]; |
|
104 | var table = new int[StateCount,AlphabetSize]; | |
105 |
|
105 | |||
106 | for (int i = 0; i < StateCount; i++) |
|
106 | for (int i = 0; i < StateCount; i++) | |
107 | for (int j = 0; i < AlphabetSize; j++) |
|
107 | for (int j = 0; i < AlphabetSize; j++) | |
108 |
table[i, j] = |
|
108 | table[i, j] = AutomatonConst.UNREACHABLE_STATE; | |
109 |
|
109 | |||
110 | foreach (var t in this) |
|
110 | foreach (var t in this) | |
111 | table[t.s1,t.edge] = t.s2; |
|
111 | table[t.s1,t.edge] = t.s2; | |
112 |
|
112 | |||
113 | return table; |
|
113 | return table; | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | public bool[] CreateFinalStateTable() { |
|
116 | public bool[] CreateFinalStateTable() { | |
117 | var table = new bool[StateCount]; |
|
117 | var table = new bool[StateCount]; | |
118 |
|
118 | |||
119 | foreach (var s in FinalStates) |
|
119 | foreach (var s in FinalStates) | |
120 | table[s] = true; |
|
120 | table[s] = true; | |
121 |
|
121 | |||
122 | return table; |
|
122 | return table; | |
123 | } |
|
123 | } | |
124 |
|
124 | |||
125 | /// <summary>Формирует множества конечных состояний перед началом работы алгоритма минимизации.</summary> |
|
125 | /// <summary>Формирует множества конечных состояний перед началом работы алгоритма минимизации.</summary> | |
126 | /// <remarks> |
|
126 | /// <remarks> | |
127 | /// В процессе построения минимального автомата требуется разделить множество состояний, |
|
127 | /// В процессе построения минимального автомата требуется разделить множество состояний, | |
128 | /// на два подмножества - конечные состояния и все остальные, после чего эти подмножества |
|
128 | /// на два подмножества - конечные состояния и все остальные, после чего эти подмножества | |
129 | /// будут резделены на более мелкие. Иногда требуется гарантировать различия конечных сосотяний, |
|
129 | /// будут резделены на более мелкие. Иногда требуется гарантировать различия конечных сосотяний, | |
130 | /// для этого необходимо переопределить даннцю фукнцию, для получения множеств конечных состояний. |
|
130 | /// для этого необходимо переопределить даннцю фукнцию, для получения множеств конечных состояний. | |
131 | /// </remarks> |
|
131 | /// </remarks> | |
132 | /// <returns>The final states.</returns> |
|
132 | /// <returns>The final states.</returns> | |
133 | protected virtual IEnumerable<HashSet<int>> GroupFinalStates() { |
|
133 | protected virtual IEnumerable<HashSet<int>> GroupFinalStates() { | |
134 | return new HashSet<int>[] { m_finalStates }; |
|
134 | return new HashSet<int>[] { m_finalStates }; | |
135 | } |
|
135 | } | |
136 |
|
136 | |||
137 | protected void Optimize( |
|
137 | protected void Optimize( | |
138 | IDFATableBuilder optimalDFA, |
|
138 | IDFATableBuilder optimalDFA, | |
139 | IDictionary<int,int> alphabetMap, |
|
139 | IDictionary<int,int> alphabetMap, | |
140 | IDictionary<int,int> stateMap |
|
140 | IDictionary<int,int> stateMap | |
141 | ) { |
|
141 | ) { | |
142 | Safe.ArgumentNotNull(optimalDFA, "dfa"); |
|
142 | Safe.ArgumentNotNull(optimalDFA, "dfa"); | |
143 | Safe.ArgumentNotNull(alphabetMap, "alphabetMap"); |
|
143 | Safe.ArgumentNotNull(alphabetMap, "alphabetMap"); | |
144 | Safe.ArgumentNotNull(stateMap, "stateMap"); |
|
144 | Safe.ArgumentNotNull(stateMap, "stateMap"); | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | var setComparer = new CustomEqualityComparer<HashSet<int>>( |
|
147 | var setComparer = new CustomEqualityComparer<HashSet<int>>( | |
148 | (x, y) => x.SetEquals(y), |
|
148 | (x, y) => x.SetEquals(y), | |
149 | s => s.Sum(x => x.GetHashCode()) |
|
149 | s => s.Sum(x => x.GetHashCode()) | |
150 | ); |
|
150 | ); | |
151 |
|
151 | |||
152 | var optimalStates = new HashSet<HashSet<int>>(setComparer); |
|
152 | var optimalStates = new HashSet<HashSet<int>>(setComparer); | |
153 | var queue = new HashSet<HashSet<int>>(setComparer); |
|
153 | var queue = new HashSet<HashSet<int>>(setComparer); | |
154 |
|
154 | |||
155 | // получаем конечные состояния, сгруппированные по маркерам |
|
155 | // получаем конечные состояния, сгруппированные по маркерам | |
156 | optimalStates.UnionWith( |
|
156 | optimalStates.UnionWith( | |
157 | GroupFinalStates() |
|
157 | GroupFinalStates() | |
158 | ); |
|
158 | ); | |
159 |
|
159 | |||
160 | var state = new HashSet<int>( |
|
160 | var state = new HashSet<int>( | |
161 | Enumerable |
|
161 | Enumerable | |
162 | .Range(0, m_stateCount - 1) |
|
162 | .Range(0, m_stateCount - 1) | |
163 | .Where(i => !m_finalStates.Contains(i)) |
|
163 | .Where(i => !m_finalStates.Contains(i)) | |
164 | ); |
|
164 | ); | |
165 |
|
165 | |||
166 | optimalStates.Add(state); |
|
166 | optimalStates.Add(state); | |
167 | queue.Add(state); |
|
167 | queue.Add(state); | |
168 |
|
168 | |||
169 | var rmap = m_transitions |
|
169 | var rmap = m_transitions | |
170 | .GroupBy(t => t.s2) |
|
170 | .GroupBy(t => t.s2) | |
171 | .ToLookup( |
|
171 | .ToLookup( | |
172 | g => g.Key, // s2 |
|
172 | g => g.Key, // s2 | |
173 | g => g.ToLookup(t => t.edge, t => t.s1) |
|
173 | g => g.ToLookup(t => t.edge, t => t.s1) | |
174 | ); |
|
174 | ); | |
175 |
|
175 | |||
176 | while (queue.Count > 0) { |
|
176 | while (queue.Count > 0) { | |
177 | var stateA = queue.First(); |
|
177 | var stateA = queue.First(); | |
178 | queue.Remove(stateA); |
|
178 | queue.Remove(stateA); | |
179 |
|
179 | |||
180 | for (int c = 0; c < m_symbolCount; c++) { |
|
180 | for (int c = 0; c < m_symbolCount; c++) { | |
181 | var stateX = new HashSet<int>(); |
|
181 | var stateX = new HashSet<int>(); | |
182 | foreach(var a in stateA) |
|
182 | foreach(var a in stateA) | |
183 | stateX.UnionWith(rmap[a][c]); // all states from wich 'c' leads to 'a' |
|
183 | stateX.UnionWith(rmap[a][c]); // all states from wich 'c' leads to 'a' | |
184 |
|
184 | |||
185 | foreach (var stateY in optimalStates.ToArray()) { |
|
185 | foreach (var stateY in optimalStates.ToArray()) { | |
186 | if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) { |
|
186 | if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) { | |
187 | var stateR1 = new HashSet<int>(stateY); |
|
187 | var stateR1 = new HashSet<int>(stateY); | |
188 | var stateR2 = new HashSet<int>(stateY); |
|
188 | var stateR2 = new HashSet<int>(stateY); | |
189 |
|
189 | |||
190 | stateR1.IntersectWith(stateX); |
|
190 | stateR1.IntersectWith(stateX); | |
191 | stateR2.ExceptWith(stateX); |
|
191 | stateR2.ExceptWith(stateX); | |
192 |
|
192 | |||
193 | optimalStates.Remove(stateY); |
|
193 | optimalStates.Remove(stateY); | |
194 | optimalStates.Add(stateR1); |
|
194 | optimalStates.Add(stateR1); | |
195 | optimalStates.Add(stateR2); |
|
195 | optimalStates.Add(stateR2); | |
196 |
|
196 | |||
197 | if (queue.Contains(stateY)) { |
|
197 | if (queue.Contains(stateY)) { | |
198 | queue.Remove(stateY); |
|
198 | queue.Remove(stateY); | |
199 | queue.Add(stateR1); |
|
199 | queue.Add(stateR1); | |
200 | queue.Add(stateR2); |
|
200 | queue.Add(stateR2); | |
201 | } else { |
|
201 | } else { | |
202 | queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2); |
|
202 | queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2); | |
203 | } |
|
203 | } | |
204 | } |
|
204 | } | |
205 | } |
|
205 | } | |
206 | } |
|
206 | } | |
207 | } |
|
207 | } | |
208 |
|
208 | |||
209 | // карта получения оптимального состояния по соотвествующему ему простому состоянию |
|
209 | // карта получения оптимального состояния по соотвествующему ему простому состоянию | |
210 | var nextState = 0; |
|
210 | var nextState = 0; | |
211 | foreach (var item in optimalStates) { |
|
211 | foreach (var item in optimalStates) { | |
212 | var id = nextState++; |
|
212 | var id = nextState++; | |
213 | foreach (var s in item) |
|
213 | foreach (var s in item) | |
214 | stateMap[s] = id; |
|
214 | stateMap[s] = id; | |
215 | } |
|
215 | } | |
216 |
|
216 | |||
217 | // получаем минимальный алфавит |
|
217 | // получаем минимальный алфавит | |
218 | // входные символы не различимы, если Move(s,a1) == Move(s,a2), для любого s |
|
218 | // входные символы не различимы, если Move(s,a1) == Move(s,a2), для любого s | |
219 | // для этого используем алгоритм кластеризации, сначала |
|
219 | // для этого используем алгоритм кластеризации, сначала | |
220 | // считаем, что все символы не различимы |
|
220 | // считаем, что все символы не различимы | |
221 |
|
221 | |||
222 | var minClasses = new HashSet<HashSet<int>>(setComparer); |
|
222 | var minClasses = new HashSet<HashSet<int>>(setComparer); | |
223 | var alphaQueue = new Queue<HashSet<int>>(); |
|
223 | var alphaQueue = new Queue<HashSet<int>>(); | |
224 | alphaQueue.Enqueue(new HashSet<int>(Enumerable.Range(0,AlphabetSize))); |
|
224 | alphaQueue.Enqueue(new HashSet<int>(Enumerable.Range(0,AlphabetSize))); | |
225 |
|
225 | |||
226 | // для всех состояний, будем проверять каждый класс на различимость, |
|
226 | // для всех состояний, будем проверять каждый класс на различимость, | |
227 | // т.е. символы различимы, если они приводят к разным состояниям |
|
227 | // т.е. символы различимы, если они приводят к разным состояниям | |
228 | for (int s = 0 ; s < optimalStates.Count; s++) { |
|
228 | for (int s = 0 ; s < optimalStates.Count; s++) { | |
229 | var newQueue = new Queue<HashSet<int>>(); |
|
229 | var newQueue = new Queue<HashSet<int>>(); | |
230 |
|
230 | |||
231 | foreach (var A in alphaQueue) { |
|
231 | foreach (var A in alphaQueue) { | |
232 | // классы из одного символа делить бесполезно, переводим их сразу в |
|
232 | // классы из одного символа делить бесполезно, переводим их сразу в | |
233 | // результирующий алфавит |
|
233 | // результирующий алфавит | |
234 | if (A.Count == 1) { |
|
234 | if (A.Count == 1) { | |
235 | minClasses.Add(A); |
|
235 | minClasses.Add(A); | |
236 | continue; |
|
236 | continue; | |
237 | } |
|
237 | } | |
238 |
|
238 | |||
239 | // различаем классы символов, которые переводят в различные оптимальные состояния |
|
239 | // различаем классы символов, которые переводят в различные оптимальные состояния | |
240 | // optimalState -> alphaClass |
|
240 | // optimalState -> alphaClass | |
241 | var classes = new Dictionary<int, HashSet<int>>(); |
|
241 | var classes = new Dictionary<int, HashSet<int>>(); | |
242 |
|
242 | |||
243 | foreach (var term in A) { |
|
243 | foreach (var term in A) { | |
244 | // ищем все переходы класса по символу term |
|
244 | // ищем все переходы класса по символу term | |
245 | var res = m_transitions.Where(t => stateMap[t.s1] == s && t.edge == term).Select(t => stateMap[t.s2]).ToArray(); |
|
245 | var res = m_transitions.Where(t => stateMap[t.s1] == s && t.edge == term).Select(t => stateMap[t.s2]).ToArray(); | |
246 |
|
246 | |||
247 | var s2 = res.Length > 0 ? res[0] : -1; |
|
247 | var s2 = res.Length > 0 ? res[0] : -1; | |
248 |
|
248 | |||
249 | HashSet<int> a2; |
|
249 | HashSet<int> a2; | |
250 | if (!classes.TryGetValue(s2, out a2)) { |
|
250 | if (!classes.TryGetValue(s2, out a2)) { | |
251 | a2 = new HashSet<int>(); |
|
251 | a2 = new HashSet<int>(); | |
252 | newQueue.Enqueue(a2); |
|
252 | newQueue.Enqueue(a2); | |
253 | classes[s2] = a2; |
|
253 | classes[s2] = a2; | |
254 | } |
|
254 | } | |
255 | a2.Add(term); |
|
255 | a2.Add(term); | |
256 | } |
|
256 | } | |
257 | } |
|
257 | } | |
258 |
|
258 | |||
259 | if (newQueue.Count == 0) |
|
259 | if (newQueue.Count == 0) | |
260 | break; |
|
260 | break; | |
261 | alphaQueue = newQueue; |
|
261 | alphaQueue = newQueue; | |
262 | } |
|
262 | } | |
263 |
|
263 | |||
264 | // после окончания работы алгоритма в очереди останутся минимальные различимые классы |
|
264 | // после окончания работы алгоритма в очереди останутся минимальные различимые классы | |
265 | // входных символов |
|
265 | // входных символов | |
266 | foreach (var A in alphaQueue) |
|
266 | foreach (var A in alphaQueue) | |
267 | minClasses.Add(A); |
|
267 | minClasses.Add(A); | |
268 |
|
268 | |||
269 | // построение отображения алфавитов входных символов. |
|
269 | // построение отображения алфавитов входных символов. | |
270 | // поскольку символ DFAConst.UNCLASSIFIED_INPUT может иметь |
|
270 | // поскольку символ DFAConst.UNCLASSIFIED_INPUT может иметь | |
271 | // специальное значение, тогда сохраним минимальный класс, |
|
271 | // специальное значение, тогда сохраним минимальный класс, | |
272 | // содержащий этот символ на томже месте. |
|
272 | // содержащий этот символ на томже месте. | |
273 |
|
273 | |||
274 | var nextCls = 0; |
|
274 | var nextCls = 0; | |
275 | foreach (var item in minClasses) { |
|
275 | foreach (var item in minClasses) { | |
276 |
if (nextCls == |
|
276 | if (nextCls == AutomatonConst.UNCLASSIFIED_INPUT) | |
277 | nextCls++; |
|
277 | nextCls++; | |
278 |
|
278 | |||
279 | // сохраняем DFAConst.UNCLASSIFIED_INPUT |
|
279 | // сохраняем DFAConst.UNCLASSIFIED_INPUT | |
280 |
var cls = item.Contains( |
|
280 | var cls = item.Contains(AutomatonConst.UNCLASSIFIED_INPUT) ? AutomatonConst.UNCLASSIFIED_INPUT : nextCls; | |
281 |
|
281 | |||
282 | foreach (var a in item) |
|
282 | foreach (var a in item) | |
283 | alphabetMap[a] = cls; |
|
283 | alphabetMap[a] = cls; | |
284 |
|
284 | |||
285 | nextCls++; |
|
285 | nextCls++; | |
286 | } |
|
286 | } | |
287 |
|
287 | |||
288 | // построение автомата |
|
288 | // построение автомата | |
289 | optimalDFA.SetInitialState(stateMap[m_initialState]); |
|
289 | optimalDFA.SetInitialState(stateMap[m_initialState]); | |
290 |
|
290 | |||
291 | foreach (var sf in m_finalStates.Select(s => stateMap[s]).Distinct()) |
|
291 | foreach (var sf in m_finalStates.Select(s => stateMap[s]).Distinct()) | |
292 | optimalDFA.MarkFinalState(sf); |
|
292 | optimalDFA.MarkFinalState(sf); | |
293 |
|
293 | |||
294 | foreach (var t in m_transitions.Select(t => new AutomatonTransition(stateMap[t.s1],stateMap[t.s2],alphabetMap[t.edge])).Distinct()) |
|
294 | foreach (var t in m_transitions.Select(t => new AutomatonTransition(stateMap[t.s1],stateMap[t.s2],alphabetMap[t.edge])).Distinct()) | |
295 | optimalDFA.Add(t); |
|
295 | optimalDFA.Add(t); | |
296 | } |
|
296 | } | |
297 |
|
297 | |||
298 | protected void PrintDFA<TInput, TState>(IAlphabet<TInput> inputAlphabet, IAlphabet<TState> stateAlphabet) { |
|
298 | protected void PrintDFA<TInput, TState>(IAlphabet<TInput> inputAlphabet, IAlphabet<TState> stateAlphabet) { | |
299 | Safe.ArgumentNotNull(inputAlphabet, "inputAlphabet"); |
|
299 | Safe.ArgumentNotNull(inputAlphabet, "inputAlphabet"); | |
300 | Safe.ArgumentNotNull(stateAlphabet, "stateAlphabet"); |
|
300 | Safe.ArgumentNotNull(stateAlphabet, "stateAlphabet"); | |
301 |
|
301 | |||
302 | foreach(var t in m_transitions) |
|
302 | foreach(var t in m_transitions) | |
303 | Console.WriteLine( |
|
303 | Console.WriteLine( | |
304 | "[{0}] -{{{1}}}-> [{2}]{3}", |
|
304 | "[{0}] -{{{1}}}-> [{2}]{3}", | |
305 | String.Join(",", stateAlphabet.GetSymbols(t.s1)), |
|
305 | String.Join(",", stateAlphabet.GetSymbols(t.s1)), | |
306 | String.Join("", inputAlphabet.GetSymbols(t.edge)), |
|
306 | String.Join("", inputAlphabet.GetSymbols(t.edge)), | |
307 | String.Join(",", stateAlphabet.GetSymbols(t.s2)), |
|
307 | String.Join(",", stateAlphabet.GetSymbols(t.s2)), | |
308 | m_finalStates.Contains(t.s2) ? "$" : "" |
|
308 | m_finalStates.Contains(t.s2) ? "$" : "" | |
309 | ); |
|
309 | ); | |
310 | } |
|
310 | } | |
311 |
|
311 | |||
312 | } |
|
312 | } | |
313 | } |
|
313 | } |
@@ -1,84 +1,84 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Linq; |
|
3 | using System.Linq; | |
4 |
|
4 | |||
5 | namespace Implab.Automaton { |
|
5 | namespace Implab.Automaton { | |
6 | public class MapAlphabet<T> : IAlphabetBuilder<T> { |
|
6 | public class MapAlphabet<T> : IAlphabetBuilder<T> { | |
7 | readonly Dictionary<T,int> m_map; |
|
7 | readonly Dictionary<T,int> m_map; | |
8 | int m_nextCls; |
|
8 | int m_nextCls; | |
9 | readonly bool m_supportUnclassified; |
|
9 | readonly bool m_supportUnclassified; | |
10 |
|
10 | |||
11 | public MapAlphabet(bool supportUnclassified, IEqualityComparer<T> comparer) { |
|
11 | public MapAlphabet(bool supportUnclassified, IEqualityComparer<T> comparer) { | |
12 | m_map = comparer != null ? new Dictionary<T, int>(comparer) : new Dictionary<T,int>(); |
|
12 | m_map = comparer != null ? new Dictionary<T, int>(comparer) : new Dictionary<T,int>(); | |
13 | m_supportUnclassified = supportUnclassified; |
|
13 | m_supportUnclassified = supportUnclassified; | |
14 | m_nextCls = supportUnclassified ? 1 : 0; |
|
14 | m_nextCls = supportUnclassified ? 1 : 0; | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 | #region IAlphabetBuilder implementation |
|
17 | #region IAlphabetBuilder implementation | |
18 |
|
18 | |||
19 | public int DefineSymbol(T symbol) { |
|
19 | public int DefineSymbol(T symbol) { | |
20 | int cls; |
|
20 | int cls; | |
21 | return m_map.TryGetValue(symbol, out cls) ? cls : DefineSymbol(symbol, m_nextCls); |
|
21 | return m_map.TryGetValue(symbol, out cls) ? cls : DefineSymbol(symbol, m_nextCls); | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | public int DefineSymbol(T symbol, int cls) { |
|
24 | public int DefineSymbol(T symbol, int cls) { | |
25 | Safe.ArgumentAssert(cls >= 0, "cls"); |
|
25 | Safe.ArgumentAssert(cls >= 0, "cls"); | |
26 |
|
26 | |||
27 | m_nextCls = Math.Max(cls + 1, m_nextCls); |
|
27 | m_nextCls = Math.Max(cls + 1, m_nextCls); | |
28 | m_map.Add(symbol, cls); |
|
28 | m_map.Add(symbol, cls); | |
29 | return cls; |
|
29 | return cls; | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | public int DefineClass(IEnumerable<T> symbols) { |
|
32 | public int DefineClass(IEnumerable<T> symbols) { | |
33 | return DefineClass(symbols, m_nextCls); |
|
33 | return DefineClass(symbols, m_nextCls); | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | public int DefineClass(IEnumerable<T> symbols, int cls) { |
|
36 | public int DefineClass(IEnumerable<T> symbols, int cls) { | |
37 | Safe.ArgumentAssert(cls >= 0, "cls"); |
|
37 | Safe.ArgumentAssert(cls >= 0, "cls"); | |
38 | Safe.ArgumentNotNull(symbols, "symbols"); |
|
38 | Safe.ArgumentNotNull(symbols, "symbols"); | |
39 |
|
39 | |||
40 | m_nextCls = Math.Max(cls + 1, m_nextCls); |
|
40 | m_nextCls = Math.Max(cls + 1, m_nextCls); | |
41 |
|
41 | |||
42 | foreach (var symbol in symbols) |
|
42 | foreach (var symbol in symbols) | |
43 | m_map[symbol] = cls; |
|
43 | m_map[symbol] = cls; | |
44 | return cls; |
|
44 | return cls; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | #endregion |
|
47 | #endregion | |
48 |
|
48 | |||
49 | #region IAlphabet implementation |
|
49 | #region IAlphabet implementation | |
50 |
|
50 | |||
51 | public int Translate(T symbol) { |
|
51 | public int Translate(T symbol) { | |
52 | int cls; |
|
52 | int cls; | |
53 | if (m_map.TryGetValue(symbol, out cls)) |
|
53 | if (m_map.TryGetValue(symbol, out cls)) | |
54 | return cls; |
|
54 | return cls; | |
55 | if (!m_supportUnclassified) |
|
55 | if (!m_supportUnclassified) | |
56 | throw new ArgumentOutOfRangeException("symbol", "The specified symbol isn't in the alphabet"); |
|
56 | throw new ArgumentOutOfRangeException("symbol", "The specified symbol isn't in the alphabet"); | |
57 |
return |
|
57 | return AutomatonConst.UNCLASSIFIED_INPUT; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | public int Count { |
|
60 | public int Count { | |
61 | get { |
|
61 | get { | |
62 | return m_nextCls; |
|
62 | return m_nextCls; | |
63 | } |
|
63 | } | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | public bool Contains(T symbol) { |
|
66 | public bool Contains(T symbol) { | |
67 | return m_supportUnclassified || m_map.ContainsKey(symbol); |
|
67 | return m_supportUnclassified || m_map.ContainsKey(symbol); | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | public IEnumerable<T> GetSymbols(int cls) { |
|
71 | public IEnumerable<T> GetSymbols(int cls) { | |
72 | Safe.ArgumentAssert(cls > 0, "cls"); |
|
72 | Safe.ArgumentAssert(cls > 0, "cls"); | |
73 | return m_map.Where(p => p.Value == cls).Select(p => p.Key); |
|
73 | return m_map.Where(p => p.Value == cls).Select(p => p.Key); | |
74 | } |
|
74 | } | |
75 | #endregion |
|
75 | #endregion | |
76 |
|
76 | |||
77 | public IEnumerable<KeyValuePair<T,int>> Mappings { |
|
77 | public IEnumerable<KeyValuePair<T,int>> Mappings { | |
78 | get { |
|
78 | get { | |
79 | return m_map; |
|
79 | return m_map; | |
80 | } |
|
80 | } | |
81 | } |
|
81 | } | |
82 | } |
|
82 | } | |
83 | } |
|
83 | } | |
84 |
|
84 |
@@ -1,33 +1,23 | |||||
1 | using Implab; |
|
1 | namespace Implab.Automaton.RegularExpressions { | |
2 |
|
||||
3 | namespace Implab.Automaton.RegularExpressions { |
|
|||
4 | /// <summary> |
|
2 | /// <summary> | |
5 | /// Конечный символ расширенного регулярного выражения, при построении ДКА |
|
3 | /// Конечный символ расширенного регулярного выражения, при построении ДКА | |
6 | /// используется для определения конечных состояний. |
|
4 | /// используется для определения конечных состояний. | |
7 | /// </summary> |
|
5 | /// </summary> | |
8 | public class EndToken<TTag>: Token { |
|
6 | public class EndToken<TTag>: EndToken { | |
9 |
|
7 | |||
10 | TTag m_tag; |
|
8 | readonly TTag m_tag; | |
11 |
|
9 | |||
12 | public EndToken(TTag tag) { |
|
10 | public EndToken(TTag tag) { | |
13 | m_tag = tag; |
|
11 | m_tag = tag; | |
14 | } |
|
12 | } | |
15 |
|
13 | |||
16 | public EndToken() |
|
14 | public EndToken() | |
17 | : this(default(TTag)) { |
|
15 | : this(default(TTag)) { | |
18 | } |
|
16 | } | |
19 |
|
17 | |||
20 | public TTag Tag { |
|
18 | public TTag Tag { | |
21 | get { return m_tag; } |
|
19 | get { return m_tag; } | |
22 | } |
|
20 | } | |
23 |
|
21 | |||
24 | public override void Accept(IVisitor visitor) { |
|
|||
25 | Safe.ArgumentOfType(visitor, typeof(IVisitor<TTag>), "visitor"); |
|
|||
26 | Safe.ArgumentNotNull(visitor, "visitor"); |
|
|||
27 | ((IVisitor<TTag>)visitor).Visit(this); |
|
|||
28 | } |
|
|||
29 | public override string ToString() { |
|
|||
30 | return "#"; |
|
|||
31 |
|
|
22 | } | |
32 | } |
|
23 | } | |
33 | } |
|
@@ -1,83 +1,83 | |||||
1 | using System.Collections.Generic; |
|
1 | using System.Collections.Generic; | |
2 | using System.Linq; |
|
2 | using System.Linq; | |
3 |
|
3 | |||
4 | namespace Implab.Automaton.RegularExpressions { |
|
4 | namespace Implab.Automaton.RegularExpressions { | |
5 |
public class |
|
5 | public class TaggedDFA<TInput, TTag> : DFATable, ITaggedDFABuilder<TTag> { | |
6 |
|
6 | |||
7 | readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>(); |
|
7 | readonly Dictionary<int,TTag[]> m_tags = new Dictionary<int, TTag[]>(); | |
8 | readonly IAlphabet<TInput> m_alphabet; |
|
8 | readonly IAlphabet<TInput> m_alphabet; | |
9 |
|
9 | |||
10 |
public |
|
10 | public TaggedDFA(IAlphabet<TInput> alphabet) { | |
11 | Safe.ArgumentNotNull(alphabet, "aplhabet"); |
|
11 | Safe.ArgumentNotNull(alphabet, "aplhabet"); | |
12 |
|
12 | |||
13 | m_alphabet = alphabet; |
|
13 | m_alphabet = alphabet; | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | public IAlphabet<TInput> InputAlphabet { |
|
17 | public IAlphabet<TInput> InputAlphabet { | |
18 | get { |
|
18 | get { | |
19 | return m_alphabet; |
|
19 | return m_alphabet; | |
20 | } |
|
20 | } | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | public void MarkFinalState(int s, TTag[] tags) { |
|
23 | public void MarkFinalState(int s, TTag[] tags) { | |
24 | MarkFinalState(s); |
|
24 | MarkFinalState(s); | |
25 | SetStateTag(s, tags); |
|
25 | SetStateTag(s, tags); | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | public void SetStateTag(int s, TTag[] tags) { |
|
28 | public void SetStateTag(int s, TTag[] tags) { | |
29 | Safe.ArgumentNotNull(tags, "tags"); |
|
29 | Safe.ArgumentNotNull(tags, "tags"); | |
30 | m_tags[s] = tags; |
|
30 | m_tags[s] = tags; | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | public TTag[] GetStateTag(int s) { |
|
33 | public TTag[] GetStateTag(int s) { | |
34 | TTag[] tags; |
|
34 | TTag[] tags; | |
35 | return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0]; |
|
35 | return m_tags.TryGetValue(s, out tags) ? tags : new TTag[0]; | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | public TTag[][] CreateTagTable() { |
|
38 | public TTag[][] CreateTagTable() { | |
39 | var table = new TTag[StateCount][]; |
|
39 | var table = new TTag[StateCount][]; | |
40 |
|
40 | |||
41 | foreach (var pair in m_tags) |
|
41 | foreach (var pair in m_tags) | |
42 | table[pair.Key] = pair.Value; |
|
42 | table[pair.Key] = pair.Value; | |
43 |
|
43 | |||
44 | return table; |
|
44 | return table; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | /// <summary> |
|
47 | /// <summary> | |
48 | /// Optimize the specified alphabet. |
|
48 | /// Optimize the specified alphabet. | |
49 | /// </summary> |
|
49 | /// </summary> | |
50 | /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param> |
|
50 | /// <param name="alphabet">Пустой алфавит, который будет зполнен в процессе оптимизации.</param> | |
51 |
public |
|
51 | public TaggedDFA<TInput,TTag> Optimize(IAlphabetBuilder<TInput> alphabet) { | |
52 | Safe.ArgumentNotNull(alphabet, "alphabet"); |
|
52 | Safe.ArgumentNotNull(alphabet, "alphabet"); | |
53 |
|
53 | |||
54 |
var dfa = new |
|
54 | var dfa = new TaggedDFA<TInput, TTag>(alphabet); | |
55 |
|
55 | |||
56 | var states = new DummyAlphabet(StateCount); |
|
56 | var states = new DummyAlphabet(StateCount); | |
57 | var alphaMap = new Dictionary<int,int>(); |
|
57 | var alphaMap = new Dictionary<int,int>(); | |
58 | var stateMap = new Dictionary<int,int>(); |
|
58 | var stateMap = new Dictionary<int,int>(); | |
59 |
|
59 | |||
60 | Optimize(dfa, alphaMap, stateMap); |
|
60 | Optimize(dfa, alphaMap, stateMap); | |
61 |
|
61 | |||
62 | // mark tags in the new DFA |
|
62 | // mark tags in the new DFA | |
63 | foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value )) |
|
63 | foreach (var g in m_tags.Where(x => x.Key < StateCount).GroupBy(x => stateMap[x.Key], x => x.Value )) | |
64 | dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); |
|
64 | dfa.SetStateTag(g.Key, g.SelectMany(x => x).ToArray()); | |
65 |
|
65 | |||
66 | // make the alphabet for the new DFA |
|
66 | // make the alphabet for the new DFA | |
67 | foreach (var pair in alphaMap) |
|
67 | foreach (var pair in alphaMap) | |
68 | alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value); |
|
68 | alphabet.DefineClass(m_alphabet.GetSymbols(pair.Key), pair.Value); | |
69 |
|
69 | |||
70 | return dfa; |
|
70 | return dfa; | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | protected override IEnumerable<HashSet<int>> GroupFinalStates() { |
|
73 | protected override IEnumerable<HashSet<int>> GroupFinalStates() { | |
74 | var arrayComparer = new CustomEqualityComparer<TTag[]>( |
|
74 | var arrayComparer = new CustomEqualityComparer<TTag[]>( | |
75 | (x,y) => x.Length == y.Length && x.All(it => y.Contains(it)), |
|
75 | (x,y) => x.Length == y.Length && x.All(it => y.Contains(it)), | |
76 | x => x.Sum(it => x.GetHashCode()) |
|
76 | x => x.Sum(it => x.GetHashCode()) | |
77 | ); |
|
77 | ); | |
78 | return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet<int>(g)); |
|
78 | return FinalStates.GroupBy(x => m_tags[x], arrayComparer).Select(g => new HashSet<int>(g)); | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | } |
|
81 | } | |
82 | } |
|
82 | } | |
83 |
|
83 |
@@ -1,206 +1,212 | |||||
1 | using Implab; |
|
1 | using Implab; | |
2 | using System; |
|
2 | using System; | |
3 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
4 | using System.Diagnostics; |
|
4 | using System.Diagnostics; | |
5 | using System.Linq; |
|
5 | using System.Linq; | |
6 |
|
6 | |||
7 | namespace Implab.Automaton.RegularExpressions { |
|
7 | namespace Implab.Automaton.RegularExpressions { | |
8 | /// <summary> |
|
8 | /// <summary> | |
9 | /// Используется для построения ДКА по регулярному выражению, сначала обходит |
|
9 | /// Используется для построения ДКА по регулярному выражению, сначала обходит | |
10 | /// регулярное выражение и вычисляет followpos, затем используется метод |
|
10 | /// регулярное выражение и вычисляет followpos, затем используется метод | |
11 | /// <see cref="BuildDFA(IDFADefinition)"/> для построения автомата. |
|
11 | /// <see cref="BuildDFA(IDFADefinition)"/> для построения автомата. | |
12 | /// </summary> |
|
12 | /// </summary> | |
13 |
public class RegularExpressionVisitor |
|
13 | public class RegularExpressionVisitor : IVisitor { | |
14 | int m_idx; |
|
14 | int m_idx; | |
15 | Token m_root; |
|
15 | Token m_root; | |
16 | HashSet<int> m_firstpos; |
|
16 | HashSet<int> m_firstpos; | |
17 | HashSet<int> m_lastpos; |
|
17 | HashSet<int> m_lastpos; | |
18 |
|
18 | |||
19 | readonly Dictionary<int, HashSet<int>> m_followpos = new Dictionary<int, HashSet<int>>(); |
|
19 | readonly Dictionary<int, HashSet<int>> m_followpos = new Dictionary<int, HashSet<int>>(); | |
20 | readonly Dictionary<int, int> m_indexes = new Dictionary<int, int>(); |
|
20 | readonly Dictionary<int, int> m_indexes = new Dictionary<int, int>(); | |
21 | readonly HashSet<int> m_ends = new HashSet<int>(); |
|
21 | readonly HashSet<int> m_ends = new HashSet<int>(); | |
22 | readonly Dictionary<int, TTag> m_tags = new Dictionary<int, TTag>(); |
|
|||
23 |
|
22 | |||
24 | public Dictionary<int, HashSet<int>> FollowposMap { |
|
23 | readonly IDFATableBuilder m_builder; | |
25 | get { return m_followpos; } |
|
24 | readonly IAlphabetBuilder<HashSet<int>> m_states = new MapAlphabet<HashSet<int>>( | |
|
25 | false, | |||
|
26 | new CustomEqualityComparer<HashSet<int>>( | |||
|
27 | (x, y) => x.SetEquals(y), | |||
|
28 | x => x.Sum(n => n.GetHashCode()) | |||
|
29 | ) | |||
|
30 | ); | |||
|
31 | ||||
|
32 | public RegularExpressionVisitor(IDFATableBuilder builder) { | |||
|
33 | Safe.ArgumentNotNull(builder, "builder"); | |||
|
34 | ||||
|
35 | m_builder = builder; | |||
26 | } |
|
36 | } | |
27 |
|
37 | |||
28 |
|
|
38 | HashSet<int> Followpos(int pos) { | |
29 | HashSet<int> set; |
|
39 | HashSet<int> set; | |
30 | return m_followpos.TryGetValue(pos, out set) ? set : m_followpos[pos] = new HashSet<int>(); |
|
40 | return m_followpos.TryGetValue(pos, out set) ? set : m_followpos[pos] = new HashSet<int>(); | |
31 | } |
|
41 | } | |
32 |
|
42 | |||
33 | bool Nullable(object n) { |
|
43 | bool Nullable(object n) { | |
34 | if (n is EmptyToken || n is StarToken) |
|
44 | if (n is EmptyToken || n is StarToken) | |
35 | return true; |
|
45 | return true; | |
36 | var altToken = n as AltToken; |
|
46 | var altToken = n as AltToken; | |
37 | if (altToken != null) |
|
47 | if (altToken != null) | |
38 | return Nullable(altToken.Left) || Nullable(altToken.Right); |
|
48 | return Nullable(altToken.Left) || Nullable(altToken.Right); | |
39 | var catToken = n as CatToken; |
|
49 | var catToken = n as CatToken; | |
40 | if (catToken != null) |
|
50 | if (catToken != null) | |
41 | return Nullable(catToken.Left) && Nullable(catToken.Right); |
|
51 | return Nullable(catToken.Left) && Nullable(catToken.Right); | |
42 | return false; |
|
52 | return false; | |
43 | } |
|
53 | } | |
44 |
|
54 | |||
|
55 | protected int Index { | |||
|
56 | get { return m_idx; } | |||
|
57 | } | |||
45 |
|
58 | |||
46 | public void Visit(AltToken token) { |
|
59 | public void Visit(AltToken token) { | |
47 | if (m_root == null) |
|
60 | if (m_root == null) | |
48 | m_root = token; |
|
61 | m_root = token; | |
49 | var firtspos = new HashSet<int>(); |
|
62 | var firtspos = new HashSet<int>(); | |
50 | var lastpos = new HashSet<int>(); |
|
63 | var lastpos = new HashSet<int>(); | |
51 |
|
64 | |||
52 | token.Left.Accept(this); |
|
65 | token.Left.Accept(this); | |
53 | firtspos.UnionWith(m_firstpos); |
|
66 | firtspos.UnionWith(m_firstpos); | |
54 | lastpos.UnionWith(m_lastpos); |
|
67 | lastpos.UnionWith(m_lastpos); | |
55 |
|
68 | |||
56 | token.Right.Accept(this); |
|
69 | token.Right.Accept(this); | |
57 | firtspos.UnionWith(m_firstpos); |
|
70 | firtspos.UnionWith(m_firstpos); | |
58 | lastpos.UnionWith(m_lastpos); |
|
71 | lastpos.UnionWith(m_lastpos); | |
59 |
|
72 | |||
60 | m_firstpos = firtspos; |
|
73 | m_firstpos = firtspos; | |
61 | m_lastpos = lastpos; |
|
74 | m_lastpos = lastpos; | |
62 | } |
|
75 | } | |
63 |
|
76 | |||
64 | public void Visit(StarToken token) { |
|
77 | public void Visit(StarToken token) { | |
65 | if (m_root == null) |
|
78 | if (m_root == null) | |
66 | m_root = token; |
|
79 | m_root = token; | |
67 | token.Token.Accept(this); |
|
80 | token.Token.Accept(this); | |
68 |
|
81 | |||
69 | foreach (var i in m_lastpos) |
|
82 | foreach (var i in m_lastpos) | |
70 | Followpos(i).UnionWith(m_firstpos); |
|
83 | Followpos(i).UnionWith(m_firstpos); | |
71 | } |
|
84 | } | |
72 |
|
85 | |||
73 | public void Visit(CatToken token) { |
|
86 | public void Visit(CatToken token) { | |
74 | if (m_root == null) |
|
87 | if (m_root == null) | |
75 | m_root = token; |
|
88 | m_root = token; | |
76 |
|
89 | |||
77 | var firtspos = new HashSet<int>(); |
|
90 | var firtspos = new HashSet<int>(); | |
78 | var lastpos = new HashSet<int>(); |
|
91 | var lastpos = new HashSet<int>(); | |
79 | token.Left.Accept(this); |
|
92 | token.Left.Accept(this); | |
80 | firtspos.UnionWith(m_firstpos); |
|
93 | firtspos.UnionWith(m_firstpos); | |
81 | var leftLastpos = m_lastpos; |
|
94 | var leftLastpos = m_lastpos; | |
82 |
|
95 | |||
83 | token.Right.Accept(this); |
|
96 | token.Right.Accept(this); | |
84 | lastpos.UnionWith(m_lastpos); |
|
97 | lastpos.UnionWith(m_lastpos); | |
85 | var rightFirstpos = m_firstpos; |
|
98 | var rightFirstpos = m_firstpos; | |
86 |
|
99 | |||
87 | if (Nullable(token.Left)) |
|
100 | if (Nullable(token.Left)) | |
88 | firtspos.UnionWith(rightFirstpos); |
|
101 | firtspos.UnionWith(rightFirstpos); | |
89 |
|
102 | |||
90 | if (Nullable(token.Right)) |
|
103 | if (Nullable(token.Right)) | |
91 | lastpos.UnionWith(leftLastpos); |
|
104 | lastpos.UnionWith(leftLastpos); | |
92 |
|
105 | |||
93 | m_firstpos = firtspos; |
|
106 | m_firstpos = firtspos; | |
94 | m_lastpos = lastpos; |
|
107 | m_lastpos = lastpos; | |
95 |
|
108 | |||
96 | foreach (var i in leftLastpos) |
|
109 | foreach (var i in leftLastpos) | |
97 | Followpos(i).UnionWith(rightFirstpos); |
|
110 | Followpos(i).UnionWith(rightFirstpos); | |
98 |
|
111 | |||
99 | } |
|
112 | } | |
100 |
|
113 | |||
101 | public void Visit(EmptyToken token) { |
|
114 | public void Visit(EmptyToken token) { | |
102 | if (m_root == null) |
|
115 | if (m_root == null) | |
103 | m_root = token; |
|
116 | m_root = token; | |
104 | } |
|
117 | } | |
105 |
|
118 | |||
106 | public void Visit(SymbolToken token) { |
|
119 | public void Visit(SymbolToken token) { | |
107 | if (m_root == null) |
|
120 | if (m_root == null) | |
108 | m_root = token; |
|
121 | m_root = token; | |
109 | m_idx++; |
|
122 | m_idx++; | |
110 | m_indexes[m_idx] = token.Value; |
|
123 | m_indexes[m_idx] = token.Value; | |
111 | m_firstpos = new HashSet<int>(new[] { m_idx }); |
|
124 | m_firstpos = new HashSet<int>(new[] { m_idx }); | |
112 | m_lastpos = new HashSet<int>(new[] { m_idx }); |
|
125 | m_lastpos = new HashSet<int>(new[] { m_idx }); | |
113 | } |
|
126 | } | |
114 |
|
127 | |||
115 |
public void Visit(EndToken |
|
128 | public virtual void Visit(EndToken token) { | |
116 | if (m_root == null) |
|
129 | if (m_root == null) | |
117 | m_root = token; |
|
130 | m_root = token; | |
118 | m_idx++; |
|
131 | m_idx++; | |
119 |
m_indexes[m_idx] = |
|
132 | m_indexes[m_idx] = AutomatonConst.UNCLASSIFIED_INPUT; | |
120 | m_firstpos = new HashSet<int>(new[] { m_idx }); |
|
|||
121 | m_lastpos = new HashSet<int>(new[] { m_idx }); |
|
|||
122 | Followpos(m_idx); |
|
|||
123 | m_ends.Add(m_idx); |
|
|||
124 | m_tags.Add(m_idx, token.Tag); |
|
|||
125 | } |
|
|||
126 |
|
||||
127 | public void Visit(EndToken token) { |
|
|||
128 | if (m_root == null) |
|
|||
129 | m_root = token; |
|
|||
130 | m_idx++; |
|
|||
131 | m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT; |
|
|||
132 | m_firstpos = new HashSet<int>(new[] { m_idx }); |
|
133 | m_firstpos = new HashSet<int>(new[] { m_idx }); | |
133 | m_lastpos = new HashSet<int>(new[] { m_idx }); |
|
134 | m_lastpos = new HashSet<int>(new[] { m_idx }); | |
134 | Followpos(m_idx); |
|
135 | Followpos(m_idx); | |
135 | m_ends.Add(m_idx); |
|
136 | m_ends.Add(m_idx); | |
136 | } |
|
137 | } | |
137 |
|
138 | |||
138 |
public void BuildDFA( |
|
139 | public void BuildDFA() { | |
139 | Safe.ArgumentNotNull(dfa,"dfa"); |
|
140 | AddState(m_firstpos); | |
|
141 | SetInitialState(m_firstpos); | |||
140 |
|
142 | |||
141 | var states = new MapAlphabet<HashSet<int>>( |
|
143 | if(IsFinal(m_firstpos)) | |
142 | false, |
|
144 | MarkFinalState(m_firstpos); | |
143 | new CustomEqualityComparer<HashSet<int>>( |
|
|||
144 | (x, y) => x.SetEquals(y), |
|
|||
145 | x => x.Sum(n => n.GetHashCode()) |
|
|||
146 | )); |
|
|||
147 |
|
||||
148 | var initialState = states.DefineSymbol(m_firstpos); |
|
|||
149 | dfa.SetInitialState(initialState); |
|
|||
150 |
|
||||
151 | var tags = GetStateTags(m_firstpos); |
|
|||
152 | if (tags != null && tags.Length > 0) |
|
|||
153 | dfa.MarkFinalState(initialState, tags); |
|
|||
154 |
|
145 | |||
155 | var inputMax = m_indexes.Values.Max(); |
|
146 | var inputMax = m_indexes.Values.Max(); | |
156 | var queue = new Queue<HashSet<int>>(); |
|
147 | var queue = new Queue<HashSet<int>>(); | |
157 |
|
148 | |||
158 | queue.Enqueue(m_firstpos); |
|
149 | queue.Enqueue(m_firstpos); | |
159 |
|
150 | |||
160 | while (queue.Count > 0) { |
|
151 | while (queue.Count > 0) { | |
161 |
var s |
|
152 | var s1 = queue.Dequeue(); | |
162 | var s1 = states.Translate(state); |
|
|||
163 | Debug.Assert(s1 != DFAConst.UNCLASSIFIED_INPUT); |
|
|||
164 |
|
153 | |||
165 | for (int a = 0; a <= inputMax; a++) { |
|
154 | for (int a = 0; a <= inputMax; a++) { | |
166 |
var |
|
155 | var s2 = new HashSet<int>(); | |
167 |
foreach (var p in s |
|
156 | foreach (var p in s1) { | |
168 | if (m_indexes[p] == a) { |
|
157 | if (m_indexes[p] == a) { | |
169 |
|
|
158 | s2.UnionWith(Followpos(p)); | |
170 | } |
|
159 | } | |
171 | } |
|
160 | } | |
172 |
if ( |
|
161 | if (s2.Count > 0) { | |
173 |
i |
|
162 | if (!HasState(s2)) { | |
174 |
|
|
163 | AddState(s2); | |
175 |
s2 |
|
164 | if (IsFinal(s2)) | |
176 |
|
|
165 | MarkFinalState(s2); | |
177 | s2 = states.DefineSymbol(next); |
|
|||
178 |
|
166 | |||
179 |
|
|
167 | queue.Enqueue(s2); | |
180 |
|
||||
181 | dfa.MarkFinalState(s2); |
|
|||
182 | tags = GetStateTags(next); |
|
|||
183 | if (tags != null && tags.Length > 0) |
|
|||
184 | dfa.SetStateTag(s2, tags); |
|
|||
185 |
|
|
168 | } | |
186 |
|
169 | |||
187 |
|
|
170 | DefineTransition(s1, s2, a); | |
188 |
|
|
171 | } | |
189 | dfa.Add(new AutomatonTransition(s1, s2, a)); |
|
172 | ||
190 |
|
|
173 | } | |
191 |
|
|
174 | } | |
192 |
|
|
175 | } | |
|
176 | ||||
|
177 | protected bool HasState(HashSet<int> state) { | |||
|
178 | return m_states.Contains(state); | |||
|
179 | } | |||
|
180 | ||||
|
181 | protected void AddState(HashSet<int> state) { | |||
|
182 | Debug.Assert(!HasState(state)); | |||
|
183 | ||||
|
184 | m_states.DefineSymbol(state); | |||
|
185 | } | |||
|
186 | ||||
|
187 | protected int Translate(HashSet<int> state) { | |||
|
188 | Debug.Assert(HasState(state)); | |||
|
189 | ||||
|
190 | return m_states.Translate(state); | |||
|
191 | } | |||
|
192 | ||||
|
193 | protected virtual void SetInitialState(HashSet<int> state) { | |||
|
194 | m_builder.SetInitialState(Translate(state)); | |||
|
195 | } | |||
|
196 | ||||
|
197 | protected virtual void MarkFinalState(HashSet<int> state) { | |||
|
198 | m_builder.MarkFinalState(Translate(state)); | |||
|
199 | } | |||
|
200 | ||||
|
201 | protected virtual void DefineTransition(HashSet<int> s1, HashSet<int> s2, int ch) { | |||
|
202 | ||||
|
203 | m_builder.Add(new AutomatonTransition(Translate(s1), Translate(s2), ch)); | |||
193 | } |
|
204 | } | |
194 |
|
205 | |||
195 | bool IsFinal(IEnumerable<int> state) { |
|
206 | bool IsFinal(IEnumerable<int> state) { | |
196 | Debug.Assert(state != null); |
|
207 | Debug.Assert(state != null); | |
197 | return state.Any(m_ends.Contains); |
|
208 | return state.Any(m_ends.Contains); | |
198 | } |
|
209 | } | |
199 |
|
210 | |||
200 | TTag[] GetStateTags(IEnumerable<int> state) { |
|
|||
201 | Debug.Assert(state != null); |
|
|||
202 | return state.Where(m_tags.ContainsKey).Select(pos => m_tags[pos]).ToArray(); |
|
|||
203 | } |
|
|||
204 |
|
||||
205 | } |
|
211 | } | |
206 | } |
|
212 | } |
@@ -1,63 +1,63 | |||||
1 | using Implab; |
|
1 | using Implab; | |
2 | using System; |
|
2 | using System; | |
3 | using System.Linq; |
|
3 | using System.Linq; | |
4 |
|
4 | |||
5 | namespace Implab.Automaton.RegularExpressions { |
|
5 | namespace Implab.Automaton.RegularExpressions { | |
6 | public abstract class Token { |
|
6 | public abstract class Token { | |
7 | public abstract void Accept(IVisitor visitor); |
|
7 | public abstract void Accept(IVisitor visitor); | |
8 |
|
8 | |||
9 |
public Token E |
|
9 | public Token End() { | |
10 | return Cat(new EndToken()); |
|
10 | return Cat(new EndToken()); | |
11 | } |
|
11 | } | |
12 |
|
12 | |||
13 | public Token Tag<TTag>(TTag tag) { |
|
13 | public Token Tag<TTag>(TTag tag) { | |
14 | return Cat(new EndToken<TTag>(tag)); |
|
14 | return Cat(new EndToken<TTag>(tag)); | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 | public Token Cat(Token right) { |
|
17 | public Token Cat(Token right) { | |
18 | return new CatToken(this, right); |
|
18 | return new CatToken(this, right); | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | public Token Or(Token right) { |
|
21 | public Token Or(Token right) { | |
22 | return new AltToken(this, right); |
|
22 | return new AltToken(this, right); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | public Token Optional() { |
|
25 | public Token Optional() { | |
26 | return Or(new EmptyToken()); |
|
26 | return Or(new EmptyToken()); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | public Token EClosure() { |
|
29 | public Token EClosure() { | |
30 | return new StarToken(this); |
|
30 | return new StarToken(this); | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | public Token Closure() { |
|
33 | public Token Closure() { | |
34 | return Cat(new StarToken(this)); |
|
34 | return Cat(new StarToken(this)); | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | public Token Repeat(int count) { |
|
37 | public Token Repeat(int count) { | |
38 | Token token = null; |
|
38 | Token token = null; | |
39 |
|
39 | |||
40 | for (int i = 0; i < count; i++) |
|
40 | for (int i = 0; i < count; i++) | |
41 | token = token != null ? token.Cat(this) : this; |
|
41 | token = token != null ? token.Cat(this) : this; | |
42 | return token ?? new EmptyToken(); |
|
42 | return token ?? new EmptyToken(); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | public Token Repeat(int min, int max) { |
|
45 | public Token Repeat(int min, int max) { | |
46 | if (min > max || min < 1) |
|
46 | if (min > max || min < 1) | |
47 | throw new ArgumentOutOfRangeException(); |
|
47 | throw new ArgumentOutOfRangeException(); | |
48 | var token = Repeat(min); |
|
48 | var token = Repeat(min); | |
49 |
|
49 | |||
50 | for (int i = min; i < max; i++) |
|
50 | for (int i = min; i < max; i++) | |
51 | token = token.Cat( Optional() ); |
|
51 | token = token.Cat( Optional() ); | |
52 | return token; |
|
52 | return token; | |
53 | } |
|
53 | } | |
54 |
|
54 | |||
55 | public static Token New(params int[] set) { |
|
55 | public static Token New(params int[] set) { | |
56 | Safe.ArgumentNotNull(set, "set"); |
|
56 | Safe.ArgumentNotNull(set, "set"); | |
57 | Token token = null; |
|
57 | Token token = null; | |
58 | foreach(var c in set.Distinct()) |
|
58 | foreach(var c in set.Distinct()) | |
59 | token = token == null ? new SymbolToken(c) : token.Or(new SymbolToken(c)); |
|
59 | token = token == null ? new SymbolToken(c) : token.Or(new SymbolToken(c)); | |
60 | return token; |
|
60 | return token; | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 | } |
|
63 | } |
@@ -1,100 +1,99 | |||||
1 | using Implab; |
|
1 | using Implab; | |
2 | using System; |
|
2 | using System; | |
3 | using System.Collections.Generic; |
|
3 | using System.Collections.Generic; | |
4 | using System.Linq; |
|
4 | using System.Linq; | |
5 | using Implab.Automaton; |
|
5 | using Implab.Automaton; | |
6 | using Implab.Automaton.RegularExpressions; |
|
6 | using Implab.Automaton.RegularExpressions; | |
7 |
|
7 | |||
8 | namespace Implab.Formats { |
|
8 | namespace Implab.Formats { | |
9 | /// <summary> |
|
9 | /// <summary> | |
10 | /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. |
|
10 | /// Базовый абстрактный класс. Грамматика, позволяет формулировать выражения над алфавитом типа <c>char</c>. | |
11 | /// </summary> |
|
11 | /// </summary> | |
12 |
public abstract class Grammar<TSymbol |
|
12 | public abstract class Grammar<TSymbol> { | |
13 |
|
13 | |||
14 | protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder { |
|
14 | protected abstract IAlphabetBuilder<TSymbol> AlphabetBuilder { | |
15 | get; |
|
15 | get; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 |
protected SymbolToken |
|
18 | protected SymbolToken UnclassifiedToken() { | |
19 |
return new SymbolToken |
|
19 | return new SymbolToken(AutomatonConst.UNCLASSIFIED_INPUT); | |
20 | } |
|
20 | } | |
21 |
|
21 | |||
22 | protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) { |
|
22 | protected void DefineAlphabet(IEnumerable<TSymbol> alphabet) { | |
23 | Safe.ArgumentNotNull(alphabet, "alphabet"); |
|
23 | Safe.ArgumentNotNull(alphabet, "alphabet"); | |
24 |
|
24 | |||
25 | foreach (var ch in alphabet) |
|
25 | foreach (var ch in alphabet) | |
26 | AlphabetBuilder.DefineSymbol(ch); |
|
26 | AlphabetBuilder.DefineSymbol(ch); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 |
protected Token |
|
29 | protected Token SymbolToken(TSymbol symbol) { | |
30 |
return Token |
|
30 | return Token.New(TranslateOrAdd(symbol)); | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 |
protected Token |
|
33 | protected Token SymbolToken(IEnumerable<TSymbol> symbols) { | |
34 | Safe.ArgumentNotNull(symbols, "symbols"); |
|
34 | Safe.ArgumentNotNull(symbols, "symbols"); | |
35 |
|
35 | |||
36 |
return Token |
|
36 | return Token.New(TranslateOrAdd(symbols).ToArray()); | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 |
protected Token |
|
39 | protected Token SymbolSetToken(params TSymbol[] set) { | |
40 | return SymbolToken(set); |
|
40 | return SymbolToken(set); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | int TranslateOrAdd(TSymbol ch) { |
|
43 | int TranslateOrAdd(TSymbol ch) { | |
44 | var t = AlphabetBuilder.Translate(ch); |
|
44 | var t = AlphabetBuilder.Translate(ch); | |
45 |
if (t == |
|
45 | if (t == AutomatonConst.UNCLASSIFIED_INPUT) | |
46 | t = AlphabetBuilder.DefineSymbol(ch); |
|
46 | t = AlphabetBuilder.DefineSymbol(ch); | |
47 | return t; |
|
47 | return t; | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { |
|
50 | IEnumerable<int> TranslateOrAdd(IEnumerable<TSymbol> symbols) { | |
51 | return symbols.Distinct().Select(TranslateOrAdd); |
|
51 | return symbols.Distinct().Select(TranslateOrAdd); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | int TranslateOrDie(TSymbol ch) { |
|
54 | int TranslateOrDie(TSymbol ch) { | |
55 | var t = AlphabetBuilder.Translate(ch); |
|
55 | var t = AlphabetBuilder.Translate(ch); | |
56 |
if (t == |
|
56 | if (t == AutomatonConst.UNCLASSIFIED_INPUT) | |
57 | throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); |
|
57 | throw new ApplicationException(String.Format("Symbol '{0}' is UNCLASSIFIED", ch)); | |
58 | return t; |
|
58 | return t; | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { |
|
61 | IEnumerable<int> TranslateOrDie(IEnumerable<TSymbol> symbols) { | |
62 | return symbols.Distinct().Select(TranslateOrDie); |
|
62 | return symbols.Distinct().Select(TranslateOrDie); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 |
protected Token |
|
65 | protected Token SymbolTokenExcept(IEnumerable<TSymbol> symbols) { | |
66 | Safe.ArgumentNotNull(symbols, "symbols"); |
|
66 | Safe.ArgumentNotNull(symbols, "symbols"); | |
67 |
|
67 | |||
68 |
return Token |
|
68 | return Token.New( Enumerable.Range(0, AlphabetBuilder.Count).Except(TranslateOrDie(symbols)).ToArray() ); | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet(); |
|
71 | protected abstract IndexedAlphabetBase<TSymbol> CreateAlphabet(); | |
72 |
|
72 | |||
73 |
protected ScannerContext<TTag> BuildScannerContext |
|
73 | protected ScannerContext<TTag> BuildScannerContext<TTag>(Token regexp) { | |
74 |
|
74 | |||
75 | var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder); |
|
75 | var dfa = new RegularDFA<TSymbol, TTag>(AlphabetBuilder); | |
76 |
|
76 | |||
77 | var visitor = new RegularExpressionVisitor<TTag>(); |
|
77 | var visitor = new RegularExpressionVisitor<TTag>(dfa); | |
78 |
regexp.Accept( |
|
78 | regexp.Accept(visitor); | |
79 |
|
79 | visitor.BuildDFA(); | ||
80 | visitor.BuildDFA(dfa); |
|
|||
81 |
|
80 | |||
82 | if (dfa.IsFinalState(dfa.InitialState)) |
|
81 | if (dfa.IsFinalState(dfa.InitialState)) | |
83 | throw new ApplicationException("The specified language contains empty token"); |
|
82 | throw new ApplicationException("The specified language contains empty token"); | |
84 |
|
83 | |||
85 | var ab = CreateAlphabet(); |
|
84 | var ab = CreateAlphabet(); | |
86 | var optimal = dfa.Optimize(ab); |
|
85 | var optimal = dfa.Optimize(ab); | |
87 |
|
86 | |||
88 | return new ScannerContext<TTag>( |
|
87 | return new ScannerContext<TTag>( | |
89 | optimal.CreateTransitionTable(), |
|
88 | optimal.CreateTransitionTable(), | |
90 | optimal.CreateFinalStateTable(), |
|
89 | optimal.CreateFinalStateTable(), | |
91 | optimal.CreateTagTable(), |
|
90 | optimal.CreateTagTable(), | |
92 | optimal.InitialState, |
|
91 | optimal.InitialState, | |
93 | ab.GetTranslationMap() |
|
92 | ab.GetTranslationMap() | |
94 | ); |
|
93 | ); | |
95 | } |
|
94 | } | |
96 |
|
95 | |||
97 | } |
|
96 | } | |
98 |
|
97 | |||
99 |
|
98 | |||
100 | } |
|
99 | } |
@@ -1,11 +1,10 | |||||
1 | namespace Implab.Formats.JSON { |
|
1 | namespace Implab.Formats.JSON { | |
2 | /// <summary> |
|
2 | /// <summary> | |
3 | /// internal |
|
3 | /// internal | |
4 | /// </summary> |
|
4 | /// </summary> | |
5 | enum JSONElementContext { |
|
5 | enum JSONElementContext { | |
6 | None, |
|
6 | None, | |
7 | Object, |
|
7 | Object, | |
8 |
Array |
|
8 | Array | |
9 | Closed |
|
|||
10 | } |
|
9 | } | |
11 | } |
|
10 | } |
@@ -1,109 +1,111 | |||||
1 | using System.Linq; |
|
1 | using System.Linq; | |
2 | using Implab.Automaton.RegularExpressions; |
|
2 | using Implab.Automaton.RegularExpressions; | |
3 | using System; |
|
3 | using System; | |
4 | using Implab.Automaton; |
|
4 | using Implab.Automaton; | |
5 |
|
5 | |||
6 | namespace Implab.Formats.JSON { |
|
6 | namespace Implab.Formats.JSON { | |
7 |
class JSONGrammar : Grammar<char |
|
7 | class JSONGrammar : Grammar<char> { | |
8 | public enum TokenType { |
|
8 | public enum TokenType { | |
9 | None, |
|
9 | None, | |
10 | BeginObject, |
|
10 | BeginObject, | |
11 | EndObject, |
|
11 | EndObject, | |
12 | BeginArray, |
|
12 | BeginArray, | |
13 | EndArray, |
|
13 | EndArray, | |
14 | String, |
|
14 | String, | |
15 | Number, |
|
15 | Number, | |
16 | Literal, |
|
16 | Literal, | |
17 | NameSeparator, |
|
17 | NameSeparator, | |
18 | ValueSeparator, |
|
18 | ValueSeparator, | |
19 |
|
19 | |||
20 | StringBound, |
|
20 | StringBound, | |
21 | EscapedChar, |
|
21 | EscapedChar, | |
22 | UnescapedChar, |
|
22 | UnescapedChar, | |
23 | EscapedUnicode |
|
23 | EscapedUnicode | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>(); |
|
26 | static Lazy<JSONGrammar> _instance = new Lazy<JSONGrammar>(); | |
27 |
|
27 | |||
28 | public static JSONGrammar Instance { |
|
28 | public static JSONGrammar Instance { | |
29 | get { return _instance.Value; } |
|
29 | get { return _instance.Value; } | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 |
readonly ScannerContext<TokenType> m_json |
|
32 | readonly ScannerContext<TokenType> m_jsonExpression; | |
33 |
readonly ScannerContext<TokenType> m_string |
|
33 | readonly ScannerContext<TokenType> m_stringExpression; | |
34 |
|
34 | |||
35 | public JSONGrammar() { |
|
35 | public JSONGrammar() { | |
36 | DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); |
|
36 | DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); | |
37 | var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); |
|
37 | var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); | |
38 | var digit9 = SymbolRangeToken('1', '9'); |
|
38 | var digit9 = SymbolRangeToken('1', '9'); | |
39 | var zero = SymbolToken('0'); |
|
39 | var zero = SymbolToken('0'); | |
40 | var digit = zero.Or(digit9); |
|
40 | var digit = zero.Or(digit9); | |
41 | var dot = SymbolToken('.'); |
|
41 | var dot = SymbolToken('.'); | |
42 | var minus = SymbolToken('-'); |
|
42 | var minus = SymbolToken('-'); | |
43 | var sign = SymbolSetToken('-', '+'); |
|
43 | var sign = SymbolSetToken('-', '+'); | |
44 | var expSign = SymbolSetToken('e', 'E'); |
|
44 | var expSign = SymbolSetToken('e', 'E'); | |
45 | var letters = SymbolRangeToken('a', 'z'); |
|
45 | var letters = SymbolRangeToken('a', 'z'); | |
46 | var integer = zero.Or(digit9.Cat(digit.EClosure())); |
|
46 | var integer = zero.Or(digit9.Cat(digit.EClosure())); | |
47 | var frac = dot.Cat(digit.Closure()); |
|
47 | var frac = dot.Cat(digit.Closure()); | |
48 | var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure()); |
|
48 | var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure()); | |
49 | var quote = SymbolToken('"'); |
|
49 | var quote = SymbolToken('"'); | |
50 | var backSlash = SymbolToken('\\'); |
|
50 | var backSlash = SymbolToken('\\'); | |
51 | var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r'); |
|
51 | var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r'); | |
52 | var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4)); |
|
52 | var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4)); | |
53 | var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure(); |
|
53 | var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure(); | |
54 | var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace); |
|
54 | var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace); | |
55 | var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace); |
|
55 | var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace); | |
56 | var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace); |
|
56 | var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace); | |
57 | var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace); |
|
57 | var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace); | |
58 | var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace); |
|
58 | var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace); | |
59 | var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace); |
|
59 | var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace); | |
60 |
|
60 | |||
61 | var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional()); |
|
61 | var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional()); | |
62 | var literal = letters.Closure(); |
|
62 | var literal = letters.Closure(); | |
63 | var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x)); |
|
63 | var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x)); | |
64 |
|
64 | |||
65 | var jsonExpression = |
|
65 | var jsonExpression = | |
66 | number.Tag(TokenType.Number) |
|
66 | number.Tag(TokenType.Number) | |
67 | .Or(literal.Tag(TokenType.Literal)) |
|
67 | .Or(literal.Tag(TokenType.Literal)) | |
68 | .Or(quote.Tag(TokenType.StringBound)) |
|
68 | .Or(quote.Tag(TokenType.StringBound)) | |
69 | .Or(beginObject.Tag(TokenType.BeginObject)) |
|
69 | .Or(beginObject.Tag(TokenType.BeginObject)) | |
70 | .Or(endObject.Tag(TokenType.EndObject)) |
|
70 | .Or(endObject.Tag(TokenType.EndObject)) | |
71 | .Or(beginArray.Tag(TokenType.BeginArray)) |
|
71 | .Or(beginArray.Tag(TokenType.BeginArray)) | |
72 | .Or(endArray.Tag(TokenType.EndArray)) |
|
72 | .Or(endArray.Tag(TokenType.EndArray)) | |
73 | .Or(nameSep.Tag(TokenType.NameSeparator)) |
|
73 | .Or(nameSep.Tag(TokenType.NameSeparator)) | |
74 | .Or(valueSep.Tag(TokenType.ValueSeparator)); |
|
74 | .Or(valueSep.Tag(TokenType.ValueSeparator)); | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | var jsonStringExpression = |
|
77 | var jsonStringExpression = | |
78 | quote.Tag(TokenType.StringBound) |
|
78 | quote.Tag(TokenType.StringBound) | |
79 | .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) |
|
79 | .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) | |
80 | .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) |
|
80 | .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) | |
81 | .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); |
|
81 | .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); | |
82 |
|
82 | |||
83 |
|
83 | |||
84 |
m_json |
|
84 | m_jsonExpression = BuildScannerContext<TokenType>(jsonExpression); | |
85 |
m_string |
|
85 | m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression); | |
|
86 | ||||
|
87 | ||||
86 | } |
|
88 | } | |
87 |
|
89 | |||
88 |
public ScannerContext<TokenType> Json |
|
90 | public ScannerContext<TokenType> JsonExpression { | |
89 | get { |
|
91 | get { | |
90 |
return m_json |
|
92 | return m_jsonExpression; | |
91 | } |
|
93 | } | |
92 | } |
|
94 | } | |
93 |
|
95 | |||
94 |
public ScannerContext<TokenType> JsonString |
|
96 | public ScannerContext<TokenType> JsonStringExpression { | |
95 | get { |
|
97 | get { | |
96 |
return m_string |
|
98 | return m_stringExpression; | |
97 | } |
|
99 | } | |
98 | } |
|
100 | } | |
99 |
|
101 | |||
100 |
Token |
|
102 | Token SymbolRangeToken(char start, char stop) { | |
101 | return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); |
|
103 | return SymbolToken(Enumerable.Range(start,stop - start).Cast<char>()); | |
102 | } |
|
104 | } | |
103 |
|
105 | |||
104 | protected override IAlphabetBuilder<char> CreateAlphabet() { |
|
106 | protected override IAlphabetBuilder<char> CreateAlphabet() { | |
105 | return new CharAlphabet(); |
|
107 | return new CharAlphabet(); | |
106 | } |
|
108 | } | |
107 |
|
109 | |||
108 | } |
|
110 | } | |
109 | } |
|
111 | } |
@@ -1,280 +1,295 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Diagnostics; |
|
2 | using System.Diagnostics; | |
3 | using System.IO; |
|
3 | using System.IO; | |
4 | using Implab.Automaton; |
|
4 | using Implab.Automaton; | |
5 | using Implab.Automaton.RegularExpressions; |
|
5 | using Implab.Automaton.RegularExpressions; | |
6 | using System.Linq; |
|
6 | using System.Linq; | |
7 | using Implab.Components; |
|
7 | using Implab.Components; | |
8 |
|
8 | |||
9 | namespace Implab.Formats.JSON { |
|
9 | namespace Implab.Formats.JSON { | |
10 | /// <summary> |
|
10 | /// <summary> | |
11 | /// internal |
|
11 | /// internal | |
12 | /// </summary> |
|
12 | /// </summary> | |
13 | public struct JSONParserContext { |
|
13 | public struct JSONParserContext { | |
14 | public string memberName; |
|
14 | public string memberName; | |
15 | public JSONElementContext elementContext; |
|
15 | public JSONElementContext elementContext; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | /// <summary> |
|
18 | /// <summary> | |
19 | /// Pull парсер JSON данных. |
|
19 | /// Pull парсер JSON данных. | |
20 | /// </summary> |
|
20 | /// </summary> | |
21 | /// <remarks> |
|
21 | /// <remarks> | |
22 | /// Следует отметить отдельную интерпретацию свойства <see cref="Level"/>, |
|
22 | /// Следует отметить отдельную интерпретацию свойства <see cref="Level"/>, | |
23 | /// оно означает текущий уровень вложенности объектов, однако закрывающий |
|
23 | /// оно означает текущий уровень вложенности объектов, однако закрывающий | |
24 | /// элемент объекта и массива имеет уровень меньше, чем сам объект. |
|
24 | /// элемент объекта и массива имеет уровень меньше, чем сам объект. | |
25 | /// <code> |
|
25 | /// <code> | |
26 | /// { // Level = 1 |
|
26 | /// { // Level = 1 | |
27 | /// "name" : "Peter", // Level = 1 |
|
27 | /// "name" : "Peter", // Level = 1 | |
28 | /// "address" : { // Level = 2 |
|
28 | /// "address" : { // Level = 2 | |
29 | /// city : "Stern" // Level = 2 |
|
29 | /// city : "Stern" // Level = 2 | |
30 | /// } // Level = 1 |
|
30 | /// } // Level = 1 | |
31 | /// } // Level = 0 |
|
31 | /// } // Level = 0 | |
32 | /// </code> |
|
32 | /// </code> | |
33 | /// </remarks> |
|
33 | /// </remarks> | |
34 | public class JSONParser : Disposable { |
|
34 | public class JSONParser : Disposable { | |
35 |
|
35 | |||
36 | enum MemberContext { |
|
36 | enum MemberContext { | |
37 | MemberName, |
|
37 | MemberName, | |
38 | MemberValue |
|
38 | MemberValue | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
|
41 | #region Parser rules | |||
41 | struct ParserContext { |
|
42 | struct ParserContext { | |
42 | DFAStateDescriptior<object> |
|
43 | readonly int[,] m_dfa; | |
|
44 | int m_state; | |||
|
45 | ||||
|
46 | readonly JSONElementContext m_elementContext; | |||
|
47 | ||||
|
48 | public ParserContext(int[,] dfa, int state, JSONElementContext context) { | |||
|
49 | m_dfa = dfa; | |||
|
50 | m_state = state; | |||
|
51 | m_elementContext = context; | |||
43 | } |
|
52 | } | |
44 |
|
53 | |||
45 | static readonly EnumAlphabet<JsonTokenType> _alphabet = EnumAlphabet<JsonTokenType>.FullAlphabet; |
|
54 | public bool Move(JsonTokenType token) { | |
46 | static readonly DFAStateDescriptior<object>[] _jsonDFA; |
|
55 | var next = m_dfa[m_state, token]; | |
47 | static readonly int _jsonDFAInitialState; |
|
56 | if (next == AutomatonConst.UNREACHABLE_STATE) | |
48 | static readonly DFAStateDescriptior<object>[] _objectDFA; |
|
57 | return false; | |
49 | static readonly int _objectDFAInitialState; |
|
58 | m_state = next; | |
50 | static readonly DFAStateDescriptior<object>[] _arrayDFA; |
|
59 | } | |
51 | static readonly int _arrayDFAInitialState; |
|
60 | ||
|
61 | public JSONElementContext ElementContext { | |||
|
62 | get { return m_elementContext; } | |||
|
63 | } | |||
|
64 | } | |||
52 |
|
65 | |||
53 | static JSONParser() { |
|
66 | static JSONParser() { | |
54 |
|
67 | |||
55 |
|
68 | |||
56 | var valueExpression = Token(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String); |
|
69 | var valueExpression = Token(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String); | |
57 | var memberExpression = Token(JsonTokenType.String).Cat(Token(JsonTokenType.NameSeparator)).Cat(valueExpression); |
|
70 | var memberExpression = Token(JsonTokenType.String).Cat(Token(JsonTokenType.NameSeparator)).Cat(valueExpression); | |
58 |
|
71 | |||
59 | var objectExpression = memberExpression |
|
72 | var objectExpression = memberExpression | |
60 | .Cat( |
|
73 | .Cat( | |
61 | Token(JsonTokenType.ValueSeparator) |
|
74 | Token(JsonTokenType.ValueSeparator) | |
62 | .Cat(memberExpression) |
|
75 | .Cat(memberExpression) | |
63 | .EClosure() |
|
76 | .EClosure() | |
64 | ) |
|
77 | ) | |
65 | .Optional() |
|
78 | .Optional() | |
66 | .Cat(Token(JsonTokenType.EndObject)) |
|
79 | .Cat(Token(JsonTokenType.EndObject)) | |
67 |
. |
|
80 | .End(); | |
|
81 | ||||
68 | var arrayExpression = valueExpression |
|
82 | var arrayExpression = valueExpression | |
69 | .Cat( |
|
83 | .Cat( | |
70 | Token(JsonTokenType.ValueSeparator) |
|
84 | Token(JsonTokenType.ValueSeparator) | |
71 | .Cat(valueExpression) |
|
85 | .Cat(valueExpression) | |
72 | .EClosure() |
|
86 | .EClosure() | |
73 | ) |
|
87 | ) | |
74 | .Optional() |
|
88 | .Optional() | |
75 | .Cat(Token(JsonTokenType.EndArray)) |
|
89 | .Cat(Token(JsonTokenType.EndArray)) | |
76 |
. |
|
90 | .End(); | |
77 |
|
91 | |||
78 |
var jsonExpression = valueExpression. |
|
92 | var jsonExpression = valueExpression.End(); | |
79 |
|
93 | |||
80 |
_jsonDFA = Create |
|
94 | _jsonDFA = CreateParserContext(jsonExpression, JSONElementContext.None); | |
81 |
_objectDFA = Create |
|
95 | _objectDFA = CreateParserContext(objectExpression, JSONElementContext.Object); | |
82 |
_arrayDFA = Create |
|
96 | _arrayDFA = CreateParserContext(arrayExpression, JSONElementContext.Array); | |
83 | } |
|
97 | } | |
84 |
|
98 | |||
85 |
static Token |
|
99 | static Token Token(params JsonTokenType[] input) { | |
86 |
return Token |
|
100 | return Token.New( input.Select(t => (int)t).ToArray() ); | |
87 | } |
|
101 | } | |
88 |
|
102 | |||
89 | static RegularDFA<JsonTokenType,object> CreateDFA(Token<object> expr) { |
|
103 | static ParserContext CreateParserContext(Token expr, JSONElementContext context) { | |
90 | var builder = new RegularExpressionVisitor<object>(); |
|
|||
91 | var dfa = new RegularDFA<JsonTokenType,object>(_alphabet); |
|
|||
92 |
|
104 | |||
|
105 | var dfa = new DFATable(); | |||
|
106 | var builder = new RegularExpressionVisitor(dfa); | |||
93 | expr.Accept(builder); |
|
107 | expr.Accept(builder); | |
|
108 | builder.BuildDFA(); | |||
94 |
|
109 | |||
95 | builder.BuildDFA(dfa); |
|
110 | return new ParserContext(dfa.CreateTransitionTable(), dfa.InitialState, context); | |
96 | return dfa; |
|
|||
97 | } |
|
111 | } | |
98 |
|
112 | |||
|
113 | #endregion | |||
|
114 | ||||
99 | JSONScanner m_scanner; |
|
115 | JSONScanner m_scanner; | |
100 | MemberContext m_memberContext; |
|
116 | MemberContext m_memberContext; | |
101 |
|
117 | |||
102 | JSONElementType m_elementType; |
|
118 | JSONElementType m_elementType; | |
103 | object m_elementValue; |
|
119 | object m_elementValue; | |
104 |
|
120 | |||
105 | /// <summary> |
|
121 | /// <summary> | |
106 | /// Создает новый парсер на основе строки, содержащей JSON |
|
122 | /// Создает новый парсер на основе строки, содержащей JSON | |
107 | /// </summary> |
|
123 | /// </summary> | |
108 | /// <param name="text"></param> |
|
124 | /// <param name="text"></param> | |
109 | public JSONParser(string text) |
|
125 | public JSONParser(string text) | |
110 | : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) { |
|
126 | : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) { | |
111 | Safe.ArgumentNotEmpty(text, "text"); |
|
127 | Safe.ArgumentNotEmpty(text, "text"); | |
112 | m_scanner = new JSONScanner(); |
|
128 | m_scanner = new JSONScanner(); | |
113 | m_scanner.Feed(text.ToCharArray()); |
|
129 | m_scanner.Feed(text.ToCharArray()); | |
114 | } |
|
130 | } | |
115 |
|
131 | |||
116 | /// <summary> |
|
132 | /// <summary> | |
117 | /// Создает новый экземпляр парсера, на основе текстового потока. |
|
133 | /// Создает новый экземпляр парсера, на основе текстового потока. | |
118 | /// </summary> |
|
134 | /// </summary> | |
119 | /// <param name="reader">Текстовый поток.</param> |
|
135 | /// <param name="reader">Текстовый поток.</param> | |
120 | /// <param name="dispose">Признак того, что парсер должен конролировать время жизни входного потока.</param> |
|
136 | public JSONParser(TextReader reader) | |
121 | public JSONParser(TextReader reader, bool dispose) |
|
|||
122 | : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) { |
|
137 | : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) { | |
123 | Safe.ArgumentNotNull(reader, "reader"); |
|
138 | Safe.ArgumentNotNull(reader, "reader"); | |
124 | m_scanner = new JSONScanner(); |
|
139 | m_scanner = new JSONScanner(); | |
125 | m_scanner.Feed(reader, dispose); |
|
140 | m_scanner.Feed(reader, dispose); | |
126 | } |
|
141 | } | |
127 |
|
142 | |||
128 | /// <summary> |
|
143 | /// <summary> | |
129 | /// Тип текущего элемента на котором стоит парсер. |
|
144 | /// Тип текущего элемента на котором стоит парсер. | |
130 | /// </summary> |
|
145 | /// </summary> | |
131 | public JSONElementType ElementType { |
|
146 | public JSONElementType ElementType { | |
132 | get { return m_elementType; } |
|
147 | get { return m_elementType; } | |
133 | } |
|
148 | } | |
134 |
|
149 | |||
135 | /// <summary> |
|
150 | /// <summary> | |
136 | /// Имя элемента - имя свойства родительского контейнера. Для элементов массивов и корневого всегда |
|
151 | /// Имя элемента - имя свойства родительского контейнера. Для элементов массивов и корневого всегда | |
137 | /// пустая строка. |
|
152 | /// пустая строка. | |
138 | /// </summary> |
|
153 | /// </summary> | |
139 | public string ElementName { |
|
154 | public string ElementName { | |
140 | get { return m_context.info.memberName; } |
|
155 | get { return m_context.info.memberName; } | |
141 | } |
|
156 | } | |
142 |
|
157 | |||
143 | /// <summary> |
|
158 | /// <summary> | |
144 | /// Значение элемента. Только для элементов типа <see cref="JSONElementType.Value"/>, для остальных <c>null</c> |
|
159 | /// Значение элемента. Только для элементов типа <see cref="JSONElementType.Value"/>, для остальных <c>null</c> | |
145 | /// </summary> |
|
160 | /// </summary> | |
146 | public object ElementValue { |
|
161 | public object ElementValue { | |
147 | get { return m_elementValue; } |
|
162 | get { return m_elementValue; } | |
148 | } |
|
163 | } | |
149 |
|
164 | |||
150 | /// <summary> |
|
165 | /// <summary> | |
151 | /// Читает слеюудущий объект из потока |
|
166 | /// Читает слеюудущий объект из потока | |
152 | /// </summary> |
|
167 | /// </summary> | |
153 | /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns> |
|
168 | /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns> | |
154 | public bool Read() { |
|
169 | public bool Read() { | |
155 | if (m_context.current == UNREACHEBLE_STATE) |
|
170 | if (m_context.current == UNREACHEBLE_STATE) | |
156 | throw new InvalidOperationException("The parser is in invalid state"); |
|
171 | throw new InvalidOperationException("The parser is in invalid state"); | |
157 | object tokenValue; |
|
172 | object tokenValue; | |
158 | JsonTokenType tokenType; |
|
173 | JsonTokenType tokenType; | |
159 | m_context.info.memberName = String.Empty; |
|
174 | m_context.info.memberName = String.Empty; | |
160 | while (m_scanner.ReadToken(out tokenValue, out tokenType)) { |
|
175 | while (m_scanner.ReadToken(out tokenValue, out tokenType)) { | |
161 | Move((int)tokenType); |
|
176 | Move((int)tokenType); | |
162 | if (m_context.current == UNREACHEBLE_STATE) |
|
177 | if (m_context.current == UNREACHEBLE_STATE) | |
163 | UnexpectedToken(tokenValue, tokenType); |
|
178 | UnexpectedToken(tokenValue, tokenType); | |
164 | switch (tokenType) { |
|
179 | switch (tokenType) { | |
165 | case JsonTokenType.BeginObject: |
|
180 | case JsonTokenType.BeginObject: | |
166 | Switch( |
|
181 | Switch( | |
167 | _objectDFA, |
|
182 | _objectDFA, | |
168 | INITIAL_STATE, |
|
183 | INITIAL_STATE, | |
169 | new JSONParserContext { |
|
184 | new JSONParserContext { | |
170 | memberName = m_context.info.memberName, |
|
185 | memberName = m_context.info.memberName, | |
171 | elementContext = JSONElementContext.Object |
|
186 | elementContext = JSONElementContext.Object | |
172 | } |
|
187 | } | |
173 | ); |
|
188 | ); | |
174 | m_elementValue = null; |
|
189 | m_elementValue = null; | |
175 | m_memberContext = MemberContext.MemberName; |
|
190 | m_memberContext = MemberContext.MemberName; | |
176 | m_elementType = JSONElementType.BeginObject; |
|
191 | m_elementType = JSONElementType.BeginObject; | |
177 | return true; |
|
192 | return true; | |
178 | case JsonTokenType.EndObject: |
|
193 | case JsonTokenType.EndObject: | |
179 | Restore(); |
|
194 | Restore(); | |
180 | m_elementValue = null; |
|
195 | m_elementValue = null; | |
181 | m_elementType = JSONElementType.EndObject; |
|
196 | m_elementType = JSONElementType.EndObject; | |
182 | return true; |
|
197 | return true; | |
183 | case JsonTokenType.BeginArray: |
|
198 | case JsonTokenType.BeginArray: | |
184 | Switch( |
|
199 | Switch( | |
185 | _arrayDFA, |
|
200 | _arrayDFA, | |
186 | INITIAL_STATE, |
|
201 | INITIAL_STATE, | |
187 | new JSONParserContext { |
|
202 | new JSONParserContext { | |
188 | memberName = m_context.info.memberName, |
|
203 | memberName = m_context.info.memberName, | |
189 | elementContext = JSONElementContext.Array |
|
204 | elementContext = JSONElementContext.Array | |
190 | } |
|
205 | } | |
191 | ); |
|
206 | ); | |
192 | m_elementValue = null; |
|
207 | m_elementValue = null; | |
193 | m_memberContext = MemberContext.MemberValue; |
|
208 | m_memberContext = MemberContext.MemberValue; | |
194 | m_elementType = JSONElementType.BeginArray; |
|
209 | m_elementType = JSONElementType.BeginArray; | |
195 | return true; |
|
210 | return true; | |
196 | case JsonTokenType.EndArray: |
|
211 | case JsonTokenType.EndArray: | |
197 | Restore(); |
|
212 | Restore(); | |
198 | m_elementValue = null; |
|
213 | m_elementValue = null; | |
199 | m_elementType = JSONElementType.EndArray; |
|
214 | m_elementType = JSONElementType.EndArray; | |
200 | return true; |
|
215 | return true; | |
201 | case JsonTokenType.String: |
|
216 | case JsonTokenType.String: | |
202 | if (m_memberContext == MemberContext.MemberName) { |
|
217 | if (m_memberContext == MemberContext.MemberName) { | |
203 | m_context.info.memberName = (string)tokenValue; |
|
218 | m_context.info.memberName = (string)tokenValue; | |
204 | break; |
|
219 | break; | |
205 | } |
|
220 | } | |
206 | m_elementType = JSONElementType.Value; |
|
221 | m_elementType = JSONElementType.Value; | |
207 | m_elementValue = tokenValue; |
|
222 | m_elementValue = tokenValue; | |
208 | return true; |
|
223 | return true; | |
209 | case JsonTokenType.Number: |
|
224 | case JsonTokenType.Number: | |
210 | m_elementType = JSONElementType.Value; |
|
225 | m_elementType = JSONElementType.Value; | |
211 | m_elementValue = tokenValue; |
|
226 | m_elementValue = tokenValue; | |
212 | return true; |
|
227 | return true; | |
213 | case JsonTokenType.Literal: |
|
228 | case JsonTokenType.Literal: | |
214 | m_elementType = JSONElementType.Value; |
|
229 | m_elementType = JSONElementType.Value; | |
215 | m_elementValue = ParseLiteral((string)tokenValue); |
|
230 | m_elementValue = ParseLiteral((string)tokenValue); | |
216 | return true; |
|
231 | return true; | |
217 | case JsonTokenType.NameSeparator: |
|
232 | case JsonTokenType.NameSeparator: | |
218 | m_memberContext = MemberContext.MemberValue; |
|
233 | m_memberContext = MemberContext.MemberValue; | |
219 | break; |
|
234 | break; | |
220 | case JsonTokenType.ValueSeparator: |
|
235 | case JsonTokenType.ValueSeparator: | |
221 | m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue; |
|
236 | m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue; | |
222 | break; |
|
237 | break; | |
223 | default: |
|
238 | default: | |
224 | UnexpectedToken(tokenValue, tokenType); |
|
239 | UnexpectedToken(tokenValue, tokenType); | |
225 | break; |
|
240 | break; | |
226 | } |
|
241 | } | |
227 | } |
|
242 | } | |
228 | if (m_context.info.elementContext != JSONElementContext.None) |
|
243 | if (m_context.info.elementContext != JSONElementContext.None) | |
229 | throw new ParserException("Unexpedted end of data"); |
|
244 | throw new ParserException("Unexpedted end of data"); | |
230 | return false; |
|
245 | return false; | |
231 | } |
|
246 | } | |
232 |
|
247 | |||
233 | object ParseLiteral(string literal) { |
|
248 | object ParseLiteral(string literal) { | |
234 | switch (literal) { |
|
249 | switch (literal) { | |
235 | case "null": |
|
250 | case "null": | |
236 | return null; |
|
251 | return null; | |
237 | case "false": |
|
252 | case "false": | |
238 | return false; |
|
253 | return false; | |
239 | case "true": |
|
254 | case "true": | |
240 | return true; |
|
255 | return true; | |
241 | default: |
|
256 | default: | |
242 | UnexpectedToken(literal, JsonTokenType.Literal); |
|
257 | UnexpectedToken(literal, JsonTokenType.Literal); | |
243 | return null; // avoid compliler error |
|
258 | return null; // avoid compliler error | |
244 | } |
|
259 | } | |
245 | } |
|
260 | } | |
246 |
|
261 | |||
247 | void UnexpectedToken(object value, JsonTokenType tokenType) { |
|
262 | void UnexpectedToken(object value, JsonTokenType tokenType) { | |
248 | throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value)); |
|
263 | throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value)); | |
249 | } |
|
264 | } | |
250 |
|
265 | |||
251 |
|
266 | |||
252 | /// <summary> |
|
267 | /// <summary> | |
253 | /// Признак конца потока |
|
268 | /// Признак конца потока | |
254 | /// </summary> |
|
269 | /// </summary> | |
255 | public bool EOF { |
|
270 | public bool EOF { | |
256 | get { |
|
271 | get { | |
257 | return m_scanner.EOF; |
|
272 | return m_scanner.EOF; | |
258 | } |
|
273 | } | |
259 | } |
|
274 | } | |
260 |
|
275 | |||
261 | protected override void Dispose(bool disposing) { |
|
276 | protected override void Dispose(bool disposing) { | |
262 | if (disposing) { |
|
277 | if (disposing) { | |
263 | m_scanner.Dispose(); |
|
278 | m_scanner.Dispose(); | |
264 | } |
|
279 | } | |
265 | } |
|
280 | } | |
266 |
|
281 | |||
267 | /// <summary> |
|
282 | /// <summary> | |
268 | /// Переходит в конец текущего объекта. |
|
283 | /// Переходит в конец текущего объекта. | |
269 | /// </summary> |
|
284 | /// </summary> | |
270 | public void SeekElementEnd() { |
|
285 | public void SeekElementEnd() { | |
271 | var level = Level - 1; |
|
286 | var level = Level - 1; | |
272 |
|
287 | |||
273 | Debug.Assert(level >= 0); |
|
288 | Debug.Assert(level >= 0); | |
274 |
|
289 | |||
275 | while (Level != level) |
|
290 | while (Level != level) | |
276 | Read(); |
|
291 | Read(); | |
277 | } |
|
292 | } | |
278 | } |
|
293 | } | |
279 |
|
294 | |||
280 | } |
|
295 | } |
@@ -1,150 +1,150 | |||||
1 | using System; |
|
1 | using System; | |
2 | using Implab.Components; |
|
2 | using Implab.Components; | |
3 | using System.Diagnostics; |
|
3 | using System.Diagnostics; | |
4 | using Implab.Automaton; |
|
4 | using Implab.Automaton; | |
5 | using System.Text; |
|
5 | using System.Text; | |
6 |
|
6 | |||
7 | namespace Implab.Formats { |
|
7 | namespace Implab.Formats { | |
8 | public abstract class TextScanner : Disposable { |
|
8 | public abstract class TextScanner : Disposable { | |
9 | readonly int m_bufferMax; |
|
9 | readonly int m_bufferMax; | |
10 | readonly int m_chunkSize; |
|
10 | readonly int m_chunkSize; | |
11 |
|
11 | |||
12 | char[] m_buffer; |
|
12 | char[] m_buffer; | |
13 | int m_bufferOffset; |
|
13 | int m_bufferOffset; | |
14 | int m_bufferSize; |
|
14 | int m_bufferSize; | |
15 | int m_tokenOffset; |
|
15 | int m_tokenOffset; | |
16 | int m_tokenLength; |
|
16 | int m_tokenLength; | |
17 |
|
17 | |||
18 | /// <summary> |
|
18 | /// <summary> | |
19 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. |
|
19 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. | |
20 | /// </summary> |
|
20 | /// </summary> | |
21 | /// <param name="bufferMax">Buffer max.</param> |
|
21 | /// <param name="bufferMax">Buffer max.</param> | |
22 | /// <param name="chunkSize">Chunk size.</param> |
|
22 | /// <param name="chunkSize">Chunk size.</param> | |
23 | protected TextScanner(int bufferMax, int chunkSize) { |
|
23 | protected TextScanner(int bufferMax, int chunkSize) { | |
24 | Debug.Assert(m_chunkSize <= m_bufferMax); |
|
24 | Debug.Assert(m_chunkSize <= m_bufferMax); | |
25 |
|
25 | |||
26 | m_bufferMax = bufferMax; |
|
26 | m_bufferMax = bufferMax; | |
27 | m_chunkSize = chunkSize; |
|
27 | m_chunkSize = chunkSize; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | /// <summary> |
|
30 | /// <summary> | |
31 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. |
|
31 | /// Initializes a new instance of the <see cref="Implab.Formats.TextScanner"/> class. | |
32 | /// </summary> |
|
32 | /// </summary> | |
33 | /// <param name="buffer">Buffer.</param> |
|
33 | /// <param name="buffer">Buffer.</param> | |
34 | protected TextScanner(char[] buffer) { |
|
34 | protected TextScanner(char[] buffer) { | |
35 | if (buffer != null) { |
|
35 | if (buffer != null) { | |
36 | m_buffer = buffer; |
|
36 | m_buffer = buffer; | |
37 | m_bufferSize = buffer.Length; |
|
37 | m_bufferSize = buffer.Length; | |
38 | } |
|
38 | } | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | /// <summary> |
|
41 | /// <summary> | |
42 | /// (hungry) Reads the next token. |
|
42 | /// (hungry) Reads the next token. | |
43 | /// </summary> |
|
43 | /// </summary> | |
44 | /// <returns><c>true</c>, if token internal was read, <c>false</c> if there is no more tokens in the stream.</returns> |
|
44 | /// <returns><c>true</c>, if token internal was read, <c>false</c> if there is no more tokens in the stream.</returns> | |
45 | /// <param name="dfa">The transition map for the automaton</param> |
|
45 | /// <param name="dfa">The transition map for the automaton</param> | |
46 | /// <param name="final">Final states of the automaton.</param> |
|
46 | /// <param name="final">Final states of the automaton.</param> | |
47 | /// <param name="tags">Tags.</param> |
|
47 | /// <param name="tags">Tags.</param> | |
48 | /// <param name="state">The initial state for the automaton.</param> |
|
48 | /// <param name="state">The initial state for the automaton.</param> | |
49 | /// <param name="alphabet"></param> |
|
49 | /// <param name="alphabet"></param> | |
50 | /// <param name = "tag"></param> |
|
50 | /// <param name = "tag"></param> | |
51 | internal bool ReadToken<TTag>(int[,] dfa, bool[] final, TTag[][] tags, int state, int[] alphabet, out TTag[] tag) { |
|
51 | internal bool ReadToken<TTag>(int[,] dfa, bool[] final, TTag[][] tags, int state, int[] alphabet, out TTag[] tag) { | |
52 | Safe.ArgumentNotNull(); |
|
52 | Safe.ArgumentNotNull(); | |
53 | m_tokenLength = 0; |
|
53 | m_tokenLength = 0; | |
54 |
|
54 | |||
55 | var maxSymbol = alphabet.Length - 1; |
|
55 | var maxSymbol = alphabet.Length - 1; | |
56 |
|
56 | |||
57 | do { |
|
57 | do { | |
58 | // after the next chunk is read the offset in the buffer may change |
|
58 | // after the next chunk is read the offset in the buffer may change | |
59 | int pos = m_bufferOffset + m_tokenLength; |
|
59 | int pos = m_bufferOffset + m_tokenLength; | |
60 |
|
60 | |||
61 | while (pos < m_bufferSize) { |
|
61 | while (pos < m_bufferSize) { | |
62 | var ch = m_buffer[pos]; |
|
62 | var ch = m_buffer[pos]; | |
63 |
|
63 | |||
64 |
state = dfa[state, ch > maxSymbol ? |
|
64 | state = dfa[state, ch > maxSymbol ? AutomatonConst.UNCLASSIFIED_INPUT : alphabet[ch]]; | |
65 |
if (state == |
|
65 | if (state == AutomatonConst.UNREACHABLE_STATE) | |
66 | break; |
|
66 | break; | |
67 |
|
67 | |||
68 | pos++; |
|
68 | pos++; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | m_tokenLength = pos - m_bufferOffset; |
|
71 | m_tokenLength = pos - m_bufferOffset; | |
72 |
} while (state != |
|
72 | } while (state != AutomatonConst.UNREACHABLE_STATE && Feed()); | |
73 |
|
73 | |||
74 | m_tokenOffset = m_bufferOffset; |
|
74 | m_tokenOffset = m_bufferOffset; | |
75 | m_bufferOffset += m_tokenLength; |
|
75 | m_bufferOffset += m_tokenLength; | |
76 |
|
76 | |||
77 | if (final[state]) { |
|
77 | if (final[state]) { | |
78 | tag = tags[state]; |
|
78 | tag = tags[state]; | |
79 | return true; |
|
79 | return true; | |
80 | } |
|
80 | } | |
81 |
|
81 | |||
82 | if (m_bufferOffset == m_bufferSize) { |
|
82 | if (m_bufferOffset == m_bufferSize) { | |
83 | if (m_tokenLength == 0) //EOF |
|
83 | if (m_tokenLength == 0) //EOF | |
84 | return false; |
|
84 | return false; | |
85 |
|
85 | |||
86 | throw new ParserException(); |
|
86 | throw new ParserException(); | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | throw new ParserException(String.Format("Unexpected symbol '{0}'", m_buffer[m_bufferOffset])); |
|
89 | throw new ParserException(String.Format("Unexpected symbol '{0}'", m_buffer[m_bufferOffset])); | |
90 |
|
90 | |||
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | protected void Feed(char[] buffer, int offset, int length) { |
|
93 | protected void Feed(char[] buffer, int offset, int length) { | |
94 | m_buffer = buffer; |
|
94 | m_buffer = buffer; | |
95 | m_bufferOffset = offset; |
|
95 | m_bufferOffset = offset; | |
96 | m_bufferSize = offset + length; |
|
96 | m_bufferSize = offset + length; | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | protected bool Feed() { |
|
99 | protected bool Feed() { | |
100 | if (m_chunkSize <= 0) |
|
100 | if (m_chunkSize <= 0) | |
101 | return false; |
|
101 | return false; | |
102 |
|
102 | |||
103 | if (m_buffer != null) { |
|
103 | if (m_buffer != null) { | |
104 | var free = m_buffer.Length - m_bufferSize; |
|
104 | var free = m_buffer.Length - m_bufferSize; | |
105 |
|
105 | |||
106 | if (free < m_chunkSize) { |
|
106 | if (free < m_chunkSize) { | |
107 | free += m_chunkSize; |
|
107 | free += m_chunkSize; | |
108 | var used = m_bufferSize - m_bufferOffset; |
|
108 | var used = m_bufferSize - m_bufferOffset; | |
109 | var size = used + free; |
|
109 | var size = used + free; | |
110 |
|
110 | |||
111 | if (size > m_bufferMax) |
|
111 | if (size > m_bufferMax) | |
112 | throw new ParserException(String.Format("The buffer limit ({0} Kb) is reached", m_bufferMax/1024)); |
|
112 | throw new ParserException(String.Format("The buffer limit ({0} Kb) is reached", m_bufferMax/1024)); | |
113 |
|
113 | |||
114 | var temp = new char[size]; |
|
114 | var temp = new char[size]; | |
115 |
|
115 | |||
116 | var read = Read(temp, used, m_chunkSize); |
|
116 | var read = Read(temp, used, m_chunkSize); | |
117 | if (read == 0) |
|
117 | if (read == 0) | |
118 | return false; |
|
118 | return false; | |
119 |
|
119 | |||
120 | Array.Copy(m_buffer, m_bufferOffset, temp, 0, used); |
|
120 | Array.Copy(m_buffer, m_bufferOffset, temp, 0, used); | |
121 |
|
121 | |||
122 | m_bufferOffset = 0; |
|
122 | m_bufferOffset = 0; | |
123 | m_bufferSize = used + read; |
|
123 | m_bufferSize = used + read; | |
124 | m_buffer = temp; |
|
124 | m_buffer = temp; | |
125 | } |
|
125 | } | |
126 | } else { |
|
126 | } else { | |
127 | Debug.Assert(m_bufferOffset == 0); |
|
127 | Debug.Assert(m_bufferOffset == 0); | |
128 | m_buffer = new char[m_chunkSize]; |
|
128 | m_buffer = new char[m_chunkSize]; | |
129 | m_bufferSize = Read(m_buffer, 0, m_chunkSize); |
|
129 | m_bufferSize = Read(m_buffer, 0, m_chunkSize); | |
130 | return (m_bufferSize != 0); |
|
130 | return (m_bufferSize != 0); | |
131 | } |
|
131 | } | |
132 | } |
|
132 | } | |
133 |
|
133 | |||
134 | protected abstract int Read(char[] buffer, int offset, int size); |
|
134 | protected abstract int Read(char[] buffer, int offset, int size); | |
135 |
|
135 | |||
136 | public string GetTokenValue() { |
|
136 | public string GetTokenValue() { | |
137 | return new String(m_buffer, m_tokenOffset, m_tokenLength); |
|
137 | return new String(m_buffer, m_tokenOffset, m_tokenLength); | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 | public void CopyTokenTo(char[] buffer, int offset) { |
|
140 | public void CopyTokenTo(char[] buffer, int offset) { | |
141 | m_buffer.CopyTo(buffer, offset); |
|
141 | m_buffer.CopyTo(buffer, offset); | |
142 | } |
|
142 | } | |
143 |
|
143 | |||
144 | public void CopyTokenTo(StringBuilder sb) { |
|
144 | public void CopyTokenTo(StringBuilder sb) { | |
145 | sb.Append(m_buffer, m_tokenOffset, m_tokenLength); |
|
145 | sb.Append(m_buffer, m_tokenOffset, m_tokenLength); | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | } |
|
148 | } | |
149 | } |
|
149 | } | |
150 |
|
150 |
@@ -1,275 +1,276 | |||||
1 | <?xml version="1.0" encoding="utf-8"?> |
|
1 | <?xml version="1.0" encoding="utf-8"?> | |
2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
3 | <PropertyGroup> |
|
3 | <PropertyGroup> | |
4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |
5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |
6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> | |
7 | <OutputType>Library</OutputType> |
|
7 | <OutputType>Library</OutputType> | |
8 | <RootNamespace>Implab</RootNamespace> |
|
8 | <RootNamespace>Implab</RootNamespace> | |
9 | <AssemblyName>Implab</AssemblyName> |
|
9 | <AssemblyName>Implab</AssemblyName> | |
10 | <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> |
|
10 | <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | |
11 | <ReleaseVersion>0.2</ReleaseVersion> |
|
11 | <ReleaseVersion>0.2</ReleaseVersion> | |
12 | <ProductVersion>8.0.30703</ProductVersion> |
|
12 | <ProductVersion>8.0.30703</ProductVersion> | |
13 | <SchemaVersion>2.0</SchemaVersion> |
|
13 | <SchemaVersion>2.0</SchemaVersion> | |
14 | </PropertyGroup> |
|
14 | </PropertyGroup> | |
15 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
15 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |
16 | <DebugSymbols>true</DebugSymbols> |
|
16 | <DebugSymbols>true</DebugSymbols> | |
17 | <DebugType>full</DebugType> |
|
17 | <DebugType>full</DebugType> | |
18 | <Optimize>false</Optimize> |
|
18 | <Optimize>false</Optimize> | |
19 | <OutputPath>bin\Debug</OutputPath> |
|
19 | <OutputPath>bin\Debug</OutputPath> | |
20 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
20 | <DefineConstants>TRACE;DEBUG;</DefineConstants> | |
21 | <ErrorReport>prompt</ErrorReport> |
|
21 | <ErrorReport>prompt</ErrorReport> | |
22 | <WarningLevel>4</WarningLevel> |
|
22 | <WarningLevel>4</WarningLevel> | |
23 | <ConsolePause>false</ConsolePause> |
|
23 | <ConsolePause>false</ConsolePause> | |
24 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
24 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
25 | </PropertyGroup> |
|
25 | </PropertyGroup> | |
26 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
26 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |
27 | <DebugType>full</DebugType> |
|
27 | <DebugType>full</DebugType> | |
28 | <Optimize>true</Optimize> |
|
28 | <Optimize>true</Optimize> | |
29 | <OutputPath>bin\Release</OutputPath> |
|
29 | <OutputPath>bin\Release</OutputPath> | |
30 | <ErrorReport>prompt</ErrorReport> |
|
30 | <ErrorReport>prompt</ErrorReport> | |
31 | <WarningLevel>4</WarningLevel> |
|
31 | <WarningLevel>4</WarningLevel> | |
32 | <ConsolePause>false</ConsolePause> |
|
32 | <ConsolePause>false</ConsolePause> | |
33 | </PropertyGroup> |
|
33 | </PropertyGroup> | |
34 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
34 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> | |
35 | <DebugSymbols>true</DebugSymbols> |
|
35 | <DebugSymbols>true</DebugSymbols> | |
36 | <DebugType>full</DebugType> |
|
36 | <DebugType>full</DebugType> | |
37 | <Optimize>false</Optimize> |
|
37 | <Optimize>false</Optimize> | |
38 | <OutputPath>bin\Debug</OutputPath> |
|
38 | <OutputPath>bin\Debug</OutputPath> | |
39 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
39 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> | |
40 | <ErrorReport>prompt</ErrorReport> |
|
40 | <ErrorReport>prompt</ErrorReport> | |
41 | <WarningLevel>4</WarningLevel> |
|
41 | <WarningLevel>4</WarningLevel> | |
42 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
42 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
43 | <ConsolePause>false</ConsolePause> |
|
43 | <ConsolePause>false</ConsolePause> | |
44 | </PropertyGroup> |
|
44 | </PropertyGroup> | |
45 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
45 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> | |
46 | <Optimize>true</Optimize> |
|
46 | <Optimize>true</Optimize> | |
47 | <OutputPath>bin\Release</OutputPath> |
|
47 | <OutputPath>bin\Release</OutputPath> | |
48 | <ErrorReport>prompt</ErrorReport> |
|
48 | <ErrorReport>prompt</ErrorReport> | |
49 | <WarningLevel>4</WarningLevel> |
|
49 | <WarningLevel>4</WarningLevel> | |
50 | <ConsolePause>false</ConsolePause> |
|
50 | <ConsolePause>false</ConsolePause> | |
51 | <DefineConstants>NET_4_5</DefineConstants> |
|
51 | <DefineConstants>NET_4_5</DefineConstants> | |
52 | </PropertyGroup> |
|
52 | </PropertyGroup> | |
53 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> |
|
53 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> | |
54 | <DebugSymbols>true</DebugSymbols> |
|
54 | <DebugSymbols>true</DebugSymbols> | |
55 | <DebugType>full</DebugType> |
|
55 | <DebugType>full</DebugType> | |
56 | <Optimize>false</Optimize> |
|
56 | <Optimize>false</Optimize> | |
57 | <OutputPath>bin\Debug</OutputPath> |
|
57 | <OutputPath>bin\Debug</OutputPath> | |
58 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> |
|
58 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> | |
59 | <ErrorReport>prompt</ErrorReport> |
|
59 | <ErrorReport>prompt</ErrorReport> | |
60 | <WarningLevel>4</WarningLevel> |
|
60 | <WarningLevel>4</WarningLevel> | |
61 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
61 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
62 | <ConsolePause>false</ConsolePause> |
|
62 | <ConsolePause>false</ConsolePause> | |
63 | </PropertyGroup> |
|
63 | </PropertyGroup> | |
64 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> |
|
64 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> | |
65 | <Optimize>true</Optimize> |
|
65 | <Optimize>true</Optimize> | |
66 | <OutputPath>bin\Release</OutputPath> |
|
66 | <OutputPath>bin\Release</OutputPath> | |
67 | <DefineConstants>NET_4_5;MONO;</DefineConstants> |
|
67 | <DefineConstants>NET_4_5;MONO;</DefineConstants> | |
68 | <ErrorReport>prompt</ErrorReport> |
|
68 | <ErrorReport>prompt</ErrorReport> | |
69 | <WarningLevel>4</WarningLevel> |
|
69 | <WarningLevel>4</WarningLevel> | |
70 | <ConsolePause>false</ConsolePause> |
|
70 | <ConsolePause>false</ConsolePause> | |
71 | </PropertyGroup> |
|
71 | </PropertyGroup> | |
72 | <ItemGroup> |
|
72 | <ItemGroup> | |
73 | <Reference Include="System" /> |
|
73 | <Reference Include="System" /> | |
74 | <Reference Include="System.Xml" /> |
|
74 | <Reference Include="System.Xml" /> | |
75 | <Reference Include="mscorlib" /> |
|
75 | <Reference Include="mscorlib" /> | |
76 | </ItemGroup> |
|
76 | </ItemGroup> | |
77 | <ItemGroup> |
|
77 | <ItemGroup> | |
78 | <Compile Include="CustomEqualityComparer.cs" /> |
|
78 | <Compile Include="CustomEqualityComparer.cs" /> | |
79 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
79 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> | |
80 | <Compile Include="Diagnostics\EventText.cs" /> |
|
80 | <Compile Include="Diagnostics\EventText.cs" /> | |
81 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
81 | <Compile Include="Diagnostics\LogChannel.cs" /> | |
82 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
82 | <Compile Include="Diagnostics\LogicalOperation.cs" /> | |
83 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
83 | <Compile Include="Diagnostics\TextFileListener.cs" /> | |
84 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
84 | <Compile Include="Diagnostics\TraceLog.cs" /> | |
85 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
85 | <Compile Include="Diagnostics\TraceEvent.cs" /> | |
86 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
86 | <Compile Include="Diagnostics\TraceEventType.cs" /> | |
87 | <Compile Include="ICancellable.cs" /> |
|
87 | <Compile Include="ICancellable.cs" /> | |
88 | <Compile Include="IProgressHandler.cs" /> |
|
88 | <Compile Include="IProgressHandler.cs" /> | |
89 | <Compile Include="IProgressNotifier.cs" /> |
|
89 | <Compile Include="IProgressNotifier.cs" /> | |
90 | <Compile Include="IPromiseT.cs" /> |
|
90 | <Compile Include="IPromiseT.cs" /> | |
91 | <Compile Include="IPromise.cs" /> |
|
91 | <Compile Include="IPromise.cs" /> | |
92 | <Compile Include="IServiceLocator.cs" /> |
|
92 | <Compile Include="IServiceLocator.cs" /> | |
93 | <Compile Include="ITaskController.cs" /> |
|
93 | <Compile Include="ITaskController.cs" /> | |
94 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
94 | <Compile Include="Parallels\DispatchPool.cs" /> | |
95 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
95 | <Compile Include="Parallels\ArrayTraits.cs" /> | |
96 | <Compile Include="Parallels\MTQueue.cs" /> |
|
96 | <Compile Include="Parallels\MTQueue.cs" /> | |
97 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
97 | <Compile Include="Parallels\WorkerPool.cs" /> | |
98 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
98 | <Compile Include="ProgressInitEventArgs.cs" /> | |
99 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
99 | <Compile Include="Properties\AssemblyInfo.cs" /> | |
100 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
100 | <Compile Include="Parallels\AsyncPool.cs" /> | |
101 | <Compile Include="Safe.cs" /> |
|
101 | <Compile Include="Safe.cs" /> | |
102 | <Compile Include="ValueEventArgs.cs" /> |
|
102 | <Compile Include="ValueEventArgs.cs" /> | |
103 | <Compile Include="PromiseExtensions.cs" /> |
|
103 | <Compile Include="PromiseExtensions.cs" /> | |
104 | <Compile Include="SyncContextPromise.cs" /> |
|
104 | <Compile Include="SyncContextPromise.cs" /> | |
105 | <Compile Include="Diagnostics\OperationContext.cs" /> |
|
105 | <Compile Include="Diagnostics\OperationContext.cs" /> | |
106 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
106 | <Compile Include="Diagnostics\TraceContext.cs" /> | |
107 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
107 | <Compile Include="Diagnostics\LogEventArgs.cs" /> | |
108 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
108 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> | |
109 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
109 | <Compile Include="Diagnostics\Extensions.cs" /> | |
110 | <Compile Include="PromiseEventType.cs" /> |
|
110 | <Compile Include="PromiseEventType.cs" /> | |
111 | <Compile Include="Parallels\AsyncQueue.cs" /> |
|
111 | <Compile Include="Parallels\AsyncQueue.cs" /> | |
112 | <Compile Include="PromiseT.cs" /> |
|
112 | <Compile Include="PromiseT.cs" /> | |
113 | <Compile Include="IDeferred.cs" /> |
|
113 | <Compile Include="IDeferred.cs" /> | |
114 | <Compile Include="IDeferredT.cs" /> |
|
114 | <Compile Include="IDeferredT.cs" /> | |
115 | <Compile Include="Promise.cs" /> |
|
115 | <Compile Include="Promise.cs" /> | |
116 | <Compile Include="PromiseTransientException.cs" /> |
|
116 | <Compile Include="PromiseTransientException.cs" /> | |
117 | <Compile Include="Parallels\Signal.cs" /> |
|
117 | <Compile Include="Parallels\Signal.cs" /> | |
118 | <Compile Include="Parallels\SharedLock.cs" /> |
|
118 | <Compile Include="Parallels\SharedLock.cs" /> | |
119 | <Compile Include="Diagnostics\ILogWriter.cs" /> |
|
119 | <Compile Include="Diagnostics\ILogWriter.cs" /> | |
120 | <Compile Include="Diagnostics\ListenerBase.cs" /> |
|
120 | <Compile Include="Diagnostics\ListenerBase.cs" /> | |
121 | <Compile Include="Parallels\BlockingQueue.cs" /> |
|
121 | <Compile Include="Parallels\BlockingQueue.cs" /> | |
122 | <Compile Include="AbstractEvent.cs" /> |
|
122 | <Compile Include="AbstractEvent.cs" /> | |
123 | <Compile Include="AbstractPromise.cs" /> |
|
123 | <Compile Include="AbstractPromise.cs" /> | |
124 | <Compile Include="AbstractPromiseT.cs" /> |
|
124 | <Compile Include="AbstractPromiseT.cs" /> | |
125 | <Compile Include="FuncTask.cs" /> |
|
125 | <Compile Include="FuncTask.cs" /> | |
126 | <Compile Include="FuncTaskBase.cs" /> |
|
126 | <Compile Include="FuncTaskBase.cs" /> | |
127 | <Compile Include="FuncTaskT.cs" /> |
|
127 | <Compile Include="FuncTaskT.cs" /> | |
128 | <Compile Include="ActionChainTaskBase.cs" /> |
|
128 | <Compile Include="ActionChainTaskBase.cs" /> | |
129 | <Compile Include="ActionChainTask.cs" /> |
|
129 | <Compile Include="ActionChainTask.cs" /> | |
130 | <Compile Include="ActionChainTaskT.cs" /> |
|
130 | <Compile Include="ActionChainTaskT.cs" /> | |
131 | <Compile Include="FuncChainTaskBase.cs" /> |
|
131 | <Compile Include="FuncChainTaskBase.cs" /> | |
132 | <Compile Include="FuncChainTask.cs" /> |
|
132 | <Compile Include="FuncChainTask.cs" /> | |
133 | <Compile Include="FuncChainTaskT.cs" /> |
|
133 | <Compile Include="FuncChainTaskT.cs" /> | |
134 | <Compile Include="ActionTaskBase.cs" /> |
|
134 | <Compile Include="ActionTaskBase.cs" /> | |
135 | <Compile Include="ActionTask.cs" /> |
|
135 | <Compile Include="ActionTask.cs" /> | |
136 | <Compile Include="ActionTaskT.cs" /> |
|
136 | <Compile Include="ActionTaskT.cs" /> | |
137 | <Compile Include="ICancellationToken.cs" /> |
|
137 | <Compile Include="ICancellationToken.cs" /> | |
138 | <Compile Include="SuccessPromise.cs" /> |
|
138 | <Compile Include="SuccessPromise.cs" /> | |
139 | <Compile Include="SuccessPromiseT.cs" /> |
|
139 | <Compile Include="SuccessPromiseT.cs" /> | |
140 | <Compile Include="PromiseAwaiterT.cs" /> |
|
140 | <Compile Include="PromiseAwaiterT.cs" /> | |
141 | <Compile Include="PromiseAwaiter.cs" /> |
|
141 | <Compile Include="PromiseAwaiter.cs" /> | |
142 | <Compile Include="Components\ComponentContainer.cs" /> |
|
142 | <Compile Include="Components\ComponentContainer.cs" /> | |
143 | <Compile Include="Components\Disposable.cs" /> |
|
143 | <Compile Include="Components\Disposable.cs" /> | |
144 | <Compile Include="Components\DisposablePool.cs" /> |
|
144 | <Compile Include="Components\DisposablePool.cs" /> | |
145 | <Compile Include="Components\ObjectPool.cs" /> |
|
145 | <Compile Include="Components\ObjectPool.cs" /> | |
146 | <Compile Include="Components\ServiceLocator.cs" /> |
|
146 | <Compile Include="Components\ServiceLocator.cs" /> | |
147 | <Compile Include="Components\IInitializable.cs" /> |
|
147 | <Compile Include="Components\IInitializable.cs" /> | |
148 | <Compile Include="TaskController.cs" /> |
|
148 | <Compile Include="TaskController.cs" /> | |
149 | <Compile Include="Components\App.cs" /> |
|
149 | <Compile Include="Components\App.cs" /> | |
150 | <Compile Include="Components\IRunnable.cs" /> |
|
150 | <Compile Include="Components\IRunnable.cs" /> | |
151 | <Compile Include="Components\ExecutionState.cs" /> |
|
151 | <Compile Include="Components\ExecutionState.cs" /> | |
152 | <Compile Include="Components\RunnableComponent.cs" /> |
|
152 | <Compile Include="Components\RunnableComponent.cs" /> | |
153 | <Compile Include="Components\IFactory.cs" /> |
|
153 | <Compile Include="Components\IFactory.cs" /> | |
154 | <Compile Include="Automaton\EnumAlphabet.cs" /> |
|
154 | <Compile Include="Automaton\EnumAlphabet.cs" /> | |
155 | <Compile Include="Automaton\IAlphabet.cs" /> |
|
155 | <Compile Include="Automaton\IAlphabet.cs" /> | |
156 | <Compile Include="Automaton\ParserException.cs" /> |
|
156 | <Compile Include="Automaton\ParserException.cs" /> | |
157 | <Compile Include="Automaton\IndexedAlphabetBase.cs" /> |
|
157 | <Compile Include="Automaton\IndexedAlphabetBase.cs" /> | |
158 | <Compile Include="Automaton\IAlphabetBuilder.cs" /> |
|
158 | <Compile Include="Automaton\IAlphabetBuilder.cs" /> | |
159 | <Compile Include="Automaton\RegularExpressions\AltToken.cs" /> |
|
159 | <Compile Include="Automaton\RegularExpressions\AltToken.cs" /> | |
160 | <Compile Include="Automaton\RegularExpressions\BinaryToken.cs" /> |
|
160 | <Compile Include="Automaton\RegularExpressions\BinaryToken.cs" /> | |
161 | <Compile Include="Automaton\RegularExpressions\CatToken.cs" /> |
|
161 | <Compile Include="Automaton\RegularExpressions\CatToken.cs" /> | |
162 | <Compile Include="Automaton\DFAConst.cs" /> |
|
|||
163 | <Compile Include="Automaton\RegularExpressions\StarToken.cs" /> |
|
162 | <Compile Include="Automaton\RegularExpressions\StarToken.cs" /> | |
164 | <Compile Include="Automaton\RegularExpressions\SymbolToken.cs" /> |
|
163 | <Compile Include="Automaton\RegularExpressions\SymbolToken.cs" /> | |
165 | <Compile Include="Automaton\RegularExpressions\EmptyToken.cs" /> |
|
164 | <Compile Include="Automaton\RegularExpressions\EmptyToken.cs" /> | |
166 | <Compile Include="Automaton\RegularExpressions\Token.cs" /> |
|
165 | <Compile Include="Automaton\RegularExpressions\Token.cs" /> | |
167 | <Compile Include="Automaton\RegularExpressions\IVisitor.cs" /> |
|
166 | <Compile Include="Automaton\RegularExpressions\IVisitor.cs" /> | |
168 | <Compile Include="Automaton\AutomatonTransition.cs" /> |
|
167 | <Compile Include="Automaton\AutomatonTransition.cs" /> | |
169 | <Compile Include="Formats\JSON\JSONElementContext.cs" /> |
|
168 | <Compile Include="Formats\JSON\JSONElementContext.cs" /> | |
170 | <Compile Include="Formats\JSON\JSONElementType.cs" /> |
|
169 | <Compile Include="Formats\JSON\JSONElementType.cs" /> | |
171 | <Compile Include="Formats\JSON\JSONGrammar.cs" /> |
|
170 | <Compile Include="Formats\JSON\JSONGrammar.cs" /> | |
172 | <Compile Include="Formats\JSON\JSONParser.cs" /> |
|
171 | <Compile Include="Formats\JSON\JSONParser.cs" /> | |
173 | <Compile Include="Formats\JSON\JSONScanner.cs" /> |
|
172 | <Compile Include="Formats\JSON\JSONScanner.cs" /> | |
174 | <Compile Include="Formats\JSON\JsonTokenType.cs" /> |
|
173 | <Compile Include="Formats\JSON\JsonTokenType.cs" /> | |
175 | <Compile Include="Formats\JSON\JSONWriter.cs" /> |
|
174 | <Compile Include="Formats\JSON\JSONWriter.cs" /> | |
176 | <Compile Include="Formats\JSON\JSONXmlReader.cs" /> |
|
175 | <Compile Include="Formats\JSON\JSONXmlReader.cs" /> | |
177 | <Compile Include="Formats\JSON\JSONXmlReaderOptions.cs" /> |
|
176 | <Compile Include="Formats\JSON\JSONXmlReaderOptions.cs" /> | |
178 | <Compile Include="Formats\JSON\StringTranslator.cs" /> |
|
177 | <Compile Include="Formats\JSON\StringTranslator.cs" /> | |
179 | <Compile Include="Automaton\MapAlphabet.cs" /> |
|
178 | <Compile Include="Automaton\MapAlphabet.cs" /> | |
180 | <Compile Include="Automaton\DummyAlphabet.cs" /> |
|
179 | <Compile Include="Automaton\DummyAlphabet.cs" /> | |
181 | <Compile Include="Formats\CharAlphabet.cs" /> |
|
180 | <Compile Include="Formats\CharAlphabet.cs" /> | |
182 | <Compile Include="Formats\ByteAlphabet.cs" /> |
|
181 | <Compile Include="Formats\ByteAlphabet.cs" /> | |
183 | <Compile Include="Automaton\IDFATable.cs" /> |
|
182 | <Compile Include="Automaton\IDFATable.cs" /> | |
184 | <Compile Include="Automaton\IDFATableBuilder.cs" /> |
|
183 | <Compile Include="Automaton\IDFATableBuilder.cs" /> | |
185 | <Compile Include="Automaton\DFATable.cs" /> |
|
184 | <Compile Include="Automaton\DFATable.cs" /> | |
186 | <Compile Include="Automaton\RegularExpressions\RegularDFA.cs" /> |
|
|||
187 | <Compile Include="Automaton\RegularExpressions\RegularExpressionVisitor.cs" /> |
|
185 | <Compile Include="Automaton\RegularExpressions\RegularExpressionVisitor.cs" /> | |
188 | <Compile Include="Automaton\RegularExpressions\ITaggedDFABuilder.cs" /> |
|
186 | <Compile Include="Automaton\RegularExpressions\ITaggedDFABuilder.cs" /> | |
189 | <Compile Include="Formats\TextScanner.cs" /> |
|
187 | <Compile Include="Formats\TextScanner.cs" /> | |
190 | <Compile Include="Formats\StringScanner.cs" /> |
|
188 | <Compile Include="Formats\StringScanner.cs" /> | |
191 | <Compile Include="Formats\ReaderScanner.cs" /> |
|
189 | <Compile Include="Formats\ReaderScanner.cs" /> | |
192 | <Compile Include="Formats\ScannerContext.cs" /> |
|
190 | <Compile Include="Formats\ScannerContext.cs" /> | |
193 | <Compile Include="Formats\Grammar.cs" /> |
|
191 | <Compile Include="Formats\Grammar.cs" /> | |
194 | <Compile Include="Automaton\RegularExpressions\EndTokenT.cs" /> |
|
192 | <Compile Include="Automaton\RegularExpressions\EndTokenT.cs" /> | |
195 | <Compile Include="Automaton\RegularExpressions\EndToken.cs" /> |
|
193 | <Compile Include="Automaton\RegularExpressions\EndToken.cs" /> | |
196 |
<Compile Include="Automaton\RegularExpressions\ |
|
194 | <Compile Include="Automaton\RegularExpressions\RegularExpressionVisitorT.cs" /> | |
|
195 | <Compile Include="Automaton\AutomatonConst.cs" /> | |||
|
196 | <Compile Include="Automaton\RegularExpressions\RegularDFA.cs" /> | |||
|
197 | <Compile Include="Components\LazyAndWeak.cs" /> | |||
197 | </ItemGroup> |
|
198 | </ItemGroup> | |
198 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
199 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
199 | <ItemGroup /> |
|
200 | <ItemGroup /> | |
200 | <ProjectExtensions> |
|
201 | <ProjectExtensions> | |
201 | <MonoDevelop> |
|
202 | <MonoDevelop> | |
202 | <Properties> |
|
203 | <Properties> | |
203 | <Policies> |
|
204 | <Policies> | |
204 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> |
|
205 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> | |
205 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
206 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> | |
206 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
207 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> | |
207 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
208 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> | |
208 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
209 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> | |
209 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
210 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> | |
210 | <NameConventionPolicy> |
|
211 | <NameConventionPolicy> | |
211 | <Rules> |
|
212 | <Rules> | |
212 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
213 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
213 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
214 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
214 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
215 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
215 | <RequiredPrefixes> |
|
216 | <RequiredPrefixes> | |
216 | <String>I</String> |
|
217 | <String>I</String> | |
217 | </RequiredPrefixes> |
|
218 | </RequiredPrefixes> | |
218 | </NamingRule> |
|
219 | </NamingRule> | |
219 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
220 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
220 | <RequiredSuffixes> |
|
221 | <RequiredSuffixes> | |
221 | <String>Attribute</String> |
|
222 | <String>Attribute</String> | |
222 | </RequiredSuffixes> |
|
223 | </RequiredSuffixes> | |
223 | </NamingRule> |
|
224 | </NamingRule> | |
224 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
225 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
225 | <RequiredSuffixes> |
|
226 | <RequiredSuffixes> | |
226 | <String>EventArgs</String> |
|
227 | <String>EventArgs</String> | |
227 | </RequiredSuffixes> |
|
228 | </RequiredSuffixes> | |
228 | </NamingRule> |
|
229 | </NamingRule> | |
229 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
230 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
230 | <RequiredSuffixes> |
|
231 | <RequiredSuffixes> | |
231 | <String>Exception</String> |
|
232 | <String>Exception</String> | |
232 | </RequiredSuffixes> |
|
233 | </RequiredSuffixes> | |
233 | </NamingRule> |
|
234 | </NamingRule> | |
234 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
235 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
235 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
236 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> | |
236 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
237 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
237 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
238 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> | |
238 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
239 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> | |
239 | <RequiredPrefixes> |
|
240 | <RequiredPrefixes> | |
240 | <String>m_</String> |
|
241 | <String>m_</String> | |
241 | </RequiredPrefixes> |
|
242 | </RequiredPrefixes> | |
242 | </NamingRule> |
|
243 | </NamingRule> | |
243 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
244 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> | |
244 | <RequiredPrefixes> |
|
245 | <RequiredPrefixes> | |
245 | <String>_</String> |
|
246 | <String>_</String> | |
246 | </RequiredPrefixes> |
|
247 | </RequiredPrefixes> | |
247 | </NamingRule> |
|
248 | </NamingRule> | |
248 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
249 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> | |
249 | <RequiredPrefixes> |
|
250 | <RequiredPrefixes> | |
250 | <String>m_</String> |
|
251 | <String>m_</String> | |
251 | </RequiredPrefixes> |
|
252 | </RequiredPrefixes> | |
252 | </NamingRule> |
|
253 | </NamingRule> | |
253 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
254 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
254 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
255 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
255 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
256 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
256 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
257 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
257 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
258 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> | |
258 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
259 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> | |
259 | <RequiredPrefixes> |
|
260 | <RequiredPrefixes> | |
260 | <String>T</String> |
|
261 | <String>T</String> | |
261 | </RequiredPrefixes> |
|
262 | </RequiredPrefixes> | |
262 | </NamingRule> |
|
263 | </NamingRule> | |
263 | </Rules> |
|
264 | </Rules> | |
264 | </NameConventionPolicy> |
|
265 | </NameConventionPolicy> | |
265 | </Policies> |
|
266 | </Policies> | |
266 | </Properties> |
|
267 | </Properties> | |
267 | </MonoDevelop> |
|
268 | </MonoDevelop> | |
268 | </ProjectExtensions> |
|
269 | </ProjectExtensions> | |
269 | <ItemGroup> |
|
270 | <ItemGroup> | |
270 | <Folder Include="Components\" /> |
|
271 | <Folder Include="Components\" /> | |
271 | <Folder Include="Automaton\RegularExpressions\" /> |
|
272 | <Folder Include="Automaton\RegularExpressions\" /> | |
272 | <Folder Include="Formats\" /> |
|
273 | <Folder Include="Formats\" /> | |
273 | <Folder Include="Formats\JSON\" /> |
|
274 | <Folder Include="Formats\JSON\" /> | |
274 | </ItemGroup> |
|
275 | </ItemGroup> | |
275 | </Project> No newline at end of file |
|
276 | </Project> |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now