Skip to content

Commit 2545cc4

Browse files
Add estree position information to py-slang errors
1 parent 493e7b4 commit 2545cc4

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/errors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Token} from "./tokenizer";
2+
import {Position} from "estree";
23

34
/*The offset is calculated as follows:
45
Current position is one after real position of end of token: 1
@@ -30,17 +31,23 @@ function getFullLine(source: string, current: number): string {
3031
return '\n' + source.slice(back, forward);
3132
}
3233

34+
function toEstreeLocation(line: number, column: number, offset: number) {
35+
return {line, column, offset}
36+
}
37+
3338
export namespace TokenizerErrors {
3439
export class BaseTokenizerError extends SyntaxError {
3540
line: number;
3641
col: number;
42+
loc: Position;
3743

3844
constructor(message: string, line: number, col: number) {
3945
super(`SyntaxError at line ${line} column ${col-1}
4046
${message}`);
4147
this.line = line;
4248
this.col = col;
4349
this.name = "BaseTokenizerError";
50+
this.loc = toEstreeLocation(line, col, 0);
4451
}
4552
}
4653

@@ -103,13 +110,15 @@ export namespace ParserErrors {
103110
export class BaseParserError extends SyntaxError {
104111
line: number;
105112
col: number;
113+
loc: Position;
106114

107115
constructor(message: string, line: number, col: number) {
108116
super(`SyntaxError at line ${line} column ${col-1}
109117
${message}`);
110118
this.line = line;
111119
this.col = col;
112120
this.name = "BaseParserError";
121+
this.loc = toEstreeLocation(line, col, 0);
113122
}
114123
}
115124
export class ExpectedTokenError extends BaseParserError {
@@ -147,13 +156,15 @@ export namespace ResolverErrors {
147156
export class BaseResolverError extends SyntaxError {
148157
line: number;
149158
col: number;
159+
loc: Position;
150160

151161
constructor(message: string, line: number, col: number) {
152162
super(`ResolverError at line ${line} column ${col-1}
153163
${message}`);
154164
this.line = line;
155165
this.col = col;
156166
this.name = "BaseResolverError";
167+
this.loc = toEstreeLocation(line, col, 0);
157168
}
158169
}
159170
export class NameNotFoundError extends BaseResolverError {

0 commit comments

Comments
 (0)