Code generation has been a well-established technique in software development for decades. Whether you just need a short script converting data into comma-separated values or you require complex language processing, string segments - the code - must be composed eventually. In my daily work, for example, I use code generation to compare the results of unit tests and integration tests with human-readable expectations.
Template expansion is one way of doing code generation. In web development, for instance, it is a standard tool, as indicated by the introduction of ‘Template Literals’ in ECMAScript 2015 (ES6). Code generation also plays a prominent role in the development of tools for domain-specific languages (DSLs). It enables us to turn definitions in DSLs into executable code and run them. With Eclipse Langium, we can now build DSL tools with web technologies and run them in the browser, on the backend, or within our local CLIs.
Less surprisingly, practice taught us that plain ES6 templates are not the ultimate solution to all possible code generation tasks. While it’s easy to substitute values into single or few lines of strings, composing entire files can be quite cumbersome. Additionally, I envision further features like intelligent indentation handling and the ability for associating ranges of generated code with their representations in the data.
In this talk, I will discuss issues with plain ES6 templates and present ideas for overcoming them using typical web development technologies, like Typescript, Node.js, and browsers. This includes:
- Smart indentation handling enabling properly formatted source code
- Achieving more fine-grained control and tracing of source and target text ranges through an intermediate representation
- Enabling interactive debugging of imperative DSL specifications
The implementations I will use for live demos are open source and partly published within the Langium project. However, the principles and core implementations are mostly generally applicable and not limited to Langium-based DSL tool projects.