Project

General

Profile

Bug #471

Nested multiline comments

Added by John Abbott over 10 years ago. Updated about 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Parser/Interpreter
Target version:
Start date:
12 Mar 2014
Due date:
% Done:

100%

Estimated time:
5.00 h
Spent time:

Description

C5 does not support nested multiline comments.

The following input produces 7

/* 1+2; /* 2+3; */ 3+4;

Attempting to close the "outer" comment produces a syntax error Invalid start of expression

However the manual claims that nested multiline comments are allowed, and gives an example (which doesn't work in C5).

Action: make documentation and behaviour agree!


Related issues

Related to CoCoA-5 - Bug #469: Emacs UI: keywords in multiline commentsResolved2014-03-12

Related to CoCoA-5 - Design #481: Multiline comments -- obsolescent?Closed2014-03-18

History

#1 Updated by John Abbott over 10 years ago

As already pointed out in #469 (update 2) I think we should aim to make multiline comments obsolescent. Here are some options:
(A) eliminate the syntax /* ... */ completely, but that would make Anna's choice of prompt pointless.
(B) allow the syntax, but require that the comment be closed on the same line (rather than allowing the comment to span many lines, as currently we do).
(C) no change (yet), but keep the syntax "well-hidden" in the documentation (hoping that no one uses it)

Option (B) maintains some backward compatibility, and I do quite like the possibility of having an embedded comment in a line (I even used it in RealRoots.cpkg5); it would also accept Anna's prompt without problem.

#2 Updated by John Abbott over 10 years ago

One reason for having multiline comments is that it becomes easy to comment out blocks of code; this is also what prompted the designers of CoCoA-3 to recognise nested /*...*/ comments.

However, in CoCoA-5 the nesting of comments is not recognised/respected, so it is "dangerous" to use /*...*/ to comment out a block of code -- it won't produce the desired effect if that block of code already contains a /*...*/ comment.

The cocoa5-mode for Emacs makes it easy to comment out consecutive lines: it simply inserts -- at the start of each line; blocks may also be easily uncommented. So there is no need for a special syntax making it easy to comment out blocks of lines.

I suppose the GUI has similar capabilities for commenting and uncommenting consecutive lines (if not, surely it can be added easily).

I believe these reasons are more than sufficient to prohibit /*...*/ from delineating multiline comments.

#3 Updated by John Abbott over 10 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

Here is another option:
(C2) In CoCoA-5.1 continue to allow the multiline syntax, but issue warnings every time a newline is found in the comment; in CoCoA-5.2 we can replace the warnings by errors, and adopt option (B) or even option (A).

This would allow users time to update their code.

In any case, the documentation should discourage use of the syntax /*...*/ (at the moment it is suggested as the normal/best way to write comments).

Comments? Opinions?

#4 Updated by John Abbott over 10 years ago

I've updated the CoCoAManual entry; it doesn't say that /*...*/ works for multiline comments (but it doesn't forbid it either). I've removed the bit about nested comments.

Another (mild) reason for avoiding the drastic option (A) is that matrices are printed in a way which assumes that /*...*/ is a comment. This could easily be sorted out if we do adopt (A): an end-of-line comment would work just as well in this instance.

#5 Updated by John Abbott over 10 years ago

  • % Done changed from 10 to 20

I've modified all packages so that they use either end-of-line comments (if possible) or single-line embedded comments (/*...*/ within a line).

Also similarly modified all CoCoA-5 tests, except for parser.cocoa5 (not sure what to do about that...)

Do we make any distinction between -- and // for end-of-line comments?
Mostly we seem to use --; not sure why.

#6 Updated by John Abbott over 10 years ago

Places we should look at for changing the parser so that it warns about (or complains about) multiline comments:

LineProviders.C:73 in InteractiveLineProvider::prompt

Perhaps the best place to issue a warning would be around Lexer.C:88
just after a new line has been read (successfully) by a LineProvider.
[wish me luck!]

#7 Updated by John Abbott over 10 years ago

I have inserted the following in Lexer.C:83 right before the declaration of the variable chars (what purpose does chars serve???)

                if (this->status.isInMultiLineComment())
                  this->reportWarning("Multiline comments are OBSOLESCENT; please comment each line separately", WS_NORMAL);

It seems to behave as I want. The CoCoA-5 tests all pass (after a minor change).

Should I check it in? (yes, yes :-))

#8 Updated by Anna Maria Bigatti over 10 years ago

John Abbott wrote:

I have inserted the following in Lexer.C:83 right before the declaration of the variable chars (what purpose does chars serve???)
[...]

It seems to behave as I want. The CoCoA-5 tests all pass (after a minor change).

Should I check it in? (yes, yes :-))

