Implementing Domain-Specific Languages with Xtext and Xtend


Implementing Domain-Specific Languages with Xtext and Xtend
eBook: $29.99
Formats: PDF, PacktLib, ePub and Mobi formats
$25.49
save 15%!
Print + free eBook + free PacktLib access to the book: $79.98    Print cover: $49.99
$49.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Reviews
Support
Sample Chapters

Learn to quickly develop a domain-specific language with Xtext Implement any aspect of a DSL using Xtend, a fully featured Java-like programming language Discover how to test a DSL implementation and how to customize runtime and IDE aspects of the DSL

Book Details

Language : English
Paperback : 342 pages [ 235mm x 191mm ]
Release Date : August 2013
ISBN : 1782160302
ISBN 13 : 9781782160304
Author(s) : Lorenzo Bettini
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Preface
Chapter 1: Implementing a DSL
Chapter 2: Creating Your First Xtext Language
Chapter 3: The Xtend Programming Language
Chapter 4: Validation
Chapter 5: Code Generation
Chapter 6: Customizations
Chapter 7: Testing
Chapter 8: An Expression Language
Chapter 9: Type Checking
Chapter 10: Scoping
Chapter 11: Building and Releasing
Chapter 12: Xbase
Bibliography
Index
  • Chapter 1: Implementing a DSL
    • Domain Specific Languages
      • So, why should you create a new language?
    • Implementing a DSL
      • Parsing
      • The Abstract Syntax Tree (AST)
    • IDE integration
      • Syntax highlighting
      • Background parsing
      • Error markers
      • Content Assist
      • Hyperlinking
      • Quickfixes
      • Outline
      • Automatic build
      • Summarizing DSL implementation
    • Enter Xtext
      • Installing Xtext
      • Let's try Xtext
    • The aim of this book
    • Summary
    • Chapter 2: Creating Your First Xtext Language
      • A DSL for entities
        • Creating the project
        • Xtext projects
        • Modifying the grammar
        • Let's try the Editor
      • The Xtext generator
      • The Eclipse Modeling Framework (EMF)
      • Improvements to the DSL
        • Dealing with types
      • Summary
      • Chapter 3: The Xtend Programming Language
        • An introduction to Xtend
          • Using Xtend in your projects
        • Xtend – a better Java with less "noise"
          • Extension methods
          • The implicit variable – it
          • Lambda expressions
          • Multi-line template expressions
          • Additional operators
          • Polymorphic method invocation
          • Enhanced switch expressions
        • Debugging Xtend code
        • Summary
        • Chapter 4: Validation
          • Validation in Xtext
            • Default validators
            • Custom validators
          • Quickfixes
            • Textual modification
            • Model modification
            • Quickfixes for default validators
          • Summary
          • Chapter 5: Code Generation
            • Introduction to code generation
            • Writing a code generator in Xtend
            • Integration with the Eclipse build mechanism
            • Standalone command-line compiler
            • Summary
            • Chapter 6: Customizations
              • Dependency injection
              • Google Guice in Xtext
              • Customizations of IDE concepts
                • Labels
                • The Outline view
                • Customizing other aspects
              • Custom formatting
              • Other customizations
              • Summary
              • Chapter 7: Testing
                • Introduction to testing
                • Junit 4
                • The ISetup interface
                • Implementing tests for your DSL
                  • Testing the parser
                  • Testing the validator
                  • Testing the formatter
                  • Testing code generation
                • Test suite
                • Testing the UI
                  • Testing the content assist
                  • Testing workbench integration
                  • Testing the editor
                  • Other UI testing frameworks
                • Testing and modularity
                • Clean code
                • Summary
                • Chapter 8: An Expression Language
                  • The Expressions DSL
                    • Creating the project
                    • Digression on Xtext grammar rules
                  • The grammar for the Expressions DSL
                  • Left recursive grammars
                    • Associativity
                    • Precedence
                    • The complete grammar
                  • Forward references
                  • Typing expressions
                    • Type provider
                    • Validator
                  • Writing an interpreter
                    • Using the interpreter
                  • Summary
                  • Chapter 9: Type Checking
                    • SmallJava
                      • Creating the project
                      • SmallJava grammar
                        • Rules for declarations
                        • Rules for statements and syntactic predicates
                        • Rules for expressions
                        • The complete grammar
                      • Utility methods
                      • Testing the grammar
                    • First validation rules
                      • Checking cycles in class hierarchies
                      • Checking member selections
                      • Checking return statements
                      • Checking for duplicates
                    • Type checking
                      • Type provider for SmallJava
                      • Type conformance (subtyping)
                      • Expected types
                      • Checking type conformance
                      • Checking method overriding
                    • Improving the UI
                    • Summary
                    • Chapter 10: Scoping
                      • Cross-reference resolution in Xtext
                        • Containments and cross-references
                        • The index
                        • Qualified names
                        • Exported objects
                        • The linker and the scope provider
                        • Component interaction
                      • Custom scoping
                        • Scope for blocks
                        • Scope for inheritance and member visibility
                        • Visibility and accessibility
                        • Filtering unwanted objects from the scope
                      • Global scoping
                        • Packages and imports
                        • The index and the containers
                        • Checking duplicates across files
                      • Providing a library
                        • Default imports
                        • Using the library outside Eclipse
                        • Using the library in the type system and scoping
                      • Dealing with super
                      • What to put in the index?
                      • Additional automatic features
                      • Summary
                      • Chapter 11: Building and Releasing
                        • Release engineering
                          • Headless builds
                          • Target platforms
                          • Continuous integration
                        • Introduction to Buckminster
                          • Installing Buckminster
                        • Using the Xtext Buckminster wizard
                          • Building the p2 repository from Eclipse
                          • Customizations
                          • Defining the target platform
                          • Build headlessly
                        • Maintaining the examples of this book
                        • Summary
                        • Chapter 12: Xbase
                          • Getting introduced with Xbase
                          • The Expressions DSL with Xbase
                            • Creating the project
                            • The IJvmModelInferrer interface
                            • Code generation
                            • Debugging
                          • The Entities DSL with Xbase
                            • Creating the project
                            • Defining attributes
                            • Defining operations
                            • Imports
                          • Customizations
                          • Summary

                          Lorenzo Bettini

                          Lorenzo Bettini is an assistant professor (Researcher) in computer science at Dipartimento di Informatica, Università di Torino, Italy. Previously, he was a Postdoc and a contractual researcher at Dipartimento di Sistemi e Informatica, Università di Firenze, Italy. He has a Masters Degree in computer science and a PhD in theoretical computer science. His research interests cover design, theory, and the implementation of programming languages (in particular, object-oriented languages and network-aware languages). He has been using Xtext since version 0.7. He has used Xtext and Xtend for implementing many Domain Specific Languages and Java-like programming languages. He is also the author of about 60 papers published in international conferences and international journals. You can contact him at http://www.lorenzobettini.it.

                          Code Downloads

                          Download the code and support files for this book.


                          Submit Errata

                          Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                          Errata

                          - 1 submitted: last submission 03 Feb 2014

                          List of Erratas for the book. Errata type: Typo | Page number: 20 The line: The wizard will create three projects and will open the file MyDsl.xtext ,which is the grammar definition of the new DSL we are about to implement. Should be: The wizard will create three projects and will open the file MyDsl.xtext, which is the grammar definition of the new DSL we are about to implement. Errata type: Typo | Page number: 28 The line: In this rule we have three keywords, namely 'entity', 'extends', '{', and '}'. Should be: In this rule we have four keywords, namely 'entity', 'extends', '{', and '}'. Errata type: Technical | Page number: 41 The feature name for the array size in the screenshot should be "dimension" instead of "length". Errata type: Technical | Page number: 42 The UML diagram shows the AttributeType class having an attribute named "dimension". The attribute should be named "length". Errata type: Typo | Page number: 71 With validation you can implement additional constraint checks of a DSL which cannot be done at parsing time. Should be:  With validations you can implement additional constraint checks of a DSL which cannot be done at parsing time. Errata type: Code | Page number: 74 // In order to solve the compiler errors add the following import statements // in front of the class defintion of the class EntitiesValidator: import org.eclipse.xtext.validation.Check import org.example.entities.entities.Entity import org.example.entities.entities.EntitiesPackage Errata type: Code | Page number: 76 // To solve the compiler error add the following import statement: import org.example.entities.entities.Attribute Errata type: Code | Page number: 79 // To solve the compiler errors add the following import statements: import org.eclipse.xtext.ui.editor.quickfix.Fix import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor import org.eclipse.xtext.validation.Issue import org.example.entities.validation.EntitiesValidator Errata type: Code | Page number: 81 // To solve the compiler error add the following import statement: import org.example.entities.entities.Attribute Errata type: Code | Page number: 82 // To solve the compiler error add the following import statement: import org.example.entities.entities.Entity Errata type: Technical | Page number: 105 To get a representation of the AttributeType element, we use an injected extension, TypeRepresentation (it is similar to the way we generate Java code for attribute types in the generator of Chapter 5, Code Generation), in particular its method representation. Should be: To get a representation of the AttributeType element, we use an injected extension, TypeRepresentation (it is similar to the way we generate Java code for attributetypes in the generator of Chapter 5, Code Generation), in particular its method representation (you will find the code for this class "TypeRepresentation" in  the accompanying source code in the folder "org.example.entities.ui/src/org/example/entities/ui/labeling"). Errata type: Technical | Page number: 106 To do this, we create a directory where we place the image files of the icons we want to use. In order to benefit from Xtext's default handling of images, we call the directory icons, and we place two gif images there, Entity.gif and Attribute.gif (for entities and attributes respectively). Should be: To do this, we create a directory in the "org.example.entities.ui" project in Eclipse where we place the image files of the icons we want to use. In order to benefit from Xtext's default handling of images, we call the directory icons, and we place two gif images there, Entity.gif and Attribute.gif (for entities and attributes respectively). You will find the icon files in the accompanying source code in the folder "org.example.entities.ui/icons". Errata type: Typo | Page number: 106 We then define two image methods in EntitesLabelProvider where we only need to return the name of the image files (Xtext will do the rest for us): Should be: We then define two image methods in EntitiesLabelProvider where we only need to return the name of the image files (Xtext will do the rest for us): Errata type: Typo | Page number: 180 @Inject extesion ExpressionsTypeProvider Should be: @Inject extension ExpressionsTypeProvider Errata type: Code | Page number: 193 Add the following rule after the code of the rule "SJVariableDeclaration:" on page 194: SJParameter: type=[SJClass] name=ID; Errata type: Typo | Page number: 200 Note that we have a single rule for referring both to a parameter (SJParamenter) Should be: Note that we have a single rule for referring both to a parameter (SJParameter) Errata type: Code | Page number: 201 Add the following rule after the code of the rule "SJVariableDeclaration:" SJParameter: type=[SJClass] name=ID; Errata type: Code | Page number: 216 class R { public V v; } class P extends R { public R m() { return null; } } class V extends R { public N n; } Should be: class R { V v; } class P extends R { R m() { return null; } } class V extends R { N n; } Remove the keyword "public" to avoid a parsing error. Errata type: Typo | Page number: 241 In Chapter 8, An Expression Language, we solved the problem of forward references by implementinga check in the validator Should be: In Chapter 8, An Expression Language, we solved the problem of forward references by implementing a check in the validator Errata type: Typo | Page number: 254 In general, the validator approach allows you to give better feedback to the user, but it requires you to customize the content assist as shown in the following this section. Should be: In general, the validator approach allows you to give better feedback to the user, but it requires you to customize the content assist as shown in the following section. Errata type: Typo | Page number: 296 Furthermore, by navigating to New | Example... | Xtext Examples | Xtext Domain-Model Example, you can import into your workspace the Domain-Model example that ships with Xtext; Should be: Furthermore, by navigating to File | New | Example... | Xtext Examples | Xtext Domain-Model Example, you can import into your workspace the Domain-Model example that ships with Xtext; Errata type: Code | Page number: 307 @Inject extension IqualifiedNameProvider Should be: @Inject extension IQualifiedNameProvider Errata type: Typo | Page number: 308 First of all, we specify that the the superclass of the mapped class will be the entity's superType if one is given. Should be: First of all, we specify that the superclass of the mapped class will be the entity's superType if one is given.

                          Sample chapters

                          You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                          Frequently bought together

                          Implementing Domain-Specific Languages with Xtext and Xtend +    Oracle Advanced PL/SQL Developer Professional Guide =
                          50% Off
                          the second eBook
                          Price for both: ₨406.40

                          Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                          What you will learn from this book

                          Learn to write an Xtext grammar for a DSL Use Xtend as an alternative to Java to write cleaner, easier-to-read, and more maintainable code Write constraint checks for a DSL using the validator mechanism Learn how to write a code generator and an interpreter for a DSL Discover how automatic symbol resolution works in Xtext and how to customize it Build and deploy a DSL implementation so that others can install it Get well versed with the typical Xtext development workflow

                          In Detail

                          Xtext is an open source Eclipse framework for implementing domain-specific languages together with its IDE functionalities. It lets you implement languages really quickly, and, most of all, it covers all aspects of a complete language infrastructure, starting from the parser, code generator, interpreter, and more. "Implementing Domain-Specific Languages with Xtext and Xtend" will teach you how to develop a DSL with Xtext, an Eclipse framework for implementing domain-specific languages. The chapters are like tutorials that describe the main concepts of Xtext such as grammar definition, validation, code generation, customizations, and many more, through uncomplicated and easy-to-understand examples. Starting with briefly covering the features of Xtext that are involved in a DSL implementation, including integration in an IDE, the book will then introduce you to Xtend as this language will be used in all the examples throughout the book. We then proceed by explaining the main concepts of Xtext, such as validation, code generation, and customizations of runtime and UI aspects. By the end of the book, you will have learned how to test a DSL implemented in Xtext with Junit, in order to follow a test-driven development strategy that will help the developer implement maintainable code that is much faster and cleaner. A test-driven approach is used throughout the book when presenting advanced concepts such as type checking and scoping. The book also shows you how to build and release a DSL so that it can be installed in Eclipse, and gives you hints on how to build the DSL headlessly in a continuous integration server. "Implementing Domain-Specific Languages with Xtext and Xtend" aims to complement the official Xtext documentation to explain the main concepts through simplified examples and to teach the best practices for a DSL implementation in Xtext. It is a Beginner’s Guide which should set you up for professional development DSL and its Eclipse IDE tooling.

                          <html />

                          Approach

                          A step-by-step guide that enables you to quickly implement a DSL with Xtext and Xtend in a test-driven way with the aid of simplified examples.

                          Who this book is for

                          This book is for programmers who want to learn about Xtext and how to use it to implement a DSL (or a programming language) together with Eclipse IDE tooling. It assumes that the user is familiar with Eclipse and its functionality. Existing basic knowledge of a compiler implementation would be useful, though not strictly required, since the book will explain all the stages of the development of a DSL.

                          Code Download and Errata
                          Packt Anytime, Anywhere
                          Register Books
                          Print Upgrades
                          eBook Downloads
                          Video Support
                          Contact Us
                          Awards Voting Nominations Previous Winners
                          Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                          Resources
                          Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software