Providing a library

Our implementation of SmallJava does not yet allow us to make references to types such as Object, String, Integer, and so on. These types would allow us to declare variables initialized with constant expressions. In this section we show how to create a library with predefined types.

One might be tempted to hardcode these classes/types directly in the grammar, but this is not the best approach. There are many reasons for not doing that; mostly, that the grammar should deal with syntax only. Moreover, if we hardcoded, for example, Object in the grammar, we would only be able to use it as a type, but what if we wanted Object to have some methods? We would not be able to express that in the grammar.

Instead, we will follow the

Get Implementing Domain-Specific Languages with Xtext and Xtend now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.