.NET Framework Developer Center >
.NET Development Forums
>
SQL Server Modeling
>
MGrammar: Enforcing new line at end of statement?
MGrammar: Enforcing new line at end of statement?
- Lets say that I have a grammar, and I want to treat line breaks as whitespace.
But I also want to enforce that a new line is at the end of each statement.
eg:
if expression then {newline}
body
end
How do I do that with MGrammar?
Answers
- Thanks.
I solved it by removing the LF character from my whitespace list and then placing an LF token in my syntax definitions instead of in my tokens as in your examples.
This way I have no problems with trailing whitespace nor do I have to care about it in the syntax.
//Roger- Marked As Answer byMike Weinhardt Tuesday, November 25, 2008 1:48 AM
All Replies
- 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 - Thanks.
I solved it by removing the LF character from my whitespace list and then placing an LF token in my syntax definitions instead of in my tokens as in your examples.
This way I have no problems with trailing whitespace nor do I have to care about it in the syntax.
//Roger- Marked As Answer byMike Weinhardt Tuesday, November 25, 2008 1:48 AM

