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

Configure Feed

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

Simplify codegen variable names

+96 -99
+81 -81
src/babel/__snapshots__/plugin.test.js.snap
··· 9 9 _node_expression2 = (0, _reghex._pattern)(2); 10 10 11 11 const node = function (state) { 12 - var index_1 = state.index; 12 + var idx1 = state.index; 13 13 var node = []; 14 - var match; 14 + var x; 15 15 16 - if (match = (0, _reghex._exec)(state, _node_expression)) { 17 - node.push(match); 16 + if (x = (0, _reghex._exec)(state, _node_expression)) { 17 + node.push(x); 18 18 } else { 19 - state.index = index_1; 19 + state.index = idx1; 20 20 return; 21 21 } 22 22 23 - if (match = (0, _reghex._exec)(state, _node_expression2)) { 24 - node.push(match); 23 + if (x = (0, _reghex._exec)(state, _node_expression2)) { 24 + node.push(x); 25 25 } else { 26 - state.index = index_1; 26 + state.index = idx1; 27 27 return; 28 28 } 29 29 ··· 43 43 var _inner_expression = _pattern(/inner/); 44 44 45 45 const inner = function (state) { 46 - var index_1 = state.index; 46 + var idx1 = state.index; 47 47 var node = []; 48 - var match; 48 + var x; 49 49 50 - if (match = _exec(state, _inner_expression)) { 51 - node.push(match); 50 + if (x = _exec(state, _inner_expression)) { 51 + node.push(x); 52 52 } else { 53 - state.index = index_1; 53 + state.index = idx1; 54 54 return; 55 55 } 56 56 ··· 59 59 }; 60 60 61 61 const node = function (state) { 62 - var index_1 = state.index; 62 + var idx1 = state.index; 63 63 var node = []; 64 - var match; 64 + var x; 65 65 66 - if (match = inner(state)) { 67 - node.push(match); 66 + if (x = inner(state)) { 67 + node.push(x); 68 68 } else { 69 - state.index = index_1; 69 + state.index = idx1; 70 70 return; 71 71 } 72 72 ··· 83 83 _node_expression3 = _pattern(3); 84 84 85 85 const node = function (state) { 86 - var index_1 = state.index; 86 + var idx1 = state.index; 87 87 var node = []; 88 - var match; 88 + var x; 89 89 90 - if (match = _exec(state, _node_expression)) { 91 - node.push(match); 90 + if (x = _exec(state, _node_expression)) { 91 + node.push(x); 92 92 } else { 93 - state.index = index_1; 93 + state.index = idx1; 94 94 return; 95 95 } 96 96 97 - var length_2 = node.length; 97 + var ln2 = node.length; 98 98 99 99 alternation_3: { 100 100 block_3: { 101 - var index_3 = state.index; 101 + var idx3 = state.index; 102 102 103 - if (match = _exec(state, _node_expression2)) { 104 - node.push(match); 103 + if (x = _exec(state, _node_expression2)) { 104 + node.push(x); 105 105 } else { 106 - state.index = index_3; 107 - node.length = length_2; 106 + state.index = idx3; 107 + node.length = ln2; 108 108 break block_3; 109 109 } 110 110 111 111 break alternation_3; 112 112 } 113 113 114 - loop_3: for (var count_3 = 0; true; count_3++) { 115 - var index_3 = state.index; 114 + loop_3: for (var j3 = 0; 1; j3++) { 115 + var idx3 = state.index; 116 116 117 117 if (!_exec(state, _node_expression3)) { 118 - if (count_3) { 119 - state.index = index_3; 118 + if (j3) { 119 + state.index = idx3; 120 120 break loop_3; 121 121 } else {} 122 122 123 - state.index = index_1; 124 - node.length = length_2; 123 + state.index = idx1; 124 + node.length = ln2; 125 125 return; 126 126 } 127 127 } ··· 136 136 "import { match, tag, _exec, _pattern } from 'reghex'; 137 137 138 138 const inner = function (state) { 139 - var index_1 = state.index; 139 + var idx1 = state.index; 140 140 var node = []; 141 - var match; 141 + var x; 142 142 143 - if (match = node(state)) { 144 - node.push(match); 143 + if (x = node(state)) { 144 + node.push(x); 145 145 } else { 146 - state.index = index_1; 146 + state.index = idx1; 147 147 return; 148 148 } 149 149 ··· 152 152 }; 153 153 154 154 const node = function (state) { 155 - var index_1 = state.index; 155 + var idx1 = state.index; 156 156 var node = []; 157 - var match; 157 + var x; 158 158 159 - if (match = inner(state)) { 160 - node.push(match); 159 + if (x = inner(state)) { 160 + node.push(x); 161 161 } else { 162 - state.index = index_1; 162 + state.index = idx1; 163 163 return; 164 164 } 165 165 ··· 178 178 _node_expression5 = _pattern(5); 179 179 180 180 const node = function (state) { 181 - var index_1 = state.index; 181 + var idx1 = state.index; 182 182 var node = []; 183 - var match; 183 + var x; 184 184 185 185 alternation_2: { 186 186 block_2: { 187 - var index_2 = state.index; 187 + var idx2 = state.index; 188 188 189 - loop_2: for (var count_2 = 0; true; count_2++) { 190 - var index_2 = state.index; 189 + loop_2: for (var j2 = 0; 1; j2++) { 190 + var idx2 = state.index; 191 191 192 - if (match = _exec(state, _node_expression)) { 193 - node.push(match); 192 + if (x = _exec(state, _node_expression)) { 193 + node.push(x); 194 194 } else { 195 - if (count_2) { 196 - state.index = index_2; 195 + if (j2) { 196 + state.index = idx2; 197 197 break loop_2; 198 198 } else {} 199 199 200 - state.index = index_2; 200 + state.index = idx2; 201 201 break block_2; 202 202 } 203 203 } ··· 205 205 break alternation_2; 206 206 } 207 207 208 - loop_2: for (var count_2 = 0; true; count_2++) { 209 - var index_2 = state.index; 208 + loop_2: for (var j2 = 0; 1; j2++) { 209 + var idx2 = state.index; 210 210 211 - if (match = _exec(state, _node_expression2)) { 212 - node.push(match); 211 + if (x = _exec(state, _node_expression2)) { 212 + node.push(x); 213 213 } else { 214 - if (count_2) { 215 - state.index = index_2; 214 + if (j2) { 215 + state.index = idx2; 216 216 break loop_2; 217 217 } else {} 218 218 219 - state.index = index_1; 219 + state.index = idx1; 220 220 return; 221 221 } 222 222 } 223 223 224 - loop_2: while (true) { 225 - var index_2 = state.index; 226 - var length_2 = node.length; 224 + loop_2: for (;;) { 225 + var idx2 = state.index; 226 + var ln2 = node.length; 227 227 228 - if (match = _exec(state, _node_expression3)) { 229 - node.push(match); 228 + if (x = _exec(state, _node_expression3)) { 229 + node.push(x); 230 230 } else { 231 - state.index = index_2; 232 - node.length = length_2; 231 + state.index = idx2; 232 + node.length = ln2; 233 233 break loop_2; 234 234 } 235 235 236 - var index_4 = state.index; 236 + var idx4 = state.index; 237 237 238 - if (match = _exec(state, _node_expression4)) { 239 - node.push(match); 238 + if (x = _exec(state, _node_expression4)) { 239 + node.push(x); 240 240 } else { 241 - state.index = index_4; 241 + state.index = idx4; 242 242 } 243 243 244 - if (match = _exec(state, _node_expression5)) { 245 - node.push(match); 244 + if (x = _exec(state, _node_expression5)) { 245 + node.push(x); 246 246 } else { 247 - state.index = index_2; 248 - node.length = length_2; 247 + state.index = idx2; 248 + node.length = ln2; 249 249 break loop_2; 250 250 } 251 251 } ··· 262 262 var _inner_transform = x => x; 263 263 264 264 const first = function (state) { 265 - var index_1 = state.index; 265 + var idx1 = state.index; 266 266 var node = []; 267 - var match; 267 + var x; 268 268 node.tag = 'inner'; 269 269 return _inner_transform(node); 270 270 }; ··· 272 272 const transform = x => x; 273 273 274 274 const second = function (state) { 275 - var index_1 = state.index; 275 + var idx1 = state.index; 276 276 var node = []; 277 - var match; 277 + var x; 278 278 node.tag = 'node'; 279 279 return transform(node); 280 280 };"
+15 -18
src/codegen.js
··· 1 1 const _state = 'state'; 2 - const _match = 'match'; 3 2 const _node = 'node'; 3 + const _match = 'x'; 4 4 5 5 function js(/* arguments */) { 6 6 let body = arguments[0][0]; ··· 16 16 }; 17 17 18 18 const assignIndex = (depth) => 19 - depth ? js`var index_${depth} = ${_state}.index;` : ''; 19 + depth ? js`var idx${depth} = ${_state}.index;` : ''; 20 20 21 21 const restoreIndex = (depth) => 22 - depth ? js`${_state}.index = index_${depth};` : ''; 22 + depth ? js`${_state}.index = idx${depth};` : ''; 23 23 24 24 const abortOnCondition = (condition, hooks) => js` 25 25 if (${condition}) { 26 26 ${restoreIndex(opts.index)} 27 - ${opts.abort || ''} 27 + ${opts.abort} 28 28 } else { 29 - ${opts.onAbort || ''} 29 + ${opts.onAbort} 30 30 } 31 31 `; 32 32 33 33 const astExpression = (ast, depth, opts) => { 34 34 const restoreLength = 35 - opts.length && 36 - opts.abort && 37 - js` 38 - ${_node}.length = length_${opts.length}; 39 - `; 35 + (opts.length && opts.abort && js`${_node}.length = ln${opts.length};`) || 36 + ''; 40 37 41 38 const abort = js` 42 - ${opts.onAbort || ''} 39 + ${opts.onAbort} 43 40 ${restoreIndex(opts.index)} 44 - ${restoreLength || ''} 45 - ${opts.abort || ''} 41 + ${restoreLength} 42 + ${opts.abort} 46 43 `; 47 44 48 45 if (!opts.capture) { ··· 72 69 if (!opts.length && capture) { 73 70 opts.length = depth; 74 71 return js` 75 - ${js`var length_${depth} = ${_node}.length;`} 72 + ${js`var ln${depth} = ${_node}.length;`} 76 73 ${astSequence(ast.sequence, depth + 1, opts)} 77 74 `; 78 75 } ··· 85 82 86 83 const astQuantifier = (ast, depth, opts) => { 87 84 const { index, abort, onAbort } = opts; 88 - const invert = `invert_${depth}`; 85 + const invert = `inv_${depth}`; 89 86 const loop = `loop_${depth}`; 90 - const count = `count_${depth}`; 87 + const count = `j${depth}`; 91 88 92 89 opts = copy(opts); 93 90 if (ast.capture === '!') { ··· 107 104 `; 108 105 109 106 child = js` 110 - ${loop}: for (var ${count} = 0; true; ${count}++) { 107 + ${loop}: for (var ${count} = 0; 1; ${count}++) { 111 108 ${assignIndex(depth)} 112 109 ${astChild(ast, depth, opts)} 113 110 } ··· 119 116 opts.onAbort = ''; 120 117 121 118 child = js` 122 - ${loop}: while (true) { 119 + ${loop}: for (;;) { 123 120 ${assignIndex(depth)} 124 121 ${astChild(ast, depth, opts)} 125 122 }