Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

another indentation bordercase #3010

Closed
loveencounterflow opened this issue May 29, 2013 · 18 comments
Closed

another indentation bordercase #3010

loveencounterflow opened this issue May 29, 2013 · 18 comments
Labels

Comments

@loveencounterflow
Copy link

looks like this is becoming a habit: #2998

all of these will compile & run just fine:

if true
# ok
  x = 1

if true
  # ok
  x = 1

if true
  ### ok ###
  x = 1

but not this one:

if true
### oops ###
  x = 1

the error message says, unhelpfully:

error: unexpected POST_IF
if true
^^

which makes it look like i had inserted an expression, not a comment at the point in question.

use case:

f = ->
  ###TAINT this clause will always be executed ###
  if true
    x = 1
  ###TAINT else clause is unreachable ###
  else
    x = 0

i like to comment on clauses etc before the indent when the comment is about the entire clause. that's ok with ordinary comments, but not w/ herecomments (which have the distinction to be preserved for the accidental JS aficionado).

incidentally, the error message is just as unhelpful in this case:

error: unexpected INDENT
###TAINT else clause is unreachable ###
                                      ^ 

i can somehow understand & live w/ the first message, but this is bollocks: how can the last character on a line not be correctly indented? or have an unexpected indent? only line-initial linear whitespace followed by non-whitespace ('printing') characters can ever constitute indentation and is, therefore, the only eligible entity for this kind of complaint. other stuff–the rest of the line, blank lines, and, i'd've thought, comments, too–are simply not in a position for that (quite literally).

(i'll try and get a module online the next few days that does nothing but line-initial whitespace parsing. btw i was a little sad the other day when realizing that CS Redux would appear to have major issues concerning indentation, which seems to support my hunch that whitespace grammar should be kept apart from the rest of the language.)

@michaelficarra
Copy link
Collaborator

There are, like, 1000 of these. Please search next time. The error could be improved, so I guess I'll leave it open for that.

CS Redux would appear to have major issues concerning indentation

Not to my knowledge.

@vendethiel
Copy link
Collaborator

Could maybe be merged with #1120 (or #1131).

@loveencounterflow
Copy link
Author

is there any effort to writeup on the rules of CS? i mean nowhere does the homepage tell me this. i think it's important to tell people; also, with unfixed border-cases, it would be great to tell people to adhere to a certain 'best-practice style' to avoid breakage in the wild once something gets fixed. like "though you can write x%6[][] right now and it does evaluate to 42, this is a weakness in the language so don't do it". also adding "for the time being, block comments must obey the same indentation rules as expressions, so these examples won't fly" might be much easier than fixing the thing (i know from experience, it's hairy).

@vendethiel
Copy link
Collaborator

I think it'd actually be more surprised if, for some reason, some constructs of the language did not follow the same rules (usability not being taken into account)

@loveencounterflow
Copy link
Author

@Nami-Doc not sure i understand that

@vendethiel
Copy link
Collaborator

A block is a indent, some lines, then a dedent. The reason why singe-line comments aren't part of this is because we just ignore them (since we don't "render" them).

@loveencounterflow
Copy link
Author

the documentation-worthy part of this is that block comments are rendered as JS comments, so they get a 100% ignorance from the next turtle down the way. this makes them exceptional since you can never do anything of computational relevance with them... they're not expressions since they cannot express computation, they're not expressions 'cause the cannot mix with expressions, yet the must obey the whitespace formatting rules of expressions. they end up as something that could, as far as JS is concerned, be placed almost anywhere (except inside a string literal) without affecting functionality at all. makes them an underdocumented strange beast... 'everything is an expression in CS', except for comments, which we ignore, and block comments, which are strange.

btw i just found that there is also an identity crisis here ;-) f + 2 ### ooops ### gives

error: unexpected HERECOMMENT
f + 2 ### ooops ###
      ^^^^^^^^^^^^^

which rightly marks and delineates the culprit, but misidentifies it as HERECOMMENT, where the docs call it a block comment. now this is nitpicking on my part, sorry, just saying.

@vendethiel
Copy link
Collaborator

That's the correct name.

they're not expressions since they cannot express computation

They're Comments, which is part of Statement.

@loveencounterflow
Copy link
Author

... expression-phobic, indentation-scopic comments ... and underdocumented

@loveencounterflow
Copy link
Author

wait, what's the correct name? to quote (https://github.com/jashkenas/coffee-script/blob/master/src/grammar.coffee#L180) :

# A block comment.
  Comment: [
    o 'HERECOMMENT', -> new Comment $1
  ]

i fully get this.

@vendethiel
Copy link
Collaborator

That's a tree. A "Statement" can be a "Return", a "STATEMENT" or a "Comment", which itself can be a "HERECOMMENT" (or nothing).

@loveencounterflow
Copy link
Author

yeah, i think i got the tree part.

Comment in the grammar is only ever a block comment, right?–the end-of-line comments already being filtered out.

as far as i understand it, a Comment has a single production with a single member 'HERECOMMENT', right? so Comment is really short for 'block comment', of which a 'here comment' is the only implemented instance. hmm.

just found out you're serious ;-) i can do x = 1; ### ok ###. bummer. cui bono?

@vendethiel
Copy link
Collaborator

; is a TERMINATOR, just as a newline.

@xixixao
Copy link
Contributor

xixixao commented Jun 20, 2013

I would much prefer to ignore lines where after removing block comments, there is no code.

if a
### comment ###
  b()
if a
  b()

I am sure there is an issue about this but what exactly are we gaining by having herecomments be statements?

@loveencounterflow
Copy link
Author

guess that's just how they were designed to be. your example above seems to ignore the intent that block comments should appear in the compiled source, which is a good thing.

@xixixao
Copy link
Contributor

xixixao commented Oct 11, 2013

I don't see it that way, I presented the semantics of CoffeeScript, the output would still be

if (a) {
/* comment */
  b();
}

In other words, the JavaScript output doesn't have necessarily anything to do with the comment block semantics in CoffeeScript.

@jashkenas
Copy link
Owner

Let's lump this in with all of the other herecomments-should-be-parsed-in-a-far-better-way ...

@GeoffreyBooth
Copy link
Collaborator

if true
### oops ###
  x = 1

compiles now, via #4572. Sorry it took awhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants