feat(parser): support === and !== operators

Closes #1496

Closes #1500
This commit is contained in:
Pawel Kozlowski 2015-04-22 11:45:33 +02:00
parent 2e3e41ba64
commit afe0e45453
5 changed files with 32 additions and 5 deletions

View File

@ -281,6 +281,8 @@ export class Binary extends AST {
case '%' : return left % right; case '%' : return left % right;
case '==' : return left == right; case '==' : return left == right;
case '!=' : return left != right; case '!=' : return left != right;
case '===' : return left === right;
case '!==' : return left !== right;
case '<' : return left < right; case '<' : return left < right;
case '>' : return left > right; case '>' : return left > right;
case '<=' : return left <= right; case '<=' : return left <= right;

View File

@ -304,7 +304,7 @@ class _Scanner {
assert(this.peek == StringWrapper.charCodeAt(one, 0)); assert(this.peek == StringWrapper.charCodeAt(one, 0));
this.advance(); this.advance();
var str:string = one; var str:string = one;
if (this.peek == code) { while (this.peek == code) {
this.advance(); this.advance();
str += two; str += two;
} }
@ -461,6 +461,8 @@ var OPERATORS = SetWrapper.createFromList([
'=', '=',
'==', '==',
'!=', '!=',
'===',
'!==',
'<', '<',
'>', '>',
'<=', '<=',

View File

@ -277,13 +277,17 @@ class _ParseAST {
} }
parseEquality() { parseEquality() {
// '==','!=' // '==','!=','===','!=='
var result = this.parseRelational(); var result = this.parseRelational();
while (true) { while (true) {
if (this.optionalOperator('==')) { if (this.optionalOperator('==')) {
result = new Binary('==', result, this.parseRelational()); result = new Binary('==', result, this.parseRelational());
} else if (this.optionalOperator('===')) {
result = new Binary('===', result, this.parseRelational());
} else if (this.optionalOperator('!=')) { } else if (this.optionalOperator('!=')) {
result = new Binary('!=', result, this.parseRelational()); result = new Binary('!=', result, this.parseRelational());
} else if (this.optionalOperator('!==')) {
result = new Binary('!==', result, this.parseRelational());
} else { } else {
return result; return result;
} }

View File

@ -1,4 +1,4 @@
import {describe, it, expect} from 'angular2/test_lib'; import {ddescribe, describe, it, expect} from 'angular2/test_lib';
import {Lexer, Token} from 'angular2/src/change_detection/parser/lexer'; import {Lexer, Token} from 'angular2/src/change_detection/parser/lexer';
@ -150,7 +150,7 @@ export function main() {
}); });
it('should tokenize relation', function() { it('should tokenize relation', function() {
var tokens:List<Token> = lex("! == != < > <= >="); var tokens:List<Token> = lex("! == != < > <= >= === !==");
expectOperatorToken(tokens[0], 0, '!'); expectOperatorToken(tokens[0], 0, '!');
expectOperatorToken(tokens[1], 2, '=='); expectOperatorToken(tokens[1], 2, '==');
expectOperatorToken(tokens[2], 5, '!='); expectOperatorToken(tokens[2], 5, '!=');
@ -158,6 +158,8 @@ export function main() {
expectOperatorToken(tokens[4], 10, '>'); expectOperatorToken(tokens[4], 10, '>');
expectOperatorToken(tokens[5], 12, '<='); expectOperatorToken(tokens[5], 12, '<=');
expectOperatorToken(tokens[6], 15, '>='); expectOperatorToken(tokens[6], 15, '>=');
expectOperatorToken(tokens[7], 18, '===');
expectOperatorToken(tokens[8], 22, '!==');
}); });
it('should tokenize statements', function() { it('should tokenize statements', function() {

View File

@ -104,6 +104,7 @@ export function main() {
it('should parse unary ! expressions', () => { it('should parse unary ! expressions', () => {
expectEval("!true").toEqual(!true); expectEval("!true").toEqual(!true);
expectEval("!!true").toEqual(!!true); expectEval("!!true").toEqual(!!true);
expectEval("!!!true").toEqual(!!!true);
}); });
it('should parse multiplicative expressions', () => { it('should parse multiplicative expressions', () => {
@ -123,7 +124,23 @@ export function main() {
it('should parse equality expressions', () => { it('should parse equality expressions', () => {
expectEval("2==3").toEqual(2 == 3); expectEval("2==3").toEqual(2 == 3);
expectEval("2=='2'").toEqual(2 == '2');
expectEval("2=='3'").toEqual(2 == '3');
expectEval("2!=3").toEqual(2 != 3); expectEval("2!=3").toEqual(2 != 3);
expectEval("2!='3'").toEqual(2 != '3');
expectEval("2!='2'").toEqual(2 != '2');
expectEval("2!=!false").toEqual(2!=!false);
});
it('should parse strict equality expressions', () => {
expectEval("2===3").toEqual(2 === 3);
expectEval("2==='3'").toEqual(2 === '3');
expectEval("2==='2'").toEqual(2 === '2');
expectEval("2!==3").toEqual(2 !== 3);
expectEval("2!=='3'").toEqual(2 !== '3');
expectEval("2!=='2'").toEqual(2 !== '2');
expectEval("false===!true").toEqual(false===!true);
expectEval("false!==!!true").toEqual(false!==!!true);
}); });
it('should parse logicalAND expressions', () => { it('should parse logicalAND expressions', () => {
@ -608,4 +625,4 @@ export function main() {
}); });
}); });
}); });
} }