0

I am writing a translator which converts DSL to multiple programming language (It seems like Apache Thrift).

For example,

// an example DSL
LOG_TYPE: COMMERCE

COMMON_FIELD : session_id

KEY: buy
FIELD: item_id, transaction_id 

KEY: add_to_cart
FIELD: item_id

// will be converted to Java
class Commerce {
  private String session_id
  private String key;
  private String item_id;
  private String transaction_id

  // auto-created setter, getter, helper methods 
  ...
}

It also should be translated into objective-c and javascript.

To implement it, I have to replace string

// 1. create or load code fragments
String variableDeclarationInJava = "private String {$field};";
String variableDeclarationInJavascript = "...";
String variableDeclarationInObjC = "...";

// 2. replace it
variableDeclarationInJava.replace(pattern, fieldName)
...

Replacing code fragment in String is not type safe and frustrating since it does not any information even if there are errors.

So, my question is It is possible to parse String at compile time? like Scala sqltyped library

If it is possible, I would like to know how can I achieve it.

Thanks.

1ambda
  • 1,145
  • 8
  • 19
  • You can hardly check SQL syntax at compile time, as it depends on the DB vendor. Can use macro and/or string interpolation to check string syntax. – cchantep Dec 16 '15 at 08:10
  • 1
    In my case, it would be easier than SQL syntax checking since I have to validate **restricted** language syntax (e.g creating function, class, variables..) Could you recommend some useful resource for macro to translate DSL? – 1ambda Dec 16 '15 at 08:25
  • There are various existing open projects doing so (e.g Slick). You can look at sources. – cchantep Dec 16 '15 at 08:34

1 Answers1

3

As far, as I understand, it could be. Please take a look at string interpolation. You implement a custom interpolator, (like it was done for quasi quotations or in Slick).

A nice example of the thing you may want to do is here

ppopoff
  • 658
  • 7
  • 17