8000 Switching to PeggySyntaxError didn't work. Moving back to SyntaxError with careful inheritance from globalThis.SyntaxError by hildjj · Pull Request #616 · peggyjs/peggy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Switching to PeggySyntaxError didn't work. Moving back to SyntaxError with careful inheritance from globalThis.SyntaxError #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class PeggyCLI extends Command {
};

if (opts.error) {
const er = /** @type {peggy.parser.PeggySyntaxError} */(opts.error);
const er = /** @type {peggy.parser.SyntaxError} */(opts.error);
if (typeof er.format === "function") {
const fmt = er.format(/** @type {peggy.SourceText[]} */(opts.sources));
message = `${message}\n${fmt}`;
Expand Down
53 changes: 24 additions & 29 deletions lib/peg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ type Problem = [
];

/** Thrown if the grammar contains a semantic error. */
export class GrammarError extends SyntaxError {
export class GrammarError extends globalThis.SyntaxError {
/** Location of the error in the source. */
public location?: LocationRange;

Expand Down Expand Up @@ -559,7 +559,7 @@ export namespace parser {
* @param grammar Source text of the grammar
* @param options Parser options
*
* @throws {PeggySyntaxError} If `grammar` has an incorrect format
* @throws {SyntaxError} If `grammar` has an incorrect format
*/
function parse(grammar: string, options?: Options): ast.Grammar;

Expand Down Expand Up @@ -635,28 +635,8 @@ export namespace parser {
| LiteralExpectation
| OtherExpectation;

interface PeggySyntaxErrorConstructor {
readonly prototype: PeggySyntaxError;

new (
message: string,
expected: Expectation[] | null,
found: string | null,
location: LocationRange
): PeggySyntaxError;

// 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 PeggySyntaxError extends SyntaxError {
class SyntaxError extends globalThis.SyntaxError {
/** Location where error was originated. */
public location: LocationRange;

Expand All @@ -672,6 +652,13 @@ export namespace parser {
*/
public found: string | null;

public constructor(
message: string,
expected: Expectation[] | null,
found: string | null,
location: LocationRange
): SyntaxError;

/**
* Format the error with associated sources. The `location.source` should have
* a `toString()` representation in order the result to look nice. If source
Expand All @@ -691,6 +678,14 @@ export namespace parser {
* @returns the formatted error
*/
public 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
*/
public static buildMessage(expected: Expectation[], found: string): string;
}
}

Expand Down Expand Up @@ -1109,7 +1104,7 @@ export interface LibraryResults {

export interface Parser {
StartRules: string[];
SyntaxError: parser.PeggySyntaxErrorConstructor;
SyntaxError: typeof parser.SyntaxError;

parse(
input: string,
Expand Down Expand Up @@ -1452,7 +1447,7 @@ export type GrammarInp 8000 ut = SourceText[] | string;
* `parser.pegjs` file
* @param options Options that allow you to customize returned parser object
*
* @throws {PeggySyntaxError} If the grammar contains a syntax error, for example,
* @throws {SyntaxError} If the grammar contains a syntax error, for example,
* an unclosed brace
* @throws {GrammarError} If the grammar contains a semantic error, for example,
* duplicated labels
Expand All @@ -1469,7 +1464,7 @@ export function generate(
* `parser.pegjs` file
* @param options Options that allow you to customize returned parser object
*
* @throws {PeggySyntaxError} If the grammar contains a syntax error, for example,
* @throws {SyntaxError} If the grammar contains a syntax error, for example,
* an unclosed brace
* @throws {GrammarError} If the grammar contains a semantic error, for example,
* duplicated labels
Expand All @@ -1491,7 +1486,7 @@ export function generate(
* `parser.pegjs` file
* @param options Options that allow you to customize returned parser object
*
* @throws {PeggySyntaxError} If the grammar contains a syntax error, for example,
* @throws {SyntaxError} If the grammar contains a syntax error, for example,
* an unclosed brace
* @throws {GrammarError} If the grammar contains a semantic error, for
* example, duplicated labels
Expand Down Expand Up @@ -1529,7 +1524,7 @@ export function generate(
* `parser.pegjs` file
* @param options Options that allow you to customize returned parser object
*
* @throws {PeggySyntaxError} If the grammar contains a syntax error, for example,
* @throws {SyntaxError} If the grammar contains a syntax error, for example,
* an unclosed brace
* @throws {GrammarError} If the grammar contains a semantic error, for example,
* duplicated labels
Expand All @@ -1550,7 +1545,7 @@ export function generate(
* `parser.pegjs` file
* @param options Options that allow you to customize returned AST
*
* @throws {PeggySyntaxError} If the grammar contains a syntax error, for example,
* @throws {SyntaxError} If the grammar contains a syntax error, for example,
* an unclosed brace
* @throws {GrammarError} If the grammar contains a semantic error, for example,
* duplicated labels
Expand Down
4 changes: 2 additions & 2 deletions test/types/peg.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ describe("peg.d.ts", () => {
it("types SyntaxError correctly", () => {
const parser = peggy.generate(src);

expectExact<peggy.parser.PeggySyntaxErrorConstructor>()(
expectExact<typeof peggy.parser.SyntaxError>()(
parser.SyntaxError
)();
expectExact<peggy.parser.PeggySyntaxError>()(new parser.SyntaxError("", null, null, {
expectExact<peggy.parser.SyntaxError>()(new parser.SyntaxError("", null, null, {
source: null,
start: { line: 0, column: 0, offset: 0 },
end: { line: 0, column: 0, offset: 0 },
Expand Down
0