Jazoon 2010 - Building DSLs with Eclipse

59
Building DSLs with Eclipse Peter Friese, itemis @peterfriese @xtext www.itemis.de (c) 2009 Peter Friese. Distributed under the EDL V1.0 - http://www.eclipse.org/org/documents/edl-v10.php More info: http://www.peterfriese.de / http://www.itemis.com

description

In this presentation, I show how to build an Xtext based DSL for iPhone and Android aplications.

Transcript of Jazoon 2010 - Building DSLs with Eclipse

Page 1: Jazoon 2010 - Building DSLs with Eclipse

Building DSLs with EclipsePeter Friese, itemis

@peterfriese@xtext

www.itemis.de

(c) 2009 Peter Friese. Distributed under the EDL V1.0 - http://www.eclipse.org/org/documents/edl-v10.phpMore info: http://www.peterfriese.de / http://www.itemis.com

Page 2: Jazoon 2010 - Building DSLs with Eclipse

Think about your job!

Page 3: Jazoon 2010 - Building DSLs with Eclipse

Feel like this?

Page 4: Jazoon 2010 - Building DSLs with Eclipse

Because

Page 5: Jazoon 2010 - Building DSLs with Eclipse

Becauseyou need to write

Page 6: Jazoon 2010 - Building DSLs with Eclipse

Becauseyou need to writelots of boring code?

Page 7: Jazoon 2010 - Building DSLs with Eclipse

Wrong Level of Abstraction!http://www.flickr.com/photos/rykerstribe/3222969466/

Page 8: Jazoon 2010 - Building DSLs with Eclipse

DSLomain

pecific

anguage

Page 9: Jazoon 2010 - Building DSLs with Eclipse

DSLA language...

...that lets you express your intention

... that doesn’t get in your way

... that fits your situation

... that’s formal and processable

... that’s simple and easy to learn

... that solves one problem

Page 10: Jazoon 2010 - Building DSLs with Eclipse
Page 11: Jazoon 2010 - Building DSLs with Eclipse
Page 12: Jazoon 2010 - Building DSLs with Eclipse
Page 13: Jazoon 2010 - Building DSLs with Eclipse
Page 14: Jazoon 2010 - Building DSLs with Eclipse

Some DSLsyou might have

heard of

Page 15: Jazoon 2010 - Building DSLs with Eclipse

select name, salaryfrom employees where salary > 2000order by salary

Page 16: Jazoon 2010 - Building DSLs with Eclipse

<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 17: Jazoon 2010 - Building DSLs with Eclipse

<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 18: Jazoon 2010 - Building DSLs with Eclipse

Externalor

Internal

Page 19: Jazoon 2010 - Building DSLs with Eclipse

Internal DSLs

Page 20: Jazoon 2010 - Building DSLs with Eclipse

Mailer.mail().to(“[email protected]”).from(“[email protected]”).subject(“Writing DSLs in Java”).body(“...”).send();

Page 21: Jazoon 2010 - Building DSLs with Eclipse

Simple

Page 22: Jazoon 2010 - Building DSLs with Eclipse

Limited

Page 23: Jazoon 2010 - Building DSLs with Eclipse

External DSLs

Page 24: Jazoon 2010 - Building DSLs with Eclipse
Page 25: Jazoon 2010 - Building DSLs with Eclipse

1)Create ANTLR grammar2)Generate lexer / parser3)Parser will create parse tree4)Transform parse tree to semantic model

5)Iterate model6)Pass model element(s) to template

Page 26: Jazoon 2010 - Building DSLs with Eclipse

Lack of symbolic integration

Writing parsers / generators is complicated

IDE support is inferior / non-existent

Page 27: Jazoon 2010 - Building DSLs with Eclipse

Flexible

Page 28: Jazoon 2010 - Building DSLs with Eclipse

Adaptable

Page 29: Jazoon 2010 - Building DSLs with Eclipse

Complicated

Page 30: Jazoon 2010 - Building DSLs with Eclipse

... for building DSLs?

Why not use a DSL...

Page 31: Jazoon 2010 - Building DSLs with Eclipse

http://www.eclipse.org/Xtext/

@xtext

Page 32: Jazoon 2010 - Building DSLs with Eclipse

Superclass

Subclass Class

ECore meta modelLL(*) Parser Editor

Model

Grammar

