angular_ast 1.0.1
angular_ast: ^1.0.1 copied to clipboard

Parser and utilities for AngularDart templates

angular_ast #


Parser and utilities for AngularDart templates.

This package is platform agnostic (no HTML or Dart VM dependencies).

Usage #

Currently in development and not stable.

import 'package:angular_ast/angular_ast.dart';

main() {
  // Create an AST tree by parsing an AngularDart template.
  var tree = parse('<button [title]="someTitle">Hello</button>');

  // Print to console.

  // Output:
  // [
  //    ElementAst <button> {
  //      properties=
  //        PropertyAst {
  //          title="ExpressionAst {someTitle}"}
  //          childNodes=TextAst {Hello}
  //      }
  //    }
  // ]

Additional flags can be passed to change the behavior of the parser:

Data typeNameDescriptionDefault Value
StringsourceUrlString describing the path of the HTML string.
booldesugarEnabled desugaring of banana-syntax, star syntax, and pipes.true
boolparseExpressionsParses Dart expressions raises exceptions if occurred.true
ExceptionHandlerexceptionHandlerSwitch to 'new RecoveringExceptionHandler()' to enable error recovery.ThrowingExceptionHandler

When using RecoveringExceptionHandler, the accumulated exceptions can be accessed through the RecoveringExceptionHandler object. Refer to the following example:

void parse(String content, String sourceUrl) {
  var exceptionHandler = new RecoveringExceptionHandler();
  var asts = parse(
    sourceUrl: sourceUrl,
    desugar: false,
    parseExpressions: false,
    exceptionHandler: exceptionHandler,
  for (AngularParserException e in exceptionHandler.exceptions) {
    // Do something with exception.