-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
226 lines (217 loc) · 6.77 KB
/
Copy pathparser.cpp
File metadata and controls
226 lines (217 loc) · 6.77 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
#include "parser.h"
// Skips whitespace, single-line comments and multi-line comments.
void Parser::skipWhitespace() {
unsigned long skipped = 0;
bool prevWasCR = false;
while (skipped < str.size()) {
if (skipped+1 < str.size() && str[skipped] == '/' && str[skipped+1] == '/') {
col += 2;
skipped += 2;
while (skipped < str.size() && str[skipped] != '\r' && str[skipped] != '\n') {
col++;
skipped++;
}
if (skipped == str.size()) {
break;
}
} else if (skipped+1 < str.size() && str[skipped] == '/' && str[skipped+1] == '*') {
col += 2;
skipped += 2;
unsigned int multilineCommentNestingLevel = 1;
while (skipped < str.size()) {
if (skipped+1 < str.size() && str[skipped] == '/' && str[skipped+1] == '*') {
col += 2;
skipped += 2;
multilineCommentNestingLevel++;
}
if (skipped+1 < str.size() && str[skipped] == '*' && str[skipped+1] == '/') {
col += 2;
skipped += 2;
multilineCommentNestingLevel--;
if (multilineCommentNestingLevel == 0) break;
}
if (str[skipped] == '\r') {
prevWasCR = true;
line++;
col = 1;
} else if (str[skipped] == '\n') {
if (!prevWasCR) {
line++;
col = 1;
}
prevWasCR = false;
} else {
col++;
prevWasCR = false;
}
skipped++;
}
if (skipped == str.size()) {
break;
}
} else if (isWhitespaceChar(str[skipped])) {
if (str[skipped] == '\r') {
prevWasCR = true;
line++;
col = 1;
} else if (str[skipped] == '\n') {
if (!prevWasCR) {
line++;
col = 1;
}
prevWasCR = false;
} else {
col++;
prevWasCR = false;
}
skipped++;
} else {
break;
}
}
str = str.substr(skipped);
}
long Parser::readInteger(string_view errorMessage) {
bool negative = false;
long ret = 0;
if (tryReadChar('-')) negative = true;
if (str.size() == 0) throw SyntaxError(errorMessage, currentFilePos());
int digits = 0;
while (str[0] >= '0' && str[0] <= '9') {
if (digits >= 18) throw SyntaxError("Integer literal is too large"sv, currentFilePos()); // TODO
ret *= 10;
ret += str[0] - '0';
digits++;
col++;
str = str.substr(1);
}
if (digits == 0) throw SyntaxError(errorMessage, currentFilePos());
return negative ? -ret : ret;
}
[[nodiscard]] optional<string_view> Parser::tryReadIdentifier() {
if (str.size() == 0) return std::nullopt;
if (!isIdentifierChar(str[0])) return std::nullopt;
unsigned long length = 0;
do {
length++;
if (length >= str.size()) break;
} while (isIdentifierChar(str[length]));
auto ret = str.substr(0, length);
if (isKeyword(ret)) return std::nullopt;
str = str.substr(length);
col += length;
return ret;
}
void Parser::skipParenEnclosedStuff(bool considerBracketsAndSquareBrackets) {
auto parenOpenPos = currentFilePos();
if (considerBracketsAndSquareBrackets) {
vector<char> stack;
if (tryReadChar('(')) stack.push_back(')');
else if (tryReadChar('{')) stack.push_back('}');
else if (tryReadChar('[')) stack.push_back(']');
else throw SyntaxError("Expected ( or { or ["sv, currentFilePos());
while (true) {
skipWhitespace();
if (str.size() == 0) throw SyntaxError("Expected ) or } or ] to match ( or { or ["sv, currentFilePos(), parenOpenPos);
char c = readNonWhitespaceChar();
if (c == '(') stack.push_back(')');
else if (c == '{') stack.push_back('}');
else if (c == '[') stack.push_back(']');
else if (c == ')' || c == ']' || c == '}') {
if (stack.back() == c) {
stack.pop_back();
if (stack.size() == 0) return;
}
else throw SyntaxError("Closing ] or ) or } did not match"sv, currentFilePos(), parenOpenPos);
}
}
} else {
readChar('(', "Expected ("sv);
unsigned int depth = 1;
while (true) {
skipWhitespace();
if (str.size() == 0) throw SyntaxError("Expected ) to match ("sv, currentFilePos(), parenOpenPos);
char c = readNonWhitespaceChar();
if (c == '(') depth++;
else if (c == ')') {
depth--;
if (depth == 0) return;
}
}
}
}
void Parser::skipToAndIncluding(char to, bool considerBracketsAndSquareBrackets) {
while (true) {
skipWhitespace();
if (str.size() == 0 || str[0] == ')') throw SyntaxError("Expected char "s + to, currentFilePos());
if (tryReadChar(to)) {
return;
} else if (str[0] == '(' || (considerBracketsAndSquareBrackets && (str[0] == '[' || str[0] == '{'))) {
skipParenEnclosedStuff(considerBracketsAndSquareBrackets);
} else {
auto _ = readNonWhitespaceChar();
}
}
}
[[nodiscard]] string_view Parser::readUntilCharOrEnd(char c, bool considerBracketsAndSquareBrackets) {
auto startPos = currentFilePos();
if (considerBracketsAndSquareBrackets) {
while (true) {
skipWhitespace();
if (str.size() == 0 || str[0] == ')' || str[0] == ']' || str[0] == '}' || str[0] == c) {
return getStringViewFromTo(startPos, currentFilePos());
} else if (str[0] == '(' || str[0] == '[' || str[0] == '{') {
skipParenEnclosedStuff(true);
} else {
auto _ = readNonWhitespaceChar();
}
}
} else {
while (true) {
skipWhitespace();
if (str.size() == 0 || str[0] == ')' || str[0] == c) {
return getStringViewFromTo(startPos, currentFilePos());
} else if (str[0] == '(') {
skipParenEnclosedStuff(false);
} else {
auto _ = readNonWhitespaceChar();
}
}
}
}
[[nodiscard]] vector<std::variant<Id, char, Space, pair<Id, Id>>> Parser::readSyntaxPieces(Namespace& ns) {
vector<std::variant<Id, char, Space, pair<Id, Id>>> ret;
skipWhitespace();
bool skippedWhitespace = false;
while (!tryPeekChar(')')) {
auto startPos = currentFilePos();
if (auto paramName = tryReadIdentifier(); paramName.has_value()) {
auto paramNameId = ns.make(paramName.value(), FileRange::startEnd(startPos, currentFilePos()));
ret.emplace_back(paramNameId);
} else if (str.size() != 0 && isOperatorChar(str[0])) {
if (skippedWhitespace && ret.size() != 0 && std::holds_alternative<char>(ret.back())) ret.push_back(Space{});
ret.emplace_back(readNonWhitespaceChar());
} else if (tryReadChar('(')) {
skipWhitespace();
auto paramNameStartPos = currentFilePos();
auto paramName = readIdentifier(""sv);
auto paramNameId = ns.make(paramName, FileRange::startEnd(paramNameStartPos, currentFilePos()));
skipWhitespace();
readChar(':', ""sv);
skipWhitespace();
auto syntaxNameStartPos = currentFilePos();
auto syntaxName = readIdentifier(""sv);
auto syntaxNameId = ns.find(syntaxName);
if (!syntaxNameId.has_value()) throw SyntaxError("Unknown syntax name"sv, FileRange::startEnd(syntaxNameStartPos, currentFilePos()));
skipWhitespace();
readChar(')', ""sv);
ret.emplace_back(pair{paramNameId, syntaxNameId.value()});
} else {
throw SyntaxError("Expected something else"sv, currentFilePos());
}
auto before = str.size();
skipWhitespace();
skippedWhitespace = before != str.size();
}
return ret;
}