Mirror: The magical sticky regex-based parser generator 🧙
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge astAlternation into astSequence

+33 -34
+33 -34
src/codegen.js
··· 65 65 if (!opts.length && capturing) { 66 66 return js` 67 67 ${js`var length_${depth} = ${_node}.length;`} 68 - ${astAlternation(ast.sequence, depth + 1, { 68 + ${astSequence(ast.sequence, depth + 1, { 69 69 ...opts, 70 70 length: depth, 71 71 capturing, ··· 73 73 `; 74 74 } 75 75 76 - return astAlternation(ast.sequence, depth + 1, { 76 + return astSequence(ast.sequence, depth + 1, { 77 77 ...opts, 78 78 capturing, 79 79 }); ··· 172 172 }; 173 173 174 174 const astSequence = (ast, depth, opts) => { 175 - const block = `block_${depth}`; 176 - const alternation = `alternation_${depth}`; 175 + const alternation = ast.alternation ? `alternation_${depth}` : ''; 177 176 178 - if (ast.alternation) { 179 - opts = { 180 - ...opts, 181 - index: depth, 182 - abort: js`break ${block};`, 183 - onAbort: '', 184 - }; 185 - } 177 + let body = ''; 178 + for (; ast; ast = ast.alternation) { 179 + const block = `block_${depth}`; 186 180 187 - let sequence = ''; 188 - for (let i = 0; i < ast.sequence.length; i++) 189 - sequence += astQuantifier(ast.sequence[i], depth, opts); 181 + let childOpts = opts; 182 + if (ast.alternation) { 183 + childOpts = { 184 + ...childOpts, 185 + index: depth, 186 + abort: js`break ${block};`, 187 + onAbort: '', 188 + }; 189 + } 190 190 191 - if (!ast.alternation) { 192 - return sequence; 193 - } 191 + let sequence = ''; 192 + for (let i = 0; i < ast.sequence.length; i++) 193 + sequence += astQuantifier(ast.sequence[i], depth, childOpts); 194 194 195 - return js` 196 - ${block}: { 197 - ${assignIndex(depth)} 198 - ${sequence} 199 - break ${alternation}; 195 + if (!ast.alternation) { 196 + body += sequence; 197 + } else { 198 + body += js` 199 + ${block}: { 200 + ${assignIndex(depth)} 201 + ${sequence} 202 + break ${alternation}; 203 + } 204 + `; 200 205 } 201 - `; 202 - }; 206 + } 203 207 204 - const astAlternation = (ast, depth, opts) => { 205 - if (!ast.alternation) return astSequence(ast, depth, opts); 206 - 207 - let sequence = ''; 208 - for (let child = ast; child; child = child.alternation) 209 - sequence += astSequence(child, depth, opts); 208 + if (!alternation) return body; 210 209 211 210 return js` 212 - alternation_${depth}: { 213 - ${sequence} 211 + ${alternation}: { 212 + ${body} 214 213 } 215 214 `; 216 215 }; ··· 221 220 var ${_node} = []; 222 221 var ${_match}; 223 222 224 - ${astAlternation(ast, 2, { 223 + ${astSequence(ast, 2, { 225 224 index: 1, 226 225 length: 0, 227 226 onAbort: '',