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
+1 -1
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> {
@@ -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(),
@@ -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);
}
}
@@ -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));
}
}
}
@@ -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)) {