diff options
-rw-r--r-- | doc/flex.texi | 6 | ||||
-rw-r--r-- | scan.l | 64 | ||||
-rw-r--r-- | tests/test-extended/scanner.l | 16 | ||||
-rw-r--r-- | tests/test-extended/test.input | 1 |
4 files changed, 72 insertions, 15 deletions
diff --git a/doc/flex.texi b/doc/flex.texi index c8ec07f..04aff51 100644 --- a/doc/flex.texi +++ b/doc/flex.texi @@ -757,7 +757,7 @@ Options may be zero or more of the characters @samp{i}, @samp{s}, or @samp{x}. @samp{x} ignores comments and whitespace in patterns. Whitespace is ignored unless it is backslash-escaped, contained within @samp{""}s, or appears inside a -character class. TODO -- Do we ignore Perl comments, C comments, or both? +character class. The following are all valid: @@ -772,6 +772,10 @@ The following are all valid: (?x:a\ b) same as ("a b") (?x:a" "b) same as ("a b") (?x:a[ ]b) same as ("a b") +(?x:a + /* comment */ + b + c) same as (abc) @end verbatim @item (?# comment ) @@ -104,6 +104,7 @@ extern const char *escaped_qstart, *escaped_qend; %x GROUP_WITH_PARAMS %x GROUP_MINUS_PARAMS %x EXTENDED_COMMENT +%x COMMENT_DISCARD WS [[:blank:]]+ OPTWS [[:blank:]]* @@ -211,6 +212,14 @@ M4QEND "]]" {NL} ++linenum; ACTION_ECHO; } +<COMMENT_DISCARD>{ + /* This is the same as COMMENT, but is discarded rather than output. */ + "*/" yy_pop_state(); + "*" ; + [^*\n] ; + {NL} ++linenum; +} + <EXTENDED_COMMENT>{ ")" yy_pop_state(); [^\n\)]+ ; @@ -485,7 +494,12 @@ M4QEND "]]" BEGIN(PERCENT_BRACE_ACTION); } - ^{OPTWS}"<" BEGIN(SC); return '<'; + ^{OPTWS}"<" { + /* Allow "<" to appear in (?x) patterns. */ + if (!sf_skip_ws()) + BEGIN(SC); + return '<'; + } ^{OPTWS}"^" return '^'; \" BEGIN(QUOTE); return '"'; "{"/[[:digit:]] { @@ -524,10 +538,17 @@ M4QEND "]]" } ^{WS}"/*" { - yyless( yyleng - 2 ); /* put back '/', '*' */ - bracelevel = 0; - continued_action = false; - BEGIN(ACTION); + + if (sf_skip_ws()){ + /* We're in the middle of a (?x: ) pattern. */ + yy_push_state(COMMENT_DISCARD); + } + else{ + yyless( yyleng - 2 ); /* put back '/', '*' */ + bracelevel = 0; + continued_action = false; + BEGIN(ACTION); + } } ^{WS} /* allow indented rules */ ; @@ -680,8 +701,35 @@ nmstr[yyleng - 2 - end_is_ws] = '\0'; /* chop trailing brace */ } } - "(?#" yy_push_state(EXTENDED_COMMENT); - "(?" sf_push(); BEGIN(GROUP_WITH_PARAMS); return '('; + "/*" { + if (sf_skip_ws()) + yy_push_state(COMMENT_DISCARD); + else{ + /* Push back the "*" and return "/" as usual. */ + yyless(1); + return '/'; + } + } + + "(?#" { + if (lex_compat || posix_compat){ + /* Push back the "?#" and treat it like a normal parens. */ + yyless(1); + sf_push(); + return '('; + } + else + yy_push_state(EXTENDED_COMMENT); + } + "(?" { + sf_push(); + if (lex_compat || posix_compat) + /* Push back the "?" and treat it like a normal parens. */ + yyless(1); + else + BEGIN(GROUP_WITH_PARAMS); + return '('; + } "(" sf_push(); return '('; ")" sf_pop(); return ')'; @@ -881,7 +929,7 @@ nmstr[yyleng - 2 - end_is_ws] = '\0'; /* chop trailing brace */ . ACTION_ECHO; } -<COMMENT,ACTION,ACTION_STRING><<EOF>> { +<COMMENT,COMMENT_DISCARD,ACTION,ACTION_STRING><<EOF>> { synerr( _( "EOF encountered inside an action" ) ); yyterminate(); } diff --git a/tests/test-extended/scanner.l b/tests/test-extended/scanner.l index e027ec5..222c7dd 100644 --- a/tests/test-extended/scanner.l +++ b/tests/test-extended/scanner.l @@ -1,20 +1,20 @@ /* * This file is part of flex. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: - * + * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * + * * Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR @@ -31,7 +31,7 @@ %} %option 8bit outfile="scanner.c" prefix="test" -%option nounput nomain noyywrap +%option nounput nomain noyywrap %option warn @@ -41,7 +41,7 @@ abc(?# Single Line Comment )def ECHO; ghi(?# - multi-line + multi-line comment )jkl ECHO; @@ -53,6 +53,10 @@ mno(?# )pqr ECHO; (?# Start of a rule.)stu ECHO; vwxyz(?#End of a rule.) ECHO; +A(?x: B + /* comment */ + C D) ECHO; + \n ECHO; %% diff --git a/tests/test-extended/test.input b/tests/test-extended/test.input index b0883f3..829e23d 100644 --- a/tests/test-extended/test.input +++ b/tests/test-extended/test.input @@ -1 +1,2 @@ abcdefghijklmnopqrstuvwxyz +ABCD |