Generator

Runtime

Page 33: Jazoon 2010 - Building DSLs with Eclipse

Superclass

Subclass Class

ECore meta modelLL(*) Parser Editor

Model

Grammar

Generator

Runtime

Page 34: Jazoon 2010 - Building DSLs with Eclipse

Grammar (similar to EBNF)grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals

generate entity "http://www.xtext.org/example/Entity"

Model: (types+=Type)*;

Type: TypeDef | Entity; TypeDef: "typedef" name=ID ("mapsto" mappedType=JAVAID)?; JAVAID: name=ID("." ID)*; Entity: "entity" name=ID ("extends" superEntity=[Entity])? "{" (attributes+=Attribute)* "}"; Attribute: type=[Type] (many?="*")? name=ID;

Page 35: Jazoon 2010 - Building DSLs with Eclipse

grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals

generate entity "http://www.xtext.org/example/Entity"

Model: (types+=Type)*;

Type: TypeDef | Entity; TypeDef: "typedef" name=ID ("mapsto" mappedType=JAVAID)?; JAVAID: name=ID("." ID)*; Entity: "entity" name=ID ("extends" superEntity=[Entity])? "{" (attributes+=Attribute)* "}"; Attribute: type=[Type] (many?="*")? name=ID;

entity

Model

*

name: EStringType

types

TypeDef Entity

name: EStringJAVAID

superEntity

mappedType

name: EStringmany: EBoolean

Attribute

attributes

type

Meta model inference

Rules -> Classes

Page 36: Jazoon 2010 - Building DSLs with Eclipse

grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals

generate entity "http://www.xtext.org/example/Entity"

Model: (types+=Type)*;

Type: TypeDef | Entity; TypeDef: "typedef" name=ID ("mapsto" mappedType=JAVAID)?; JAVAID: name=ID("." ID)*; Entity: "entity" name=ID ("extends" superEntity=[Entity])? "{" (attributes+=Attribute)* "}"; Attribute: type=[Type] (many?="*")? name=ID;

entity

Model

*

name: EStringType

types

TypeDef Entity

name: EStringJAVAID

superEntity

mappedType

name: EStringmany: EBoolean

Attribute

attributes

type

Meta model inference

Alternatives -> Hierarchy

Page 37: Jazoon 2010 - Building DSLs with Eclipse

grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals

generate entity "http://www.xtext.org/example/Entity"

Model: (types+=Type)*;

Type: TypeDef | Entity; TypeDef: "typedef" name=ID ("mapsto" mappedType=JAVAID)?; JAVAID: name=ID("." ID)*; Entity: "entity" name=ID ("extends" superEntity=[Entity])? "{" (attributes+=Attribute)* "}"; Attribute: type=[Type] (many?="*")? name=ID;

entity

Model

*

name: EStringType

types

TypeDef Entity

name: EStringJAVAID

superEntity

mappedType

name: EStringmany: EBoolean

Attribute

attributes

type

Meta model inference

Assignment -> Feature

Page 38: Jazoon 2010 - Building DSLs with Eclipse

Let’s build a DSLfor Mobile Apps

Page 39: Jazoon 2010 - Building DSLs with Eclipse
Page 40: Jazoon 2010 - Building DSLs with Eclipse

Building DSLs

1. Analyze problem domain2. Map concepts of problem domain to language3. (Optional: write interpreter)4. (Optional: write generator)

Page 41: Jazoon 2010 - Building DSLs with Eclipse

Analyzing the problem domain

http://www.flickr.com/photos/minifig/3174009125/

Page 42: Jazoon 2010 - Building DSLs with Eclipse

Anatomy of an iPhone app

Table view

View title

Table cell

Tab bar

Tab bar button

NameImage

Speaker

TitleLocation

Session

Entity

Data Provider

Page 43: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts

Table view

View title

Table cell

Tab bar

Tab bar button

Entity

Data Provider

tabbarApplication jazoonApp { button { title= "Schedule" icon= "83-calendar.png" view= ScheduleList( AllSessions() ) } button { title= "Speakers" icon= "112-group.png" view= SpeakerList( AllSpeakers() ) } button { title= "Topics" icon= "44-shoebox.png" view= TopicList( AllTopics() ) }}

Page 44: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts

Table view

View title

Table cell

Tab bar

