octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Block comments


From: William Poetra Yoga Hadisoeseno
Subject: Re: Block comments
Date: Mon, 5 Jun 2006 12:59:01 +0800

On 6/1/06, John W. Eaton <address@hidden> wrote:
On  1-Jun-2006, William Poetra Yoga Hadisoeseno wrote:

| Hi, I've (somewhat) implemented block comments (%{ and %}), but it's
| still very crude at the moment (look at the diff, it still has
| debugging features like printf and warning everywhere :p). The current
| problems are:
|
| 1. the block comment start and ends are not stripped. So for example
| if I have a file a.m:
|
| [...]
|
| 2. The code looks ugly (lots of duplication of code)
|
| 3. I don't know whether some block comments would be missed, or
| non-block comments would be identified as such, for example:

I think this discussion belongs on the maintainers list.  Please post
any replies there.

The page

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/edit_d14.html

says that block comments should nest.


Yes, it's a new feature in Matlab 6 IIRC from some user comments on the net.

I think completely separate code should be used for in-line and block
comments.  With flex, it might be easiest to handle block comments with
an exclusive start state.  To use this method, you'll probably need to
have patterns something like this:

  "%{"  -- begin BLOCK_COMMENT state, set block_comment nesting level
           to 1, save current start state (saved_start_state = YY_START).

  <BLOCK_COMMENT>"%{"  -- increment block_comment nesting level.

  <BLOCK_COMMENT>"%}"  -- decrement block_comment nesting level.  If
                          nesting level is 0, restore previous start
                          state (BEGIN (saved_start_state)).

  <BLOCK_COMMENT>\n    -- increment line counter

  <BLOCK_COMMENT>.     -- all other text in the comment (you might
                          also be able to match more than one
                          character at a time with regular expressions
                          if you are careful -- see the flex manual
                          for an example of handline C-style comments,
                          but note that C-style comments don't have to
                          nest).

In each of these, you'll need to save the comment in the comment
buffer and keep track of the column counter.


I think we should only match for "^%{$" and "^%}$", because in Matlab,
a block comment starter and ender is only valid if the two characters
%{ or %} are the only characters on the line. So for example this one
doesn't start a block comment:

 myvar = 1; %{ this is not a block comment
 %} and this line should generate a warning

And also,

 myvar = 1;
 %{ not a block comment
 this generates an error here
 %} and a warning here

Another example:

 myvar = 1;
 %{
 i'm in a block comment
 %} this doesn't end the block comment
 myvar2 = 2; this line is still commented
 will generate an error about block comment ended by EOF here

And this is the correct usage of block comments as in Matlab:

 myvar = 1;
 %{
 block comment here
 %}
 myvar2 = 2;

Since I don't have access to Matlab here, I can't verify it
personally. So should we "extend" the syntax of block comments, so
that it doesn't need to appear by itself on a line? If we don't extend
the syntax, we don't have to worry about column numbers (since it
matches only when we begin a line and ends with a newline --> %}\n).

The only catch here is that there are a few places where comments are
already matched with regular expressions like "%.*\n", so those would
have to be modified as well.


OK, I'll take a look. Some functions also deal with '\r'. What is it
for? Is it for compatibility with Matlab scripts with the DOS/Windows
encoding?

Now that I see it is possible to save and restore start states by
using YY_START, perhaps the current comment-handling code could also
be simplified by using an exclusive start state.  So instead of trying
to handle comments directly in the <MATRIX> start state, we could
handle comments separately, but be sure to restore the previous start
state when the end of a comment is reached.

jwe


Uh, could you please briefly explain the start states and the column
numbers? I took a look at the flex info manual, and I haven't really
understood it. About the column numbers (the global variable
current_input_column, right?), yes, I saw those, but some functions
don't update it (and input_line_number, too). Why?

--
William Poetra Yoga Hadisoeseno


reply via email to

[Prev in Thread] Current Thread [Next in Thread]