Implementing DSLs in practice
What is needed to implement a DSL?
define lexer and parser using some parser generator
transform parse tree get the model
resolve references
build validation
implement a type system
interpret or compile the model
build simple editor for the language
ident = letter {letter}
IfStmt = ”if” ”(” BoolExpr ”)” {Stmt}
class IfStmt extends Stmt {
int x=1; int y=x+5;
bool x; if (x>5) {
weight x = 15 mm;
load x; load y; add;
abstract syntax tree
Code generation
interpretation instructions executed directly
compilation generate code in another language
transpilation source-to-source compiler
no need to compile to machine language
slow
machine language
Java  C#
IDE integration
DSL
should be shipped with DSLpowerful IDE adoption & success of DSL
reading code validating code executing code
IDE syntax-aware editor not the case for internal DSLs
immediate feedback report errors as they appear
incremental syntax checking validate code as it is being typed
suggesting corrections quickfixes
auto-completion
Syntax highlighting
program colored formatted
commentskeywords stringsnot just cosmetic
immediate feedback on syntactic correctness of what is written
Background validation
program write save switch to command line run compiler
rewriteerrors
errors found too late
programming
environment
continuously check code
while programmer is writing
even if the current file is not saved
highlight errors
in the background
directly in the editor
  

Content assist
provide suggestions on how to complete statement just typed automatically
on demand
Hyperlinking and hovering
navigate between references especially useful if declaration is in a different file
hovering:
display a pop-up
window with
documentation
about an element
Quickfixes
test-driven development write test that invokes a method quickfix creates it
write tests are written before the actual code to test

Implementing DSLs in practice

  • 1.
  • 2.
    What is neededto implement a DSL? define lexer and parser using some parser generator transform parse tree get the model resolve references build validation implement a type system interpret or compile the model build simple editor for the language ident = letter {letter} IfStmt = ”if” ”(” BoolExpr ”)” {Stmt} class IfStmt extends Stmt { int x=1; int y=x+5; bool x; if (x>5) { weight x = 15 mm; load x; load y; add; abstract syntax tree
  • 3.
    Code generation interpretation instructionsexecuted directly compilation generate code in another language transpilation source-to-source compiler no need to compile to machine language slow machine language Java  C#
  • 4.
    IDE integration DSL should beshipped with DSLpowerful IDE adoption & success of DSL reading code validating code executing code IDE syntax-aware editor not the case for internal DSLs immediate feedback report errors as they appear incremental syntax checking validate code as it is being typed suggesting corrections quickfixes auto-completion
  • 6.
    Syntax highlighting program coloredformatted commentskeywords stringsnot just cosmetic immediate feedback on syntactic correctness of what is written
  • 7.
    Background validation program writesave switch to command line run compiler rewriteerrors errors found too late programming environment continuously check code while programmer is writing even if the current file is not saved highlight errors in the background directly in the editor    
  • 8.
    Content assist provide suggestionson how to complete statement just typed automatically on demand
  • 9.
    Hyperlinking and hovering navigatebetween references especially useful if declaration is in a different file hovering: display a pop-up window with documentation about an element
  • 10.
    Quickfixes test-driven development writetest that invokes a method quickfix creates it write tests are written before the actual code to test