/** * The MIT License (MIT) * Copyright (c) 2017-present Dmitry Soshnikov */ 'use strict'; /** * Helper `gen` function calls node type handler. */ function gen(node) { return node ? generator[node.type](node) : ''; } /** * AST handler. */ var generator = { RegExp: function RegExp(node) { return '/' + gen(node.body) + '/' + node.flags; }, Alternative: function Alternative(node) { return (node.expressions || []).map(gen).join(''); }, Disjunction: function Disjunction(node) { return gen(node.left) + '|' + gen(node.right); }, Group: function Group(node) { var expression = gen(node.expression); if (node.capturing) { // A named group. if (node.name) { return '(?<' + (node.nameRaw || node.name) + '>' + expression + ')'; } return '(' + expression + ')'; } return '(?:' + expression + ')'; }, Backreference: function Backreference(node) { switch (node.kind) { case 'number': return '\\' + node.reference; case 'name': return '\\k<' + (node.referenceRaw || node.reference) + '>'; default: throw new TypeError('Unknown Backreference kind: ' + node.kind); } }, Assertion: function Assertion(node) { switch (node.kind) { case '^': case '$': case '\\b': case '\\B': return node.kind; case 'Lookahead': { var assertion = gen(node.assertion); if (node.negative) { return '(?!' + assertion + ')'; } return '(?=' + assertion + ')'; } case 'Lookbehind': { var _assertion = gen(node.assertion); if (node.negative) { return '(?