Based upon your example, it's not clear when a newline is required. Do you want to require a newline everywhere that you had a new line, or only after the "then" keyword where you placed the "{newline}"?
For simplicity sake, I assumed that you only want a newline to follow the "then" keyword.
To do this, I made the LF CR part of the "then" token. If you try the code below you'll see that it does what you asked.
language Sample1 |
{ |
// Define whitespace tokens |
token CR = "\u000A"; |
token LF = "\u000D"; |
token Space = " "; |
|
// Define if statement tokens, including newline characters |
token iftoken = "if"; |
token then = "then" LF CR; |
token end = "end"; |
|
// These would probably have their own syntax, but to simplify only accept these constants for now |
token expression = "expression"; |
token body = "body"; |
|
syntax Main = iftoken expression then body end; |
|
// Ignore whitespace |
interleave Whitespace = CR | LF | Space; |
} |
|
However, when testing this the grammar would fail whenever I had trailing spaces after "then" and before the newline. This is not very friendly, so I made a small change to support trailing spaces:
language Sample2 |
{ |
// Define whitespace tokens |
token CR = "\u000A"; |
token LF = "\u000D"; |
token Space = " "; |
token Spaces = Space*; |
|
// Define if statement tokens, including trailing spaces and newline characters |
token iftoken = "if"; |
token then = "then" Spaces LF CR; |
token end = "end"; |
|
// These would probably have their own syntax, but to simplify only accept these constants for now |
token expression = "expression"; |
token body = "body"; |
|
syntax Main = iftoken expression then body end; |
|
// Ignore whitespace |
interleave Whitespace = CR | LF | Space; |
} |
Be aware that the spaces and the newline characters will be in your output MGraph as part of your token unless you modify your output with a projection.
Erik