Building DSLs with Xtext - Eclipse Modeling Day 2009

65
© itemis AG 2009 – All rights reserved Building DSLs with Xtext Eclipse Modeling Day New York, 16 November 2009 Toronto, 18 November 2009 Heiko Behrens (itemis)

description

Slides of Eclipse Modeling Day in New York and Toronto http://wiki.eclipse.org/Eclipse_Modeling_Day Motivation of specific tools with apple corer analogy, Example of domain-specific language (chess notation), introduction to Xtext with demo plus outlook

Transcript of Building DSLs with Xtext - Eclipse Modeling Day 2009

Page 1: Building DSLs with Xtext - Eclipse Modeling Day 2009

© itemis AG 2009 – All rights reserved

Building DSLs with Xtext

Eclipse Modeling Day

New York, 16 November 2009

Toronto, 18 November 2009

Heiko Behrens (itemis)

Page 2: Building DSLs with Xtext - Eclipse Modeling Day 2009

FOUNDATION

MEMBER TM

Page 3: Building DSLs with Xtext - Eclipse Modeling Day 2009
Page 4: Building DSLs with Xtext - Eclipse Modeling Day 2009

Model-Driven Software Development

with DSLs

Page 5: Building DSLs with Xtext - Eclipse Modeling Day 2009
Page 6: Building DSLs with Xtext - Eclipse Modeling Day 2009

Suppose...

Page 7: Building DSLs with Xtext - Eclipse Modeling Day 2009

You’d want to core an apple...

Page 8: Building DSLs with Xtext - Eclipse Modeling Day 2009

... for your kids.

Page 9: Building DSLs with Xtext - Eclipse Modeling Day 2009

Right tool for the job?

?

Page 10: Building DSLs with Xtext - Eclipse Modeling Day 2009

Your trusty swiss army knife!

Page 11: Building DSLs with Xtext - Eclipse Modeling Day 2009

Suppose...

Page 12: Building DSLs with Xtext - Eclipse Modeling Day 2009

You’d want to core a few more apples...

Page 13: Building DSLs with Xtext - Eclipse Modeling Day 2009

... for an apple cake.

Page 14: Building DSLs with Xtext - Eclipse Modeling Day 2009

Still the best tool for the job?

Page 15: Building DSLs with Xtext - Eclipse Modeling Day 2009

Better use this one.

Page 16: Building DSLs with Xtext - Eclipse Modeling Day 2009

and this one:

Page 17: Building DSLs with Xtext - Eclipse Modeling Day 2009

... a DSL is ...

Page 18: Building DSLs with Xtext - Eclipse Modeling Day 2009

A specific toolfor a specific job

Page 19: Building DSLs with Xtext - Eclipse Modeling Day 2009

A specific toolfor a specific job

Page 20: Building DSLs with Xtext - Eclipse Modeling Day 2009

Domain-Specific Language (DSL)

A DSL is a formal, processable language targeting at a specific viewpoint or

aspect of a software system.

Its semantics, flexibility and notation is designed in order to support working

with that viewpoint as good as possible.

Page 21: Building DSLs with Xtext - Eclipse Modeling Day 2009

Rd2-c2

Page 22: Building DSLs with Xtext - Eclipse Modeling Day 2009

Rd2-c2 ,rook at d2 moves to c2.”

Queen to c7.“”

Check.

Page 23: Building DSLs with Xtext - Eclipse Modeling Day 2009

Moves in Chess:

!ook at a1 moves to a5.

"ishop at c8 captures knight at h3.

# b1 x c3

$2 - g4

Piece

Piece

Piece

Square

Square

Square

Square

Action

Action

Action

Action

Destination

Destination

Destination

Destination

Page 24: Building DSLs with Xtext - Eclipse Modeling Day 2009

Rook a1 move a5

Bishop c8 capture h3 Knight

Knight b1 capture c3 Queen

Pawn g2 move g4

SourceDestinationPiece

Move

WhitePlayerBlackPlayer

Game«enum»

Piece*

Page 25: Building DSLs with Xtext - Eclipse Modeling Day 2009

"ishop at c8 captures knight at h3

" c8 x h3

Page 26: Building DSLs with Xtext - Eclipse Modeling Day 2009

Model (textfile)

White: "Mayfield"

Black: "Trinks"

pawn at e2 moves to e4

pawn at f7 moves to g5

K b1 - c3

f7 - f5

queen at d1 moves to h5

// 1-0

Page 27: Building DSLs with Xtext - Eclipse Modeling Day 2009
Page 28: Building DSLs with Xtext - Eclipse Modeling Day 2009

Xtext is a complete environment for development of textual - programming languages and - domain-specific languages.

It is implemented in Java and is based on Eclipse, EMF, and Antlr.

Page 29: Building DSLs with Xtext - Eclipse Modeling Day 2009

Superclass

Subclass Class

ecore meta modelLL(*) Parser editor

Model

Grammar

Generator

Runtime

Page 30: Building DSLs with Xtext - Eclipse Modeling Day 2009

Grammar (similar to EBNF)Game:

! "White:" whitePlayer=STRING

! "Black:" blackPlayer=STRING

! (moves+=Move)+;!

Move:

! AlgebraicMove | SpokenMove;

AlgebraicMove:

! (piece=Piece)? source=Square (captures?='x'|'-') dest=Square;!

SpokenMove:

! piece=Piece 'at' source=Square

! (captures?='captures' capturedPiece=Piece 'at' | 'moves to')

! dest=Square;!

terminal Square:

! ('a'..'h')('1'..'8'); !

enum Piece:

! pawn = 'P' | pawn = 'pawn' |

! knight = 'N' | knight = 'knight' |

! bishop = 'B' | bishop = 'bishop' |

! rook = 'R' | rook = 'rook' |

! queen = 'Q' | queen = 'queen' |

! king = 'K' | king = 'king';

Page 31: Building DSLs with Xtext - Eclipse Modeling Day 2009

© itemis AG 2009 – All rights reserved

Demo

• Model File within Editor + Custom View

• Xtext Grammar for writing simple chess games

• Derived meta model

• Java program that works with textual models

• Xpand-based generator

31

Page 32: Building DSLs with Xtext - Eclipse Modeling Day 2009

Real World DSLs

Page 33: Building DSLs with Xtext - Eclipse Modeling Day 2009

Regular Expressions

Page 34: Building DSLs with Xtext - Eclipse Modeling Day 2009

[-+]?[0-9]*\.?[0-9]+

Page 35: Building DSLs with Xtext - Eclipse Modeling Day 2009

Complex Event Processing

Page 36: Building DSLs with Xtext - Eclipse Modeling Day 2009

from FlightBookings F, CarBookings C, HotelBookings H matching [24 hours: F, C, H ] on F.name = C.name = H.name select F.name, true bookingWithCar, into TripleBookings

from FlightBookings F, CarBookings C, HotelBookings H matching [24 hours: F, !C, H ] on F.name = C.name = H.name select F.name, false bookingWithCar into TripleBookings

from TripleBookings where prev(bookingWithCar) and not bookingWithCar select name into LostCustomers

Page 37: Building DSLs with Xtext - Eclipse Modeling Day 2009

internal DSL

(with Java)

Page 38: Building DSLs with Xtext - Eclipse Modeling Day 2009

Mailer.mail()

.to(“[email protected]”)

.from(“[email protected]”)

.subject(“Writing DSLs in Java”)

.body(“...”)

.send();

Page 39: Building DSLs with Xtext - Eclipse Modeling Day 2009

ANT

Page 40: Building DSLs with Xtext - Eclipse Modeling Day 2009

<project name="MyProject" default="dist" basedir=".">

<property name="src" location="src"/>

<property name="build" location="build"/>

<property name="dist" location="dist"/>

<target name="init">

<mkdir dir="${build}"/>

</target>

<target name="compile" depends="init">

<javac srcdir="${src}" destdir="${build}"/>

</target>

<target name="dist" depends="compile">

<mkdir dir="${dist}/lib"/>

<jar jarfile="${dist}/lib/MyProject.jar"

basedir="${build}"/>

</target>

<target name="clean">

<delete dir="${build}"/>

<delete dir="${dist}"/>

</target>

</project>

Page 41: Building DSLs with Xtext - Eclipse Modeling Day 2009

ANT ?

Page 42: Building DSLs with Xtext - Eclipse Modeling Day 2009

<project name="MyProject" default="dist" basedir=".">

<property name="src" location="src"/>

<property name="build" location="build"/>

<property name="dist" location="dist"/>

<target name="init">

<mkdir dir="${build}"/>

</target>

<target name="compile" depends="init">

<javac srcdir="${src}" destdir="${build}"/>

</target>

<target name="dist" depends="compile">

<mkdir dir="${dist}/lib"/>

<jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/>

</target>

<target name="clean">

<delete dir="${build}"/>

