-
Notifications
You must be signed in to change notification settings - Fork 17
/
RootParser.ts
333 lines (296 loc) · 10.8 KB
/
RootParser.ts
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
import { strict as assert } from "assert";
import { CstNode, CstParser, Lexer, TokenType } from "chevrotain";
import { makeLexer } from "./lexer/makeLexer";
import { makeRootMode } from "./lexer/makeRootMode";
import {
AttributeListStart,
AttributeListEnd,
PushParser,
PopParser,
BlockCommentStart,
BlockCommentEnd,
Tag,
TagEnd,
TagStart,
Newline,
Identifier,
JsonStringLiteral,
LineComment,
StringLiteral,
} from "./lexer/tokens";
import { ErrorMessageProvider } from "./ErrorMessageProvider";
import { BluehawkError } from "../BluehawkError";
import { locationFromToken } from "./locationFromToken";
import { extractTagNamesFromTokens } from "./extractTagNamesFromTokens";
import { LanguageSpecification } from "./LanguageSpecification";
// See https://sap.github.io/chevrotain/docs/tutorial/step2_parsing.html
// Comment awareness prevents bluehawk from outputting half-commented code
// blocks.
/*
BLUEHAWK SYNTAX
╔════════════════════════════════════════╗
║ LEGEND ║
║========================================║▒▒
║ starts with capital letter = token ║▒▒
║ starts with lowercase letter = rule ║▒▒
║ : = rule definition ║▒▒
║ | = or ║▒▒
║ ( )* = any ║▒▒
║ ( )? = 0 or 1 ║▒▒
║ ( )+ = 1 or more ║▒▒
║ _X = paired token of Category ║▒▒
╚════════════════════════════════════════╝▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
annotatedText
: (chunk)*
attributeList
: AttributeListStart (attributeList | JsonStringLiteral | Newline | LineComment)* AttributeListEnd
blockTagUncommentedContents
: BlockCommentEnd Newline (chunk)* BlockCommentStart
blockTag
: TagStart (tagAttribute)? (BlockCommentEnd*backtrack* | (Newline)?) ((chunk)* | blockTagUncommentedContents) TagEnd
blockComment
: BlockCommentStart (tag | LineComment | NewLine | BlockCommentStart†)* BlockCommentEnd
chunk
: (tag | blockComment | lineComment | pushParser | StringLiteral)* (Newline | EOF)††
tag
: blockTag | Tag
tagAttribute
: (Identifier)+ | attributeList
lineComment
: LineComment (Tag | LineComment | BlockCommentStart | BlockCommentEnd)*
pushParser
: PushParser_X (pushParser | Newline)* PopParser_X
† = if canNestBlockComments
†† = newline optional in blockTag
*/
type Rule = (idx?: number) => CstNode;
// While the lexer defines the tokens of the language, the parser defines the
// syntax.
export class RootParser extends CstParser {
lexer: Lexer;
annotatedText: Rule = UndefinedRule;
chunk: Rule = UndefinedRule;
blockTag: Rule = UndefinedRule;
blockTagUncommentedContents: Rule = UndefinedRule;
tag: Rule = UndefinedRule;
tagAttribute: Rule = UndefinedRule;
blockComment: Rule = UndefinedRule;
lineComment: Rule = UndefinedRule;
attributeList: Rule = UndefinedRule;
pushParser: Rule = UndefinedRule;
languageSpecification?: LanguageSpecification;
constructor(
languageTokens: TokenType[],
languageSpecification?: LanguageSpecification
) {
super(makeRootMode(languageTokens), {
nodeLocationTracking: "full",
errorMessageProvider: new ErrorMessageProvider(),
});
this.lexer = makeLexer(languageTokens);
this.languageSpecification = languageSpecification;
// annotatedText is the root rule and is a series of zero or more chunks
this.RULE("annotatedText", () => {
this.MANY(() => this.SUBRULE(this.chunk));
});
// A chunk is roughly a line of text or block(s) followed by a newline. We
// have to distinguish chunk from annotatedText in order to be able to
// distinguish a LineComment near the end of a blockTag from a
// LineComment within a blockTag's inner chunks.
this.RULE("chunk", () => {
this.MANY(() => {
this.OR([
{ ALT: () => this.SUBRULE(this.tag) },
{ ALT: () => this.SUBRULE(this.blockComment) },
{ ALT: () => this.SUBRULE(this.lineComment) },
{ ALT: () => this.SUBRULE(this.pushParser) },
{ ALT: () => this.CONSUME(StringLiteral) },
]);
});
this.OR1([
{ ALT: () => this.CONSUME(Newline) },
{
// Allow for files and lineComment lines to end without a newline.
GATE: () => {
const { name } = this.LA(1).tokenType;
return name === "EOF" || name === TagEnd.name;
},
ALT: () => {
return;
},
},
]);
});
this.RULE("tag", () => {
this.OR([
{ ALT: () => this.SUBRULE(this.blockTag) },
{ ALT: () => this.CONSUME(Tag) },
]);
});
// Rule for block comments that behave like line comments in block tags.
// Define a block tag as two block comments with an uncommented body.
// This is necessary for languages like html that do not have a line comment.
this.RULE("blockTagUncommentedContents", () => {
this.CONSUME1(BlockCommentEnd);
this.CONSUME1(Newline);
this.MANY(() => this.SUBRULE(this.chunk));
this.CONSUME2(BlockCommentStart);
});
this.RULE("blockTag", () => {
const startToken = this.CONSUME1(TagStart);
this.OPTION1(() => this.SUBRULE(this.tagAttribute));
this.OR1([
{ ALT: () => this.CONSUME2(Newline) },
{ ALT: () => this.BACKTRACK(() => this.CONSUME(BlockCommentEnd)) },
]);
this.OR2([
{ ALT: () => this.SUBRULE(this.blockTagUncommentedContents) },
{ ALT: () => this.MANY2(() => this.SUBRULE(this.chunk)) },
]);
const endToken = this.CONSUME3(startToken.payload?.endToken ?? TagEnd);
if (this.RECORDING_PHASE) {
return;
}
// Detect tag start/end name mismatch here or we might never get to
// the semantic stage that can actually tell tag names apart. We don't
// want to have a token for every registered tag, so receiving the
// paired endToken in the payload doesn't quite work out.
const [startTagName, endTagName] = extractTagNamesFromTokens(
startToken,
endToken
);
if (startTagName !== endTagName) {
this._bluehawkErrors.push({
component: "parser",
location: locationFromToken(endToken),
message: `Unexpected '${endToken.image}' closing '${startToken.image}'`,
});
}
});
this.RULE("tagAttribute", () => {
this.OR([
{ ALT: () => this.AT_LEAST_ONE(() => this.CONSUME(Identifier)) },
{ ALT: () => this.SUBRULE(this.attributeList) },
]);
});
this.RULE("blockComment", () => {
const startToken = this.CONSUME(BlockCommentStart);
this.MANY(() =>
this.OR([
{ ALT: () => this.SUBRULE(this.tag) },
{ ALT: () => this.CONSUME(LineComment) },
{ ALT: () => this.CONSUME(Newline) },
{
GATE: () => startToken.payload?.canNest !== false,
ALT: () => this.CONSUME1(BlockCommentStart),
},
])
);
const endToken = startToken.payload?.endToken ?? BlockCommentEnd;
this.CONSUME(endToken);
});
this.RULE("lineComment", () => {
this.CONSUME(LineComment);
this.MANY(() =>
this.OR([
{ ALT: () => this.SUBRULE(this.tag) },
{ ALT: () => this.CONSUME1(LineComment) },
{ ALT: () => this.CONSUME(BlockCommentStart) },
{ ALT: () => this.CONSUME(BlockCommentEnd) },
])
);
});
this.RULE("attributeList", () => {
this.CONSUME(AttributeListStart);
// Simply consume the JSON tokens and allow the JSON.parse() function in
// the visitor to report errors.
this.MANY(() => {
// Consume any token but block comment tokens. We disallow block comment
// tokens here because it prevents block comment straddling in block
// tags.
this.OR([
{ ALT: () => this.CONSUME(LineComment) },
{ ALT: () => this.CONSUME(Newline) },
{ ALT: () => this.CONSUME(JsonStringLiteral) },
{
// Handle subobjects
ALT: () => this.SUBRULE(this.attributeList),
},
]);
});
this.CONSUME(AttributeListEnd);
});
this.RULE("pushParser", () => {
// pushParser leaves a block of text unparsed by this parser, allowing the
// visitor to parse that section with a different parser
const pushToken = this.CONSUME(PushParser);
this.MANY(() =>
this.OR([
{ ALT: () => this.SUBRULE(this.pushParser) },
{ ALT: () => this.CONSUME(Newline) },
])
);
// Ensure the end token corresponds to the starting token
const popToken = pushToken.payload?.endToken ?? PopParser;
this.CONSUME(popToken);
});
this.performSelfAnalysis();
}
parse(text: string): { cst?: CstNode; errors: BluehawkError[] } {
this._bluehawkErrors = [];
const tokens = this.lexer.tokenize(text);
const tokenErrors = tokens.errors.map(
(error): BluehawkError => ({
component: "lexer",
location: {
line: error.line || 0,
column: error.column || 0,
offset: error.offset,
},
message: error.message,
})
);
if (tokenErrors.length !== 0) {
return {
cst: undefined,
errors: tokenErrors,
};
}
this.input = tokens.tokens;
const cst = this.annotatedText();
return {
cst,
errors: [
...tokenErrors,
...this._bluehawkErrors,
...this.errors.map((error): BluehawkError => {
// Retrieve the error location from the message because I can't seem
// to find it on the actual error object.
const lineColumnOffset = /^([0-9]+):([0-9]+)\(([0-9]+)\)/
.exec(error?.message)
?.map((result) => parseInt(result)) ?? [-1, -1, -1, -1];
const [, line, column, offset] = lineColumnOffset;
return {
component: "parser",
location: {
line,
column,
offset,
},
message: error.message,
};
}),
],
};
}
// Private channel returned from parse() for early warnings that can be more
// helpful than normal parser errors.
private _bluehawkErrors: BluehawkError[] = [];
}
// Chevrotain assigns the rules after construction. Initializing rule properties
// with this rule quashes the "Property 'annotatedText' has no initializer and
// is not definitely assigned in the constructor." error.
function UndefinedRule(): CstNode {
assert(false);
}