Does Dart support written embedded DSL?

I wonder if Dart is capable of performing close at runtime with the support of such a delegate, what is Groovy capable of? See this example for a better understanding: A Groovy DSL from scratch in 2 hours .

I have a DSL written in Groovy to easily define MARC library entries. I would like to be able to handle a similar Dart script initially by binding the definition calls to the delegate class in my program.

record {
   leader "00000nam a2200000 u 4500"
   controlfield "001", "LIB001"
   controlfield "005", "20120311123453.0"
   datafield("100") {
     subfield "a", "Author of record"
   }
   datafield("245", "0") {
     subfield "a", "Title of record"
   }
}

You may be wondering: why can't this be expressed in JSON? With such a DSL, I can do much more than express data. Since it is built-in, you can do anything inside DSL that is really in the host language (this is Groovy case). You can do a for loop if you need to define the same thing several times with different values, you can use GString expressions, call the database, access files, etc. With DSLD, the IDE knows your concept just as if it has always been part of the language, it can offer you help tools. It is very expressive and intuitive.

A similar thing for Darth is what I'm looking for.

+4
source share
2 answers

Dart DSL. , DSL .

. Fuzzy .

:

new Record()
  ..leader = '00000nam a2200000 u 4500'
  ..controlfield('001', 'LIB001')
  ..controlfield('005', '20120311123453.0')
  ..datafield('100', '',
      new Subfield('a', 'Author of record'))
  ..datafield('245', '0',
      new Subfield('a', 'Title of record'));

, , :

List data = [
               ['a', 'Title of record'],
               ['a', 'Something of record']
              ];
// Same record code from above with the addition of this line:
  ..datafields('245', '', data, (e) => new Subfield(e[0], e[1]));

:

class Record {
  String leader;
  List<ControlField> controlFields = [];
  List<DataField> datafieldList = [];

  void controlfield(String a, String b) {
    controlFields.add(new ControlField(a, b));
  }

  void datafield(String a, String b, Subfield subfield) {
    datafieldList.add(new DataField(a, b, subfield));
  }

  void datafields(String a, String b, Iterable data, Subfield f(E e)) {
    data.forEach( (e) {
      datafieldList.add(new DataField(a, b, f(e)));
    });
  }

}

class ControlField {

  String a;
  String b;

  ControlField(this.a, this.b);
}

class DataField {

  String a;
  String b;
  Subfield subfield;

  DataField(this.a, this.b, this.subfield);
}

class Subfield {

  String a;
  String b;

  Subfield(this.a, this.b);
}

MARC, a b , - . , , , , , , .

+4

sweet.js, JavaScript. , DSL, . JavaScript, . DSL JavaScript Dart.

0

All Articles