-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtylasu-parser.ts
312 lines (281 loc) · 11.6 KB
/
tylasu-parser.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
import {Node} from "../model/model";
import {
ATNSimulator,
CharStream,
CommonTokenStream,
ErrorNode,
Lexer,
Parser as ANTLRParser,
ParserRuleContext,
ParseTree,
Recognizer,
TerminalNode,
Token,
TokenStream
} from "antlr4ng";
import {Issue, IssueSeverity} from "../validation";
import {Point, Position, Source, StringSource} from "../model/position";
import {assignParents} from "../model/processing";
import {walk} from "../traversing/structurally";
import {
FirstStageParsingResult,
LexingResult,
ParsingResult,
TokenCategory,
TylasuANTLRToken, TylasuLexer,
TylasuToken
} from "./parsing";
import {ASTParser} from "./ast-parser";
export abstract class TokenFactory<T extends TylasuToken> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
categoryOf(t: Token): TokenCategory {
return TokenCategory.PLAIN_TEXT;
}
abstract convertToken(t: Token): T;
private convertTerminal(terminalNode: TerminalNode): T {
return this.convertToken(terminalNode.symbol);
}
extractTokens(result: ParsingResult<any>): LexingResult<T> | undefined {
const antlrTerminals: TerminalNode[] = [];
function extractTokensFromParseTree(pt?: ParseTree | null) {
if (pt instanceof TerminalNode) {
antlrTerminals.push(pt);
} else if (pt != null) {
for (let i = 0; i < pt.getChildCount(); i++) {
extractTokensFromParseTree(pt.getChild(i))
}
}
}
const ptRoot = result.firstStage?.root;
if (ptRoot != null) {
extractTokensFromParseTree(ptRoot);
antlrTerminals.sort((a, b) => a.symbol.tokenIndex - b.symbol.tokenIndex);
const tokens = antlrTerminals.map(t => this.convertTerminal(t));
return new LexingResult(result.code, tokens, result.issues, result.firstStage?.lexingTime);
}
}
}
export class ANTLRTokenFactory extends TokenFactory<TylasuANTLRToken> {
convertToken(t: Token): TylasuANTLRToken {
return new TylasuANTLRToken(this.categoryOf(t), t);
}
}
export const SYNTAX_ERROR = "parser.syntaxError";
export const INPUT_NOT_FULLY_CONSUMED = "parser.inputNotFullyConsumed";
export const ERROR_NODE_FOUND = "parser.errorNodeFound";
export abstract class TylasuANTLRLexer<T extends TylasuToken> implements TylasuLexer<T> {
constructor(public readonly tokenFactory: TokenFactory<T>) {}
/**
* Creates the lexer.
*/
protected abstract createANTLRLexer(inputStream: CharStream): Lexer | undefined;
/**
* Performs "lexing" on the given code stream, i.e., it breaks it into tokens.
*/
lex(inputStream: CharStream, onlyFromDefaultChannel = true): LexingResult<T> {
const issues: Issue[] = [];
const tokens: T[] = [];
const time = performance.now();
const lexer = this.createANTLRLexer(inputStream)!;
this.injectErrorCollectorInLexer(lexer, issues);
let t: Token;
do {
t = lexer.nextToken();
if (!t) {
break;
} else {
if (!onlyFromDefaultChannel || t.channel == Token.DEFAULT_CHANNEL) {
tokens.push(this.tokenFactory.convertToken(t));
}
}
} while (t.type != Token.EOF);
if (t && (t.type != Token.EOF)) {
const message = "The lexer didn't consume the entire input";
issues.push(Issue.lexical(message, IssueSeverity.WARNING, Position.ofTokenEnd(t), undefined,
INPUT_NOT_FULLY_CONSUMED))
}
const code = inputStream.getTextFromRange(0, inputStream.size - 1);
return new LexingResult(code, tokens, issues, performance.now() - time);
}
protected injectErrorCollectorInLexer(lexer: Lexer, issues: Issue[]): void {
lexer.removeErrorListeners();
lexer.addErrorListener({
reportAmbiguity() {},
reportAttemptingFullContext() {},
reportContextSensitivity() {},
syntaxError<S extends Token, T extends ATNSimulator>(recognizer: Recognizer<T>, offendingSymbol: S | null, line: number, charPositionInLine: number, msg: string) {
issues.push(
Issue.lexical(
msg || "unspecified",
IssueSeverity.ERROR,
Position.ofPoint(new Point(line, charPositionInLine)),
undefined,
SYNTAX_ERROR));
}
});
}
}
export abstract class TylasuParser<
R extends Node, P extends ANTLRParser, C extends ParserRuleContext, T extends TylasuToken
> extends TylasuANTLRLexer<T> implements ASTParser<R> {
/**
* Creates the first-stage parser.
*/
protected abstract createANTLRParser(tokenStream: TokenStream): P;
/**
* Invokes the parser's root rule, i.e., the method which is responsible of parsing the entire input.
* Usually this is the topmost rule, the one with index 0 (as also assumed by other libraries such as antlr4-c3),
* so this method invokes that rule. If your grammar/parser is structured differently, or if you're using this to
* parse only a portion of the input or a subset of the language, you have to override this method to invoke the
* correct entry point.
*/
protected invokeRootRule(parser: P): C {
const entryPoint = parser[parser.ruleNames[0]]
return entryPoint!.call(parser) as C
}
/**
* Transforms a parse tree into an AST (second parsing stage).
*/
protected abstract parseTreeToAst(parseTreeRoot: C, considerPosition: boolean, issues: Issue[], source?: Source): R | undefined;
/**
* Creates the first-stage lexer and parser.
*/
protected createParser(inputStream: CharStream, issues: Issue[]): P {
const lexer = this.createANTLRLexer(inputStream)!;
this.injectErrorCollectorInLexer(lexer, issues);
const tokenStream = this.createTokenStream(lexer);
const parser = this.createANTLRParser(tokenStream);
this.injectErrorCollectorInParser(parser, issues);
return parser;
}
protected createTokenStream(lexer: Lexer): TokenStream {
return new CommonTokenStream(lexer);
}
/**
* Checks the parse tree for correctness. If you're concerned about performance, you may want to override this to
* do nothing.
*/
protected verifyParseTree(parser: ANTLRParser, issues: Issue[], root: ParserRuleContext): void {
const lastToken = parser.inputStream.get(parser.inputStream.index);
if (lastToken.type != Token.EOF) {
issues.push(
Issue.syntactic(
"The whole input was not consumed",
IssueSeverity.ERROR,
Position.ofTokenEnd(lastToken),
undefined,
INPUT_NOT_FULLY_CONSUMED));
}
processDescendantsAndErrors(
root,
() => {},
it => {
const message = `Error node found (token: ${it.symbol?.type} – ${it.symbol?.text})`;
issues.push(Issue.syntactic(message, IssueSeverity.ERROR, Position.ofParseTree(it),
undefined,
ERROR_NODE_FOUND,
[
{ name: "type", value: it.symbol?.type?.toString() || "" },
{ name: "text", value: it.symbol?.text || "" },
]));
});
}
/**
* Executes only the first stage of the parser, i.e., the production of a parse tree. Usually, you'll want to use
* the [parse] method, that returns an AST which is simpler to use and query.
*/
parseFirstStage(inputStream: CharStream, measureLexingTime = false): FirstStageParsingResult<C> {
const issues: Issue[] = [];
let lexingTime: number | undefined = undefined;
const time = performance.now();
const parser = this.createParser(inputStream, issues);
if (measureLexingTime) {
const tokenStream = parser.inputStream;
if (tokenStream instanceof CommonTokenStream) {
lexingTime = performance.now();
tokenStream.fill();
tokenStream.seek(0);
lexingTime = lexingTime - performance.now();
}
}
const root = this.invokeRootRule(parser);
if (root != null) {
this.verifyParseTree(parser, issues, root);
}
const code = inputStream.getTextFromRange(0, inputStream.size - 1);
return new FirstStageParsingResult(code, root, issues, parser, performance.now() - time, lexingTime);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected postProcessAst(ast: R, issues: Issue[]): R {
return ast;
}
parse(
code: string | CharStream, considerPosition = true,
measureLexingTime = true, source?: Source
): ParsingResult<R> {
if (typeof code === "string") {
if (!source) {
source = new StringSource(code);
}
code = CharStream.fromString(code);
}
const start = performance.now();
const firstStage = this.parseFirstStage(code, measureLexingTime);
const issues = firstStage.issues;
let ast = this.parseTreeToAst(firstStage.root!, considerPosition, issues, source);
if (ast) {
this.assignParents(ast);
}
ast = ast ? this.postProcessAst(ast, issues) : ast;
if (ast != null && !considerPosition) {
for (const node of walk(ast)) {
delete node.origin;
}
}
const text = code.getTextFromRange(0, code.size - 1);
return new ParsingResult(text, ast, issues, undefined, firstStage, performance.now() - start);
}
/**
* Traverses the AST to ensure that parent nodes are correctly assigned.
*
* If you assign the parents correctly when you build the AST, or you're not interested in tracking child-parent
* relationships, you can override this method to do nothing to improve performance.
*/
protected assignParents(ast: R): void {
assignParents(ast);
}
protected injectErrorCollectorInParser(parser: ANTLRParser, issues: Issue[]): void {
parser.removeErrorListeners();
parser.addErrorListener({
reportAmbiguity() {},
reportAttemptingFullContext() {},
reportContextSensitivity() {},
syntaxError<S extends Token, T extends ATNSimulator>(recognizer: Recognizer<T>, offendingSymbol: S | null, line: number, charPositionInLine: number, msg: string) {
issues.push(
Issue.syntactic(
msg || "unspecified",
IssueSeverity.ERROR,
Position.ofPoint(new Point(line, charPositionInLine)),
undefined,
SYNTAX_ERROR));
}
});
}
}
function processDescendantsAndErrors(
self: ParserRuleContext,
operationOnParserRuleContext: (c: ParserRuleContext) => void,
operationOnError: (e: ErrorNode) => void,
includingMe = true
) {
if (includingMe) {
operationOnParserRuleContext(self);
}
if (self.children != null) {
self.children.filter(c => c instanceof ParserRuleContext).forEach(c => {
processDescendantsAndErrors(
c as ParserRuleContext, operationOnParserRuleContext, operationOnError, true);
});
self.children.filter(c => c instanceof ErrorNode).forEach(operationOnError);
}
}