Skip to content

Commit

Permalink
Add SyntaxErrorConstructor type, fixes #216
Browse files Browse the repository at this point in the history
  • Loading branch information
cmfcmf committed Nov 23, 2021
1 parent f22de58 commit ccab5b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Released: TBD
- [#169](https://github.com/peggyjs/peggy/issues/169): Expose string escape functions
- [#197](https://github.com/peggyjs/peggy/pull/197): Fix a regression of redundant commas in the
character classes in the error messages, introduced in fad4ab74d1de67ef1902cd22d479c81ccab73224
- [#216](https://github.com/peggyjs/peggy/issues/216): Fix typescript definition of SyntaxError

1.2.0
-----
Expand Down
29 changes: 20 additions & 9 deletions lib/peg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,25 @@ export namespace parser {
| EndExpectation
| OtherExpectation;

interface SyntaxErrorConstructor {
new (
message: string,
expected: Expectation[] | null,
found: string | null,
location: LocationRange
): SyntaxError;
readonly prototype: SyntaxError;

// Static methods
/**
* Constructs the human-readable message from the machine representation.
*
* @param expected Array of expected items, generated by the parser
* @param found Any text that will appear as found in the input instead of expected
*/
buildMessage(expected: Expectation[], found: string): string;
}

/** Thrown if the grammar contains a syntax error. */
class SyntaxError extends Error {
/** Location where error was originated. */
Expand Down Expand Up @@ -457,14 +476,6 @@ export namespace parser {
* @returns the formatted error
*/
format(sources: SourceText[]): string;

/**
* Constructs the human-readable message from the machine representation.
*
* @param expected Array of expected items, generated by the parser
* @param found Any text that will appear as found in the input instead of expected
*/
static buildMessage(expected: Expectation[], found: string): string;
}
}

Expand Down Expand Up @@ -817,7 +828,7 @@ export interface ParserOptions {
export interface Parser {
parse(input: string, options?: ParserOptions): any;

SyntaxError: parser.SyntaxError;
SyntaxError: parser.SyntaxErrorConstructor;
}

export interface ParserTracer {
Expand Down
13 changes: 13 additions & 0 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ describe("peg.d.ts", () => {
expect(res).toStrictEqual(["buzz", 11, "fizz"]);
});

it("types SyntaxError correctly", () => {
const parser = peggy.generate(src);

expectType<peggy.parser.SyntaxErrorConstructor>(parser.SyntaxError);
expectType<peggy.parser.SyntaxError>(new parser.SyntaxError("", null, null, {
source: null,
start: { line: 0, column: 0, offset: 0 },
end: { line: 0, column: 0, offset: 0 },
}));

expectType<string>(parser.SyntaxError.buildMessage([], ""));
});

it("takes a valid tracer", () => {
const parser = peggy.generate(src, { trace: true });
expectType<peggy.Parser>(parser);
Expand Down

0 comments on commit ccab5b5

Please sign in to comment.