Compositional Language Engineering using
Generated, Extensible, Static Type-Safe Visitors

Robert Heim, Pedram Mir Seyed Nazari, Bernhard Rumpe, and Andreas Wortmann

Abstract

Language workbenches usually produce infrastructure to represent models as abstract syntax trees (AST) and employ analysis, transformation, and code generation techniques largely based on visitors. The visitor pattern and its derivatives suffer from the expression problem when it comes to extensibility and reuse. Current approaches either forsake static type safety, require features unavailable in popular object-oriented languages (e.g., open classes), or rely on procedural abstraction and thereby give up the object-oriented data encapsulation (the AST) itself. Our approach to visitors exploits knowledge about the AST structure under development and generation of statically type-safe external visitor interfaces that support extensibility in two dimensions: defining new operations by implementing the interface and extending the underlying data structure, usually without requiring adaptation of existing implemented visitors. We present a concept of visitor development for language engineering that enables an adaptable traversal and provides hook points for implementing concrete visitors. This approach is applicable to single DSL implementation and to language composition. It thus enables a transparent, easy to use, and static type-safe solution for the typical use cases of language analysis and transformation.

Supporting materials

The Maven project in visitors.zip demonstrates usage of the visitor infrastructure of the language workbench MontiCore. It consists of three (simplified) languages:

  • Classdiagram
    Grammar: visitors.zip/visitors/src/main/grammars/de/monticore/visitors/CD.mc4
  • Automaton
    Grammar: visitors.zip/visitors/src/main/grammars/de/monticore/visitors/Automaton.mc4
  • Classdiagram with embedded automata
    Grammar: visitors.zip/tree/master/visitors/src/main/grammars/de/monticore/visitors/­CDWithAutomaton.mc4

Requirements

  • Java 8 SDK
  • Maven 3+

Build

Extract the visitor.zip. To build the project run mvn clean install from the visitor folder that contains the pom.xml. MontiCore generates files to folder visitors/target/generated-sources/monticore/sourcecode. Put this folder on the class path after building the project.

Examples

All languages define pretty printers:

  • CDPrettyPrinter
    visitors.zip/visitors/src/main/java/de/monticore/visitors/cd/CDPrettyPrinter.java
  • AutomatonPrettyPrinter
    visitors.zip/visitors/src/main/java/de/monticore/visitors/automaton/AutomatonPrettyPrinter.java
  • CDWithAutomatonPrettyPrinter
    visitors.zip/visitors/src/main/java/de/monticore/visitors/cdwithautomaton/­CDWithAutomatonPrettyPrinter.java

The pretty printer for classdiagrams with automata is composed of the other ones. The CDWithAutomatonTest [1] demonstrates its usage and usage of a method counter [2].

[1] visitors.zip/visitors/src/test/java/de/monticore/visitors/CDWithAutomatonTest.java
[2] visitors.zip/visitors/src/main/java/de/monticore/visitors/cdwithautomaton/MethodCounter.java