forked from jamietre/CsQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIStringScanner.cs
More file actions
456 lines (366 loc) · 13.8 KB
/
Copy pathIStringScanner.cs
File metadata and controls
456 lines (366 loc) · 13.8 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Linq;
using System.Text;
using System.Diagnostics;
// TODO: This code should be fully commented at some point. It's not part of the main public API though.
#pragma warning disable 1591
#pragma warning disable 1570
namespace CsQuery.StringScanner
{
/// <summary>
/// Interface defining a StringScanner - a lexical scanner
/// </summary>
public interface IStringScanner
{
/// <summary>
/// Gets or sets the text that the scanner acts upon
/// </summary>
/// <seealso cref="Chars"/>
string Text {get;set;}
/// <summary>
/// Gets or sets the text that this scanner acts upon.
/// </summary>
///
/// <seealso cref="Text"/>
char[] Chars {get;set;}
/// <summary>
/// Gets or sets a value indicating whether the scanner should ignore whitespace. When true, it
/// is skipped automatically.
/// </summary>
bool IgnoreWhitespace { get; set; }
/// <summary>
/// Gets the length of the text bound to this scanner.
/// </summary>
int Length {get;}
/// <summary>
/// Gets or sets the current zero-based position of the scanner.
/// </summary>
int Index {get;}
/// <summary>
/// Gets the zero-based index of the scanner before the last operation.
/// </summary>
int LastIndex { get; }
/// <summary>
/// Gets the current character.
/// </summary>
char Current {get;}
/// <summary>
/// Returns the character after the current character
/// </summary>
///
/// <returns>
/// A character
/// </returns>
char Peek();
/// <summary>
/// Gets the next character, or an empty string if the pointer is at the end of the string.
/// </summary>
string CurrentOrEmpty {get;}
/// <summary>
/// Gets the current match string (usually, the text between the prior pointer position and the
/// current pointer position, possibly excluding whitespace. This depends on the last operation).
/// </summary>
string Match {get;}
/// <summary>
/// Gets the match prior to the curren one.
/// </summary>
string LastMatch {get;}
/// <summary>
/// Gets a value indicating whether the pointer is after the end of the string.
/// </summary>
bool Finished {get;}
/// <summary>
/// Gets a value indicating whether at the last character of the string.
/// </summary>
bool AtEnd {get;}
/// <summary>
/// Gets a value indicating whether the last operation succeeded. Since failure throws an error,
/// this is generally useful only if errors are trapped.
/// </summary>
bool Success {get;}
/// <summary>
/// Gets the error message when the prior operation failed.
/// </summary>
string LastError {get;}
/// <summary>
/// Causes the next action to permit quoting -- if the first character is a quote character, stop
/// characters between there and the next matching quote character will be ignored.
/// </summary>
///
/// <returns>
/// true if the next value is quoted, false if not
/// </returns>
bool AllowQuoting();
/// <summary>
/// CharacterInfo object bound to the character at the current index.
/// </summary>
ICharacterInfo Info {get; }
/// <summary>
/// If the pointer is current on whitespace, advance to the next non-whitespace character. If the
/// pointer is not on whitespace, do nothing.
/// </summary>
void SkipWhitespace();
/// <summary>
/// Advance the pointer to the next character that is not whitespace. This differes from
/// SkipShitespace in that this always advances the pointer.
/// </summary>
void NextNonWhitespace();
/// <summary>
/// Advance the pointer by one character.
/// </summary>
///
/// <returns>
/// true if the pointer can be advanced again, false if it is after the last position.
/// </returns>
bool Next();
/// <summary>
/// Move the pointer back one position.
/// </summary>
///
/// <returns>
/// true if the pointer can be moved back again, false if it is at the origin.
/// </returns>
bool Previous();
/// <summary>
/// Moves the pointer by a specific number of characters, forward or reverse.
/// </summary>
///
/// <param name="count">
/// A positive or negative integer.
/// </param>
///
/// <returns>
/// true if the pointer is not at the origin or after the end of the string, false otherwise.
/// </returns>
bool Move(int count);
/// <summary>
/// Undo the last operation
/// </summary>
void Undo();
/// <summary>
/// Moves the pointer past the last character postion.
/// </summary>
void End();
/// <summary>
/// Throw an error if the current scanner is not finished.
/// </summary>
///
/// <param name="errorMessage">
/// (optional) message describing the error.
/// </param>
void AssertFinished(string errorMessage=null);
/// <summary>
/// Throw an error if the current scanner is finished.
/// </summary>
///
/// <param name="errorMessage">
/// (optional) message describing the error.
/// </param>
void AssertNotFinished(string errorMessage=null);
/// <summary>
/// Resets the pointer to the origin and clear any state information about the scanner. This sets
/// the internal state as if it had just been created.
/// </summary>
void Reset();
/// <summary>
/// Test that the text starting at the current position matches the passed text.
/// </summary>
///
/// <param name="text">
/// The text to match
/// </param>
///
/// <returns>
/// true if it succeeds, false if it fails.
/// </returns>
bool Matches(string text);
/// <summary>
/// Test that the text starting at the current position is any of the strings passed.
/// </summary>
///
/// <param name="text">
/// A sequence of strings to match
/// </param>
///
/// <returns>
/// true if one of, false if not.
/// </returns>
bool MatchesAny(IEnumerable<string> text);
/// <summary>
/// Seeks until a specific character is found. The Match string becomes everything from the
/// current position, through the position before the matched character. If the scanner is
/// already at the end, an exception is thrown.
/// </summary>
///
/// <param name="character">
/// The character to seek.
/// </param>
/// <param name="orEnd">
/// When true, the end of the string is a valid match. When false, the end of the string will
/// cause an exception.
/// </param>
///
/// <returns>
/// The current string scanner.
/// </returns>
IStringScanner Seek(char character, bool orEnd);
/// <summary>
/// Creates a new scanner from the current match.
/// </summary>
///
/// <returns>
/// A new IStringScanner
/// </returns>
IStringScanner ToNewScanner();
/// <summary>
/// Creates a new scanner from the current match.
/// </summary>
///
/// <param name="template">
/// The template.
/// </param>
///
/// <returns>
/// A new IStringScanner.
/// </returns>
IStringScanner ToNewScanner(string template);
/// <summary>
/// Assert that the text matches the string starting at the current position. The pointer is
/// advanced to the first position beyond the matching text. If it does not, an ArgumentException
/// is thrown.
/// </summary>
///
/// <param name="text">
/// The text to match.
/// </param>
///
/// <returns>
/// The current StringScanner.
/// </returns>
IStringScanner Expect(string text);
/// <summary>
/// Assert that the text matches the pattern defined by an IExpectPattern object. The pointer is
/// advanced until the pattern stops matching. If it does not, an ArgumentException is thrown.
/// </summary>
///
/// <param name="pattern">
/// A pattern specifying the.
/// </param>
///
/// <returns>
/// The current StringScanner.
/// </returns>
IStringScanner Expect(IExpectPattern pattern);
/// <summary>
/// Assert that at least one character starting at the current position validates using a
/// function delegate. The pointer advances until the delegate returns false. If it does not, an
/// ArgumentException is thrown.
/// </summary>
///
/// <param name="validate">
/// The validate.
/// </param>
///
/// <returns>
/// The current StringScanner.
/// </returns>
IStringScanner Expect(Func<int, char, bool> validate);
/// <summary>
/// Assert that the current character matches the character passed. The pointer is advanced by
/// one position. If it does not, an ArgumentException is thrown.
/// </summary>
///
/// <param name="character">
/// The character to seek.
/// </param>
///
/// <returns>
/// .
/// </returns>
IStringScanner ExpectChar(char character);
/// <summary>
/// Assert that the current character matches any of the characters passed. The pointer is
/// advanced by one position. If it does not, an ArgumentException is thrown.
/// </summary>
///
/// <param name="characters">
/// The characters to match
/// </param>
///
/// <returns>
/// The current string scanner.
/// </returns>
IStringScanner ExpectChar(IEnumerable<char> characters);
/// <summary>
/// Assert that there is a pattern that matches a number starting at the current position. The
/// pointer is advanced to the position after the end of the number. If it does not, an
/// ArgumentException is thrown.
/// </summary>
///
/// <param name="requireWhitespaceTerminator">
/// (optional) Indicates if whitespace is the only valid terminator. If true, an
/// ArgumentException will be thrown if the first character that terminates the number is not
/// whitespace. If false, any character that is invalid as part of a number will stop matching
/// with no error.
/// </param>
///
/// <returns>
/// The current string scanner.
/// </returns>
IStringScanner ExpectNumber(bool requireWhitespaceTerminator = false);
/// <summary>
/// Assert that the current pattern is alphabetic until the next whitespace.
/// </summary>
///
/// <returns>
/// The current string scanner.
/// </returns>
IStringScanner ExpectAlpha();
/// <summary>
/// Asser that the current pattern is bounded by the start and end characters passed
/// </summary>
///
/// <param name="start">
/// The start bound character
/// </param>
/// <param name="end">
/// The end bound character
/// </param>
/// <param name="allowQuoting">
/// (optional) True if the contents of the bounds can be quoted
/// </param>
///
/// <returns>
/// The current string scanner
/// </returns>
IStringScanner ExpectBoundedBy(string start, string end, bool allowQuoting=false);
IStringScanner ExpectBoundedBy(char bound, bool allowQuoting = false);
bool TryGet(IEnumerable<string> stringList, out string result);
bool TryGet(IExpectPattern pattern, out string result);
bool TryGet(Func<int, char, bool> validate, out string result);
bool TryGetChar(char character, out string result);
bool TryGetChar(string characters, out string result);
bool TryGetChar(IEnumerable<char> characters, out string result);
bool TryGetNumber(out string result);
bool TryGetNumber<T>(out T result) where T : IConvertible;
bool TryGetNumber(out int result);
bool TryGetAlpha(out string result);
bool TryGetBoundedBy(string start, string end, bool allowQuoting, out string result);
string Get(params string[] values);
string Get(IEnumerable<string> stringList);
string Get(IExpectPattern pattern);
string Get(Func<int, char, bool> validate);
string GetNumber();
string GetAlpha();
string GetBoundedBy(string start, string end, bool allowQuoting=false);
string GetBoundedBy(char bound, bool allowQuoting=false);
char GetChar(char character);
char GetChar(string characters);
char GetChar(IEnumerable<char> characters);
void Expect(params string[] values);
void Expect(IEnumerable<string> stringList);
}
}