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

Eclipse Live Webinar on


     Sven Efftinge, Heiko Behrens
                 itemis
Agenda
      What is Xtext?
   DSLs - An Example
Underlying Technology
     Advanced Topics
Questions & Answers
?
What is
A Language Framework
Yet another
 Parser Generator?



JavaCC
Yet another
 Parser Generator?

                      t ha t!
               than
           ore
      it’sm
No,
JavaCC
How does Xtext
      relate to ...

IDE Meta-tooling Platform (IMP)


Dynamic Language Toolkit (DLTK)
How does Xtext
      relate to ...

IDE Meta-tooling Platform (IMP) !
                             at
                      an th
              o re th
      t is m
 Xt ex Language Toolkit (DLTK)
Dynamic
Is it a language workbench?


JetBrains Meta Programming System




 Intentional Domain Workbench       Microsoft Oslo
Is it a language workbench?

                           le te ly
                        m p        rc e!
                it’s co     s  ou
             ut          en
JetBrains Meta Programming System
          ,b          op
    Y es         a nd
          as ed
 t ex t-b
 Intentional Domain Workbench       Microsoft Oslo
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.
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.
Rd2-c2
“ Queen to c7.
              Check.”




  “ Rd2-c2 ,
rook at d2 moves to c2.”
Moves in Chess:
!ook at a1 moves to a5.
    P
    iece           S   q uare      A
                                   ction
                                           De   stin ation


"ishop at c8 captures knight at h3.n
    P
    iece             S   q uare  io
                                           Action
                                                             D
                                                             es tinat


# b1 x c3
Piece S   qua re ction stination
              AD      e


$2 - g4
                       ation
S A D
quar
    e   ction e   stin
Rook     a1    move      a5
              Bishop   c8   capture    h3   Knight
              Knight   b1   capture    c3   Queen
               Pawn    g2    move      g4


    Game                     Move
                         Source                      «enum»
WhitePlayer
                       * Destination                  Piece
BlackPlayer
                         Piece
"ishop at c8 captures knight at h3

           " c8 x h3
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
DEMO
Underlying Technology
ar
Model




                                    m
                                   m
                               ra
                               G
                             Generator



        Runtime
                  Superclass




                  Subclass              Class




 LL(*) Parser   ecore meta model                editor
Java Persistence API (JPA)
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() {                               entity Customer {
!    !     return id;
!    }                                                   ! property name : String
!    public void setId(Long id) {                        ! property address : Address
!    !     this.id = id;
!    }                                                   ! property orders : Order[]
!    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;
!    }
}
DEMO
ar
Model




                                    m
                                   m
                               ra
                               G
                                                Generator Fragments
                             Generator



        Runtime
                  Superclass




                  Subclass              Class




 LL(*) Parser   ecore meta model                      editor
Composable
    Code Generator
! generator is composed of “fragments”
! fragments can contribute to:
 ! Manifest.MF
 ! plugin.xml
 ! Guice modules
! Add your own fragments
ar
Model




                                    m
                                   m
                               ra
                               G
                             Generator



        Runtime




                                                         Google Guice
                  Superclass




                  Subclass              Class




 LL(*) Parser   ecore meta model                editor
licensed by http://www.wordle.net/
MyCustomLabelProvider




            licensed by http://www.wordle.net/
Dependency Injection with
            Googlee




!No dependency to concrete implementation
!No explicit construction
!Easy testing
!Easy composition
Configuration of Components
            in Xtext
/**
  * used to register components to be used within the IDE.
  */
public class DomainmodelUiModule
! ! extends GeneratedDomainmodelUiModule {
!
! @Override
! public Class<? extends ILabelProvider> bindILabelProvider() {
! ! return MySpecialLabelProvider.class;
! }
}
DEMO
Advanced Topics
Integration with EMF
                                                  eclipse

                                 Any EMF-based   modeling
Code Generator    GMF Editor                                P R O J E C T
                                  Component




                 <<abstract>>                    XMI
                                 XMIResource
                  Resource




                 XtextResource                   Text




    Parser          Linker         Serializer




ValueConverter   ScopeProvider     Formatter
Working with Xtext
// initialization and setup (only needed in standalone mode)
new EntityStandaloneSetup().createInjectorAndDoEMFRegistration();

// standard EMF API usage
ResourceSet resourceSet = new ResourceSet();
Resource resource = resourceSet.getResource(uriToModelFile, true);
Model model = (Model) resource.getContents().get(0);

