|
1 |
| -See https://github.com/angulardart-community for current updates on this project. |
| 1 | +# ngast |
| 2 | + |
| 3 | +<!-- Badges --> |
| 4 | + |
| 5 | +[](https://pub.dartlang.org/packages/ngast) |
| 6 | +[](https://github.com/angulardart-community/angular/actions/workflows/dart.yml) |
| 7 | +[](https://gitter.im/angulardart/community) |
| 8 | + |
| 9 | +Parser and utilities for [AngularDart][gh_angular_dart] templates. |
| 10 | + |
| 11 | +[gh_angular_dart]: https://github.com/angulardart-community/angular |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +*Currently in development* and **not stable**. |
| 16 | + |
| 17 | +```dart |
| 18 | +import 'package:ngast/ngast.dart'; |
| 19 | +
|
| 20 | +main() { |
| 21 | + // Create an AST tree by parsing an AngularDart template. |
| 22 | + var tree = parse('<button [title]="someTitle">Hello</button>'); |
| 23 | +
|
| 24 | + // Print to console. |
| 25 | + print(tree); |
| 26 | +
|
| 27 | + // Output: |
| 28 | + // [ |
| 29 | + // ElementAst <button> { |
| 30 | + // properties= |
| 31 | + // PropertyAst { |
| 32 | + // title="ExpressionAst {someTitle}"} |
| 33 | + // childNodes=TextAst {Hello} |
| 34 | + // } |
| 35 | + // } |
| 36 | + // ] |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Additional flags can be passed to change the behavior of the parser: |
| 41 | + |
| 42 | +| Data type | Name | Description | Default Value | |
| 43 | +|------------|------------|--------------|---------------| |
| 44 | +| `String` | _sourceUrl_ | String describing the path of the HTML string. | | |
| 45 | +| `bool` | _desugar_ | Enabled desugaring of banana-syntax, star syntax, and pipes. | true | |
| 46 | +| `bool` | _parseExpressions_ | Parses Dart expressions raises exceptions if occurred. | true | |
| 47 | +| `ExceptionHandler` | _exceptionHandler_ | Switch to 'new RecoveringExceptionHandler()' to enable error recovery. | ThrowingExceptionHandler | |
| 48 | + |
| 49 | +When using RecoveringExceptionHandler, the accumulated exceptions can be |
| 50 | +accessed through the RecoveringExceptionHandler object. Refer to the following |
| 51 | +example: |
| 52 | + |
| 53 | +```dart |
| 54 | +void parse(String content, String sourceUrl) { |
| 55 | + var exceptionHandler = new RecoveringExceptionHandler(); |
| 56 | + var asts = parse( |
| 57 | + content, |
| 58 | + sourceUrl: sourceUrl, |
| 59 | + desugar: false, |
| 60 | + parseExpressions: false, |
| 61 | + exceptionHandler: exceptionHandler, |
| 62 | + ); |
| 63 | + for (AngularParserException e in exceptionHandler.exceptions) { |
| 64 | + // Do something with exception. |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments