-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParseUtils.cs
More file actions
103 lines (94 loc) · 2.02 KB
/
ParseUtils.cs
File metadata and controls
103 lines (94 loc) · 2.02 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
namespace MagesScriptTool;
class ParseUtils {
public static bool TrySkip(TextStream reader, char c) {
if (!reader.Has(0) || reader.Peek(0) != c) {
return false;
}
reader.Skip(1);
return true;
}
public static bool TrySkip(TextStream reader, string s) {
var runes = s.EnumerateRunes();
int i = 0;
foreach (var rune in runes) {
if (!reader.Has(i) || reader.PeekRune(i) != rune) {
return false;
}
i++;
}
reader.Skip(i);
return true;
}
public static bool SkipSpaceComments(TextStream reader) {
bool consumed = false;
while (true) {
consumed |= SkipSpace(reader);
if (!SkipComment(reader)) {
break;
}
consumed = true;
}
return consumed;
}
public static bool SkipHSpaceComments(TextStream reader) {
bool consumed = false;
while (true) {
consumed |= SkipHSpace(reader);
if (!SkipComment(reader)) {
break;
}
consumed = true;
}
return consumed;
}
public static bool SkipComment(TextStream reader) {
TextStream.Position startPos = reader.Tell();
if (TrySkip(reader, "/*")) {
while (true) {
if (!reader.Has(0)) {
reader.Seek(startPos);
throw new ParsingException("Unterminated multiline comment.");
}
if (TrySkip(reader, "*/")) {
break;
}
reader.Skip(1);
}
} else if (TrySkip(reader, "//")) {
while (reader.Has(0) && reader.Peek(0) != '\n') {
reader.Skip(1);
}
} else {
return false;
}
return true;
}
public static bool SkipSpace(TextStream reader) {
bool consumed = false;
while (true) {
if (!IsSpace(reader.Peek(0))) {
break;
}
reader.Skip(1);
consumed = true;
}
return consumed;
}
public static bool SkipHSpace(TextStream reader) {
bool consumed = false;
while (true) {
if (!IsHSpace(reader.Peek(0))) {
break;
}
reader.Skip(1);
consumed = true;
}
return consumed;
}
public static bool IsHSpace(char ch) {
return ch is '\t' or ' ';
}
public static bool IsSpace(char ch) {
return IsHSpace(ch) || ch == '\n';
}
}