-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.ts
120 lines (100 loc) · 3.94 KB
/
compile.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
import { CharStream, CommonTokenStream, ParserRuleContext, ParseTreeWalker, TokenStream } from 'antlr4ng';
import { ChicoryLexer } from './generated/ChicoryLexer';
import { ChicoryParser } from './generated/ChicoryParser';
import { ChicoryParserVisitor } from './ChicoryVisitor';
import { ChicoryTypeChecker } from './ChicoryTypeCheckerVisitor';
import { LspDiagnostic, CompilationError, SyntaxError, TypeHint, LspRange } from './env';
import { ChicoryErrorListener } from './ChicoryErrorListener';
const rangeContains = (outer: LspRange, inner: LspRange): boolean => {
return (
(outer.start.line < inner.start.line ||
(outer.start.line === inner.start.line && outer.start.character < inner.start.character))
&&
(outer.end.line > inner.end.line ||
(outer.end.line === inner.end.line && outer.end.character > inner.end.character))
);
}
const filterOutErrorsThatContainOtherErrors = (errors: LspDiagnostic[]): LspDiagnostic[] => {
const filteredErrors: LspDiagnostic[] = [];
for (const error of errors) {
let containsOtherError = false;
for (const otherError of errors) {
if (error !== otherError && rangeContains(error.range, otherError.range)) {
containsOtherError = true
}
}
if (!containsOtherError) {
filteredErrors.push(error);
}
}
return filteredErrors;
}
const getRange = (ctx: ParserRuleContext, tokenStream: TokenStream) => {
const {start, stop} = ctx.getSourceInterval()
const startToken = tokenStream.get(start)
const stopToken = tokenStream.get(stop)
return {
start: { line: startToken.line - 1, character: startToken.column },
end: { line: stopToken.line - 1, character: stopToken.column + (stopToken.text?.length || 1) }
}
}
const compilerErrorToLspError = (tokenStream: TokenStream) => ((e: CompilationError) => ({
severity: 1, // 1 is error
message: e.message as string,
range: getRange(e.context, tokenStream),
source: "chicory",
}))
const syntaxErrorToLspError = (e: SyntaxError) => ({
severity: 1, // 1 is error
source: "chicory",
...e
})
export type CompileResult = {
code: string;
errors: LspDiagnostic[];
hints: TypeHint[];
}
export default (source: string): CompileResult => {
if (!source.trim()) {
return { code: "", errors: [], hints: [] }
}
let inputStream = CharStream.fromString(source);
let lexer = new ChicoryLexer(inputStream);
let tokenStream = new CommonTokenStream(lexer);
let parser = new ChicoryParser(tokenStream);
const errorListener = new ChicoryErrorListener();
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
let tree = parser.program();
// Create type checker first
const typeChecker = new ChicoryTypeChecker();
// Create visitor with the type checker
const visitor = new ChicoryParserVisitor(typeChecker);
let code: string = ""
let errors: LspDiagnostic[] = []
let hints: TypeHint[] = []
try {
const {code: compiledCode, errors: unprocessedErrors, hints: unprocessedHints} = visitor.getOutput(tree) || {code: "", errors: [], hints: []}
const mapErrors = compilerErrorToLspError(tokenStream)
code = compiledCode
errors.push(...unprocessedErrors.map(mapErrors))
hints = unprocessedHints.map(({context, type}) => ({
range: getRange(context, tokenStream),
type
}));
}
catch (e) {
// We ensure that the compiler does not crash if the parser fails to produce a parseable parse tree
}
const syntaxErrors = errorListener.getErrors();
errors.push(...syntaxErrors.map(syntaxErrorToLspError))
errors = filterOutErrorsThatContainOtherErrors(errors)
// Convert hints to LSP format
return {
code,
errors,
hints
}
}