How do I add a newline character as part of Rascal’s Syntax Definition. I tried this but it doesn’t seem work.
syntax Entity
= @Foldable entity: "entity" Id name "{" {Field “\n”}+ fields "}"
;
syntax Field
= field: Id name ":" Type t ;
Typically you would add a layout
definition in scope, like so:
layout Whitespace = [\ \n\t\r]* !>> [\ \n\t\r];
After this is added, the other rules are augmented with the Whitespace
non-terminal everywhere before the parser is generated.
For example, internally Field+
becomes {Field Whitespace}+
en Field
becomes Id name Whitespace ":" Whitespace Type t
.
So that adds Whitespace
in between all the symbols. To be able to accept newlines and spaces also before and after the top-non-terminal add start
to it:
start syntax TranslationUnit = Entity*;
This will add this rule automatically:
syntax start[TranslationUnit] = Whitespace {Entity Whitespace}* Whitespace;