dart_parser 1.0.0-dev+7

  • README.md
  • Installing
  • Versions
  • 19

dart_parser

v1.0.0-dev

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.

Installation

dependencies:
    dart_parser: ^1.0.0-dev

This will automatically install antlr4dart as well.

Usage

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:

stringLiteral: (SINGLE_LINE_STRING | MULTI_LINE_STRING)+;

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.

Examples

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();
print(annotation.getOfType().text);

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

class MyListener extends DartlangBaseListener {
  @override
  void enterMetadatum(MetadatumContext context) {
    print(context.getOfType().text);
  }
}

class MyVisitor extends DartlangBaseVisitor<String> {
  @override
  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?).

Thanks

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:


dependencies:
  dart_parser: "^1.0.0-dev+7"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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

Analysis

We analyzed this package on Jun 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.60.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
20 / 100
Health:
Code health derived from static analysis. [more]
27 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
2 / 100
Overall score:
Weighted score of the above. [more]
19
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:dart_parser/dart_parser.dart.

Suggestions

  • Maintain CHANGELOG.md.

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

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 72 errors 7 hints.

    Strong-mode analysis of lib/src/dartlang_parser.dart failed with the following error:

    line: 3261 col: 27
    The method 'adaptivePredict' isn't defined for the class 'AtnSimulator'.

    Run dartfmt to format lib/dart_parser.dart.

    Similar analysis of the following files failed:

    • lib/src/dartlang_base_listener.dart (hint)
    • lib/src/dartlang_base_visitor.dart (hint)
    • lib/src/dartlang_lexer.dart (hint)
    • lib/src/dartlang_listener.dart (hint)
    • lib/src/dartlang_visitor.dart (hint)
  • Package is getting outdated.

    The package was released 101 weeks ago.

  • Package is pre-release.

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

  • The description is too short.

    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.

    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.

Dependencies

Package Constraint Resolved Available
Direct dependencies
antlr4dart >=0.7.0 <1.0.0 0.7.0
Transitive dependencies
bignum 0.1.0
collection 1.14.10
fixnum 0.10.7
Dev dependencies
browser >=0.10.0 <1.0.0
codemirror >=0.4.1 <1.0.0
test >=0.12.14 <1.0.0