Skip to content

Commit

Permalink
Quantifier without quantifiable token
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 27, 2024
1 parent e04c55d commit bee04ff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/index.min.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,16 @@ function parseGroupOpen(context, state) {

function parseQuantifier({token, parent}) {
const {min, max, greedy, possessive} = token;
if (!parent.elements.length) {
const quantifiedNode = parent.elements.at(-1);
if (
// First child in `Alternative`
throw new Error('Nothing to repeat');
!quantifiedNode ||
// `\K` or `(?im-x)`
quantifiedNode.type === AstTypes.Directive
) {
throw new Error(`Quantifier requires a repeatable token`);
}
const node = createQuantifier(parent.elements.at(-1), min, max, greedy, possessive);
const node = createQuantifier(quantifiedNode, min, max, greedy, possessive);
parent.elements.pop();
return node;
}
Expand Down
1 change: 1 addition & 0 deletions src/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ function createTokenForFlagMod(raw, context) {
if (raw.endsWith(')')) {
// Replace flag x value until the end of the current group
context.replaceCurrentModX(isXOn);
// Can't remove flag directives without flags like `(?-)`; they affect following quantifiers
return createToken(TokenTypes.Directive, raw, {
kind: TokenDirectiveKinds.flags,
flags: flagChanges,
Expand Down
2 changes: 1 addition & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ const FirstPassVisitor = {
Directive({node, parent, key, container, ast, remove, replaceWith, removeAllPrevSiblings, removeAllNextSiblings}, state) {
const {kind, flags} = node;
if (kind === AstDirectiveKinds.flags) {
// Flag directive with no flags; ex: `(?-)`, `(?--)`
if (!flags.enable && !flags.disable) {
// Flag directive without flags; ex: `(?-)`, `(?--)`
remove();
} else {
const flagGroup = prepContainer(createGroup({flags}), removeAllNextSiblings());
Expand Down

0 comments on commit bee04ff

Please sign in to comment.