Weitere ähnliche Inhalte Ähnlich wie Building DSLs With Eclipse (20) Mehr von Peter Friese (20) Kürzlich hochgeladen (20) Building DSLs With Eclipse1. 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
23. Because you can use it for other stuff, too.
E.g., opening a bottle of wine.
39. <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>
40. <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>
48. 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
56. @SuppressWarnings("serial")
@Entity
@Table(name = "CUSTOMER_INFO")
public class CustomerInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idSeq")
@SequenceGenerator(name = "idSeq", sequenceName = "CUST_SEQ", allocationSize = 1)
@Column(name = "CUST_ID", nullable = false)
private String customerId;
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustomerId() {
return customerId;
}
@Column(name = "EMAIL", nullable = false, length = 128)
private String emailAddress;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
String oldValue = emailAddress;
this.emailAddress = emailAddress;
firePropertyChangedEvent("emailAddress", oldValue, this.emailAddress);
}
57. @SuppressWarnings("serial")
@Entity
@Table(name = "CUSTOMER_INFO")
public class CustomerInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idSeq")
@SequenceGenerator(name = "idSeq", sequenceName = "CUST_SEQ", allocationSize = 1)
@Column(name = "CUST_ID", nullable = false)
private String customerId;
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public String getCustomerId() {
return customerId;
}
@Column(name = "EMAIL", nullable = false, length = 128)
private String emailAddress;
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
String oldValue = emailAddress;
this.emailAddress = emailAddress;
firePropertyChangedEvent("emailAddress", oldValue, this.emailAddress);
}
64. ar
Model
m
m
ra
G
Generator
Runtime
Superclass
Subclass Class
LL(*) Parser ECore meta model Editor
65. ar
Model
m
m
ra
G
Generator
Runtime
Superclass
Subclass Class
LL(*) Parser ECore meta model Editor
66. 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;
67. 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;
68. 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;
69. 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;
70. 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;
71. 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;
72. 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;
73. 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;
76. 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
81. Tooltips
Scoping
Outline
Label Provider
Validation Hyperlinking / Navigation
AST
Code Formatting
Content Assist
Folding
Quick Fix
Datatype Converter
Parser
Syntax Highlighting
82. Tooltips
Scoping
Outline
Hyperlinking / Navigation
AST
Code Formatting
Content Assist
Folding
Quick Fix
Datatype Converter
Parser
Syntax Highlighting
83. Tooltips
Scoping
Outline
Label Provider
Validation Hyperlinking / Navigation
AST
Code Formatting
Content Assist
Folding
Quick Fix
Datatype Converter
Parser
Syntax Highlighting
88. Heiko Sven Moritz Peter Dennis Jan Patrick Sebastian Michael Knut
Behrens Efftinge Eysholdt Friese Hübner Köhnlein Schönbach Zarnekow Clay Wannheden
Individual
Hinweis der Redaktion itemis = IT + Themis
Themis, goddess of
justice
order
knows the future connect people
share contact details
your resume on the web (endorsements, who you know, what you&#x2019;ve done) connect people / friends
&#x201C;friend&#x201D; someone / &#x201C;write on their wall&#x201D;
share photos
fun-oriented connect people
everybody can have their say Java = GPL
You can do everything with Java
But is it a good idea to do everything with Java? concise
precise
wouldn&#x2019;t change anything ANT, built on a single transatlantic flight
using XML to avoid writing own parser
leverage XML parser
today, we&#x2019;d do it differently ANT, built on a single transatlantic flight
using XML to avoid writing own parser
leverage XML parser
today, we&#x2019;d do it differently It&#x2019;s relatively easy to write an internal DSL Syntax of host language is the limiting factor for implementing internal DSLs External DSLs are very flexible - you can exactly adjust them to your needs You can choose the syntax of your DSL to your liking However, building external DSLs is complicated Java = GPL
You can do everything with Java
But is it a good idea to do everything with Java? Newsgroup
Foren
Prof. Support