0% found this document useful (0 votes)
33 views19 pages

DSLs For Java - 0

The document discusses using Xtext to create domain-specific languages (DSLs) for Java. It provides an example of defining a DSL for describing customer entities and shows how Xtext can generate corresponding Java code. The document also covers how Xtext supports integrating DSLs with Java through techniques like model inference and grammar inheritance.

Uploaded by

Fernando Vidoni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

DSLs For Java - 0

The document discusses using Xtext to create domain-specific languages (DSLs) for Java. It provides an example of defining a DSL for describing customer entities and shows how Xtext can generate corresponding Java code. The document also covers how Xtext supports integrating DSLs with Java through techniques like model inference and grammar inheritance.

Uploaded by

Fernando Vidoni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Java DSLs with Xtext

Jan Koehnlein
Mittwoch, 27. Mrz 13

Mittwoch, 27. Mrz 13

Mittwoch, 27. Mrz 13

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. Mrz 13

@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. Mrz 13

Domain-Specic Languages

Mittwoch, 27. Mrz 13

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

Java
@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; }

Code Generation

Mittwoch, 27. Mrz 13

Methods Logic Arithmetics Behavior

Mittwoch, 27. Mrz 13

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. Mrz 13

Expressions

Complex Syntax Typesystem Compiler / Interpreter


Mittwoch, 27. Mrz 13

Integration Patterns
Manually written code

Generated code

Mittwoch, 27. Mrz 13

Reusable powerful expression library language Javas Typesystem Access to Java-elements Compile to Java Code

Mittwoch, 27. Mrz 13

DSLs for Java


Mittwoch, 27. Mrz 13

grammar inheritance
Grammar (Parser, Lexer) Linker Type System Interpreter / Advanced Editor Eclipse Integration Debugger

JvmModel

MyLanguage

JvmModelInferrer

Mittwoch, 27. Mrz 13

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

Field name Method getName returnType String Method setName parameter name type String

Mittwoch, 27. Mrz 13

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

Method sales returnType double body Expression

Mittwoch, 27. Mrz 13

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. Mrz 13

The 7 Languages
Scripting Language DSL for MongoDB Http Routing Language Templates Language DSL for Guice Build Language Tortoise
Mittwoch, 27. Mrz 13

Find more at
www.eclipse.org/Xtext/7languages.html

Mittwoch, 27. Mrz 13

You might also like