// print the name of all types to the console
for (Type type : model.getElements()) {
  System.out.println(type.getName());
}
Eclipse Helios
•Model Index
• Grammar Mixins
• Base Language
• UI Features
Questions & Answers

Xtext Webinar

  • 1.
    http://live.eclipse.org/node/705 Eclipse Live Webinaron Sven Efftinge, Heiko Behrens itemis
  • 2.
    Agenda What is Xtext? DSLs - An Example Underlying Technology Advanced Topics Questions & Answers
  • 3.
  • 4.
  • 5.
    Yet another ParserGenerator? JavaCC
  • 6.
    Yet another ParserGenerator? t ha t! than ore it’sm No, JavaCC
  • 7.
    How does Xtext relate to ... IDE Meta-tooling Platform (IMP) Dynamic Language Toolkit (DLTK)
  • 8.
    How does Xtext relate to ... IDE Meta-tooling Platform (IMP) ! at an th o re th t is m Xt ex Language Toolkit (DLTK) Dynamic
  • 9.
    Is it alanguage workbench? JetBrains Meta Programming System Intentional Domain Workbench Microsoft Oslo
  • 10.
    Is it alanguage workbench? le te ly m p rc e! it’s co s ou ut en JetBrains Meta Programming System ,b op Y es a nd as ed t ex t-b Intentional Domain Workbench Microsoft Oslo
  • 11.
    Xtext is acomplete environment for development of textual - programming languages and - domain-specific languages. It is implemented in Java and is based on Eclipse, EMF, and Antlr.
  • 12.
    Domain-Specific Language (DSL) ADSL 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.
  • 13.
  • 14.
    “ Queen toc7. Check.” “ Rd2-c2 , rook at d2 moves to c2.”
  • 15.
    Moves in Chess: !ookat a1 moves to a5. P iece S q uare A ction De stin ation "ishop at c8 captures knight at h3.n P iece S q uare io Action D es tinat # b1 x c3 Piece S qua re ction stination AD e $2 - g4 ation S A D quar e ction e stin
  • 16.
    Rook a1 move a5 Bishop c8 capture h3 Knight Knight b1 capture c3 Queen Pawn g2 move g4 Game Move Source «enum» WhitePlayer * Destination Piece BlackPlayer Piece
  • 17.
    "ishop at c8captures knight at h3 " c8 x h3
  • 18.
    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
  • 19.
  • 20.
  • 21.
    ar Model m m ra G Generator Runtime Superclass Subclass Class LL(*) Parser ecore meta model editor
  • 22.
    Java Persistence API(JPA) 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() { entity Customer { ! ! return id; ! } ! property name : String ! public void setId(Long id) { ! property address : Address ! ! this.id = id; ! } ! property orders : Order[] ! 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; ! } }
  • 23.
  • 24.
    ar Model m m ra G Generator Fragments Generator Runtime Superclass Subclass Class LL(*) Parser ecore meta model editor
  • 25.
    Composable Code Generator ! generator is composed of “fragments” ! fragments can contribute to: ! Manifest.MF ! plugin.xml ! Guice modules ! Add your own fragments
  • 26.
    ar Model m m ra G Generator Runtime Google Guice Superclass Subclass Class LL(*) Parser ecore meta model editor
  • 27.
  • 28.
    MyCustomLabelProvider licensed by http://www.wordle.net/
  • 29.
    Dependency Injection with Googlee !No dependency to concrete implementation !No explicit construction !Easy testing !Easy composition
  • 30.
    Configuration of Components in Xtext /** * used to register components to be used within the IDE. */ public class DomainmodelUiModule ! ! extends GeneratedDomainmodelUiModule { ! ! @Override ! public Class<? extends ILabelProvider> bindILabelProvider() { ! ! return MySpecialLabelProvider.class; ! } }
  • 31.
  • 32.
  • 33.
    Integration with EMF eclipse Any EMF-based modeling Code Generator GMF Editor P R O J E C T Component <<abstract>> XMI XMIResource Resource XtextResource Text Parser Linker Serializer ValueConverter ScopeProvider Formatter
  • 34.
    Working with Xtext //initialization and setup (only needed in standalone mode) new EntityStandaloneSetup().createInjectorAndDoEMFRegistration(); // standard EMF API usage ResourceSet resourceSet = new ResourceSet(); Resource resource = resourceSet.getResource(uriToModelFile, true); Model model = (Model) resource.getContents().get(0); // print the name of all types to the console for (Type type : model.getElements()) { System.out.println(type.getName()); }
  • 35.
    Eclipse Helios •Model Index •Grammar Mixins • Base Language • UI Features
  • 37.