diff --git a/packages/jison-lex/regexp-lexer.js b/packages/jison-lex/regexp-lexer.js index a218c4d18..1d6691f45 100644 --- a/packages/jison-lex/regexp-lexer.js +++ b/packages/jison-lex/regexp-lexer.js @@ -304,8 +304,18 @@ function prepareRules(dict, actions, caseHelper, tokens, startConditions, opts) newRules.push(m); action = rule[1]; if (typeof action === 'function') { - // TODO: also cope with Arrow Functions (and inline those as well?) -- see also https://github.com/zaach/jison-lex/issues/23 - action = String(action).replace(/^\s*function\s*\(\)\s?\{/, '').replace(/\}\s*$/, ''); + // Also cope with Arrow Functions (and inline those as well?). + // See also https://github.com/zaach/jison-lex/issues/23 + action = String(action); + if (action.match(/^\s*function\s*\(\)\s*\{/)) { + action = action.replace(/^\s*function\s*\(\)\s*\{/, '').replace(/\}\s*$/, ''); + } else if (action.match(/^\s*\(\)\s*=>[\s\r\n]*[^\s\r\n\{]/)) { + // () => 'TOKEN' --> return 'TOKEN' + action = action.replace(/^\s*\(\)\s*=>/, 'return '); + } else if (action.match(/^\s*\(\)\s*=>[\s\r\n]*\{/)) { + // () => { statements } --> statements (ergo: 'inline' the given function) + action = action.replace(/^\s*\(\)\s*=>[\s\r\n]*\{/, '').replace(/\}\s*$/, ''); + } } action = action.replace(/return\s*'((?:\\'|[^']+)+)'/g, tokenNumberReplacement); action = action.replace(/return\s*"((?:\\"|[^"]+)+)"/g, tokenNumberReplacement);