From 5baa38b82c30bb31db6e873ec17ae431825e9a47 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 9 Jun 2013 00:02:21 -0600 Subject: [PATCH] Test punctuator scanning --- src/ol/expression/lexer.js | 25 ++++++-- test/spec/ol/expression/lexer.test.js | 87 +++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/ol/expression/lexer.js b/src/ol/expression/lexer.js index a107995764..1b37a740c1 100644 --- a/src/ol/expression/lexer.js +++ b/src/ol/expression/lexer.js @@ -39,6 +39,7 @@ ol.expression.Char = { PLUS: 43, RIGHT_PAREN: 41, SINGLE_QUOTE: 39, + SLASH: 47, SPACE: 32, STAR: 42, TAB: 9, @@ -607,19 +608,17 @@ ol.expression.Lexer.prototype.scanOctalLiteral_ = function() { ol.expression.Lexer.prototype.scanPunctuator_ = function() { var code = this.getCurrentCharCode_(); - // single char punctuation + // single char punctuation that also doesn't start longer punctuation + // (we disallow assignment, so no += etc.) if (code === ol.expression.Char.DOT || code === ol.expression.Char.LEFT_PAREN || code === ol.expression.Char.RIGHT_PAREN || code === ol.expression.Char.COMMA || - code === ol.expression.Char.GREATER || - code === ol.expression.Char.LESS || code === ol.expression.Char.PLUS || code === ol.expression.Char.MINUS || code === ol.expression.Char.STAR || + code === ol.expression.Char.SLASH || code === ol.expression.Char.PERCENT || - code === ol.expression.Char.PIPE || - code === ol.expression.Char.AMPERSAND || code === ol.expression.Char.TILDE) { this.increment_(1); @@ -639,7 +638,7 @@ ol.expression.Lexer.prototype.scanPunctuator_ = function() { this.increment_(2); // check for triple - if (this.getCharCode_(1) === ol.expression.Char.EQUAL) { + if (this.getCurrentCharCode_() === ol.expression.Char.EQUAL) { this.increment_(1); return { type: ol.expression.TokenType.PUNCTUATOR, @@ -679,6 +678,20 @@ ol.expression.Lexer.prototype.scanPunctuator_ = function() { // we don't allow 4-character punctuator (>>>=) // and the allowed 3-character punctuators (!==, ===) are already consumed + // other single character punctuators + if (code === ol.expression.Char.GREATER || + code === ol.expression.Char.LESS || + code === ol.expression.Char.BANG || + code === ol.expression.Char.AMPERSAND || + code === ol.expression.Char.PIPE) { + + this.increment_(1); + return { + type: ol.expression.TokenType.PUNCTUATOR, + value: String.fromCharCode(code) + }; + } + throw new Error('Unexpected token at index ' + (this.index_ - 1) + ': ' + String.fromCharCode(code)); }; diff --git a/test/spec/ol/expression/lexer.test.js b/test/spec/ol/expression/lexer.test.js index 69aff7a6a3..52eec7ae99 100644 --- a/test/spec/ol/expression/lexer.test.js +++ b/test/spec/ol/expression/lexer.test.js @@ -147,6 +147,93 @@ describe('ol.expression.Lexer', function() { }); + describe('#scanPunctuator_()', function() { + + function scan(source) { + var lexer = new ol.expression.Lexer(source); + return lexer.scanPunctuator_(); + } + + it('works for dot', function() { + var token = scan('.'); + expect(token.value).to.be('.'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for bang', function() { + var token = scan('!'); + expect(token.value).to.be('!'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for double equal', function() { + var token = scan('=='); + expect(token.value).to.be('=='); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for triple equal', function() { + var token = scan('==='); + expect(token.value).to.be('==='); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for not double equal', function() { + var token = scan('!='); + expect(token.value).to.be('!='); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for not triple equal', function() { + var token = scan('!=='); + expect(token.value).to.be('!=='); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for logical or', function() { + var token = scan('||'); + expect(token.value).to.be('||'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for logical and', function() { + var token = scan('&&'); + expect(token.value).to.be('&&'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for plus', function() { + var token = scan('+'); + expect(token.value).to.be('+'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for minus', function() { + var token = scan('-'); + expect(token.value).to.be('-'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for star', function() { + var token = scan('*'); + expect(token.value).to.be('*'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for slash', function() { + var token = scan('/'); + expect(token.value).to.be('/'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + it('works for percent', function() { + var token = scan('%'); + expect(token.value).to.be('%'); + expect(token.type).to.be(ol.expression.TokenType.PUNCTUATOR); + }); + + }); + describe('#scanStringLiteral_()', function() { function scan(source) {