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

Configure Feed

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

Add runtime-only implementation using JIT/eval method

+36 -57
+18 -6
rollup.config.js
··· 40 40 entryFileNames: 'reghex-[name]' + ext, 41 41 dir: './dist', 42 42 exports: 'named', 43 + preserveModules: true, 43 44 externalLiveBindings: false, 44 45 sourcemap: true, 45 46 esModule: false, ··· 49 50 format, 50 51 }); 51 52 52 - export default { 53 - input: { 54 - core: './src/core.js', 55 - babel: './src/babel/plugin.js', 56 - macro: './src/babel/macro.js', 57 - }, 53 + const base = { 58 54 onwarn: () => {}, 59 55 external: () => false, 60 56 treeshake: { ··· 63 59 plugins, 64 60 output: [output('cjs', '.js'), output('esm', '.mjs')], 65 61 }; 62 + 63 + export default [ 64 + { 65 + ...base, 66 + input: { 67 + core: './src/core.js', 68 + }, 69 + }, 70 + { 71 + ...base, 72 + input: { 73 + babel: './src/babel/plugin.js', 74 + macro: './src/babel/macro.js', 75 + }, 76 + }, 77 + ];
+3 -42
src/babel/__tests__/suite.js src/core.test.js
··· 1 - import * as reghex from '../../../src/core'; 2 - import * as types from '@babel/types'; 3 - import template from '@babel/template'; 4 - import { transform } from '@babel/core'; 5 - import { makeHelpers } from '../transform'; 6 - 7 - const match = (name) => (quasis, ...expressions) => { 8 - const helpers = makeHelpers({ types, template }); 9 - 10 - let str = ''; 11 - for (let i = 0; i < quasis.length; i++) { 12 - str += quasis[i]; 13 - if (i < expressions.length) str += '${' + expressions[i].toString() + '}'; 14 - } 15 - 16 - const testPlugin = () => ({ 17 - visitor: { 18 - TaggedTemplateExpression(path) { 19 - helpers.transformMatch(path); 20 - }, 21 - }, 22 - }); 23 - 24 - const { code } = transform( 25 - `(function () { return match('${name}')\`${str}\`; })()`, 26 - { 27 - babelrc: false, 28 - presets: [], 29 - plugins: [testPlugin], 30 - } 31 - ); 32 - 33 - const argKeys = Object.keys(reghex).filter((x) => { 34 - return x.startsWith('_') || x === 'tag'; 35 - }); 36 - 37 - const args = argKeys.map((key) => reghex[key]); 38 - return new Function(...argKeys, 'return ' + code)(...args); 39 - }; 1 + import { match } from './core'; 40 2 41 3 const expectToParse = (node, input, result, lastIndex = 0) => { 42 4 const state = { input, index: 0 }; 43 - expect(node(state)).toEqual( 44 - result === undefined ? result : reghex.tag(result, 'node') 45 - ); 5 + if (result) result.tag = 'node'; 6 + expect(node(state)).toEqual(result); 46 7 47 8 // NOTE: After parsing we expect the current index to exactly match the 48 9 // sum amount of matched characters
+15 -9
src/core.js
··· 1 + import { js, astRoot } from './codegen'; 2 + import { parse as parseDSL } from './parser'; 3 + 1 4 const isStickySupported = typeof /./g.sticky === 'boolean'; 2 5 3 6 export const _pattern = (input) => { ··· 39 42 return match; 40 43 }; 41 44 42 - export const tag = (array, tag) => { 43 - array.tag = tag; 44 - return array; 45 - }; 46 - 47 45 export const parse = (pattern) => (input) => { 48 46 const state = { input, index: 0 }; 49 47 return pattern(state); 50 48 }; 51 49 52 - export const match = (_name) => { 53 - throw new TypeError( 54 - 'This match() function was not transformed. ' + 55 - 'Ensure that the Babel plugin is set up correctly and try again.' 50 + export const match = (name, transform) => (quasis, ...expressions) => { 51 + const ast = parseDSL( 52 + quasis, 53 + expressions.map((expression, i) => `_exec(state, _e${i})`) 54 + ); 55 + const makeMatcher = new Function( 56 + '_exec', 57 + '_name', 58 + '_transform', 59 + ...expressions.map((_expression, i) => `_e${i}`), 60 + 'return ' + astRoot(ast, '_name', transform ? '_transform' : null) 56 61 ); 62 + return makeMatcher(_exec, name, transform, ...expressions.map(_pattern)); 57 63 };