fix(parser): handle empty strings

This commit is contained in:
vsavkin 2014-11-25 18:38:28 -08:00
parent 6f889e3094
commit a3d9f0fead
3 changed files with 19 additions and 2 deletions

View File

@ -18,6 +18,16 @@ export class AST {
} }
} }
export class EmptyExpr extends AST {
eval(context) {
return null;
}
visit(visitor, args) {
//do nothing
}
}
export class ImplicitReceiver extends AST { export class ImplicitReceiver extends AST {
eval(context) { eval(context) {
return context; return context;

View File

@ -5,6 +5,7 @@ import {Lexer, EOF, Token, $PERIOD, $COLON, $SEMICOLON, $LBRACKET, $RBRACKET,
import {reflector, Reflector} from 'reflection/reflection'; import {reflector, Reflector} from 'reflection/reflection';
import { import {
AST, AST,
EmptyExpr,
ImplicitReceiver, ImplicitReceiver,
AccessMember, AccessMember,
LiteralPrimitive, LiteralPrimitive,
@ -146,7 +147,9 @@ class _ParseAST {
this.error(`Unexpected token '${this.next}'`); this.error(`Unexpected token '${this.next}'`);
} }
} }
return exprs.length == 1 ? exprs[0] : new Chain(exprs); if (exprs.length == 0) return new EmptyExpr();
if (exprs.length == 1) return exprs[0];
return new Chain(exprs);
} }
parseFormatter() { parseFormatter() {

View File

@ -150,6 +150,10 @@ export function main() {
expectEvalError("null + null").toThrowError(); expectEvalError("null + null").toThrowError();
expectEvalError("null - null").toThrowError(); expectEvalError("null - null").toThrowError();
}); });
it('should parse an empty string', () => {
expectEval('').toBeNull();
});
}); });
describe("literals", () => { describe("literals", () => {
@ -376,7 +380,7 @@ export function main() {
} }
it('should parse an empty string', () => { it('should parse an empty string', () => {
var bindings = parseTemplateBindings(""); var bindings = parseTemplateBindings('');
expect(bindings).toEqual([]); expect(bindings).toEqual([]);
}); });