O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition

Book Description

Learn how to implement a DSL with Xtext and Xtend using easy-to-understand examples and best practices

About This Book

  • Leverage the latest features of Xtext and Xtend to develop a domain-specific language.
  • Integrate Xtext with popular third party IDEs and get the best out of both worlds.
  • Discover how to test a DSL implementation and how to customize runtime and IDE aspects of the DSL

Who This Book Is For

This book is targeted at programmers and developers who want to create a domain-specific language with Xtext. They should have a basic familiarity with Eclipse and its functionality. Previous experience with compiler implementation can be helpful but is not necessary since this book will explain all the development stages of a DSL.

What You Will Learn

  • Write Xtext grammar for a DSL;
  • Use Xtend as an alternative to Java to write cleaner, easier-to-read, and more maintainable code;
  • Build your Xtext DSLs easily with Maven/Tycho and Gradle;
  • Write a code generator and an interpreter for a DSL;
  • Explore the Xtext scoping mechanism for symbol resolution;
  • Test most aspects of the DSL implementation with JUnit;
  • Understand best practices in DSL implementations with Xtext and Xtend;
  • Develop your Xtext DSLs using Continuous Integration mechanisms;
  • Use an Xtext editor in a web application

In Detail

Xtext is an open source Eclipse framework for implementing domain-specific languages together with IDE functionalities. It lets you implement languages really quickly; most of all, it covers all aspects of a complete language infrastructure, including the parser, code generator, interpreter, and more.

This book will enable you to implement Domain Specific Languages (DSL) efficiently, together with their IDE tooling, with Xtext and Xtend. Opening with brief coverage of Xtext features involved in 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. You will then explore the typical programming development workflow with Xtext when we modify the grammar of the DSL.

Further, the Xtend programming language (a fully-featured Java-like language tightly integrated with Java) will be introduced. We then explain the main concepts of Xtext, such as validation, code generation, and customizations of runtime and UI aspects. You will have learned how to test a DSL implemented in Xtext with JUnit and will progress to advanced concepts such as type checking and scoping. You will then integrate the typical Continuous Integration systems built in to Xtext DSLs and familiarize yourself with Xbase. By the end of the book, you will manually maintain the EMF model for an Xtext DSL and will see how an Xtext DSL can also be used in IntelliJ.

Style and approach

A step-by step-tutorial with illustrative examples that will let you master using Xtext and implementing DSLs with its custom language, Xtend.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition
    1. Table of Contents
    2. Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition
    3. Credits
    4. Foreword
    5. About the Author
    6. Acknowledgments
    7. About the Reviewer
    8. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    9. 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
    10. Preface to the second edition
    11. 1. Implementing a DSL
      1. Domain-Specific Languages
        1. Need for a new language
      2. Implementing a DSL
        1. Parsing
        2. The Abstract Syntax Tree (AST)
      3. IDE integration
        1. Syntax highlighting
        2. Background validation
        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
    12. 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
    13. 3. Working with the Xtend Programming Language
      1. An introduction to Xtend
        1. Using Xtend in your projects
      2. Xtend – a better Java with less "noise"
        1. Types
        2. Methods
        3. Fields and Variables
        4. Operators
        5. Syntactic sugar
        6. Static members and inner types
        7. Literals
        8. Extension methods
        9. The implicit variable – it
        10. Lambda expressions
        11. Multi-line template expressions
      3. Additional operators
        1. Polymorphic method invocation
        2. Enhanced switch expressions
        3. Other Xtend expressions
        4. Xtend IDE
        5. Debugging Xtend code
      4. Summary
    14. 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
    15. 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
    16. 6. Customizing Xtext Components
      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
    17. 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. Learning Tests
        5. Testing the outline
        6. Other testing frameworks
      7. Testing and modularity
      8. Clean code
      9. Summary
    18. 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
        1. Custom Content Assist
      5. Typing expressions
        1. Loose type computation, strict type checking
        2. Type computer
        3. Validator
      6. Writing an interpreter
        1. Using the interpreter
      7. Optimizations and fine tuning
      8. Summary
    19. 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. Rule fragments
          5. 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 computer for SmallJava
        2. Type conformance (subtyping)
        3. Expected types
        4. Checking type conformance
        5. Checking method overriding
      4. Improving the UI
      5. Summary
    20. 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. Classes of the same package
      6. Dealing with super
      7. What to put in the index?
      8. Additional automatic features
      9. Providing a project wizard
      10. Summary
    21. 11. Continuous Integration
      1. Eclipse features and p2 repositories
      2. Release engineering
        1. Headless builds
        2. Target platforms
        3. Continuous integration
      3. Introduction to Maven/Tycho
      4. Using the Xtext project wizard
        1. Running the Maven build
        2. Customizing the feature
        3. Using and customizing the target platform
        4. Customizing the pom files
      5. Continuous Integration systems
        1. Maintaining the examples of this book
      6. Your DSL editor on the Web
      7. IntelliJ and Gradle
      8. Pitfalls with Maven/Tycho
        1. Versioning
        2. PDE test problems
      9. Concluding remarks
        1. Installation requirements
        2. Make contributions easy
      10. Summary
    22. 12. Xbase
      1. Introduction to Xbase
        1. The common Java type model
      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
        5. Validation
      4. Additional Xbase features
        1. Annotations
        2. Reusing Java error markers
        3. Using 'extension'
        4. Using type parameters
        5. Formatter
        6. Further Customizations
      5. Summary
    23. 13. Advanced Topics
      1. Creating an Xtext project from an Ecore model
        1. Defining the Ecore model
        2. Creating the Xtext project
        3. Fixing the StandaloneSetup
        4. Tweaking the Ecore model
        5. Derived State
        6. Adding new rules to the language
      2. Switching to an imported Ecore model
        1. Generating EMF classes during the build
        2. Customizing the EMF Java classes
      3. Xcore
        1. Creating the Xcore project
        2. Creating the Xtext project from an existing Xcore model
        3. Modifying the Xcore model
      4. Extending Xbase
        1. Overriding a rule in an Xtext grammar
        2. Customizing the type system
        3. Testing the type computer
        4. Customizing the validator
        5. Customizing the compiler
        6. Using the type system in the compiler
        7. Testing the compiled code
        8. Improving code generation
        9. Smoke tests
      5. Summary
    24. 14. Conclusions
    25. A. Bibliography
    26. Index