Building DSLs With Eclipse - Presentation Transcript
Building DSLs with
Eclipse
Peter Friese, itemis
@peterfriese
@xtext
(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
What is a DSL?
Domain
Language
Suppose...
You’d want to core an apple...
... for your kids.
?
Right tool for the job?
Your trusty swiss army knife!
Because you can use it for other stuff, too.
E.g., opening a bottle of wine.
Suppose...
You’d want to core a few more apples...
... for an apple cake.
Still the best tool for the job?
Better use this one.
and this one:
BUT
avoid the unitasker!
BUT
avoid the unitasker!
... a DSL is ...
A specific tool
for a specific job
A specific tool
for a specific job
Samples for DSLs
SQL
select name, salary
from employees
where salary > 2000
order by salary
Mailer.mail()
.to(“you@gmail.com”)
.from(“me@gmail.com”)
.subject(“Writing DSLs in Java”)
.body(“...”)
.send();
Simple
Limited
External DSLs
1)Create ANTLR grammar
2)Generate lexer / parser
3)Parser will create parse tree
4)Transform parse tree to semantic model
5)Iterate model
6)Pass model element(s) to template
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity Rules -> Classes
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity Alternatives -> Hierarchy
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity Assignment -> Feature
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
grammar org.xtext.example.Entity
with org.eclipse.xtext.common.Terminals Meta model inference
generate entity
"http://www.xtext.org/example/Entity"
entity
Model:
(types+=Type)*;
Model
Type:
TypeDef | Entity;
types
TypeDef: *
Type
"typedef" name=ID
name: EString
("mapsto" mappedType=JAVAID)?;
superEntity
JAVAID:
name=ID("." ID)*; TypeDef Entity
Entity: mappedType attributes
"entity" name=ID
Attribute
("extends" superEntity=[Entity])? JAVAID
name: EString
"{" name: EString
many: EBoolean
(attributes+=Attribute)*
"}"; type
Attribute:
type=[Type] (many?="*")? name=ID;
Demo 2
Poll "Simple poll"
question "What's your name?" name
question "How do you feel today?" howareyou
() "Fine" fine
() "Great" great
() "Bad" bad
() "Not too bad" not2bad
question "What's your favourite food?" food
[] "Pizza" pizza
[] "Pasta" pasta
[] "Sushi" sushi
[] "Mexican" mexican
Support
Newsgroup
Community Forum
Professional Support
Heiko Sven Moritz Peter Dennis Jan Patrick Sebastian Michael Knut
Behrens Efftinge Eysholdt Friese Hübner Köhnlein Schönbach Zarnekow Clay Wannheden
Individual
3 comments
Comments 1 - 3 of 3 previous next Post a comment