-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83500dd
commit fd45c0c
Showing
8 changed files
with
96 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
node_modules | ||
dist | ||
pnpm-lock.yaml | ||
data/test.no |
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,23 @@ | ||
import { TokenName } from './parser/parser' | ||
import { indexToLocation, LocationRange, prettyIndex, prettyLocation } from './location' | ||
import { Source } from './source' | ||
|
||
export interface SyntaxErrorInfo { | ||
expect: TokenName[], | ||
got: TokenName, | ||
location: LocationRange | ||
} | ||
|
||
export const prettySyntaxError = (error: SyntaxErrorInfo): string => | ||
`syntax error: expected \`${error.expect}\`, got \`${error.got}\`` | ||
|
||
export const prettySourceMessage = (message: string, index: number, source: Source): string => { | ||
const location = indexToLocation(index, source) | ||
const locationStr = `${location ? `${source.filename}:${prettyLocation(location)}` : '<unknwon location>'}` | ||
const indent = ' '.repeat(4) | ||
return `\ | ||
${prettyIndex(index, source)} | ||
${message} | ||
${indent}at ${locationStr} | ||
` | ||
} |
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
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,44 @@ | ||
import { isNewline } from './lexer/lexer' | ||
import { Source } from './source' | ||
|
||
export interface LocationRange { | ||
start: number | ||
end: number | ||
} | ||
|
||
export interface Location { | ||
line: number, | ||
column: number | ||
} | ||
|
||
export const indexToLocation = (index: number, source: Source): Location | undefined => { | ||
let line = 0 | ||
let column = 0 | ||
for (let i = 0; i <= index; i++) { | ||
if (i === index) { | ||
return { line, column } | ||
} | ||
if (isNewline(source.str[i])) { | ||
line++ | ||
column = 0 | ||
} else { | ||
column++ | ||
} | ||
} | ||
return undefined | ||
} | ||
|
||
export const prettyIndex = (index: number, source: Source): string => { | ||
const start = indexToLocation(index, source) | ||
if (!start) return '<outside of a file>' | ||
|
||
const lines = source.str.split('\n') | ||
const line = lines[start.line] | ||
const highlight = ' '.repeat(start.column) + '^' | ||
const lineNum = `${start.line + 1} | ` | ||
return `\ | ||
${lineNum}${line} | ||
${' '.repeat(lineNum.length)}${highlight}` | ||
} | ||
|
||
export const prettyLocation = (location: Location): string => `${location.line + 1}:${location.column + 1}` |
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
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,4 @@ | ||
export interface Source { | ||
str: string | ||
filename: string | ||
} |