Skip to content

Commit

Permalink
fix: zaach/jison-lex#23 -- added basic support for JavaScript Arrow F…
Browse files Browse the repository at this point in the history
…unctions in the lexer spec, both simple functions, e.g. `() => 'TOKEN'` and "complex/multiline" arrow functions, e.g. `() => { statements; return 'TOKEN'; ... }`
  • Loading branch information
GerHobbelt committed Dec 13, 2017
1 parent 23ee824 commit 68420ac
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/jison-lex/regexp-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 68420ac

Please sign in to comment.