Repository: peggyjs/peggy
Pull request #619 (2025-05-07 18:04:28)

ts SyntaxError
- **Fix SyntaxError one more time**
- **Updated fizzbuzz example with some unicode.  Catch bad unicode escapes at compile time.**



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd2c28d..8a7a97a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,18 +3,19 @@ Change Log
 
 This file documents all notable changes to Peggy.
 
-Unreleased
-----------
-
-Released: TBD
-
-### Major Changes
+5.0.2
+-----
 
-### New features
+Released: 2025-05-07
 
 ### Bug fixes
 
-### Documentation
+- Fix TypeScript error in peg.d.ts, SyntaxError constructor should not have a return type.
+  [#619](https://github.com/peggyjs/peggy/pull/619)
+- Add more Unicode to fizzbuzz example.
+  [#619](https://github.com/peggyjs/peggy/pull/619)
+- Catch invalid unicode property escapes at compile time.
+  [#619](https://github.com/peggyjs/peggy/pull/619)
 
 5.0.1
 -----
diff --git a/examples/fizzbuzz.peggy b/examples/fizzbuzz.peggy
index f87bb24..3ee69a9 100644
--- a/examples/fizzbuzz.peggy
+++ b/examples/fizzbuzz.peggy
@@ -33,12 +33,17 @@ line
   / buzz
 
 fizzbuzz = f:fizz _ b:buzz { return f + b }
-fizz = @"fizz"i !{ return currentNumber % 3 }
-buzz = @"buzz"i !{ return currentNumber % 5 }
+fizz
+  = @"fizz"i !{ return currentNumber % 3 }
+  / "🍺"
+
+buzz
+  = @"buzz"i !{ return currentNumber % 5 }
+  / "\u{1F41D}"
 
 // Arbitrary requirement needing &
 number "number without trailing comment"
-  = "0x" n:$[0-9a-f]i+ &"\n" { return parseInt(n, 16) }
+  = "0x" n:$[\p{ASCII_Hex_Digit}]+ &"\n" { return parseInt(n, 16) }
   / n:$[0-9]+ &"\n" { return parseInt(n, 10) }
 
 _ = $[ \t]*
diff --git a/lib/parser.js b/lib/parser.js
index a2d1025..5ffc1a3 100644
--- a/lib/parser.js
+++ b/lib/parser.js
@@ -612,6 +612,11 @@ function peg$parse(input, options) {
     };
   }
   function peg$f43(value) {
+    try {
+      new RegExp(`[\\${value}]`, "u");
+    } catch (er) {
+      error("Invalid Unicode property escape");
+    }
     return {
       type: "classEscape",
       value,
diff --git a/lib/peg.d.ts b/lib/peg.d.ts
index 7d43fa3..86303d5 100644
--- a/lib/peg.d.ts
+++ b/lib/peg.d.ts
@@ -657,7 +657,7 @@ export namespace parser {
       expected: Expectation[] | null,
       found: string | null,
       location: LocationRange
-    ): SyntaxError;
+    );
 
     /**
      * Format the error with associated sources.  The `location.source` should have
diff --git a/src/parser.pegjs b/src/parser.pegjs
index 016cd20..db8a3d1 100644
--- a/src/parser.pegjs
+++ b/src/parser.pegjs
@@ -527,6 +527,11 @@ AtomEscape
 
 CharacterClassEscape
   = value:$("p"i "{" UnicodePropertyValueExpression "}") {
+    try {
+      new RegExp(`[\\${value}]`, "u");
+    } catch (er) {
+      error("Invalid Unicode property escape");
+    }
     return {
       type: "classEscape",
       value,


Test patch:
diff --git a/test/unit/parser.spec.js b/test/unit/parser.spec.js
index de82fb2..550dbe0 100644
--- a/test/unit/parser.spec.js
+++ b/test/unit/parser.spec.js
@@ -1132,6 +1132,7 @@ describe("Peggy grammar parser", () => {
     expect("start = [\\p{a=]").to.failToParse();
     expect("start = [\\p{a=b]").to.failToParse();
     expect("start = [\\p{\u0661}]").to.failToParse();
+    expect("start = [\\p{foooo}]u").to.failToParse();
   });
 
   // Canonical ClassCharacterRange is "a-d".