Skip to content

Regex normalization - #30

Merged
streamich merged 2 commits into
masterfrom
regex-norm
Jun 8, 2026
Merged

streamich merged 2 commits into
masterfrom
regex-norm

Conversation

@streamich

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings June 8, 2026 11:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 RegexPattern and a runtime type guard isRegexPattern.
  • Adds RegexPattern support 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 thread src/util.ts
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 thread src/types.ts
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 thread JSON-Grammar-spec.md
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 thread JSON-Grammar-spec.md
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.
@streamich
streamich merged commit 045a994 into master Jun 8, 2026
3 checks passed
@streamich
streamich deleted the regex-norm branch June 8, 2026 12:22
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants