diff --git a/src/babel/__snapshots__/plugin.test.js.snap b/src/babel/__snapshots__/plugin.test.js.snap index 3f990b3..8b94608 100644 --- a/src/babel/__snapshots__/plugin.test.js.snap +++ b/src/babel/__snapshots__/plugin.test.js.snap @@ -192,14 +192,14 @@ const node = function (state) { return; } - loop_3: for (;;) { + group_3: for (;;) { var y3 = state.y, x3 = state.x; if ((x = _node_expression3(state)) == null) { state.y = y3; state.x = x3; - break loop_3; + break group_3; } } } @@ -277,7 +277,7 @@ const node = function (state) { break block_2; } - loop_2: for (;;) { + group_2: for (;;) { var y2 = state.y, x2 = state.x; @@ -286,7 +286,7 @@ const node = function (state) { } else { state.y = y2; state.x = x2; - break loop_2; + break group_2; } } @@ -301,7 +301,7 @@ const node = function (state) { return; } - loop_2: for (;;) { + group_2: for (;;) { var y2 = state.y, x2 = state.x; @@ -310,11 +310,11 @@ const node = function (state) { } else { state.y = y2; state.x = x2; - break loop_2; + break group_2; } } - loop_2: for (;;) { + group_2: for (;;) { var y2 = state.y, x2 = state.x; var ln2 = node.length; @@ -325,17 +325,21 @@ const node = function (state) { state.y = y2; state.x = x2; node.length = ln2; - break loop_2; + break group_2; } - var y4 = state.y, - x4 = state.x; + group_4: { + var y4 = state.y, + x4 = state.x; - if ((x = _node_expression4(state)) != null) { - node.push(x); - } else { - state.y = y4; - state.x = x4; + if ((x = _node_expression4(state)) != null) { + node.push(x); + } else { + state.y = y4; + state.x = x4; + node.length = ln2; + break group_4; + } } if ((x = _node_expression5(state)) != null) { @@ -344,7 +348,7 @@ const node = function (state) { state.y = y2; state.x = x2; node.length = ln2; - break loop_2; + break group_2; } } } diff --git a/src/codegen.js b/src/codegen.js index 9349e2e..3160391 100644 --- a/src/codegen.js +++ b/src/codegen.js @@ -71,7 +71,7 @@ const astChild = (ast, depth, opts) => const astQuantifier = (ast, depth, opts) => { const { index, abort } = opts; const invert = `inv_${depth}`; - const loop = `loop_${depth}`; + const group = `group_${depth}`; opts = copy(opts); if (ast.capture === '!') { @@ -90,21 +90,23 @@ const astQuantifier = (ast, depth, opts) => { } else if (ast.quantifier === '*') { opts.length = 0; opts.index = depth; - opts.abort = js`break ${loop};`; + opts.abort = js`break ${group};`; child = js` - ${loop}: for (;;) { + ${group}: for (;;) { ${assignIndex(depth)} ${astChild(ast, depth, opts)} } `; } else if (ast.quantifier === '?') { opts.index = depth; - opts.abort = ''; + opts.abort = js`break ${group}`; child = js` - ${assignIndex(depth)} - ${astChild(ast, depth, opts)} + ${group}: { + ${assignIndex(depth)} + ${astChild(ast, depth, opts)} + } `; } else { child = astChild(ast, depth, opts); diff --git a/src/core.test.js b/src/core.test.js index 9333647..0892b11 100644 --- a/src/core.test.js +++ b/src/core.test.js @@ -115,6 +115,7 @@ describe('optional group then required matcher', () => { input | result ${'123'} | ${['1', '2', '3']} ${'3'} | ${['3']} + ${'23'} | ${undefined} ${'_'} | ${undefined} `('should return $result when $input is passed', ({ input, result }) => { expectToParse(node, input, result); @@ -128,6 +129,7 @@ describe('star group then required matcher', () => { ${'123'} | ${['1', '2', '3']} ${'12123'} | ${['1', '2', '1', '2', '3']} ${'3'} | ${['3']} + ${'23'} | ${undefined} ${'13'} | ${undefined} ${'_'} | ${undefined} `('should return $result when $input is passed', ({ input, result }) => { @@ -141,6 +143,7 @@ describe('plus group then required matcher', () => { input | result ${'123'} | ${['1', '2', '3']} ${'12123'} | ${['1', '2', '1', '2', '3']} + ${'23'} | ${undefined} ${'3'} | ${undefined} ${'13'} | ${undefined} ${'_'} | ${undefined}