OSDN Git Service

Fix multi-line comment regular expression to handle (non) nested comments.
authorCarl Worth <cworth@cworth.org>
Wed, 2 Jun 2010 17:48:47 +0000 (10:48 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 2 Jun 2010 17:57:18 +0000 (10:57 -0700)
Ken reminded me of a couple cases that I should be testing. These are
the non-nestedness of things that look like nested comments as well as
potentially tricky things like "/*/" and "/*/*/".

The (non) nested comment case was not working in the case of the
comment terminator with multiple '*' characters. We fix this by not
considering a '*' as the "non-slash" to terminate a sequence of '*'
characters within the comment. We also fix the final match of the
terminator to use '+' rather than '*' to require the presence of a
final '*' character in the comment terminator.

glcpp-lex.l
tests/063-comments.c

index 0954ab7..7bc5fab 100644 (file)
@@ -53,7 +53,7 @@ HEXADECIMAL_INTEGER   0[xX][0-9a-fA-F]+[uU]?
 }
 
        /* Multi-line comments */
-[/][*]([^*]*[*]+[^/])*[^*]*[*]*[/] {
+[/][*]([^*]*[*]+[^*/])*[^*]*[*]+[/] {
        if (yyextra->space_tokens)
                return SPACE;
 }
index 4cda522..e641d2f 100644 (file)
@@ -13,3 +13,8 @@ and slashes / *** /
 and other stuff.
 ****/
 more code here
+/* Test that /* nested
+   comments */
+are not treated like comments.
+/*/ this is a comment */
+/*/*/