parse method

List<TemplateAst> parse(
  1. CompileDirectiveMetadata compMeta,
  2. String template,
  3. List<CompileDirectiveMetadata?> directives,
  4. List<CompilePipeMetadata?> pipes,
  5. String name,
  6. String templateSourceUrl,
)

Parses the template into a structured tree of ng.TemplateAst nodes.

This parsing is done in multiple phases:

  1. Parse the raw template using angular_ast package.
  2. Post-process the raw ast.TemplateAst nodes.
  3. Bind any matching Directives and Providers, converting into ng.TemplateAst nodes.
  4. 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 angular_ast that in turn
  // uses mechanics similar to CompileContext; it is intentionally separate in
  // order ot avoid a dependency on angular_compiler from angular_ast.
  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;
}