Regex normalization - #30
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a JSON-serializable regular-expression representation ({rx, flags}) and threads it through the grammar type system, codegen, generator, printer, tests, and the JSON grammar spec so regex terminals can be expressed unambiguously in JSON grammars.
Changes:
- Introduces
RegexPatternand a runtime type guardisRegexPattern. - Adds
RegexPatternsupport to terminal printing, parsing codegen, and sample generation. - Updates documentation/spec and adds Jest coverage for
{rx, flags}terminals.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/util.ts | Adds isRegexPattern type guard used by generator/printer/codegen. |
| src/types.ts | Defines RegexPattern and extends TerminalNode.t to accept it. |
| src/print.ts | Prints {rx, flags} terminals in a regex-like form. |
| src/generator.ts | Generates sample strings from {rx, flags} terminals via reregexp. |
| src/codegen/CodegenTerminal.ts | Converts {rx, flags} terminals into RegExp for parser codegen. |
| src/codegen/tests/CodegenTerminal.spec.ts | Adds test coverage for {rx, flags} terminal matching behavior. |
| JSON-Grammar-spec.md | Updates the JSON grammar spec to use {rx, flags} as the canonical JSON regex form. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+18
to
+19
| export const isRegexPattern = (item: any): item is RegexPattern => | ||
| typeof item === 'object' && !!item && typeof item.rx === 'string' && !Array.isArray(item); |
Comment on lines
+61
to
+62
| * - `flags` — optional flag string; allowed values: `i`, `s`, `m`, `u`, `v` | ||
| * (do **not** use `g` or `y` — the engine manages position internally) |
Comment on lines
+133
to
+136
| interface RegexPattern { | ||
| rx: string; // ECMAScript RegExp source | ||
| flags?: string; // Allowed: i, s, m, u, v (do NOT use g or y) | ||
| } |
Comment on lines
+186
to
+189
| - The canonical dialect is ECMAScript (`RegExp`). The `rx` string is the exact RegExp source. | ||
| - The engine anchors every match at the current position by wrapping with `^(?:…)` internally — do **not** add a leading `^` yourself. | ||
| - Allowed flags: `i`, `s`, `m`, `u`, `v`. Never use `g` or `y`; the engine owns position management. | ||
| - For portability across implementations, prefer the common portable core (character classes, quantifiers, groups, alternation) and avoid JS-specific features (lookbehind, named groups, `\p{…}`) unless you target JS only. |
|
🎉 This PR is included in version 1.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.