I'm not entirely convinced, but you may.
... but I reserve the right to complain afterwards ;-)
[JAA "right to complain"? Tipica genovese!!]

#9 Updated by John Abbott over 10 years ago

  • % Done changed from 20 to 30

While checking in I noticed a "valid use" for multiline comments: in the file latex.cpkg5 there are some examples/tests in a block of lines commented out using /*...*/. I suppose the developer of the package found it helpful to have some quick tests which could be run via cocoa5-send-line.

Of course, it would be rather better to have a proper test file; and the examples should be in the proper documentation. So those lines certainly shouldn't be there in a finished package.

Not having multiline comments would have encouraged the developer of that package to write a proper test file; so I continue to favour forbidding multiline comments!

#10 Updated by Anna Maria Bigatti over 10 years ago

CoCoA-4 had nested comments /* /* */ */.
In CoCoA-5 G.Lagorio refused to maintain that feature because "C does not allow that, and there is a good reason why" (even though I never understood why).

This, of course, makes it not backward compatible.
That said, I like multiline comments (and I have a macro to emulate that in LaTeX ;-)
Anyway I use the emacs interface, so I have no problems in making "Comment Region" with the -- syntax ;-)

NOTE 20140318 The point is that YOU must be careful when writing multiline comments; if you should accidentally put a /* or a */ in your comment then strange things will happen -- if you put /* in your comment then CoCoA will notice the error only when it reaches the end-of-file (and it cannot give you much help locating the true cause!)

#11 Updated by John Abbott over 10 years ago

Here is why I think nested "multiline" comments are problematic; essentially the parser is compelled to parse the comments as though they were code, but comments can contain "any old junk".

Consider what happens when you try to comment out this function:

Define JAA(x)
  If x < 0 Or IsPrime(x) Then --> easy if x is *negative*/*prime*
    Return 0;
  EndIf;
  Return 1;
EndDefine;

I'm definitely with Giovanni on this question.

NOTE 20140323 there is also the point that the designers of C/C++ chose not to have nested comments, and they're surely smarter/more experienced than we are...

#12 Updated by John Abbott over 10 years ago

  • % Done changed from 30 to 40

A purely practical point: the C5 interpreter was not designed to handle nested /*...*/ comments, and I'm pretty certain that modifying it to do so would be relatively expensive (in man-hours).

The question of nested comments does not really arise if comments are limited to residing on a single line -- technically one could write a nested comment on a single line, but it's more likely to be an act of "vandalism" rather than a useful coding/debugging stratagem.

#13 Updated by John Abbott over 10 years ago

I suggest adding a warning if /* is found inside a /*...*/ comment. This would alert users to a (probable?) presence of nested comments.

Relevant code: Lexer.C:247--261

What do you think?

#14 Updated by John Abbott over 10 years ago

  • % Done changed from 40 to 60

Well, I didn't wait for anyone else's opinion. I've implemented the warning.

A := 1; /* abc/def */
A := 2; /* abc/*def */
WARNING: Possible nested comment!
A := 2; /* abc/*def */
              ^^

The code changed was in the line range indicated in the previous update.

The C5 tests all pass.

#15 Updated by John Abbott over 10 years ago

  • Status changed from In Progress to Feedback
  • % Done changed from 60 to 90

I think the current state is a fair compromise; so changing status to feedback

#16 Updated by Anna Maria Bigatti about 10 years ago

  • Target version set to CoCoA-5.1.0 Easter14

#17 Updated by John Abbott about 10 years ago

  • Status changed from Feedback to Closed
  • Assignee set to John Abbott
  • % Done changed from 90 to 100
  • Estimated time set to 5.00 h

Current solution is satisfactory, so closing!

Also available in: Atom PDF