Welcome to the Tsonnet series!
If you're just joining, you can check out how it all started in the first post of the series.
In the previous post, we explored concatenating strings a bit more:
An essential feature of any programming language is the option of adding comments to the source code.
How can we add memes to our code without comments?! 😜
Jokes aside, let's add comments to Tsonnet.
Let's start with the TDD approach, adding the sample Jsonnet files and the cram tests:
We're going to test inline and multi-line comments at once, while also handling the unterminated block error for multi-line comments.
Let's look at the lexing implementation:
The inline_comment
pattern takes care of swallowing inline comments. For multi-line comments, we need to match the start /*
, keep reading until we find the end */
, and raise an error if the block is not properly terminated.
And there you have it! We've successfully added support for both inline and multi-line comments to Tsonnet.