feat(compiler): pass compilation unit to the parser

This commit is contained in:
vsavkin 2014-12-10 19:21:15 -08:00
parent d985045983
commit d5fcac4d7a
17 changed files with 109 additions and 72 deletions

View File

@ -108,16 +108,16 @@ function setUpChangeDetection() {
var parentRange = parentProto.instantiate(dispatcher, MapWrapper.create());
var astWithSource = [
parser.parseBinding('field0'),
parser.parseBinding('field1'),
parser.parseBinding('field2'),
parser.parseBinding('field3'),
parser.parseBinding('field4'),
parser.parseBinding('field5'),
parser.parseBinding('field6'),
parser.parseBinding('field7'),
parser.parseBinding('field8'),
parser.parseBinding('field9')
parser.parseBinding('field0', null),
parser.parseBinding('field1', null),
parser.parseBinding('field2', null),
parser.parseBinding('field3', null),
parser.parseBinding('field4', null),
parser.parseBinding('field5', null),
parser.parseBinding('field6', null),
parser.parseBinding('field7', null),
parser.parseBinding('field8', null),
parser.parseBinding('field9', null)
];
function proto(i) {

View File

@ -385,8 +385,10 @@ export class FunctionCall extends AST {
export class ASTWithSource extends AST {
ast:AST;
source:string;
constructor(ast:AST, source:string) {
location:string;
constructor(ast:AST, source:string, location:string) {
this.source = source;
this.location = location;
this.ast = ast;
}
@ -407,7 +409,7 @@ export class ASTWithSource extends AST {
}
toString():string {
return this.source;
return `${this.source} in ${this.location}`;
}
}

View File

@ -36,32 +36,34 @@ export class Parser {
this._reflector = isPresent(providedReflector) ? providedReflector : reflector;
}
parseAction(input:string):ASTWithSource {
parseAction(input:string, location:any):ASTWithSource {
var tokens = this._lexer.tokenize(input);
var ast = new _ParseAST(input, tokens, this._reflector, true).parseChain();
return new ASTWithSource(ast, input);
var ast = new _ParseAST(input, location, tokens, this._reflector, true).parseChain();
return new ASTWithSource(ast, input, location);
}
parseBinding(input:string):ASTWithSource {
parseBinding(input:string, location:any):ASTWithSource {
var tokens = this._lexer.tokenize(input);
var ast = new _ParseAST(input, tokens, this._reflector, false).parseChain();
return new ASTWithSource(ast, input);
var ast = new _ParseAST(input, location, tokens, this._reflector, false).parseChain();
return new ASTWithSource(ast, input, location);
}
parseTemplateBindings(input:string):List<TemplateBinding> {
parseTemplateBindings(input:string, location:any):List<TemplateBinding> {
var tokens = this._lexer.tokenize(input);
return new _ParseAST(input, tokens, this._reflector, false).parseTemplateBindings();
return new _ParseAST(input, location, tokens, this._reflector, false).parseTemplateBindings();
}
}
class _ParseAST {
input:string;
location:any;
tokens:List<Token>;
reflector:Reflector;
parseAction:boolean;
index:int;
constructor(input:string, tokens:List, reflector:Reflector, parseAction:boolean) {
constructor(input:string, location:any, tokens:List, reflector:Reflector, parseAction:boolean) {
this.input = input;
this.location = location;
this.tokens = tokens;
this.index = 0;
this.reflector = reflector;
@ -451,7 +453,7 @@ class _ParseAST {
var start = this.inputIndex;
var ast = this.parseExpression();
var source = this.input.substring(start, this.inputIndex);
expression = new ASTWithSource(ast, source);
expression = new ASTWithSource(ast, source, this.location);
}
}
ListWrapper.push(bindings, new TemplateBinding(key, name, expression));
@ -469,6 +471,6 @@ class _ParseAST {
? `at column ${this.tokens[index].index + 1} in`
: `at the end of the expression`;
throw new BaseException(`Parser Error: ${message} ${location} [${this.input}]`);
throw new BaseException(`Parser Error: ${message} ${location} [${this.input}] in ${this.location}`);
}
}

View File

@ -20,7 +20,7 @@ import {Record} from 'change_detection/record';
export function main() {
function ast(exp:string) {
var parser = new Parser(new Lexer());
return parser.parseBinding(exp);
return parser.parseBinding(exp, 'location');
}
function createChangeDetector(memo:string, exp:string, context = null, formatters = null,
@ -451,7 +451,7 @@ export function main() {
expect(() => {
var cd = new ChangeDetector(rr, true);
cd.detectChanges();
}).toThrowError(new RegExp("Expression 'a' has changed after it was checked"));
}).toThrowError(new RegExp("Expression 'a in location' has changed after it was checked"));
});
});
});

View File

@ -35,16 +35,16 @@ export function main() {
return new Parser(new Lexer(), reflector);
}
function parseAction(text) {
return createParser().parseAction(text).ast;
function parseAction(text, location = null) {
return createParser().parseAction(text, location);
}
function parseBinding(text) {
return createParser().parseBinding(text).ast;
function parseBinding(text, location = null) {
return createParser().parseBinding(text, location);
}
function parseTemplateBindings(text) {
return createParser().parseTemplateBindings(text);
function parseTemplateBindings(text, location = null) {
return createParser().parseTemplateBindings(text, location);
}
function expectEval(text, passedInContext = null) {
@ -340,7 +340,7 @@ export function main() {
it('should pass exceptions', () => {
expect(() => {
createParser().parseAction('a()').ast.eval(td(() => {throw new BaseException("boo to you")}));
parseAction('a()').eval(td(() => {throw new BaseException("boo to you")}));
}).toThrowError('boo to you');
});
@ -352,20 +352,24 @@ export function main() {
});
it('should store the source in the result', () => {
expect(createParser().parseAction('someExpr').source).toBe('someExpr');
expect(parseAction('someExpr').source).toBe('someExpr');
});
it('should store the passed-in location', () => {
expect(parseAction('someExpr', 'location').location).toBe('location');
});
});
describe("parseBinding", () => {
describe("formatters", () => {
it("should parse formatters", () => {
var exp = parseBinding("'Foo'|uppercase");
var exp = parseBinding("'Foo'|uppercase").ast;
expect(exp).toBeAnInstanceOf(Formatter);
expect(exp.name).toEqual("uppercase");
});
it("should parse formatters with args", () => {
var exp = parseBinding("1|increment:2");
var exp = parseBinding("1|increment:2").ast;
expect(exp).toBeAnInstanceOf(Formatter);
expect(exp.name).toEqual("increment");
expect(exp.args[0]).toBeAnInstanceOf(LiteralPrimitive);
@ -380,7 +384,11 @@ export function main() {
});
it('should store the source in the result', () => {
expect(createParser().parseBinding('someExpr').source).toBe('someExpr');
expect(parseBinding('someExpr').source).toBe('someExpr');
});
it('should store the passed-in location', () => {
expect(parseBinding('someExpr', 'location').location).toBe('location');
});
it('should throw on chain expressions', () => {
@ -409,7 +417,7 @@ export function main() {
function exprAsts(templateBindings) {
return ListWrapper.map(templateBindings,
(binding) => isPresent(binding.expression) ? binding.expression.ast : null );
(binding) => isPresent(binding.expression) ? binding.expression : null );
}
it('should parse an empty string', () => {
@ -482,6 +490,11 @@ export function main() {
expect(bindings[0].expression.source).toEqual('1');
expect(bindings[1].expression.source).toEqual('2');
});
it('should store the passed-in location', () => {
var bindings = parseTemplateBindings("a 1,b 2", 'location');
expect(bindings[0].expression.location).toEqual('location');
});
});
});
}

View File

@ -62,7 +62,7 @@ export class Compiler {
for (var i=0; i<directives.length; i++) {
ListWrapper.push(annotatedDirectives, this._reader.annotatedType(directives[i]));
}
return createDefaultSteps(this._parser, annotatedDirectives);
return createDefaultSteps(this._parser, component, annotatedDirectives);
}
compile(component:Type, templateRoot:Element = null):Promise<ProtoView> {

View File

@ -9,17 +9,22 @@ import {ElementBindingMarker} from './element_binding_marker';
import {ProtoViewBuilder} from './proto_view_builder';
import {ProtoElementInjectorBuilder} from './proto_element_injector_builder';
import {ElementBinderBuilder} from './element_binder_builder';
import {AnnotatedType} from 'core/compiler/annotated_type';
import {stringify} from 'facade/lang';
/**
* Default steps used for compiling a template.
* Takes in an HTMLElement and produces the ProtoViews,
* ProtoElementInjectors and ElementBinders in the end.
*/
export function createDefaultSteps(parser:Parser, directives: List<AnnotatedType>) {
export function createDefaultSteps(parser:Parser, compiledComponent: AnnotatedType,
directives: List<AnnotatedType>) {
var compilationUnit = stringify(compiledComponent.type);
return [
new ViewSplitter(parser),
new TextInterpolationParser(parser),
new PropertyBindingParser(parser),
new ViewSplitter(parser, compilationUnit),
new TextInterpolationParser(parser, compilationUnit),
new PropertyBindingParser(parser, compilationUnit),
new DirectiveParser(directives),
new ElementBindingMarker(),
new ProtoViewBuilder(),

View File

@ -3,6 +3,7 @@ import {MapWrapper} from 'facade/collection';
import {TemplateElement} from 'facade/dom';
import {Parser} from 'change_detection/parser/parser';
import {AST} from 'change_detection/parser/ast';
import {ExpressionWithSource} from 'change_detection/parser/ast';
import {CompileStep} from './compile_step';
@ -24,8 +25,10 @@ var BIND_NAME_REGEXP = RegExpWrapper.create('^(?:(?:(bind)|(let)|(on))-(.+))|\\[
*/
export class PropertyBindingParser extends CompileStep {
_parser:Parser;
constructor(parser:Parser) {
_compilationUnit:any;
constructor(parser:Parser, compilationUnit:any) {
this._parser = parser;
this._compilationUnit = compilationUnit;
}
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
@ -35,7 +38,7 @@ export class PropertyBindingParser extends CompileStep {
if (isPresent(bindParts)) {
if (isPresent(bindParts[1])) {
// match: bind-prop
current.addPropertyBinding(bindParts[4], this._parser.parseBinding(attrValue));
current.addPropertyBinding(bindParts[4], this._parseBinding(attrValue));
} else if (isPresent(bindParts[2])) {
// match: let-prop
// Note: We assume that the ViewSplitter already did its work, i.e. template directive should
@ -46,20 +49,28 @@ export class PropertyBindingParser extends CompileStep {
current.addVariableBinding(bindParts[4], attrValue);
} else if (isPresent(bindParts[3])) {
// match: on-prop
current.addEventBinding(bindParts[4], this._parser.parseAction(attrValue));
current.addEventBinding(bindParts[4], this._parseAction(attrValue));
} else if (isPresent(bindParts[5])) {
// match: [prop]
current.addPropertyBinding(bindParts[5], this._parser.parseBinding(attrValue));
current.addPropertyBinding(bindParts[5], this._parseBinding(attrValue));
} else if (isPresent(bindParts[6])) {
// match: (prop)
current.addEventBinding(bindParts[6], this._parser.parseBinding(attrValue));
current.addEventBinding(bindParts[6], this._parseBinding(attrValue));
}
} else {
var expression = interpolationToExpression(attrValue);
if (isPresent(expression)) {
current.addPropertyBinding(attrName, this._parser.parseBinding(expression));
current.addPropertyBinding(attrName, this._parseBinding(expression));
}
}
});
}
_parseBinding(input:string):AST {
return this._parser.parseBinding(input, this._compilationUnit);
}
_parseAction(input:string):AST {
return this._parser.parseAction(input, this._compilationUnit);
}
}

View File

@ -47,8 +47,10 @@ export function interpolationToExpression(value:string):string {
*/
export class TextInterpolationParser extends CompileStep {
_parser:Parser;
constructor(parser:Parser) {
_compilationUnit:any;
constructor(parser:Parser, compilationUnit:any) {
this._parser = parser;
this._compilationUnit = compilationUnit;
}
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
@ -66,7 +68,7 @@ export class TextInterpolationParser extends CompileStep {
var expression = interpolationToExpression(node.nodeValue);
if (isPresent(expression)) {
DOM.setText(node, ' ');
pipelineElement.addTextNodeBinding(nodeIndex, this._parser.parseBinding(expression));
pipelineElement.addTextNodeBinding(nodeIndex, this._parser.parseBinding(expression, this._compilationUnit));
}
}
}

View File

@ -31,8 +31,10 @@ import {CompileControl} from './compile_control';
*/
export class ViewSplitter extends CompileStep {
_parser:Parser;
constructor(parser:Parser) {
_compilationUnit:any;
constructor(parser:Parser, compilationUnit:any) {
this._parser = parser;
this._compilationUnit = compilationUnit;
}
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
@ -74,7 +76,7 @@ export class ViewSplitter extends CompileStep {
}
_parseTemplateBindings(templateBindings:string, compileElement:CompileElement) {
var bindings = this._parser.parseTemplateBindings(templateBindings);
var bindings = this._parser.parseTemplateBindings(templateBindings, this._compilationUnit);
for (var i=0; i<bindings.length; i++) {
var binding = bindings[i];
if (isPresent(binding.name)) {

View File

@ -34,7 +34,7 @@ export function main() {
return new CompilePipeline([new MockStep((parent, current, control) => {
if (isPresent(propertyBindings)) {
StringMapWrapper.forEach(propertyBindings, (v, k) => {
current.addPropertyBinding(k, parser.parseBinding(v));
current.addPropertyBinding(k, parser.parseBinding(v, null));
});
}
if (isPresent(variableBindings)) {

View File

@ -35,21 +35,21 @@ export function main() {
var hasBinding = false;
if (isPresent(current.element.getAttribute('text-binding'))) {
MapWrapper.forEach(textNodeBindings, (v,k) => {
current.addTextNodeBinding(k, parser.parseBinding(v));
current.addTextNodeBinding(k, parser.parseBinding(v, null));
});
hasBinding = true;
}
if (isPresent(current.element.getAttribute('prop-binding'))) {
if (isPresent(propertyBindings)) {
MapWrapper.forEach(propertyBindings, (v,k) => {
current.addPropertyBinding(k, parser.parseBinding(v));
current.addPropertyBinding(k, parser.parseBinding(v, null));
});
}
hasBinding = true;
}
if (isPresent(current.element.getAttribute('event-binding'))) {
MapWrapper.forEach(eventBindings, (v,k) => {
current.addEventBinding(k, parser.parseAction(v));
current.addEventBinding(k, parser.parseAction(v, null));
});
hasBinding = true;
}

View File

@ -10,7 +10,7 @@ import {Lexer} from 'change_detection/parser/lexer';
export function main() {
describe('PropertyBindingParser', () => {
function createPipeline() {
return new CompilePipeline([new PropertyBindingParser(new Parser(new Lexer()))]);
return new CompilePipeline([new PropertyBindingParser(new Parser(new Lexer()), null)]);
}
it('should detect [] syntax', () => {

View File

@ -10,7 +10,7 @@ import {Lexer} from 'change_detection/parser/lexer';
export function main() {
describe('TextInterpolationParser', () => {
function createPipeline() {
return new CompilePipeline([new TextInterpolationParser(new Parser(new Lexer()))]);
return new CompilePipeline([new TextInterpolationParser(new Parser(new Lexer()), null)]);
}
it('should find text interpolation in normal elements', () => {

View File

@ -13,7 +13,7 @@ export function main() {
describe('ViewSplitter', () => {
function createPipeline() {
return new CompilePipeline([new ViewSplitter(new Parser(new Lexer()))]);
return new CompilePipeline([new ViewSplitter(new Parser(new Lexer()), null)]);
}
it('should mark root elements as viewRoot', () => {

View File

@ -105,7 +105,7 @@ export function main() {
it('should collect property bindings on the root element if it has the ng-binding class', () => {
var pv = new ProtoView(templateAwareCreateElement('<div [prop]="a" class="ng-binding"></div>'), new ProtoRecordRange());
pv.bindElement(null);
pv.bindElementProperty(parser.parseBinding('a').ast, 'prop', reflector.setter('prop'));
pv.bindElementProperty(parser.parseBinding('a', null), 'prop', reflector.setter('prop'));
var view = pv.instantiate(null);
view.hydrate(null, null, null);
@ -117,7 +117,7 @@ export function main() {
var pv = new ProtoView(templateAwareCreateElement('<div><span></span><span class="ng-binding"></span></div>'),
new ProtoRecordRange());
pv.bindElement(null);
pv.bindElementProperty(parser.parseBinding('b').ast, 'a', reflector.setter('a'));
pv.bindElementProperty(parser.parseBinding('b', null), 'a', reflector.setter('a'));
var view = pv.instantiate(null);
view.hydrate(null, null, null);
@ -132,8 +132,8 @@ export function main() {
it('should collect text nodes under the root element', () => {
var pv = new ProtoView(templateAwareCreateElement('<div class="ng-binding">{{}}<span></span>{{}}</div>'), new ProtoRecordRange());
pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('a'));
pv.bindTextNode(2, parser.parseBinding('b'));
pv.bindTextNode(0, parser.parseBinding('a', null));
pv.bindTextNode(2, parser.parseBinding('b', null));
var view = pv.instantiate(null);
view.hydrate(null, null, null);
@ -146,7 +146,7 @@ export function main() {
var pv = new ProtoView(templateAwareCreateElement('<div><span> </span><span class="ng-binding">{{}}</span></div>'),
new ProtoRecordRange());
pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('b'));
pv.bindTextNode(0, parser.parseBinding('b', null));
var view = pv.instantiate(null);
view.hydrate(null, null, null);
@ -358,7 +358,7 @@ export function main() {
var pv = new ProtoView(createElement('<div class="ng-binding">{{}}</div>'),
new ProtoRecordRange());
pv.bindElement(null);
pv.bindTextNode(0, parser.parseBinding('foo'));
pv.bindTextNode(0, parser.parseBinding('foo', null));
createViewAndChangeDetector(pv);
ctx.foo = 'buz';
@ -370,7 +370,7 @@ export function main() {
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'),
new ProtoRecordRange());
pv.bindElement(null);
pv.bindElementProperty(parser.parseBinding('foo').ast, 'id', reflector.setter('id'));
pv.bindElementProperty(parser.parseBinding('foo', null), 'id', reflector.setter('id'));
createViewAndChangeDetector(pv);
ctx.foo = 'buz';
@ -382,7 +382,7 @@ export function main() {
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'),
new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 0, [SomeDirective]));
pv.bindDirectiveProperty(0, parser.parseBinding('foo'), 'prop', reflector.setter('prop'), false);
pv.bindDirectiveProperty(0, parser.parseBinding('foo', null), 'prop', reflector.setter('prop'), false);
createViewAndChangeDetector(pv);
ctx.foo = 'buz';
@ -395,8 +395,8 @@ export function main() {
new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 0, [DirectiveImplementingOnChange]));
pv.bindDirectiveProperty( 0, parser.parseBinding('a'), 'a', reflector.setter('a'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('b'), 'b', reflector.setter('b'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('a', null), 'a', reflector.setter('a'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('b', null), 'b', reflector.setter('b'), false);
createViewAndChangeDetector(pv);
ctx.a = 100;
@ -412,8 +412,8 @@ export function main() {
new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 0, [DirectiveImplementingOnChange]));
pv.bindDirectiveProperty( 0, parser.parseBinding('a').ast, 'a', reflector.setter('a'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('b').ast, 'b', reflector.setter('b'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('a', null), 'a', reflector.setter('a'), false);
pv.bindDirectiveProperty( 0, parser.parseBinding('b', null), 'b', reflector.setter('b'), false);
createViewAndChangeDetector(pv);
ctx.a = 0;

View File

@ -125,7 +125,7 @@ export function main() {
var pv = new ProtoView(createElement('<div class="ng-binding">{{}}</div>'),
new ProtoRecordRange());
pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective]));
pv.bindTextNode(0, parser.parseBinding('foo').ast);
pv.bindTextNode(0, parser.parseBinding('foo', null));
fancyView = pv.instantiate(null);
});