annotate Implab/Parsing/DFAutomaton.cs @ 95:0141a165d032 v2

minor changes
author cin
date Wed, 29 Oct 2014 18:54:46 +0300
parents 62b440d46313
children 97fbbf816844
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
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 public const int INITIAL_STATE = DFADefinitionBase.INITIAL_STATE;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 public const int UNREACHEBLE_STATE = DFADefinitionBase.UNREACHEBLE_STATE;
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);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 m_context. states = states;
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 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 }