annotate Implab/Parsing/DFAutomaton.cs @ 159:5558e43c79bb v2

minor fix
author cin
date Thu, 18 Feb 2016 16:06:14 +0300
parents 130781364799
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Diagnostics;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 namespace Implab.Parsing {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 public abstract class DFAutomaton<T> {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 protected struct ContextFrame {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 public DFAStateDescriptior[] states;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 public int current;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 public T info;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16
158
130781364799 refactoring, code cleanup
cin
parents: 156
diff changeset
17 public const int INITIAL_STATE = DFADefinition.INITIAL_STATE;
130781364799 refactoring, code cleanup
cin
parents: 156
diff changeset
18 public const int UNREACHEBLE_STATE = DFADefinition.UNREACHEBLE_STATE;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 protected ContextFrame m_context;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 Stack<ContextFrame> m_contextStack = new Stack<ContextFrame>();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22
62
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 55
diff changeset
23 protected int Level {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 get { return m_contextStack.Count; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 protected DFAutomaton(DFAStateDescriptior[] states, int startState, T info) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28 Safe.ArgumentNotNull(states, "states");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 Safe.ArgumentInRange(startState, 0, states.Length - 1, "startState");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 m_context.states = states;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 m_context.current = startState;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 m_context.info = info;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 protected void Switch(DFAStateDescriptior[] states, int current, T info) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 Debug.Assert(states != null);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 Debug.Assert(current >= 0 && current < states.Length);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 m_contextStack.Push(m_context);
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
40 m_context.states = states;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 m_context.current = current;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42 m_context.info = info;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 protected void Restore() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 Debug.Assert(m_contextStack.Count > 0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 m_context = m_contextStack.Pop();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
49 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 protected void Move(int input) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
53 m_context.current = m_context.states[m_context.current].transitions[input];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 }
156
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
55
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
56 protected bool CanMove(int input) {
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
57 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
58 return m_context.states[m_context.current].transitions[input] != UNREACHEBLE_STATE;
97fbbf816844 Promises: SignalXXX methods merged into SignalHandler method.
cin
parents: 62
diff changeset
59 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 }