Test punctuator scanning
This commit is contained in:
@@ -39,6 +39,7 @@ ol.expression.Char = {
|
|||||||
PLUS: 43,
|
PLUS: 43,
|
||||||
RIGHT_PAREN: 41,
|
RIGHT_PAREN: 41,
|
||||||
SINGLE_QUOTE: 39,
|
SINGLE_QUOTE: 39,
|
||||||
|
SLASH: 47,
|
||||||
SPACE: 32,
|
SPACE: 32,
|
||||||
STAR: 42,
|
STAR: 42,
|
||||||
TAB: 9,
|
TAB: 9,
|
||||||
@@ -607,19 +608,17 @@ ol.expression.Lexer.prototype.scanOctalLiteral_ = function() {
|
|||||||
ol.expression.Lexer.prototype.scanPunctuator_ = function() {
|
ol.expression.Lexer.prototype.scanPunctuator_ = function() {
|
||||||
var code = this.getCurrentCharCode_();
|
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 ||
|
if (code === ol.expression.Char.DOT ||
|
||||||
code === ol.expression.Char.LEFT_PAREN ||
|
code === ol.expression.Char.LEFT_PAREN ||
|
||||||
code === ol.expression.Char.RIGHT_PAREN ||
|
code === ol.expression.Char.RIGHT_PAREN ||
|
||||||
code === ol.expression.Char.COMMA ||
|
code === ol.expression.Char.COMMA ||
|
||||||
code === ol.expression.Char.GREATER ||
|
|
||||||
code === ol.expression.Char.LESS ||
|
|
||||||
code === ol.expression.Char.PLUS ||
|
code === ol.expression.Char.PLUS ||
|
||||||
code === ol.expression.Char.MINUS ||
|
code === ol.expression.Char.MINUS ||
|
||||||
code === ol.expression.Char.STAR ||
|
code === ol.expression.Char.STAR ||
|
||||||
|
code === ol.expression.Char.SLASH ||
|
||||||
code === ol.expression.Char.PERCENT ||
|
code === ol.expression.Char.PERCENT ||
|
||||||
code === ol.expression.Char.PIPE ||
|
|
||||||
code === ol.expression.Char.AMPERSAND ||
|
|
||||||
code === ol.expression.Char.TILDE) {
|
code === ol.expression.Char.TILDE) {
|
||||||
|
|
||||||
this.increment_(1);
|
this.increment_(1);
|
||||||
@@ -639,7 +638,7 @@ ol.expression.Lexer.prototype.scanPunctuator_ = function() {
|
|||||||
this.increment_(2);
|
this.increment_(2);
|
||||||
|
|
||||||
// check for triple
|
// check for triple
|
||||||
if (this.getCharCode_(1) === ol.expression.Char.EQUAL) {
|
if (this.getCurrentCharCode_() === ol.expression.Char.EQUAL) {
|
||||||
this.increment_(1);
|
this.increment_(1);
|
||||||
return {
|
return {
|
||||||
type: ol.expression.TokenType.PUNCTUATOR,
|
type: ol.expression.TokenType.PUNCTUATOR,
|
||||||
@@ -679,6 +678,20 @@ ol.expression.Lexer.prototype.scanPunctuator_ = function() {
|
|||||||
// we don't allow 4-character punctuator (>>>=)
|
// we don't allow 4-character punctuator (>>>=)
|
||||||
// and the allowed 3-character punctuators (!==, ===) are already consumed
|
// 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) +
|
throw new Error('Unexpected token at index ' + (this.index_ - 1) +
|
||||||
': ' + String.fromCharCode(code));
|
': ' + String.fromCharCode(code));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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() {
|
describe('#scanStringLiteral_()', function() {
|
||||||
|
|
||||||
function scan(source) {
|
function scan(source) {
|
||||||
|
|||||||
Reference in New Issue
Block a user