On 1/28/08, Clifford Heath <no / spam.please.net> wrote:

>
>  This is essentially the "C comment matching problem". Here's what I use
>  for C-style comments, C++ style comments, and whitespace:
>
>    rule s      # Optional space
>      S?   end
>
>    rule S      # Mandatory space
>      (white / comment_to_eol / comment_c_style)+
>    end
>
>    rule white
>      [ \t\n\r]+
>    end
>        rule comment_to_eol
>      '//' (!"\n" .)+
>    end
>    rule comment_c_style
>        '/*' (!'*/' . )* '*/'
>    end
>

I gave this a try with the following grammar:

  #foo_grammar.treetop
  grammar Foo_grammar
   rule top_level
     ( comment_to_eol / comment_c_style )
     {  def is_comment?
         true
        end
     }
   end
   rule comment_to_eol
     '--' (!"\n" .)+
   end
   rule comment_c_style
       '/*' (!'*/' . )* '*/'
   end
 end

And then tested it like so:

class FooParserTest < Test::Unit::TestCase
  include ParserTestHelper
  def setup
    puts "setup..."
    @parser = Foo_grammarParser.new
  end

  def test_eol_comment
    assert( @parser.parse "-- this is a comment. \n")
  end

  def test_c_style_comment
    comment = @parser.parse("/* this is a comment. */")
    assert comment.is_comment?
  end
end

The first testcase (test_eol_comment) fails, but the second one
passes.  Any idea what's wrong with that comment_to_eol rule?

Phil