Implementing Domain-Specific Languages with Xtext and Xtend

Book description

If you know Eclipse then learning how to implement a DSL using Xtext is a natural progression. And this guide makes it easy to get started through a step-by-step approach accompanied with simple examples.

  • 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

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.

Publisher resources

Download Example Code

Table of contents

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

Product information

  • Title: Implementing Domain-Specific Languages with Xtext and Xtend
  • Author(s): Lorenzo Bettini
  • Release date: August 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781782160304