Repository: peggyjs/peggy
Pull request #616 (2025-05-07 03:38:29)

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


Patch:
diff --git a/bin/peggy-cli.js b/bin/peggy-cli.js
index 71c57e8..2288be4 100644
--- a/bin/peggy-cli.js
+++ b/bin/peggy-cli.js
@@ -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}`;
diff --git a/lib/peg.d.ts b/lib/peg.d.ts
index 0c64895..7d43fa3 100644
--- a/lib/peg.d.ts
+++ b/lib/peg.d.ts
@@ -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;
 
@@ -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;
 
@@ -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;
 
@@ -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
@@ -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;
   }
 }
 
@@ -1109,7 +1104,7 @@ export interface LibraryResults {
 
 export interface Parser {
   StartRules: string[];
-  SyntaxError: parser.PeggySyntaxErrorConstructor;
+  SyntaxError: typeof parser.SyntaxError;
 
   parse(
     input: string,
@@ -1452,7 +1447,7 @@ export type GrammarInput = 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
@@ -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
@@ -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
@@ -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
@@ -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


Test patch:
diff --git a/test/types/peg.test-d.ts b/test/types/peg.test-d.ts
index 3876cc5..5c4b311 100644
--- a/test/types/peg.test-d.ts
+++ b/test/types/peg.test-d.ts
@@ -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 },