<delete dir="${dist}"/>

</target></project>

Page 43: Building DSLs with Xtext - Eclipse Modeling Day 2009

vs.

Entities by Example

Page 44: Building DSLs with Xtext - Eclipse Modeling Day 2009

package templates;

import java.util.*;

import java.io.Serializable;

import javax.persistence.*;

@SuppressWarnings("serial")

@Entity

public class Customer implements Serializable {

! private Long id;

! private String name;

! private Address address;

! private Set<Order> orders = new HashSet<Order>();

! // No-arg constructor

! public Customer() {

! }

! @Id

! public Long getId() {

! ! return id;

! }

! public void setId(Long id) {

! ! this.id = id;

! }

! 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;

! }

! @OneToMany

! public Collection<Order> getOrders() {

! ! return orders;

! }

! public void setOrders(Set<Order> orders) {

! ! this.orders = orders;

! }

}

Page 45: Building DSLs with Xtext - Eclipse Modeling Day 2009

package templates;

import java.io.Serializable;

import java.util.*;

import javax.persistence.*;

@SuppressWarnings("serial")

@Entity

public class Customer implements Serializable {

! private Long id;

! private String name;

! private Address address;

! private Set<Order> orders = new HashSet<Order>();

! // No-arg constructor

! public Customer() {

! }

! @Id

! public Long getId() {

! ! return id;

! }

! public void setId(Long id) {

! ! this.id = id;

! }

! 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;

! }

! @OneToMany

! public Collection<Order> getOrders() {

! ! return orders;

! }

! public void setOrders(Set<Order> orders) {

! ! this.orders = orders;

! }

}

Page 46: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity Customer {

! property name : String

! property address : Address

! property orders : Order[]

}

POJOs DAOs

Page 47: Building DSLs with Xtext - Eclipse Modeling Day 2009

The Finder Method

Page 48: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity Customer {

! property name : String

! finder findFixed : name = "John Doe"

}

<entity name="Customer">

<attribute type="String" name="name"/>

<findMethod name="findFixed"> <expression> <equals> <attributeRef attr="name"/> <constant val="John Doe"/> </equals> </expression> <findMethod></entity>

Page 49: Building DSLs with Xtext - Eclipse Modeling Day 2009

Demo

! Implement simple Entity DSL

! Use prepared Generator

Page 50: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 51: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 52: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 53: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 54: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 55: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 56: Building DSLs with Xtext - Eclipse Modeling Day 2009

entity

*

*

Model

name: EStringType

SimpleType

Entity

name: EStringmany: EBoolean

Property

types

extends

properties

type

grammar org.xtext.webinar.Entity

! with org.eclipse.xtext.common.Terminals

generate entity

"http://www.xtext.org/webinar/Entity"

Model:

! (elements+=Type)*;

Type:

! SimpleType | Entity;

SimpleType:

! 'type' name=ID;

Entity:

! 'entity' name=ID

('extends' extends=[Entity])? '{'

! ! properties+=Property*

! '}';

Property:

! 'property' name=ID ':'

type=[Type] (many?='[]')?;

Page 57: Building DSLs with Xtext - Eclipse Modeling Day 2009

my WritePimp

Page 58: Building DSLs with Xtext - Eclipse Modeling Day 2009
Page 59: Building DSLs with Xtext - Eclipse Modeling Day 2009

Whitespace-Aware

Languages

Page 60: Building DSLs with Xtext - Eclipse Modeling Day 2009

Incremental Generation

Page 61: Building DSLs with Xtext - Eclipse Modeling Day 2009

Conclusion

Page 62: Building DSLs with Xtext - Eclipse Modeling Day 2009

Abstraction

Page 63: Building DSLs with Xtext - Eclipse Modeling Day 2009

Use a DSL to develop your tools

Page 64: Building DSLs with Xtext - Eclipse Modeling Day 2009

SupportNewsgroupCommunity ForumProfessional Support

Page 65: Building DSLs with Xtext - Eclipse Modeling Day 2009

@HBehrenshttp://HeikoBehrens.net

[email protected]://www.xing.com/profile/Heiko_Behrenshttp://www.linkedin.com/in/HeikoBehrens

twitterblog

mailxinglinkedin

www.xtext.org@XtexttwitterThe Committer Team

JanKöhnlein

DennisHübner

MoritzEysholdt

PeterFriese

MichaelClay

PatrickSchönbach

KnutWannheden

SebastianZarnekow

HeikoBehrens

Sven Efftinge