Java DSLs with Xtext

19
Java DSLs with Xtext Jan Koehnlein Mittwoch, 27. März 13

description

Slides of my talk at EclipseCon 2013 (Boston)

Transcript of Java DSLs with Xtext

Page 1: Java DSLs with Xtext

Java DSLs with Xtext

Jan Koehnlein

Mittwoch, 27. März 13

Page 2: Java DSLs with Xtext

Mittwoch, 27. März 13

Page 3: Java DSLs with Xtext

Mittwoch, 27. März 13

Page 4: Java DSLs with Xtext

public class Customer implements Serializable {

private String name; private Address address; private List<Item> purchase;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; }}

Mittwoch, 27. März 13

Page 5: Java DSLs with Xtext

@Entity public class Customer implements Serializable { @Id private String name; private Address address; private List<Item> purchase;

public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="ADDR", nullable=false) public Address getAddress() return address; } public void setAddress(Address address) { this.address = address; } @ManyToMany public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; }}

Mittwoch, 27. März 13

Page 6: Java DSLs with Xtext

Domain-Specific Languages

Mittwoch, 27. März 13

Page 7: Java DSLs with Xtext

@Entity public class Customer implements Serializable { @Id private String name; private Address address; private List<Item> purchase;

public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="ADDR", nullable=false) public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @ManyToMany public List<Item> getPurchase() { return purchase; } public void setPurchase(List<Item> items) { this.purchase = purchase; }}

entity Customer { name: String address: Address purchase: Item*}

CodeGeneration

DSL Java

Mittwoch, 27. März 13

Page 8: Java DSLs with Xtext

Methods Logic

ArithmeticsBehavior ?

Mittwoch, 27. März 13

Page 9: Java DSLs with Xtext

entity Customer { name: String address: Address purchase: Item* double sales() { double result = 0.0; for(Item item: purchase) result += item.getPrice(); return result; }}

Mittwoch, 27. März 13

Page 10: Java DSLs with Xtext

Typesystem

Compiler / Interpreter

Complex Syntax

Expressions

Mittwoch, 27. März 13

Page 11: Java DSLs with Xtext

Integration Patterns

Generatedcode

Manually writtencode

Mittwoch, 27. März 13

Page 12: Java DSLs with Xtext

Reusable powerful expression library language

Java‘s Typesystem

Compile to Java Code

Access to Java-elements

Mittwoch, 27. März 13

Page 13: Java DSLs with Xtext

DSLs for

JavaMittwoch, 27. März 13

Page 14: Java DSLs with Xtext

Type System

Interpreter /

Debugger

Grammar (Parser, Lexer)Linker

Advanced Editor

Eclipse IntegrationJvm

Model

MyLanguage

grammar inheritance

JvmModelInferrer

Mittwoch, 27. März 13

Page 15: Java DSLs with Xtext

Model Inference

entity Customer { name: String address: Address purchasedItems: Item* double sales() { purchasedItems .map[price] .reduce[a,b|a+b] }}

Class Customer

parameter name

returnType String

Field name

Method getName

Method setName

type String

Mittwoch, 27. März 13

Page 16: Java DSLs with Xtext

Model Inference

entity Customer { name: String address: Address purchasedItems: Item* double sales() { purchasedItems .map[price] .reduce[a,b|a+b] }}

returnType double

Method sales

body Expression

Mittwoch, 27. März 13

Page 17: Java DSLs with Xtext

class ScriptingJvmModelInferrer extends AbstractModelInferrer { @Inject extension JvmTypesBuilder

def dispatch void infer(Script script, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {

acceptor.accept(script.toClass('MyClass')).initializeLater [

// the class gets one main method members += script.toMethod(

'main', script.newTypeRef(Void::TYPE)) [

parameters += script.toParameter("args", script.newTypeRef(typeof(String))

.addArrayTypeDimension) static = true varArgs = true // Associate the script as the body of the main method body = script ] ] }}

Mittwoch, 27. März 13

Page 18: Java DSLs with Xtext

The 7 LanguagesScripting Language

Http Routing Language

Templates Language

DSL for Guice

Build Language

Tortoise

DSL for MongoDB

Mittwoch, 27. März 13

Page 19: Java DSLs with Xtext

www.eclipse.org/Xtext/7languages.html

Find more at

Mittwoch, 27. März 13