Skip to content

Commit

Permalink
add yyless() ability to lexer (issue #84)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Mar 8, 2012
1 parent 3fe86e3 commit 7505e6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/jison/lexer.js
Expand Up @@ -173,6 +173,10 @@ RegExpLexer.prototype = {
this._more = true;
return this;
},
// retain first n characters of the match
less: function (n) {
this._input = this.match.slice(n) + this._input;
},
// displays upcoming input, i.e. for error messages
pastInput: function () {
var past = this.matched.substr(0, this.matched.length - this.match.length);
Expand Down
16 changes: 16 additions & 0 deletions tests/lexer/regexplexer.js
Expand Up @@ -665,3 +665,19 @@ exports["test case insensitivity"] = function() {

assert.equal(lexer.lex(), "CAT");
};

exports["test less"] = function() {
var dict = {
rules: [
["cat", "this.less(2); return 'CAT';" ],
["t", "return 'T';" ]
],
};
var input = "cat";

var lexer = new RegExpLexer(dict);
lexer.setInput(input);

assert.equal(lexer.lex(), "CAT");
assert.equal(lexer.lex(), "T");
};

0 comments on commit 7505e6c

Please sign in to comment.