Best library for generating Dart code?

644 views
Skip to first unread message

James deBoer

unread,
Aug 20, 2013, 6:24:00 PM8/20/13
to mi...@dartlang.org
We are building a tool that will automatically generate Dart code.  What is the best library for generating an AST and producing sane Dart source code?

A few months ago we were working on a project that used analyzer_experimental for reading Dart (http://pub.dartlang.org/packages/analyzer_experimental).  In this case, we want to *write* Dart. Is analyzer_experimental still the way to go or is there something else out there?

Thanks,
James.

Dan Grove

unread,
Aug 21, 2013, 11:50:00 AM8/21/13
to General Dart Discussion, Siggi Cherem, Justin Fagnani
+sig...@google.com and justin...@google.com, who have both been working on codegen


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Erik Grimes

unread,
Aug 21, 2013, 12:27:58 PM8/21/13
to mi...@dartlang.org
Hey James!

I haven't found any libraries for this sort of thing, but I've found using mirrors to analyze the source dart code and mustache4dart templates (http://pub.dartlang.org/packages/mustache4dart) to generate the resulting code fairly straightforward. If you need access to more than just source metadata this approach won't work for you. I particularly like using templates for the code generation. I find it makes it easy to visualize and evolve the generated code.

Erik

Justin Fagnani

unread,
Aug 21, 2013, 12:29:19 PM8/21/13
to General Dart Discussion, Siggi Cherem
I used dart2js to parse the code and do AST transformations. Fro outputting the code I walked the AST, but to re-create files like they were in the input I had to do a bit of extra bookkeeping on libraries and parts, and pull out source spans if tokens we're contiguous to grab comments and whitespace in certain locations.

You could also using a templating library like mustaches if you're purely generating code, but it won't be much help if you're transforming code.

Cheers,
  Justin

Peter Ahé

unread,
Aug 21, 2013, 12:37:11 PM8/21/13
to General Dart Discussion, Siggi Cherem
We added 'BackDoor.compilationUnitsOf(library)' to make it easier to
find compilation units. It's used in this sample:

https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/sdk/lib/_internal/compiler/samples/darttags/darttags.dart

Cheers,
Peter

Siggi Cherem

unread,
Aug 21, 2013, 12:37:32 PM8/21/13
to Dan Grove, General Dart Discussion, Justin Fagnani
We've done a bit of codegen in web_ui/polymer. Both creating code from scratch (for example generating brand new code after interpreting html + MDV bindings), and modifying code in-place (for example the observable transform that changes @observable classes to notify changes proactively).

In both cases we decided to work directly on the text instead of manipulating AST nodes. More specifically, we take code written in Dart, parse it with analyzer experimental and use the AST to understand the code and find where we need to inject our changes. Based on the offset positions that the analyzer gives us, we create a transaction of text edits which we finally commit to generate the final result (see TextEditTransaction below). 

Here are some links to some of our code and a description of what they do. I've been refactoring some of this code recently, so I'm mixing here pointers from new and old code:
We added to the source_maps package a few handy types that can help generate and transform code:
  • TextEditTransaction in refactor.dart: has the logic to help with in-place edits. This type can check that you don't have conflicts in your edits, which is especially useful if you want to do more than 1 change in a file and you want to split the logic of those changes in multiple phases. 
  • NestedPrinter in printer.dart: has logic for generating text: append text, new lines, track indentation, track source map locations, and nest printers (for example so you can create a printer with the contents of a method, and you want to insert it inside a printer that generates a class).
Hope you find these useful!
Cheers,
Siggi

[1]: barback is the system that pub will use internally to do codegen and code transformations before serving or deploying an app.




On Wed, Aug 21, 2013 at 8:50 AM, Dan Grove <dgr...@google.com> wrote:

James deBoer

unread,
Aug 21, 2013, 2:45:49 PM8/21/13
to mi...@dartlang.org, Dan Grove, Justin Fagnani
Sweet, thanks for the responses.  I hope the code we are writing is not that complicated.  Just printing text may be the easiest route.

Sean Eagan

unread,
Aug 21, 2013, 5:31:32 PM8/21/13
to General Dart Discussion, Dan Grove, Justin Fagnani

I actually just uploaded a codegen pub package yesterday:


It basically focuses on generating library level stuff (library, part of, import directives, package: URIs, the files themselves, not classes, methods, fields, etc.) so far.  I'm using it to generate locale data code for intlxcldr, and intl.

Would love to get feedback on it!

Cheers,
Sean Eagan

Daniel Davidson

unread,
Aug 22, 2013, 4:04:55 PM8/22/13
to mi...@dartlang.org
I have a code generation library, maybe similar to Sean Eagan's. The package is called ebisu and it only focuses on the structure, libraries, parts, classes, members, enums etc. It provides protect blocks so code can be regenerated without destroying hand written code. I use it to generate Dart code, D code (ebisu_dlang) and I started using it to generate Web UI components - before switch to polymer. 


To get a feel for it, see this which generates the structure of ebisu using ebisu:

Thanks,
Dan

On Tuesday, August 20, 2013 5:24:00 PM UTC-5, James deBoer wrote:

Günter Zöchbauer

unread,
Feb 25, 2014, 5:49:55 AM2/25/14
to mi...@dartlang.org
I'm looking for a way to generate Dart code or AST programmatically.
Did some new things emerge since this discussion took place?

Panneerselvam V.

unread,
Oct 5, 2018, 4:16:57 PM10/5/18
to Dart Misc
Hi Günter Zöchbauer,

      I am looking for generating flutter code from scratch, did you find feasible option for generating dart code? Any lead on this topic will be very helpful to me. Thanks!!!

Samuel Rawlins

unread,
Oct 5, 2018, 5:32:56 PM10/5/18
to General Dart Discussion
Hi Panneerselvam,

That post is pretty old :) Dart has changed a lot in the last 4.5 years. Luckily it's only gotten better in the code generation story.

The best resource for you is probably the build_runner package. It has a great README. Paired with the build package, you should be able to do some great code generation.

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
Reply all
Reply to author
Forward
0 new messages