2010-05-10 11:44:09 -07:00
|
|
|
%{
|
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2010-07-02 15:31:26 -07:00
|
|
|
#include <ctype.h>
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
2010-05-10 11:44:09 -07:00
|
|
|
#include "glcpp-parse.h"
|
2010-06-16 17:41:12 -07:00
|
|
|
|
2010-07-20 15:03:20 -07:00
|
|
|
/* Flex annoyingly generates some functions without making them
|
|
|
|
|
* static. Let's declare them here. */
|
|
|
|
|
int glcpp_get_column (yyscan_t yyscanner);
|
|
|
|
|
void glcpp_set_column (int column_no , yyscan_t yyscanner);
|
|
|
|
|
|
2011-03-04 12:49:55 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#define YY_NO_UNISTD_H
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-07-20 15:53:14 -07:00
|
|
|
#define YY_NO_INPUT
|
|
|
|
|
|
2012-06-09 16:31:06 -07:00
|
|
|
#define YY_USER_ACTION \
|
|
|
|
|
do { \
|
|
|
|
|
if (parser->has_new_line_number) \
|
|
|
|
|
yylineno = parser->new_line_number; \
|
|
|
|
|
if (parser->has_new_source_number) \
|
|
|
|
|
yylloc->source = parser->new_source_number; \
|
|
|
|
|
yylloc->first_column = yycolumn + 1; \
|
2014-02-05 21:18:08 +06:00
|
|
|
yylloc->first_line = yylloc->last_line = yylineno; \
|
2012-06-09 16:31:06 -07:00
|
|
|
yycolumn += yyleng; \
|
2014-02-05 21:18:08 +06:00
|
|
|
yylloc->last_column = yycolumn + 1; \
|
2012-06-09 16:31:06 -07:00
|
|
|
parser->has_new_line_number = 0; \
|
|
|
|
|
parser->has_new_source_number = 0; \
|
|
|
|
|
} while(0);
|
2010-08-23 09:29:49 -07:00
|
|
|
|
|
|
|
|
#define YY_USER_INIT \
|
|
|
|
|
do { \
|
|
|
|
|
yylineno = 1; \
|
|
|
|
|
yycolumn = 1; \
|
|
|
|
|
yylloc->source = 0; \
|
|
|
|
|
} while(0)
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-06-16 16:35:57 -07:00
|
|
|
%option bison-bridge bison-locations reentrant noyywrap
|
2010-05-12 12:45:33 -07:00
|
|
|
%option extra-type="glcpp_parser_t *"
|
2010-06-16 11:51:43 -07:00
|
|
|
%option prefix="glcpp_"
|
2010-06-16 17:41:12 -07:00
|
|
|
%option stack
|
2010-08-13 13:08:54 -07:00
|
|
|
%option never-interactive
|
2010-05-10 11:44:09 -07:00
|
|
|
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
%x DONE COMMENT UNREACHABLE SKIP DEFINE NEWLINE_CATCHUP
|
2010-06-16 12:53:19 -07:00
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
SPACE [[:space:]]
|
|
|
|
|
NONSPACE [^[:space:]]
|
2010-05-12 12:17:10 -07:00
|
|
|
NEWLINE [\n]
|
2010-05-10 16:16:06 -07:00
|
|
|
HSPACE [ \t]
|
2010-05-14 17:29:24 -07:00
|
|
|
HASH ^{HSPACE}*#{HSPACE}*
|
2010-05-10 16:16:06 -07:00
|
|
|
IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
|
glsl/glcpp: Fix glcpp to properly lex entire "preprocessing numbers"
The preprocessor defines a notions of a "preprocessing number" that
starts with either a digit or a decimal point, and continues with zero
or more of digits, decimal points, identifier characters, or the sign
symbols, ('-' and '+').
Prior to this change, preprocessing numbers were lexed as some
combination of OTHER and IDENTIFIER tokens. This had the problem of
causing undesired macro expansion in some cases.
We add tests to ensure that the undesired macro expansion does not
happen in cases such as:
#define e +1
#define xyz -2
int n = 1e;
int p = 1xyz;
In either case these macro definitions have no effect after this
change, so that the numeric literals, (whether valid or not), will be
passed on as-is from the preprocessor to the compiler proper.
This fixes the following Khronos GLES3 CTS tests:
preprocessor.basic.correct_phases_vertex
preprocessor.basic.correct_phases_fragment
v2. Thanks to Anuj Phogat for improving the original regular expression,
(which accepted a '+' or '-', where these are only allowed after one of
[eEpP]. I also expanded the test to exercise this.
v3. Also fixed regular expression to require at least one digit at the
beginning (after an optional period). Otherwise, a string such as ".xyz" was
getting sucked up as a preprocessing number, (where obviously this should be a
field access). Again, I expanded the test to exercise this.
Reviewed-by: Anuj Phogat <anuj.phogat@gmail.com>
2014-06-12 14:56:47 -07:00
|
|
|
PP_NUMBER [.]?[0-9]([._a-zA-Z0-9]|[eEpP][-+])*
|
2010-05-29 05:07:24 -07:00
|
|
|
PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
|
2012-01-21 09:24:11 -08:00
|
|
|
|
|
|
|
|
/* The OTHER class is simply a catch-all for things that the CPP
|
|
|
|
|
parser just doesn't care about. Since flex regular expressions that
|
|
|
|
|
match longer strings take priority over those matching shorter
|
|
|
|
|
strings, we have to be careful to avoid OTHER matching and hiding
|
|
|
|
|
something that CPP does care about. So we simply exclude all
|
|
|
|
|
characters that appear in any other expressions. */
|
|
|
|
|
|
|
|
|
|
OTHER [^][_#[:space:]#a-zA-Z0-9(){}.&*~!/%<>^|;,=+-]
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-08-23 09:31:42 -07:00
|
|
|
DIGITS [0-9][0-9]*
|
2010-05-24 11:29:02 -07:00
|
|
|
DECIMAL_INTEGER [1-9][0-9]*[uU]?
|
|
|
|
|
OCTAL_INTEGER 0[0-7]*[uU]?
|
|
|
|
|
HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
2013-12-19 15:14:19 -08:00
|
|
|
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
glcpp_parser_t *parser = yyextra;
|
|
|
|
|
|
|
|
|
|
/* When we lex a multi-line comment, we replace it (as
|
|
|
|
|
* specified) with a single space. But if the comment spanned
|
|
|
|
|
* multiple lines, then subsequent parsing stages will not
|
|
|
|
|
* count correct line numbers. To avoid this problem we keep
|
|
|
|
|
* track of all newlines that were commented out by a
|
|
|
|
|
* multi-line comment, and we emit a NEWLINE token for each at
|
|
|
|
|
* the next legal opportunity, (which is when the lexer would
|
|
|
|
|
* be emitting a NEWLINE token anyway).
|
|
|
|
|
*/
|
|
|
|
|
if (YY_START == NEWLINE_CATCHUP) {
|
|
|
|
|
if (parser->commented_newlines)
|
|
|
|
|
parser->commented_newlines--;
|
|
|
|
|
if (parser->commented_newlines == 0)
|
|
|
|
|
BEGIN INITIAL;
|
|
|
|
|
return NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 15:14:19 -08:00
|
|
|
/* The handling of the SKIP vs INITIAL start states requires
|
|
|
|
|
* some special handling. Typically, a lexer would change
|
|
|
|
|
* start states with statements like "BEGIN SKIP" within the
|
|
|
|
|
* lexer rules. We can't get away with that here, since we
|
|
|
|
|
* need the parser to actually evaluate expressions for
|
|
|
|
|
* directives like "#if".
|
|
|
|
|
*
|
|
|
|
|
* So, here, in code that will be executed on every call to
|
|
|
|
|
* the lexer,and before any rules, we examine the skip_stack
|
|
|
|
|
* as set by the parser to know whether to change from INITIAL
|
|
|
|
|
* to SKIP or from SKIP back to INITIAL.
|
|
|
|
|
*
|
|
|
|
|
* Three cases cause us to switch out of the SKIP state and
|
|
|
|
|
* back to the INITIAL state:
|
|
|
|
|
*
|
|
|
|
|
* 1. The top of the skip_stack is of type SKIP_NO_SKIP
|
|
|
|
|
* This means we're still evaluating some #if
|
|
|
|
|
* hierarchy, but we're on a branch of it where
|
|
|
|
|
* content should not be skipped (such as "#if 1" or
|
|
|
|
|
* "#else" or so).
|
|
|
|
|
*
|
|
|
|
|
* 2. The skip_stack is NULL meaning that we've reached
|
|
|
|
|
* the last #endif.
|
|
|
|
|
*
|
2014-06-12 10:39:39 -07:00
|
|
|
* 3. The lexing_directive bit is set. This indicates that we are
|
|
|
|
|
* lexing a pre-processor directive, (such as #if, #elif, or
|
|
|
|
|
* #else). For the #if and #elif directives we always need to
|
|
|
|
|
* parse the conditions, (even if otherwise within an #if
|
|
|
|
|
* 0). And for #else, we want to be able to generate an error
|
|
|
|
|
* if any garbage follows #else.
|
2011-03-01 12:24:58 -08:00
|
|
|
*/
|
2013-12-19 15:14:19 -08:00
|
|
|
if (YY_START == INITIAL || YY_START == SKIP) {
|
2014-06-12 10:39:39 -07:00
|
|
|
if (parser->lexing_directive ||
|
2013-12-19 15:14:19 -08:00
|
|
|
parser->skip_stack == NULL ||
|
|
|
|
|
parser->skip_stack->type == SKIP_NO_SKIP)
|
|
|
|
|
{
|
|
|
|
|
BEGIN INITIAL;
|
2011-03-01 12:24:58 -08:00
|
|
|
} else {
|
|
|
|
|
BEGIN SKIP;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-06-01 12:18:43 -07:00
|
|
|
/* Single-line comments */
|
2010-08-17 22:17:09 -07:00
|
|
|
"//"[^\n]* {
|
2010-06-01 12:18:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Multi-line comments */
|
2010-06-16 17:41:12 -07:00
|
|
|
"/*" { yy_push_state(COMMENT, yyscanner); }
|
|
|
|
|
<COMMENT>[^*\n]*
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
<COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
|
2010-06-16 17:41:12 -07:00
|
|
|
<COMMENT>"*"+[^*/\n]*
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
<COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; parser->commented_newlines++; }
|
2010-06-16 17:41:12 -07:00
|
|
|
<COMMENT>"*"+"/" {
|
|
|
|
|
yy_pop_state(yyscanner);
|
2010-06-01 12:18:43 -07:00
|
|
|
if (yyextra->space_tokens)
|
|
|
|
|
return SPACE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 17:23:42 -08:00
|
|
|
{HASH}version{HSPACE}+ {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-07-28 16:58:39 -07:00
|
|
|
yyextra->space_tokens = 0;
|
|
|
|
|
return HASH_VERSION;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 12:21:17 -07:00
|
|
|
/* glcpp doesn't handle #extension, #version, or #pragma directives.
|
|
|
|
|
* Simply pass them through to the main compiler's lexer/parser. */
|
2014-06-13 15:53:39 -07:00
|
|
|
{HASH}(extension|pragma)[^\n]* {
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
if (parser->commented_newlines)
|
|
|
|
|
BEGIN NEWLINE_CATCHUP;
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-06-18 15:23:50 -07:00
|
|
|
yylineno++;
|
|
|
|
|
yycolumn = 0;
|
2010-06-16 12:21:17 -07:00
|
|
|
return OTHER;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-20 17:23:42 -08:00
|
|
|
{HASH}line{HSPACE}+ {
|
2012-06-09 16:31:06 -07:00
|
|
|
return HASH_LINE;
|
2010-08-18 17:38:05 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-01 12:24:58 -08:00
|
|
|
<SKIP,INITIAL>{
|
2011-03-03 09:52:36 -08:00
|
|
|
{HASH}ifdef {
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 1;
|
2010-06-17 14:36:34 -07:00
|
|
|
yyextra->space_tokens = 0;
|
|
|
|
|
return HASH_IFDEF;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 09:52:36 -08:00
|
|
|
{HASH}ifndef {
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 1;
|
2010-06-17 14:36:34 -07:00
|
|
|
yyextra->space_tokens = 0;
|
|
|
|
|
return HASH_IFNDEF;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-03 09:52:36 -08:00
|
|
|
{HASH}if/[^_a-zA-Z0-9] {
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 1;
|
2010-05-25 16:59:02 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-06-01 11:20:18 -07:00
|
|
|
return HASH_IF;
|
2010-05-20 22:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2012-11-26 11:53:45 -08:00
|
|
|
{HASH}elif/[^_a-zA-Z0-9] {
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 1;
|
2010-05-25 16:59:02 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-06-01 11:20:18 -07:00
|
|
|
return HASH_ELIF;
|
2010-05-24 11:29:02 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-03 09:52:36 -08:00
|
|
|
{HASH}else {
|
2010-05-25 16:59:02 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-06-01 11:20:18 -07:00
|
|
|
return HASH_ELSE;
|
2010-05-24 11:29:02 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-03 09:52:36 -08:00
|
|
|
{HASH}endif {
|
2010-05-26 09:32:12 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-06-01 11:20:18 -07:00
|
|
|
return HASH_ENDIF;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
2010-06-01 11:20:18 -07:00
|
|
|
}
|
|
|
|
|
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
<SKIP>[^\n] {
|
|
|
|
|
if (parser->commented_newlines)
|
|
|
|
|
BEGIN NEWLINE_CATCHUP;
|
|
|
|
|
}
|
2011-03-01 12:24:58 -08:00
|
|
|
|
2010-07-02 15:31:26 -07:00
|
|
|
{HASH}error.* {
|
|
|
|
|
char *p;
|
|
|
|
|
for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
|
|
|
|
|
p += 5; /* skip "error" */
|
|
|
|
|
glcpp_error(yylloc, yyextra, "#error%s", p);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-22 10:56:46 -07:00
|
|
|
{HASH}define{HSPACE}+ {
|
2010-05-26 09:32:12 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2012-10-22 10:56:46 -07:00
|
|
|
yy_push_state(DEFINE, yyscanner);
|
|
|
|
|
return HASH_DEFINE;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2012-10-22 10:56:46 -07:00
|
|
|
<DEFINE>{IDENTIFIER}/"(" {
|
|
|
|
|
yy_pop_state(yyscanner);
|
|
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
|
|
|
|
return FUNC_IDENTIFIER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<DEFINE>{IDENTIFIER} {
|
|
|
|
|
yy_pop_state(yyscanner);
|
|
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
|
|
|
|
return OBJ_IDENTIFIER;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 11:20:18 -07:00
|
|
|
{HASH}undef {
|
2010-05-26 09:32:12 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-06-01 11:20:18 -07:00
|
|
|
return HASH_UNDEF;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
{HASH} {
|
2010-05-25 16:59:02 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2010-05-25 13:09:03 -07:00
|
|
|
return HASH;
|
2010-05-20 22:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:32:12 -07:00
|
|
|
{DECIMAL_INTEGER} {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-05-27 14:36:29 -07:00
|
|
|
return INTEGER_STRING;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{OCTAL_INTEGER} {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-05-27 14:36:29 -07:00
|
|
|
return INTEGER_STRING;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{HEXADECIMAL_INTEGER} {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-05-27 14:36:29 -07:00
|
|
|
return INTEGER_STRING;
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"<<" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return LEFT_SHIFT;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
">>" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return RIGHT_SHIFT;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"<=" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return LESS_OR_EQUAL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
">=" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return GREATER_OR_EQUAL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"==" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return EQUAL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"!=" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return NOT_EQUAL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"&&" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return AND;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"||" {
|
2010-05-24 10:37:38 -07:00
|
|
|
return OR;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"##" {
|
2012-11-27 12:18:02 -08:00
|
|
|
if (parser->is_gles)
|
|
|
|
|
glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
|
2010-05-25 13:09:03 -07:00
|
|
|
return PASTE;
|
2010-05-20 22:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:32:12 -07:00
|
|
|
"defined" {
|
|
|
|
|
return DEFINED;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:35:34 -07:00
|
|
|
{IDENTIFIER} {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-05-26 09:35:34 -07:00
|
|
|
return IDENTIFIER;
|
|
|
|
|
}
|
|
|
|
|
|
glsl/glcpp: Fix glcpp to properly lex entire "preprocessing numbers"
The preprocessor defines a notions of a "preprocessing number" that
starts with either a digit or a decimal point, and continues with zero
or more of digits, decimal points, identifier characters, or the sign
symbols, ('-' and '+').
Prior to this change, preprocessing numbers were lexed as some
combination of OTHER and IDENTIFIER tokens. This had the problem of
causing undesired macro expansion in some cases.
We add tests to ensure that the undesired macro expansion does not
happen in cases such as:
#define e +1
#define xyz -2
int n = 1e;
int p = 1xyz;
In either case these macro definitions have no effect after this
change, so that the numeric literals, (whether valid or not), will be
passed on as-is from the preprocessor to the compiler proper.
This fixes the following Khronos GLES3 CTS tests:
preprocessor.basic.correct_phases_vertex
preprocessor.basic.correct_phases_fragment
v2. Thanks to Anuj Phogat for improving the original regular expression,
(which accepted a '+' or '-', where these are only allowed after one of
[eEpP]. I also expanded the test to exercise this.
v3. Also fixed regular expression to require at least one digit at the
beginning (after an optional period). Otherwise, a string such as ".xyz" was
getting sucked up as a preprocessing number, (where obviously this should be a
field access). Again, I expanded the test to exercise this.
Reviewed-by: Anuj Phogat <anuj.phogat@gmail.com>
2014-06-12 14:56:47 -07:00
|
|
|
{PP_NUMBER} {
|
|
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
|
|
|
|
return OTHER;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
{PUNCTUATION} {
|
2010-05-25 13:09:03 -07:00
|
|
|
return yytext[0];
|
2010-05-19 13:28:24 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 15:04:32 -07:00
|
|
|
{OTHER}+ {
|
2011-01-21 14:32:31 -08:00
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext);
|
2010-05-25 15:04:32 -07:00
|
|
|
return OTHER;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 20:15:56 +06:00
|
|
|
{HSPACE} {
|
2010-05-25 16:59:02 -07:00
|
|
|
if (yyextra->space_tokens) {
|
|
|
|
|
return SPACE;
|
|
|
|
|
}
|
2010-05-12 13:19:23 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-01 12:24:58 -08:00
|
|
|
<SKIP,INITIAL>\n {
|
glcpp: Replace multi-line comment with a space (even as part of macro definition)
The preprocessor has always replaced multi-line comments with a single space
character, (as required by the specification), but as of commit
bd55ba568b301d0f764cd1ca015e84e1ae932c8b the lexer also emitted a NEWLINE
token for each newline within the comment, (in order to preserve line
numbers).
The emitting of NEWLINE tokens within the comment broke the rule of "replace a
multi-line comment with a single space" as could be exposed by code like the
following:
#define FOO a/*
*/b
FOO
Prior to commit bd55ba568b301d0f764cd1ca015e84e1ae932c8b, this code defined
the macro FOO as "a b" as desired. Since that commit, this code instead
defines FOO as "a" and leaves a stray "b" in the output.
In this commit, we fix this by not emitting the NEWLINE tokens while lexing
the comment, but instead merely counting them in the commented_newlines
variable. Then, when the lexer next encounters a non-commented newline it
switches to a NEWLINE_CATCHUP state to emit as many NEWLINE tokens as
necessary (so that subsequent parsing stages still generate correct line
numbers).
Of course, it would have been more clear if we could have written a loop to
emit all the newlines, but flex conventions prevent that, (we must use
"return" for each token we emit).
It similarly would have been clear to have a new rule restricted to the
<NEWLINE_CATCHUP> state with an action much like the body of this if
condition. The problem with that is that this rule must not consume any
characters. It might be possible to write a rule that matches a single
lookahead of any character, but then we would also need an additional rule to
ensure for the <EOF> case where there are no additional characters available
for the lookahead to match.
Given those considerations, and given that the SKIP-state manipulation already
involves a code block at the top of the lexer function, before any rules, it
seems best to me to go with the implementation here which adds a similar
pre-rule code block for the NEWLINE_CATCHUP.
Finally, this commit also changes the expected output of a few, existing glcpp
tests. The change here is that the space character resulting from the
multi-line comment is now emitted before the newlines corresponding to that
comment. (Previously, the newlines were emitted first, and the space character
afterward.)
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=72686
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2013-12-19 16:06:31 -08:00
|
|
|
if (parser->commented_newlines) {
|
|
|
|
|
BEGIN NEWLINE_CATCHUP;
|
|
|
|
|
}
|
2014-07-28 08:59:25 -07:00
|
|
|
yyextra->space_tokens = 1;
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 0;
|
2010-06-16 17:41:12 -07:00
|
|
|
yylineno++;
|
|
|
|
|
yycolumn = 0;
|
2010-05-25 15:04:32 -07:00
|
|
|
return NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 12:53:19 -07:00
|
|
|
/* Handle missing newline at EOF. */
|
|
|
|
|
<INITIAL><<EOF>> {
|
|
|
|
|
BEGIN DONE; /* Don't keep matching this rule forever. */
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 0;
|
2010-06-16 12:53:19 -07:00
|
|
|
return NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-20 15:53:14 -07:00
|
|
|
/* We don't actually use the UNREACHABLE start condition. We
|
|
|
|
|
only have this action here so that we can pretend to call some
|
|
|
|
|
generated functions, (to avoid "defined but not used"
|
|
|
|
|
warnings. */
|
|
|
|
|
<UNREACHABLE>. {
|
|
|
|
|
unput('.');
|
|
|
|
|
yy_top_state(yyextra);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
2010-06-16 12:01:17 -07:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
|
|
|
|
|
{
|
|
|
|
|
yy_scan_string(shader, parser->scanner);
|
|
|
|
|
}
|