feat(ExpressionParser): add support for this

This commit is contained in:
Victor Berchet
2016-08-04 10:14:44 -07:00
committed by Alex Rickabaugh
parent 26c9e1dc70
commit 0ca05eee45
9 changed files with 75 additions and 23 deletions

View File

@ -8,7 +8,6 @@
import {Injectable} from '@angular/core';
import * as chars from '../chars';
import {BaseException} from '../facade/exceptions';
import {NumberWrapper, StringJoiner, StringWrapper, isPresent} from '../facade/lang';
export enum TokenType {
@ -21,7 +20,7 @@ export enum TokenType {
Error
}
const KEYWORDS = ['var', 'let', 'null', 'undefined', 'true', 'false', 'if', 'else'];
const KEYWORDS = ['var', 'let', 'null', 'undefined', 'true', 'false', 'if', 'else', 'this'];
@Injectable()
export class Lexer {
@ -74,6 +73,8 @@ export class Token {
isKeywordFalse(): boolean { return this.type == TokenType.Keyword && this.strValue == 'false'; }
isKeywordThis(): boolean { return this.type == TokenType.Keyword && this.strValue == 'this'; }
isError(): boolean { return this.type == TokenType.Error; }
toNumber(): number { return this.type == TokenType.Number ? this.numValue : -1; }

View File

@ -523,6 +523,10 @@ export class _ParseAST {
this.advance();
return new LiteralPrimitive(this.span(start), false);
} else if (this.next.isKeywordThis()) {
this.advance();
return new ImplicitReceiver(this.span(start));
} else if (this.optionalCharacter(chars.$LBRACKET)) {
this.rbracketsExpected++;
const elements = this.parseExpressionList(chars.$RBRACKET);
@ -780,13 +784,7 @@ class SimpleExpressionChecker implements AstVisitor {
visitKeyedWrite(ast: KeyedWrite, context: any) { this.simple = false; }
visitAll(asts: any[]): any[] {
var res = ListWrapper.createFixedSize(asts.length);
for (var i = 0; i < asts.length; ++i) {
res[i] = asts[i].visit(this);
}
return res;
}
visitAll(asts: any[]): any[] { return asts.map(node => node.visit(this)); }
visitChain(ast: Chain, context: any) { this.simple = false; }