Syntax ambiguity?
Phil Pilgrim (PhiPi)
Posts: 23,514
The following code fragment compiles and runs as I expect it to:
However, if I try to comment it out with braces {}, I get an error:
What's happening is that the compiler is seeing the right brace inside the string on the third line as the end of the comment, rather than the one at the end of the line. I realize the intent of the commented code could well be ambiguous. After all, which should take precedence, the braces, or the quotes? Or to put it in other terms, if the parser is inside a comment, should it be doing anything else besides looking for the end of the comment?
I really don't know the answer here. I guess my preference would be to honor balanced quotes within a comment. But that could lead to other unintended consequences. Maybe a "super comment" would be the thing for cases like this: <comment> </comment>. (I have the same problem with {{ and }}, too, so they don't help.)
Anyway, no answers. I'm not even sure what the question is. Just a comment.
-Phil
t.say(string("#1mae d'og~ is n/ot u k\at. its tr''{oo}. ae can pr''{oo}v it.")) t.say(string("#0en espany/oal porfuv/or.")) t.say(string("}#1mee p'ayrroa n/oa es] oon g'ahtoa. e es va rd'ad. pw/aydoa ]proab''arloa."))
However, if I try to comment it out with braces {}, I get an error:
{ t.say(string("#1mae d'og~ is n/ot u k\at. its tr''{oo}. ae can pr''{oo}v it.")) t.say(string("#0en espany/oal porfuv/or.")) t.say(string("}#1mee p'ayrroa n/oa es] oon g'ahtoa. e es va rd'ad. pw/aydoa ]proab''arloa.")) }
What's happening is that the compiler is seeing the right brace inside the string on the third line as the end of the comment, rather than the one at the end of the line. I realize the intent of the commented code could well be ambiguous. After all, which should take precedence, the braces, or the quotes? Or to put it in other terms, if the parser is inside a comment, should it be doing anything else besides looking for the end of the comment?
I really don't know the answer here. I guess my preference would be to honor balanced quotes within a comment. But that could lead to other unintended consequences. Maybe a "super comment" would be the thing for cases like this: <comment> </comment>. (I have the same problem with {{ and }}, too, so they don't help.)
Anyway, no answers. I'm not even sure what the question is. Just a comment.
-Phil
Comments
I think the same applies here. Most modern IDEs have a comment/uncomment button that applies/removes the line comment on selected text (hell, I distinctly remember this feature being in VB3 for Windows); I suspect there's something similar in the Tool somewhere. (I use it only as a loader, so I can't point to where.)
(I am a huge opponent of comment nesting. If the brace in your string had been an open-brace, the rest of your entire program would become a comment -- unless it parsed strings too. So then if you had a single quote, it would become a String -- unless it parsed escape characters. This goes on eternally. Comment-to-end-of-line is an easy workaround for this, though in Java it has one gotcha. Anyone in the audience who can tell me what it is gets a pony.)
(Note: there is no actual pony. But I could probably ASCII one.)
I actually like the idea of nested comments. It's a handy tool that serves a useful purpose. But there needs to be a way to disambiguate the meta-syntax used by these comments from the syntax of the language itself. In a language like Spin that's line-oriented (as opposed to being free-form like Perl), this could easily be accomplished by requiring that nested comment openers and closers occupy the entire line, rather than being embedded within a line with other code. In Spin, this might look something like this:
There can be no ambiguity here, since you can't split program statements between lines.
There's still room for the embedded nested comments, too, in places like this:
There is no other reasonable way to accomplish what the {}'s do here for readibility.
-Phil
Post Edited (Phil Pilgrim (PhiPi)) : 11/8/2006 3:49:08 AM GMT
Don't get me wrong -- I'm not saying there should be no inline comments. Simply that, for disabling blocks of code like you're doing, line-oriented comments make life much easier.