Mixing single- and multi-line comments in FB

For other topics related to the FreeBASIC project or its community.
counting_pine
Site Admin
Posts: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Mixing single- and multi-line comments in FB

Postby counting_pine » Dec 28, 2008 15:29

VonGodric has recently raised the issue of FB's slightly unpredictable comment-parsing rules, and I thought I'd take this further and look for a little wider feedback.

Currently, depending on the contents of a single-line comment, FBC may allow or disallow multi-line comments in it. If a multi-line comment is allowed, then the comment continues after the end.
e.g.

Code: Select all

'   /'
      this is a multi-line comment within a single-line comment
    '/ this is part of the original single-line comment


' /'
  '/ $lang: "fblite"


'' /' this is all a single-line comment
      this is an invalid code statement, not a multi-line comment
    '/ this is a single-line comment, because the line starts with an apostrophe


The reason for this is that FB parses comments, checking for something that looks like a metacommand, e.g. '$lang, '$dynamic. While it does this, it parses the comment as a statement, allowing multi-line comments.
As soon as FB detects that it's not a metacommand, e.g. it comes across a second apostrophe, or a normal word, or pretty much anything that's not a dollar sign, it indiscriminately skips the rest of the characters in the line, so any multi-line comment markers contained in there are ignored.

Anyway, VonGodric's posted a patch to fix this, and I think it's probably worth doing to improve consistency. Besides, metacommands are very much deprecated, and only supported in lang fb to either effect a dialect switch or prevent the user from accidentally trying to add something like '$dynamic, and being left with no clue to why it doesn't work.

But at the same time, I don't want to break any existing code, so I'd like to know if anyone is making use of this parsing anomaly before changing it...
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Dec 29, 2008 0:15

Code: Select all

'   /'
      this Is a multi-Line comment within a single-Line comment
    '/ this is part of the original single-line comment


Surely anything inside a comment is ignored, so too should an embedded comment.

VonGodrics patch, how does it affect things?
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Postby VonGodric » Dec 29, 2008 0:32

It only fixes multi line issue and doesn't affect any meta commands.

I think single line comment should be exactly that. A single line comment (except the cases where it is a meta command) should be ignored completely. It can lead to some frustrating and hard to understand situations as to why some code that used to work / or should work - in fact doesn't.
Nexinarus
Posts: 146
Joined: May 28, 2005 6:08
Location: Everywhere
Contact:

Postby Nexinarus » Dec 29, 2008 7:05

I think that to parse a single line comment, all characters should be consumed/ignored until a new line character is reached: meaning no multi-line comments within a single line comment. A bug that should be fixed, even if people were abusing this anomaly.
counting_pine
Site Admin
Posts: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Dec 29, 2008 14:14

Yeah, that's roughly the point of this thread. But characters between the comment marker and the end-of-line can't be indiscriminately skipped because of metacommands.

Suffice to say: if FB ever gets remade from scratch, we probably won't reimplement dialects, and metacommands will be done away with entirely. Daft idea, statements in comments...
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Dec 29, 2008 15:38

Metacommands are only active in -lang qb, so this isn't a problem as multiline comments were not part of QB syntax.
Nexinarus
Posts: 146
Joined: May 28, 2005 6:08
Location: Everywhere
Contact:

Postby Nexinarus » Dec 29, 2008 18:52

yeah meta-commands are useless in my opinion - I have never used one in FB's existence. Comments should be.. ignored :P
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

Postby VonGodric » Dec 29, 2008 18:59

'$lang is allowed in FB mode as well.
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Dec 30, 2008 1:52

Okay, well then they should be removed.
counting_pine
Site Admin
Posts: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Dec 30, 2008 16:05

Metacommands are parsed in every dialect, and allowed in every dialect except lang fb. They're parsed in lang fb so that the compiler can prevent people from trying to use it, otherwise it would fail for no apparent reason.

'$lang is a special case that all dialects allow: the comment format means that it can be used in QBASIC and older versions of FreeBASIC without causing an error. It is also likely that the dialect it switches to will be one that does accept metacommands generally, and it shouldn't matter what the default dialect was if a new one is being specified.
counting_pine
Site Admin
Posts: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Jan 01, 2009 9:55

OK: Changes are in SVN. I've decided to disable the checking for multi-line comments in single-line ones, in the lang fb dialect at least. The other dialects, I guess, are still open to negotiation though...
Thanks for your help, VonGodric.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Jan 01, 2009 19:22

I don't even see the need for a multi line comment in the compiler itself. But I guess we're stuck with it...
counting_pine
Site Admin
Posts: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Jan 01, 2009 20:16

What's wrong with multi-line comments?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Jan 01, 2009 21:28

IMO undecorated multi-line comments are less readable that a block of single-line comments, so for my informational comments I generally use blocks of single-line comments. I think the multi-line comment capability was nevertheless a worthwhile addition, because it works very well for temporarily commenting out blocks of code.
Mysoft
Posts: 778
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Postby Mysoft » Jan 01, 2009 21:53

heh i generally use #if 0 .... #endif instead of multine comments, but probabily juz cuz i get used to that (before i knew about multine commments). but in either case maybe its easier to remember than /' or '/

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests