如何在编译时检查String中的SQL语言语法(Scala)

时间:2015-12-16 06:59:18

标签: scala macros thrift dsl shapeless

我正在编写一个将DSL转换为多种编程语言的翻译器(看起来像Apache Thrift)。

例如,

// 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 
  ...
}

它也应该被翻译成objective-c和javascript。

要实现它,我必须替换字符串

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

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

替换String中的代码片段并不是类型安全且令人沮丧的,因为即使存在错误,它也不会有任何信息。

所以,我的问题是可以在编译时解析String吗?,如Scala sqltyped library

如果有可能,我想知道如何实现它。

感谢。

1 个答案:

答案 0 :(得分:2)

据我了解,目前可能是。请看一下字符串插值。你实现了一个自定义插值器(就像它是为准引号或在Slick中完成的)。

您可能希望做的一个很好的例子是here