-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: split parser errors * refactor: improve error messages and update snapshots
- Loading branch information
1 parent
45a006b
commit b26e2e1
Showing
14 changed files
with
340 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Render error message to JSON for tests | ||
*/ | ||
|
||
import { throwInternalCompilerError } from "../errors"; | ||
import { SrcInfo } from "../grammar"; | ||
import { srcInfoEqual } from "../grammar/src-info"; | ||
import { ErrorDisplay } from "./display"; | ||
|
||
export type ErrorJson = ErrorSub | ErrorText | ErrorLink | ErrorAt; | ||
|
||
export type ErrorText = { kind: "text"; text: string }; | ||
export type ErrorSub = { kind: "sub"; parts: string[]; subst: ErrorJson[] }; | ||
export type ErrorLink = { kind: "link"; text: string; loc: SrcInfo }; | ||
export type ErrorAt = { kind: "at"; body: ErrorJson; loc: SrcInfo }; | ||
|
||
export const errorJsonEqual = (left: ErrorJson, right: ErrorJson): boolean => { | ||
switch (left.kind) { | ||
case "link": { | ||
return ( | ||
left.kind === right.kind && | ||
left.text === right.text && | ||
srcInfoEqual(left.loc, right.loc) | ||
); | ||
} | ||
case "at": { | ||
return ( | ||
left.kind === right.kind && | ||
errorJsonEqual(left.body, right.body) && | ||
srcInfoEqual(left.loc, right.loc) | ||
); | ||
} | ||
case "text": { | ||
return left.kind === right.kind && left.text === right.text; | ||
} | ||
case "sub": { | ||
if (left.kind !== right.kind) { | ||
return false; | ||
} | ||
if (left.parts.length !== right.parts.length) { | ||
return false; | ||
} | ||
if (left.parts.some((part, index) => part != right.parts[index])) { | ||
return false; | ||
} | ||
if (left.subst.length !== right.subst.length) { | ||
return false; | ||
} | ||
return left.subst.every((leftChild, index) => { | ||
const rightChild = right.subst[index]; | ||
if (typeof rightChild === "undefined") { | ||
throwInternalCompilerError( | ||
"Impossible: by this moment array lengths must match", | ||
); | ||
} | ||
return errorJsonEqual(leftChild, rightChild); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export const displayToJson: ErrorDisplay<ErrorJson> = { | ||
text: (text) => ({ kind: "text", text }), | ||
sub: (parts, ...subst) => ({ kind: "sub", parts: [...parts], subst }), | ||
link: (text, loc) => ({ kind: "link", text, loc }), | ||
at: (loc, body) => ({ kind: "at", body, loc }), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Render error message to string for compiler CLI | ||
*/ | ||
|
||
import { ErrorDisplay } from "./display"; | ||
import { locationStr } from "../errors"; | ||
|
||
export const displayToString: ErrorDisplay<string> = { | ||
text: (text) => text, | ||
sub: (parts, ...subst) => { | ||
const [head, ...tail] = parts; | ||
if (!head) { | ||
return ""; | ||
} | ||
return tail.reduce((acc, part, index) => { | ||
const sub = subst[index]; | ||
return acc + sub + part; | ||
}, head); | ||
}, | ||
link: (text, _loc) => text, | ||
at: (loc, body) => { | ||
return `${locationStr(loc)}${body}\n${loc.interval.getLineAndColumnMessage()}`; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Describes DSL for displaying errors | ||
*/ | ||
|
||
import { SrcInfo } from "../grammar"; | ||
|
||
export interface ErrorDisplay<T> { | ||
// Specify main error location | ||
at: (loc: SrcInfo, body: T) => T; | ||
|
||
// Regular string | ||
text: (text: string) => T; | ||
|
||
// Text with substitutions | ||
sub: (text: TemplateStringsArray, ...subst: T[]) => T; | ||
|
||
// Reference some code location | ||
link: (text: string, loc: SrcInfo) => T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.