parse method
List<TemplateAst>
parse(
- CompileDirectiveMetadata compMeta,
- String template,
- List<
CompileDirectiveMetadata?> directives, - List<
CompilePipeMetadata?> pipes, - String name,
- String templateSourceUrl,
Parses the template into a structured tree of ng.TemplateAst nodes.
This parsing is done in multiple phases:
- Parse the raw template using
ngast
package. - Post-process the raw
ast.TemplateAst
nodes. - Bind any matching Directives and Providers, converting into ng.TemplateAst nodes.
- Post-process the bound ng.TemplateAst nodes.
We will collect parse errors for each phase, and only continue to the next phase if no errors have occurred.
Implementation
List<ng.TemplateAst> parse(
CompileDirectiveMetadata compMeta,
String template,
List<CompileDirectiveMetadata?> directives,
List<CompilePipeMetadata?> pipes,
String name,
String templateSourceUrl,
) {
// This is a specific error handler that we pass to ngast that in turn
// uses mechanics similar to CompileContext; it is intentionally separate in
// order ot avoid a dependency on ngcompiler from ngast.
final exceptionHandler = AstExceptionHandler(
template,
templateSourceUrl,
name,
);
final parsedAst = ast.parse(
template,
exceptionHandler: exceptionHandler,
sourceUrl: templateSourceUrl,
);
exceptionHandler.throwErrorsIfAny();
if (parsedAst.isEmpty) return const [];
final filteredAst = _processRawTemplateNodes(
parsedAst,
template: template,
name: name,
preserveWhitespace: compMeta.template!.preserveWhitespace ?? false,
);
CompileContext.current.throwRecoverableErrors();
// TODO(b/155416570): Should it be filteredAst.first.sourceSpan?
final providedAsts = _bindDirectivesAndProviders(
directives,
compMeta,
filteredAst,
parsedAst.first.sourceSpan,
);
CompileContext.current.throwRecoverableErrors();
final processedAsts = _processBoundTemplateNodes(
compMeta,
providedAsts,
pipes,
);
CompileContext.current.throwRecoverableErrors();
return processedAsts;
}