annotate Implab/Formats/InputScanner.cs @ 243:b1e0ffdf3451 v3

working on promises
author cin
date Wed, 24 Jan 2018 19:24:10 +0300
parents 302ca905c19e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
1 using Implab.Automaton;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
2 using System;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
3 using System.Collections.Generic;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
4 using System.Linq;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
5 using System.Runtime.CompilerServices;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
6 using System.Text;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
7 using System.Threading.Tasks;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
8
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
9 namespace Implab.Formats {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
10 public class InputScanner<TTag> {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
11 readonly TTag[] m_tags;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
12 readonly int m_initialState;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
13 readonly int[,] m_dfa;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
14 readonly CharMap m_alphabet;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
15 readonly bool[] m_final;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
16
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
17 int m_position;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
18 int m_state;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
19
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
20 public InputScanner(int[,] dfaTable, bool[] finalStates, TTag[] tags, int initialState, CharMap alphabet) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
21 Safe.ArgumentNotNull(dfaTable, nameof(dfaTable));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
22 Safe.ArgumentNotNull(finalStates, nameof(finalStates));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
23 Safe.ArgumentNotNull(tags, nameof(tags));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
24 Safe.ArgumentNotNull(alphabet, nameof(alphabet));
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
25
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
26 m_dfa = dfaTable;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
27 m_final = finalStates;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
28 m_tags = tags;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
29 m_initialState = initialState;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
30 m_alphabet = alphabet;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
31 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
32
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
33 public TTag Tag {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
34 [MethodImpl(MethodImplOptions.AggressiveInlining)]
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
35 get {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
36 return m_tags[m_state];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
37 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
38 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
39
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
40 public int Position {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
41 [MethodImpl(MethodImplOptions.AggressiveInlining)]
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
42 get {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
43 return m_position;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
44 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
45 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
46
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
47 public bool IsFinal {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
48 [MethodImpl(MethodImplOptions.AggressiveInlining)]
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
49 get {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
50 return m_final[m_state];
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
51 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
52 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
53
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
54 [MethodImpl(MethodImplOptions.AggressiveInlining)]
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
55 public void ResetState() {
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
56 m_state = m_initialState;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
57 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
58
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
59 public InputScanner<TTag> Clone() {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
60 var clone = new InputScanner<TTag>(m_dfa, m_final, m_tags, m_initialState, m_alphabet);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
61 clone.m_state = m_state;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
62 clone.m_position = m_position;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
63 return clone;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
64 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
65
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
66 //[MethodImpl(MethodImplOptions.AggressiveInlining)]
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
67 public bool Scan(char[] data, int offset, int max) {
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
68 var next = m_state;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
69
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
70 while(offset < max) {
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
71 next = m_dfa[next, m_alphabet.Translate(data[offset])];
236
302ca905c19e JsonReader optimizations
cin
parents: 229
diff changeset
72 if (next == AutomatonConst.UnreachableState) {
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
73 // scanner stops on the next position after last recognized symbol
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
74 m_position = offset;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
75 return false;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
76 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
77 m_state = next;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
78 offset++;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
79 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
80 m_position = offset;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
81 return true;
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
82 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
83 }
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents:
diff changeset
84 }