Improve template project Common definition
When creating a new project, the syntax definition in
STRING = "\"" StringChar* "\"" StringChar = ~[\"\n] StringChar = "\\\"" StringChar = BackSlashChar BackSlashChar = "\\"
As a new Spoofax user I’m very eager to use this definitions for strings, so in my
Start.StringLit = STRING
because this can be parsed as the string
\"//aor the string
\followed by a comment
//a". Note that not every language is affected, because the program should still be valid under the second interpretation (the comment). Since the comment reaches until the end of the line, this may invalidate certain parses, making the first interpretation the only valid one.
Even though this is an edge case (you need a string ending with a backslash, and the context of the string should allow an ambiguous parse when the rest of the line is a comment), we should try to avoid ambiguous grammars. To prevent new Spoofax users from accidentally creating ambiguous grammars, and to prevent ourselves from creating even more ambiguous grammars, I suggest changing above to:
STRING = "\"" StringChar* "\"" StringChar = ~[\"\n]]] StringChar = "\\\"" StringChar = "\\\\" StringChar = BackSlashChar BackSlashChar = "\\"
this slightly changes the language, because now
"\"is no longer a valid string, but I think this is for the better because this is common in most programming languages.
In light of the other definitions inSubmitted by Martijn on 28 November 2017 at 17:55
Common.sdf3I think this has always been the intention. Someone went through the trouble of defining the rule
BackSlashChar = "\\"and a follow restriction for
BackSlashChar, but since
StringChar = ~[\"\n]allows a backslash the definition of
BackSlashCharand its follow restriction are useless right now.
Yeah, sounds like this was just a bug. I’d be all for changing it, not only in the template but also in all the language that we have that use this definition.
Log in to post comments