From 0b712cddfaf7b889349a856baec266d7bc88f089 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Sun, 12 Nov 2017 01:44:39 +0100 Subject: [PATCH] fixing the lexer `canIUse()` API method and use it (and the new `lexer.fastLex()` API) in the parser whenever appropriate. --- lib/jison-parser-kernel.js | 25 +++++++++++++++++++++++- lib/jison.js | 25 +++++++++++++++++++++++- packages/jison-lex/jison-lexer-kernel.js | 6 +++--- packages/jison-lex/regexp-lexer.js | 6 +++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/lib/jison-parser-kernel.js b/lib/jison-parser-kernel.js index 73f56b923..9d0ca4897 100644 --- a/lib/jison-parser-kernel.js +++ b/lib/jison-parser-kernel.js @@ -680,7 +680,7 @@ function parse(input, parseParams) { //_lexer_without_token_stack: - function lex() { + function stdLex() { var token = lexer.lex(); // if token isn't its numeric value, convert if (typeof token !== 'number') { @@ -690,6 +690,18 @@ function parse(input, parseParams) { return token || EOF; } + function fastLex() { + var token = lexer.fastLex(); + // if token isn't its numeric value, convert + if (typeof token !== 'number') { + token = self.symbols_[token] || token; + } + + return token || EOF; + } + + var lex = stdLex; + //_lexer_with_token_stack: // lex function that supports token stacks @@ -786,6 +798,17 @@ function parse(input, parseParams) { lexer.setInput(input, sharedState_yy); + // NOTE: we *assume* no lexer pre/post handlers are set up *after* + // this initial `setInput()` call: hence we can now check and decide + // whether we'll go with the standard, slower, lex() API or the + // `fast_lex()` one: + if (typeof lexer.canIUse === 'function') { + var lexerInfo = lexer.canIUse(); + if (lexerInfo.fastLex && typeof fastLex === 'function') { + lex = fastLex; + } + } + yyloc = lexer.yylloc; lstack[sp] = yyloc; vstack[sp] = null; diff --git a/lib/jison.js b/lib/jison.js index 9f820b1b0..ab4f68d91 100755 --- a/lib/jison.js +++ b/lib/jison.js @@ -7240,7 +7240,7 @@ parser.parse = `function parse(input, parseParams) { //_lexer_without_token_stack: - function lex() { + function stdLex() { var token = lexer.lex(); // if token isn't its numeric value, convert if (typeof token !== 'number') { @@ -7250,6 +7250,18 @@ parser.parse = `function parse(input, parseParams) { return token || EOF; } + function fastLex() { + var token = lexer.fastLex(); + // if token isn't its numeric value, convert + if (typeof token !== 'number') { + token = self.symbols_[token] || token; + } + + return token || EOF; + } + + var lex = stdLex; + //_lexer_with_token_stack: // lex function that supports token stacks @@ -7346,6 +7358,17 @@ parser.parse = `function parse(input, parseParams) { lexer.setInput(input, sharedState_yy); + // NOTE: we *assume* no lexer pre/post handlers are set up *after* + // this initial \`setInput()\` call: hence we can now check and decide + // whether we'll go with the standard, slower, lex() API or the + // \`fast_lex()\` one: + if (typeof lexer.canIUse === 'function') { + var lexerInfo = lexer.canIUse(); + if (lexerInfo.fastLex && typeof fastLex === 'function') { + lex = fastLex; + } + } + yyloc = lexer.yylloc; lstack[sp] = yyloc; vstack[sp] = null; diff --git a/packages/jison-lex/jison-lexer-kernel.js b/packages/jison-lex/jison-lexer-kernel.js index 1974e4d98..b31482bac 100644 --- a/packages/jison-lex/jison-lexer-kernel.js +++ b/packages/jison-lex/jison-lexer-kernel.js @@ -1123,16 +1123,16 @@ */ canIUse: function lexer_canIUse() { var rv = { - fast_lex: !( + fastLex: !( typeof this.pre_lex === 'function' || typeof this.options.pre_lex === 'function' || (this.yy && typeof this.yy.pre_lex === 'function') || (this.yy && typeof this.yy.post_lex === 'function') || typeof this.options.post_lex === 'function' || typeof this.post_lex === 'function' - ), + ) && typeof this.fastLex === 'function', }; - return r; + return rv; }, diff --git a/packages/jison-lex/regexp-lexer.js b/packages/jison-lex/regexp-lexer.js index b75d81a93..a218c4d18 100644 --- a/packages/jison-lex/regexp-lexer.js +++ b/packages/jison-lex/regexp-lexer.js @@ -2416,16 +2416,16 @@ return `{ */ canIUse: function lexer_canIUse() { var rv = { - fast_lex: !( + fastLex: !( typeof this.pre_lex === 'function' || typeof this.options.pre_lex === 'function' || (this.yy && typeof this.yy.pre_lex === 'function') || (this.yy && typeof this.yy.post_lex === 'function') || typeof this.options.post_lex === 'function' || typeof this.post_lex === 'function' - ), + ) && typeof this.fastLex === 'function', }; - return r; + return rv; },