/
dfRichTextTokenizer.cs
264 lines (249 loc) · 7.02 KB
/
dfRichTextTokenizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class dfRichTextTokenizer
{
private int index;
private static dfRichTextTokenizer singleton;
private string source;
private System.Collections.Generic.List<dfMarkupToken> tokens = new System.Collections.Generic.List<dfMarkupToken>();
private char Advance(int amount = 1)
{
this.index += amount;
return this.Peek(0);
}
private bool AtTagPosition()
{
if (this.Peek(0) != '<')
{
return false;
}
char c = this.Peek(1);
if (c == '/')
{
return char.IsLetter(this.Peek(2));
}
return char.IsLetter(c);
}
private dfMarkupToken parseAttributeValue()
{
int index = this.index;
int endIndex = this.index;
while (this.index < this.source.Length)
{
char c = this.Advance(1);
if ((c == '>') || char.IsWhiteSpace(c))
{
break;
}
endIndex++;
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Text, index, endIndex);
}
private dfMarkupToken parseEndTag()
{
this.Advance(2);
int index = this.index;
int endIndex = this.index;
while ((this.index < this.source.Length) && char.IsLetterOrDigit(this.Advance(1)))
{
endIndex++;
}
if (this.Peek(0) == '>')
{
this.Advance(1);
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.EndTag, index, endIndex);
}
private dfMarkupToken parseNonWhitespace()
{
int index = this.index;
int endIndex = this.index;
while (this.index < this.source.Length)
{
if (char.IsWhiteSpace(this.Advance(1)) || this.AtTagPosition())
{
break;
}
endIndex++;
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Text, index, endIndex);
}
private dfMarkupToken parseQuotedString()
{
char ch = this.Peek(0);
if ((ch != '"') && (ch != '\''))
{
return null;
}
this.Advance(1);
int index = this.index;
int endIndex = this.index;
while ((this.index < this.source.Length) && (this.Advance(1) != ch))
{
endIndex++;
}
if (this.Peek(0) == ch)
{
this.Advance(1);
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Text, index, endIndex);
}
private dfMarkupToken parseTag()
{
if (this.Peek(0) != '<')
{
return null;
}
if (this.Peek(1) == '/')
{
return this.parseEndTag();
}
this.Advance(1);
if (!char.IsLetterOrDigit(this.Peek(0)))
{
return null;
}
int index = this.index;
int endIndex = this.index;
while ((this.index < this.source.Length) && char.IsLetterOrDigit(this.Advance(1)))
{
endIndex++;
}
dfMarkupToken token = dfMarkupToken.Obtain(this.source, dfMarkupTokenType.StartTag, index, endIndex);
while ((this.index < this.source.Length) && (this.Peek(0) != '>'))
{
if (char.IsWhiteSpace(this.Peek(0)))
{
this.parseWhitespace();
continue;
}
dfMarkupToken key = this.parseWord();
if (key == null)
{
this.Advance(1);
continue;
}
if (this.Peek(0) != '=')
{
token.AddAttribute(key, key);
continue;
}
char ch = this.Advance(1);
dfMarkupToken token3 = null;
switch (ch)
{
case '"':
case '\'':
token3 = this.parseQuotedString();
break;
default:
token3 = this.parseAttributeValue();
break;
}
if (token3 == null)
{
}
token.AddAttribute(key, key);
}
if (this.Peek(0) == '>')
{
this.Advance(1);
}
return token;
}
private dfMarkupToken parseWhitespace()
{
int index = this.index;
int endIndex = this.index;
if (this.Peek(0) == '\n')
{
this.Advance(1);
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Newline, index, index);
}
while (this.index < this.source.Length)
{
char c = this.Advance(1);
if (((c == '\n') || (c == '\r')) || !char.IsWhiteSpace(c))
{
break;
}
endIndex++;
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Whitespace, index, endIndex);
}
private dfMarkupToken parseWord()
{
if (!char.IsLetter(this.Peek(0)))
{
return null;
}
int index = this.index;
int endIndex = this.index;
while ((this.index < this.source.Length) && char.IsLetter(this.Advance(1)))
{
endIndex++;
}
return dfMarkupToken.Obtain(this.source, dfMarkupTokenType.Text, index, endIndex);
}
private char Peek(int offset = 0)
{
if ((this.index + offset) > (this.source.Length - 1))
{
return '\0';
}
return this.source[this.index + offset];
}
private System.Collections.Generic.List<dfMarkupToken> tokenize(string source)
{
dfMarkupToken.Reset();
dfMarkupTokenAttribute.Reset();
this.tokens.Clear();
this.source = source;
this.index = 0;
while (this.index < source.Length)
{
char c = this.Peek(0);
if (this.AtTagPosition())
{
dfMarkupToken item = this.parseTag();
if (item != null)
{
this.tokens.Add(item);
}
}
else
{
dfMarkupToken token2 = null;
if (char.IsWhiteSpace(c))
{
if (c != '\r')
{
token2 = this.parseWhitespace();
}
}
else
{
token2 = this.parseNonWhitespace();
}
if (token2 == null)
{
this.Advance(1);
}
else
{
this.tokens.Add(token2);
}
}
}
return this.tokens;
}
public static System.Collections.Generic.List<dfMarkupToken> Tokenize(string source)
{
if (singleton == null)
{
singleton = new dfRichTextTokenizer();
}
return singleton.tokenize(source);
}
}