Tab bar button

Entity

Data Provider

entity Speaker { String speakerId String name String bio String organization String country String smallImageURL String largeImageURL Session[] talks}

entity Session { String talkId String title String abstract String ^type String location String startTime String endTime Speaker[] speakers Topic[] topics}

entity Topic { String topicId String themeId String name}

Page 45: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts

Table view

View title

Table cell

Tab bar

Tab bar button

Entity

Data Provider

contentprovider AllSessions returns Session[] fetches XML from "http://jipa.netcetera.ch/talks.xml" selects "feed.entry" contentprovider AllSpeakers returns Speaker[] fetches XML from "http://jipa.netcetera.ch/speakers.xml" selects "feed.entry"

Page 46: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts

Table view

View title

Table cell

Tab bar

Tab bar button

Entity

Data Provider

contentprovider AllSessions returns Session[] fetches XML from "http://query.yahooapis.com/v1/public/yql?q=use%20%22http%3A%2F%2FHeikoBehrens.net%2Fjazoon%2Ftalks.xml%22%20as%20talks%3B%0Aselect%20*%20from%20talks%20where%20omitDetails%3D1&debug=true" selects "query.results.talks.talk"

contentprovider SessionsByTopic(String id) returns Session[] fetches XML from ("http://query.yahooapis.com/v1/public/yql?q=use%20%22http%3A%2F%2FHeikoBehrens.net%2Fjazoon%2Ftalks.xml%22%20as%20s%3B%0Aselect%20*%20from%20s%20where%20topicId%20%3D%20%22"id"%22&diagnostics=true&debug=true&debug=true") selects "query.results.talks.talk"

Page 47: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts

Table view

View title

Table cell

Tab bar

Tab bar button

Entity

Data Provider tableview ScheduleList(Session[] sessions) { title= "Schedule" section { cell Subtitle foreach sessions as session { text= session.title details= session.startTime action= SessionDetails(

SessionById(session.talkId)) } } }

Page 48: Jazoon 2010 - Building DSLs with Eclipse

Demo 1

Page 49: Jazoon 2010 - Building DSLs with Eclipse
Page 50: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts to code

Page 51: Jazoon 2010 - Building DSLs with Eclipse

«Xpand»

Page 52: Jazoon 2010 - Building DSLs with Eclipse

Type safe

Produces any kind of text

Can run standalone(ANT / Maven)

Debugger

Profiler

Eclipse-based

Editor Protected regions

Cartridges

Outlets

Polymorphism

Page 53: Jazoon 2010 - Building DSLs with Eclipse

Mapping concepts to code

Page 54: Jazoon 2010 - Building DSLs with Eclipse

tableview SpeakerList( Speaker[] speakers) { title= "Speakers" section { cell Default foreach speakers as speaker { text= speaker.name image= speaker.smallImageURL action= SpeakerDetails (SpeakerById( speaker.speakerId)) } }}

Mapping concepts to code

Page 55: Jazoon 2010 - Building DSLs with Eclipse

«DEFINE viewModule FOR SectionedView»«FILE filenameModule()»#import "«filenameHeader()»"#import "NSObject+Applause.h"«EXPAND imports»

@implementation «className()»

«EXPAND sectionCount»«EXPAND sectionTitleHeader»«EXPAND rowCounts»«EXPAND cellDescriptions»«EXPAND cellSelections»«EXPAND staticData»@end«ENDFILE»«ENDDEFINE»

tableview SpeakerList( Speaker[] speakers) { title= "Speakers" section { cell Default foreach speakers as speaker { text= speaker.name image= speaker.smallImageURL action= SpeakerDetails (SpeakerById( speaker.speakerId)) } }}

Mapping concepts to code

Page 56: Jazoon 2010 - Building DSLs with Eclipse

Demo 2

Page 57: Jazoon 2010 - Building DSLs with Eclipse

3 things to take home

Page 58: Jazoon 2010 - Building DSLs with Eclipse

3 things to take home

Implementing DSLs is easy with Xtext1

Xtext delivers great IDE support2

Symbolic integration is possible3

Page 59: Jazoon 2010 - Building DSLs with Eclipse

More Info?Xtext Webinar:

http://live.eclipse.org/node/886

www.xtext.org

Consulting:http://www.itemis.com

@peterfriese | http://peterfriese.de