dart_parser 1.0.0-dev+7

  • README.md
  • Installing
  • Versions
  • 0



An ANTLR-based Dart parser.

Eventual goal is compliance with the ECMA Standard.

Right now, it will need a lot more tests to prove it works.

Special thanks to Tiago Mazzutti for this port of ANTLR4 to Dart.


    dart_parser: ^1.0.0-dev

This will automatically install antlr4dart as well.


This library will be of absolutely no use to you unless you learn ANTLR, which is what I wrote it with.

The root node is compilationUnit. Dart compilation units consist of either a single libraryDefinition, or a partDeclaration. Most files, whether they use the library keyword or not, are categorized as libraryDefinition.

In general, the names of all rules are the same as those in the ECMA specification. However, some rules I had to change the names of. operator became op. I removed the IDENTIFIER_NO_DOLLAR rule, as it didn't gel easily with ANTLR.' I also split metadata into metadatum and ofType:

// Example, @override() @Expose("foo")
metadata: (metadatum)*;

// Refers to an individual metadatum within the above, i.e.
// @override()
metadatum: ARROBA ofType arguments?;

// Refers to the metadatum's type, i.e. 'override'
ofType: qualified (DOT identifier)?;

In addition, I had to change the way strings are lexed. This is out of line with the specification. The stringLiteral now looks like this:


To handle the contents of strings, you will have to do it manually, like via Regex. Sorry!

In addition, I modified the rules of external declarations, so that you could include metadata before the keyword external. The rule defined in the spec didn't permit that, although that's accepted by dartanalyzer, dart2js, etc.


Create a parser:

import 'package:antlr4dart/antlr4dart.dart';
import 'package:dart_parser/dart_parser.dart';

main() {
  String input = '''
  main() {
    print("Hello, world!");

  var source = new StringSource(input);
  var lexer = new DartlangLexer(source);
  var tokens = new CommonTokenSource(lexer);
  var parser = new DartlangParser..buildParseTree = true;

You can imperatively obtain a given parse tree type, but this only makes sense if you know beforehand what type of input you are receiving.

// Find which type an annotation is.
var annotation = parser.metadatum();

Otherwise, consider implementing a listener or visitor. The difference is explained here.

class MyListener extends DartlangBaseListener {
  void enterMetadatum(MetadatumContext context) {

class MyVisitor extends DartlangBaseVisitor<String> {
  String visitMetadatum(MetadatumContext context) {
    return context.getOfType().text;

main() {
  // For a listener
  ParseTreeWalker.DEFAULT.walk(new MyListener(), parser.compilationUnit());

  // For a visitor
  new MyVisitor().visitCompilationUnit(parser.compilationUnit());

Tree View

Tree View

This package includes a web app that diagrams your parse trees. Alternatively, you can use grun, which ships with the ANTLR tool itself.

The Future

Who knows what we'll see in the future? Now that we have a ready-to-go parser available for public consumption, it's extremely possible for the community to apply Dart to all kinds of broad horizons.

In fact, the original reason I made this parser was so I could compile Dart to x86 Assembly and make an operating system. However, I doubt that will actually happen anymore.

Somebody make a Babel for Dart (or isn't that the dev_compiler?).


As always, thanks for using this.

Feel free to follow me on Twitter, or to check out my blog.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  dart_parser: ^1.0.0-dev+7

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:dart_parser/dart_parser.dart';
Version Uploaded Documentation Archive
1.0.0-dev+7 Jun 29, 2016 Go to the documentation of dart_parser 1.0.0-dev+7 Download dart_parser 1.0.0-dev+7 archive
1.0.0-dev+6 Jun 29, 2016 Go to the documentation of dart_parser 1.0.0-dev+6 Download dart_parser 1.0.0-dev+6 archive
1.0.0-dev+5 Jun 28, 2016 Go to the documentation of dart_parser 1.0.0-dev+5 Download dart_parser 1.0.0-dev+5 archive
1.0.0-dev+4 Jun 28, 2016 Go to the documentation of dart_parser 1.0.0-dev+4 Download dart_parser 1.0.0-dev+4 archive
1.0.0-dev+3 Jun 28, 2016 Go to the documentation of dart_parser 1.0.0-dev+3 Download dart_parser 1.0.0-dev+3 archive
1.0.0-dev+2 Jun 28, 2016 Go to the documentation of dart_parser 1.0.0-dev+2 Download dart_parser 1.0.0-dev+2 archive
1.0.0-dev+1 Jun 27, 2016 Go to the documentation of dart_parser 1.0.0-dev+1 Download dart_parser 1.0.0-dev+1 archive
1.0.0-dev Jun 27, 2016 Go to the documentation of dart_parser 1.0.0-dev Download dart_parser 1.0.0-dev archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.1.0.
 Because dart_parser depends on antlr4dart >=0.1.1 which requires SDK version <2.0.0, version solving failed.

Health suggestions

Format lib/dart_parser.dart.

Run dartfmt to format lib/dart_parser.dart.

Format lib/src/dartlang_base_listener.dart.

Run dartfmt to format lib/src/dartlang_base_listener.dart.

Format lib/src/dartlang_base_visitor.dart.

Run dartfmt to format lib/src/dartlang_base_visitor.dart.

Fix additional 4 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/dartlang_lexer.dart (Run dartfmt to format lib/src/dartlang_lexer.dart.)
  • lib/src/dartlang_listener.dart (Run dartfmt to format lib/src/dartlang_listener.dart.)
  • lib/src/dartlang_parser.dart (Run dartfmt to format lib/src/dartlang_parser.dart.)
  • lib/src/dartlang_visitor.dart (Run dartfmt to format lib/src/dartlang_visitor.dart.)

Maintenance issues and suggestions

Add SDK constraint in pubspec.yaml. (-50 points)

For information about setting SDK constraint, please see https://www.dartlang.org/tools/pub/pubspec#sdk-constraints.

Fix platform conflicts. (-20 points)

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Running dartdoc failed. (-10 points)

Make sure dartdoc runs without any issues.

Package is too old. (-100 points)

The package was released more than two years ago.

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dart_parser.dart.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution, their API may change in breaking ways.