A common pattern when parsing is to use a global RegExp as lexer, and loop while the value returned by lexer.exec(source)
isn't null.
This is efficient as far as parsing goes, but it allocates an array on every iteration which isn't optimal.
Proposed alternatives (assume RegExp.success
is a symbol):
const result = []
while (lexer.exec(input, result) && result[RegExp.success]) {
// parse here
}
const result = []
const into = {result}
while (lexer.exec(input, into) && result[RegExp.success]) {
// parse here
}
In both cases, exec
would blank the previous results before matching.
Another possibility:
const result = lexer.result
while (lexer.execInResult(input) && result[RegExp.success]) {
// do the parsing here
}
...where result
would be an object with non-configurable getters into the actual results.