compiler plug-in

Step by step Scala Compiler plug-in

In this article I'll try to show how to implement a basic Scala compiler plug-in. My starting point is this excellent article from Lex Spoon which provides the main information required to understand how compiler plug-ins work. It gives a simple example: a static code checker that raises compilation errors as soon as a division by zero occurs (division by the constant 0).

A compiler plug-in is some Scala code that can be injected as a specific phase of the compilation process of a given project and that allows you to access to the Abstract Syntax Tree (AST) corresponding to the Scala source code that is being compiled. Then it's a matter of matching the proper patterns, raise erros and warnings and, if needed, transform the AST (this, I did not try so far...).

Our goal in this article is to implement the so called "mutable-phobia" plug-in which raises errors as soon as a var definition gets detected !!!
Here is step by step how to do this:
Syndicate content