Pragmatic DSL Design with Xtext, Xbase and Xtend 2

16,340 views
16,180 views

Published on

Tutorial at EclipseCon 2011 (Santa Clara)
Sven Efftinge, Jan Koehnlein, Sebastian Zarnekow

Published in: Technology
0 Comments
20 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
16,340
On SlideShare
0
From Embeds
0
Number of Embeds
6,281
Actions
Shares
0
Downloads
405
Comments
0
Likes
20
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Pragmatic DSL Design with Xtext, Xbase and Xtend 2

    1. 1. Pragmatic DSL Design withSven Efftinge (itemis) / Sebastian Zarnekow (itemis) / Jan Köhnlein (itemis)
    2. 2. The plan for today :
    3. 3. The plan for today :1. Xtext 2.0 What is Xtext and what’s new in 2.0? Hands-On : Develop a DSL.
    4. 4. The plan for today :1. Xtext 2.0 What is Xtext and what’s new in 2.0? Hands-On : Develop a DSL.2. Xbase What’s that and why should I care? Hands-On : Play with it and integrate it in a DSL.
    5. 5. The plan for today :1. Xtext 2.0 What is Xtext and what’s new in 2.0? Hands-On : Develop a DSL.2. Xbase What’s that and why should I care? Hands-On : Play with it and integrate it in a DSL.3. Xtend Yet another language? Are you serious? Hands-On :Write a codegenerator with Xtend.
    6. 6. What is ?
    7. 7. Eclipse Project
    8. 8. IDE Framework
    9. 9. Language Framework
    10. 10. (Domain-Specific)Language Framework
    11. 11. UsersUsers
    12. 12. Eclipse IDE Framework (JFace Text, etc.)
    13. 13. Java Development Tools (JDT)Eclipse IDE Framework (JFace Text, etc.)
    14. 14. Java Development Tools (JDT)Eclipse IDE Framework (JFace Text, etc.)
    15. 15. Java Development Tools (JDT)Eclipse IDE Framework (JFace Text, etc.) Eclipse Modeling Framework (EMF)
    16. 16. Java Development Tools (JDT) Antlr Parser GeneratorEclipse IDE Framework (JFace Text, etc.) Eclipse Modeling Framework (EMF)
    17. 17. is Highly Adaptable
    18. 18. Advanced Language-Specific Editing Features by Default
    19. 19. Advanced Language-Specific Editing Features by Default Syntax Coloring
    20. 20. Advanced Language-Specific Editing Features by Default Syntax Coloring Wizards
    21. 21. Advanced Language-Specific Editing Features by DefaultContent Assist Syntax Coloring Wizards
    22. 22. Advanced Language-Specific Editing Features by DefaultContent Assist Syntax Coloring Code Folding Wizards
    23. 23. Advanced Language-Specific Editing Features by DefaultContent Assist Find References Syntax Coloring Code Folding Wizards
    24. 24. Advanced Language-Specific Editing Features by Default Global IndexingContent Assist Find References Syntax Coloring Code Folding Wizards
    25. 25. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Find References Syntax Coloring Code Folding Wizards
    26. 26. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Find References Syntax Coloring Code Folding Wizards Bracket Matching
    27. 27. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Find References Syntax Coloring Auto Editing Code Folding Wizards Bracket Matching
    28. 28. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Find References Syntax ColoringIncremental Builders Auto Editing Code Folding Wizards Bracket Matching
    29. 29. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Outline View Find References Syntax ColoringIncremental Builders Auto Editing Code Folding Wizards Bracket Matching
    30. 30. Advanced Language-Specific Editing Features by Default Go To Declaration Global IndexingContent Assist Outline View Find ReferencesIncremental Builders Validation Syntax Coloring Auto Editing Code Folding Wizards Bracket Matching
    31. 31. Advanced Language-Specific Editing Features by Default Go To Declaration Template Proposals Global IndexingContent Assist Outline View Find ReferencesIncremental Builders Validation Syntax Coloring Auto Editing Code Folding Wizards Bracket Matching
    32. 32. Advanced Language-Specific Editing Features by Default Go To Declaration Template Proposals Global IndexingContent Assist Outline View Quick Fixes Find ReferencesIncremental Builders Validation Syntax Coloring Auto Editing Code Folding Wizards Bracket Matching
    33. 33. Advanced Language-Specific Editing Features by Default Refactoring Go To Declaration Template Proposals Global IndexingContent Assist Outline View Quick Fixes Find ReferencesIncremental Builders Validation Syntax Coloring Auto Editing Code Folding Wizards Bracket Matching
    34. 34. Advanced Language-Specific Editing Features by Default Refactoring Go To Declaration Template Proposals Global IndexingContent Assist Outline View Quick Fixes Find ReferencesIncremental Builders Validation Syntax Coloring Rich Hovers Auto Editing Code Folding Wizards Bracket Matching
    35. 35. ... but there is a problem with external DSLs
    36. 36. What aboutExpressions?
    37. 37. Expressions are complicated !
    38. 38. Protected Regions
    39. 39. Protected Regions• generated and manually written code mixed up
    40. 40. Protected Regions• generated and manually written code mixed up• manual deletion of artifacts
    41. 41. Protected Regions• generated and manually written code mixed up• manual deletion of artifacts• platform dependent information
    42. 42. Protected Regions• generated and manually written code mixed up• manual deletion of artifacts• platform dependent information• information is spreaded!
    43. 43. Generation Gap Pattern
    44. 44. Generation Gap Pattern technically required type hierarchies
    45. 45. Generation Gap Pattern technically required type hierarchies additional complexity
    46. 46. Generation Gap Pattern technically required type hierarchies additional complexity platform dependent information
    47. 47. Generation Gap Pattern technically required type hierarchies additional complexity platform dependent information information spreaded!
    48. 48. Black boxtarget language literals
    49. 49. Black boxtarget language literals• bound to target platform
    50. 50. Black boxtarget language literals• bound to target platform • no option for MDA friends
    51. 51. Black boxtarget language literals• bound to target platform • no option for MDA friends• no tooling!
    52. 52. Black boxtarget language literals• bound to target platform • no option for MDA friends• no tooling!• long turnarounds!
    53. 53. base
    54. 54. baseModern Expression Language
    55. 55. baseModern Expression LanguageJVM-Integration
    56. 56. baseModern Expression LanguageJVM-IntegrationStatically Typed
    57. 57. baseModern Expression LanguageJVM-IntegrationStatically TypedHighly Reusable
    58. 58. Your Language o.e.xtext.common.TerminalsEMF
    59. 59. Your Language common.Types o.e.xtext.common.TerminalsJDT JVM EMF
    60. 60. Your Language Xbase Ecore Xbase Grammar common.Types o.e.xtext.common.TerminalsJDT JVM EMF
    61. 61. @Overrideprotected IScope createLocalVarScope(~) { if (context instanceof Operation) { IScope actualScope = super.createLocalVarScope(~); Operation operation = (Operation) context; return createScope(operation.getParams(), actualScope); } return super.createLocalVarScope(~);}
    62. 62. Using Xbase
    63. 63. Using XbaseMixin the Grammar
    64. 64. Using XbaseMixin the GrammarJVM Model Inference
    65. 65. Using XbaseMixin the GrammarJVM Model InferenceScoping
    66. 66. Using XbaseMixin the GrammarJVM Model InferenceScopingType System
    67. 67. Using XbaseMixin the GrammarJVM Model InferenceScopingType SystemCompiler / Interpreter
    68. 68. A DSL for Generating Code
    69. 69. Model Code Code Generator
    70. 70. Model Code package base; public class Address { private String street; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } private String city; Code public String getCity() { return city; Generator } public void setCity(String city) { this.city = city; } private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } private String postalCode; public String getPostalCode() { return postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } }
    71. 71. BUT I CANDO THAT IN
    72. 72. package example.codegen;import org.eclipse.xtext.common.types.JvmFormalParameter;import org.eclipse.xtext.common.types.JvmTypeReference;import org.eclipse.xtext.example.domainmodel.domainmodel.Entity;import org.eclipse.xtext.example.domainmodel.domainmodel.Feature;import org.eclipse.xtext.example.domainmodel.domainmodel.Operation;import org.eclipse.xtext.example.domainmodel.domainmodel.Property;public class JavaGenerator { public String compile(Entity e) { StringBuilder b = new StringBuilder(); b.append("package " + packageName(e) + "n"); b.append("n"); b.append("/**n"); b.append(" * Automatically generated. Direct modification is futile.n"); b.append(" */n"); b.append("public class ").append(e.getName()); if(e.getSuperType() != null) { b.append(" extends ").append(compile(e.getSuperType())); } b.append(" {n"); for(Feature f: e.getFeatures()) { if(f instanceof Property) feature((Property)f); else if(f instanceof Operation) feature((Operation)f); } b.append("}n"); return b.toString();
    73. 73. package example.codegen;import org.eclipse.xtext.common.types.JvmFormalParameter;import org.eclipse.xtext.common.types.JvmTypeReference;import org.eclipse.xtext.example.domainmodel.domainmodel.Entity;import org.eclipse.xtext.example.domainmodel.domainmodel.Feature;import org.eclipse.xtext.example.domainmodel.domainmodel.Operation;import org.eclipse.xtext.example.domainmodel.domainmodel.Property;public class JavaGenerator { No Multiline public String compile(Entity e) { StringBuilder b = new StringBuilder(); String Literals b.append("package " + packageName(e) + "n"); b.append("n"); b.append("/**n"); b.append(" * Automatically generated. Direct modification is futile.n"); b.append(" */n"); b.append("public class ").append(e.getName()); if(e.getSuperType() != null) { b.append(" extends ").append(compile(e.getSuperType())); } b.append(" {n"); for(Feature f: e.getFeatures()) { if(f instanceof Property) feature((Property)f); else if(f instanceof Operation) feature((Operation)f); } b.append("}n"); return b.toString();
    74. 74. public String compile(Entity e) { StringBuilder b = new StringBuilder(); b.append("package " + packageName(e) + "n"); b.append("n"); b.append("/**n"); b.append(" * Automatically generated. Direct modification is futile.n"); b.append(" */n"); b.append("public class ").append(e.getName()); if(e.getSuperType() != null) { b.append(" extends ").append(compile(e.getSuperType())); } b.append(" {n"); for(Feature f: e.getFeatures()) { if(f instanceof Property) feature((Property)f); Instanceof else if(f instanceof Operation) feature((Operation)f); Cascades } b.append("}n"); return b.toString(); } public String feature(Property property) { StringBuilder b = new StringBuilder(); String type = compile(property.getType()); String name = property.getName(); b.append("// property ").append(name).append("n"); b.append("private ").append(type).append(" ").append(name).append("n"); b.append("n"); b.append("public ").append(type).append("get").append(name).append("() {n"); b.append(" return ").append(name).append(";n");
    75. 75. } b.append("}n"); return b.toString(); } public String feature(Property property) { StringBuilder b = new StringBuilder(); String type = compile(property.getType()); String name = property.getName(); b.append("// property ").append(name).append("n"); b.append("private ").append(type).append(" ").append(name).append("n"); b.append("n"); b.append("public ").append(type).append("get").append(name).append("() {n"); b.append(" return ").append(name).append(";n"); b.append("}n"); b.append("n"); b.append("public ").append("set").append(name) .append("(").append(type).append(" ").append(name).append("){n"); b.append(" this.").append(name).append(" = ").append(name).append(";n"); b.append("}n"); b.append("n"); return b.toString(); } Noisy String public String feature(Operation operation) { StringBuilder b = new StringBuilder(); Concatenation String name = operation.getName(); b.append("// operation ").append(name).append("n"); b.append(compile(operation.getType())).append(" ").append(name).append("("); boolean isFirst = true; for(JvmFormalParameter p: operation.getParams()) {
    76. 76. Statically Typed
    77. 77. Statically Xbase Typed Expressions
    78. 78. « RICH STRINGS »
    79. 79. POLYMORPHIC dispatch
    80. 80. COMPILES TO
    81. 81. DEPENDENCY@InjectION
    82. 82. TOOLING
    83. 83. Indigo - June 22nd

    ×