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:
- the observable transformer: we recently rewrote the code
transformation for @observable as run standalone barback[1] transformer.
The code can be found in the observe package:
https://github.com/dart-lang/bleeding_edge/blob/master/dart/pkg/observe/lib/transform.dart
- the old web_ui compiler had a lot of code emitters here
https://github.com/dart-lang/web-ui/blob/master/lib/src/emitters.dart.
This code used a code generation helper 'CodePrinter' that helps keep
track of indentation, nesting, and source maps. I've recently refactored
out common parts of that type and named it NestedPrinter in the source-maps
package (more below).
We added to the source_maps package a few handy types that can help
generate and transform code:
- TextEditTransaction in
refactor.dart<https://github.com/dart-lang/bleeding_edge/blob/master/dart/pkg/source_maps/lib/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<https://github.com/dart-lang/bleeding_edge/blob/master/dart/pkg/source_maps/lib/printer.dart#L98>:
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.
Post by Dan Groveworking on codegen
Post by James deBoerWe 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.
--
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
--
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+unsubscribe-dYxm/***@public.gmane.org