annotate Implab/Formats/TextScanner.cs @ 173:ecfece82ca11 ref20160224

Working on text scanner
author cin
date Tue, 15 Mar 2016 02:11:06 +0300
parents
children 983df35b3ca1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
1 using System;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
2 using Implab.Components;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
3
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
4 namespace Implab.Formats {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
5 public abstract class TextScanner<TTag> : Disposable {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
6
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
7 char[] m_buffer;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
8 int m_offset;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
9 int m_length;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
10 int m_tokenOffset;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
11 int m_tokenLength;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
12 TTag[] m_tags;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
13
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
14 BufferScanner<TTag> m_scanner;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
15
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
16 protected bool ReadTokenInternal() {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
17 if (EOF)
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
18 return false;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
19
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
20 // create a new scanner from template (scanners are structs)
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
21 var inst = m_scanner;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
22
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
23 // initialize the scanner
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
24 inst.Init(m_buffer, m_offset, m_length);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
25
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
26 // do work
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
27 while (inst.Scan())
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
28 Feed(ref inst);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
29
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
30 // save result;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
31 m_buffer = inst.Buffer;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
32 m_length = inst.Length;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
33 m_offset = inst.Position;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
34 m_tokenOffset = inst.TokenOffset;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
35 m_tokenLength = inst.TokenLength;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
36
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
37 m_tags = inst.GetTokenTags();
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
38 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
39
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
40 protected string GetToken() {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
41 return new String(m_buffer, m_tokenOffset, m_tokenLength);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
42 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
43
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
44 protected TTag[] Tags {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
45 get {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
46 return m_tags;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
47 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
48 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
49
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
50 /// <summary>
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
51 /// Feed the specified scanner.
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
52 /// </summary>
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
53 /// <param name="scanner">Scanner.</param>
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
54 /// <example>
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
55 /// protected override void Feed(ref BufferScanner<TTag> scanner) {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
56 /// var size = scanner.Extend();
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
57 /// var actual = m_reader.Read(scanner.Buffer, scanner.HiMark, size);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
58 /// if (actual == 0) {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
59 /// m_eof = true;
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
60 /// scanner.Eof();
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
61 /// } else {
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
62 /// scanner.RaiseHiMark(actual);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
63 /// }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
64 /// }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
65 /// </example>
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
66 protected abstract void Feed(ref BufferScanner<TTag> scanner);
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
67
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
68 public abstract bool EOF { get; }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
69
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
70 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
71 }
ecfece82ca11 Working on text scanner
cin
parents:
diff changeset
72