refactor(Parser): cleanup

This commit is contained in:
vsavkin
2014-11-06 12:00:09 -08:00
parent cb276fe412
commit 90fd1a9227
4 changed files with 259 additions and 148 deletions

View File

@ -326,6 +326,9 @@ export class FunctionCall extends AST {
eval(context) {
var obj = this.target.eval(context);
if (! (obj instanceof Function)) {
throw new BaseException(`${obj} is not a function`);
}
return FunctionWrapper.apply(obj, evalList(context, this.args));
}

View File

@ -10,6 +10,9 @@ export class ClosureMap {
}
fn(name:string) {
return new Function('o', 'args', 'return o.' + name + '.apply(o, args);');
var method = `o.${name}`;
return new Function('o', 'args',
`if (!${method}) throw new Error('"${name}" is undefined');` +
`return ${method}.apply(o, args);`);
}
}

View File

@ -115,7 +115,7 @@ class _ParseAST {
expectIdentifierOrKeywordOrString():string {
var n = this.next;
if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) {
this.error(`Unexpected token ${n}, expected identifier, or keyword, or string`)
this.error(`Unexpected token ${n}, expected identifier, keyword, or string`)
}
this.advance();
return n.toString();
@ -127,10 +127,13 @@ class _ParseAST {
var expr = this.parseFormatter();
ListWrapper.push(exprs, expr);
while (this.optionalCharacter($SEMICOLON)) {
if (this.optionalCharacter($SEMICOLON)) {
if (! this.parseAction) {
this.error("Binding expression cannot contain chained expression");
}
while (this.optionalCharacter($SEMICOLON)){} //read all semicolons
} else if (this.index < this.tokens.length) {
this.error(`Unexpected token '${this.next}'`);
}
}
return exprs.length == 1 ? exprs[0] : new Chain(exprs);
@ -163,6 +166,10 @@ class _ParseAST {
this.error(`Expression ${expression} is not assignable`);
}
if (!this.parseAction) {
this.error("Binding expression cannot contain assignments");
}
this.expectOperator('=');
result = new Assignment(result, this.parseConditional());
}
@ -303,7 +310,12 @@ class _ParseAST {
}
parsePrimary() {
if (this.next.isKeywordNull() || this.next.isKeywordUndefined()) {
if (this.optionalCharacter($LPAREN)) {
var result = this.parseFormatter();
this.expectCharacter($RPAREN);
return result;
} else if (this.next.isKeywordNull() || this.next.isKeywordUndefined()) {
this.advance();
return new LiteralPrimitive(null);