-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.ts
41 lines (39 loc) · 1.16 KB
/
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
import { preProcess, getLoc, Mapper } from "./preProcessor";
import { Ast, SourceLocation } from "./types";
import { parse as envParse } from "./envParser";
import { mapAst } from "./astUtils";
import { createUserError, createCompilerError } from "./errorUtils";
function mapLoc(loc: SourceLocation, mapper: Mapper): SourceLocation {
const first = getLoc(mapper, loc.first_column);
const last = getLoc(mapper, loc.last_column);
return {
first_column: first.column,
last_column: last.column,
first_line: first.line,
last_line: last.line,
};
}
export function parse(code: string): Ast {
const [processedCode, mapper] = preProcess(code);
try {
const ast = envParse(processedCode);
return mapAst(ast, (node: Ast) => {
if (node.loc.first_line !== 1 || node.loc.last_line != 1) {
throw createCompilerError("Unexpected multiline", node.loc, code);
}
return {
...node,
loc: mapLoc(node.loc, mapper),
};
});
} catch (e) {
if (e.hash == null) {
throw e;
}
throw createUserError(
`Parse Error: ${e.message.split("\n")[3]}`,
mapLoc(e.hash.loc, mapper),
code
);
}
}