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)
|
2014-06-20 14:58:48 -07:00
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
/* It's ugly to have macros that have return statements inside of
|
|
|
|
|
* them, but flex-based lexer generation is all built around the
|
|
|
|
|
* return statement.
|
|
|
|
|
*
|
|
|
|
|
* To mitigate the ugliness, we defer as much of the logic as possible
|
|
|
|
|
* to an actual function, not a macro (see
|
|
|
|
|
* glcpplex_update_state_per_token) and we make the word RETURN
|
|
|
|
|
* prominent in all of the macros which may return.
|
|
|
|
|
*
|
|
|
|
|
* The most-commonly-used macro is RETURN_TOKEN which will perform all
|
|
|
|
|
* necessary state updates based on the provided token,, then
|
|
|
|
|
* conditionally return the token. It will not return a token if the
|
|
|
|
|
* parser is currently skipping tokens, (such as within #if
|
|
|
|
|
* 0...#else).
|
|
|
|
|
*
|
|
|
|
|
* The RETURN_TOKEN_NEVER_SKIP macro is a lower-level variant that
|
|
|
|
|
* makes the token returning unconditional. This is needed for things
|
|
|
|
|
* like #if and the tokens of its condition, (since these must be
|
|
|
|
|
* evaluated by the parser even when otherwise skipping).
|
|
|
|
|
*
|
|
|
|
|
* Finally, RETURN_STRING_TOKEN is a simple convenience wrapper on top
|
|
|
|
|
* of RETURN_TOKEN that performs a string copy of yytext before the
|
|
|
|
|
* return.
|
|
|
|
|
*/
|
|
|
|
|
#define RETURN_TOKEN_NEVER_SKIP(token) \
|
2014-06-20 14:58:48 -07:00
|
|
|
do { \
|
|
|
|
|
if (token == NEWLINE) \
|
|
|
|
|
parser->last_token_was_newline = 1; \
|
|
|
|
|
else \
|
|
|
|
|
parser->last_token_was_newline = 0; \
|
|
|
|
|
return (token); \
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
#define RETURN_TOKEN(token) \
|
|
|
|
|
do { \
|
|
|
|
|
if (! parser->skipping) { \
|
|
|
|
|
RETURN_TOKEN_NEVER_SKIP(token); \
|
|
|
|
|
} \
|
2014-06-20 14:58:48 -07:00
|
|
|
} while(0)
|
|
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
#define RETURN_STRING_TOKEN(token) \
|
|
|
|
|
do { \
|
|
|
|
|
if (! parser->skipping) { \
|
|
|
|
|
yylval->str = ralloc_strdup (yyextra, yytext); \
|
|
|
|
|
RETURN_TOKEN_NEVER_SKIP (token); \
|
|
|
|
|
} \
|
2014-06-20 15:30:21 -07:00
|
|
|
} 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
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
%x DONE COMMENT UNREACHABLE 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;
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (NEWLINE);
|
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
|
|
|
}
|
|
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
/* Set up the parser->skipping bit here before doing any lexing.
|
2013-12-19 15:14:19 -08:00
|
|
|
*
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
* This bit controls whether tokens are skipped, (as implemented by
|
|
|
|
|
* RETURN_TOKEN), such as between "#if 0" and "#endif".
|
2013-12-19 15:14:19 -08:00
|
|
|
*
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
* The parser maintains a skip_stack indicating whether we should be
|
|
|
|
|
* skipping, (and nested levels of #if/#ifdef/#ifndef/#endif) will
|
|
|
|
|
* push and pop items from the stack.
|
2013-12-19 15:14:19 -08:00
|
|
|
*
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
* Here are the rules for determining whether we are skipping:
|
2013-12-19 15:14:19 -08:00
|
|
|
*
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
* 1. If the skip stack is NULL, we are outside of all #if blocks
|
|
|
|
|
* and we are not skipping.
|
2013-12-19 15:14:19 -08:00
|
|
|
*
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
* 2. If the skip stack is non-NULL, the type of the top node in
|
|
|
|
|
* the stack determines whether to skip. A type of
|
|
|
|
|
* SKIP_NO_SKIP is used for blocks wheere we are emitting
|
|
|
|
|
* tokens, (such as between #if 1 and #endif, or after the
|
|
|
|
|
* #else of an #if 0, etc.).
|
|
|
|
|
*
|
|
|
|
|
* 3. The lexing_directive bit overrides the skip stack. This bit
|
|
|
|
|
* is set when we are actively lexing the expression for a
|
|
|
|
|
* pre-processor condition, (such as #if, #elif, or #else). In
|
|
|
|
|
* this case, even if otherwise skipping, we need to emit the
|
|
|
|
|
* tokens for this condition so that the parser can evaluate
|
|
|
|
|
* the expression. (For, #else, there's no expression, but we
|
|
|
|
|
* emit tokens so the parser can generate a nice error message
|
|
|
|
|
* if there are any tokens here).
|
2011-03-01 12:24:58 -08:00
|
|
|
*/
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
if (parser->skip_stack &&
|
|
|
|
|
parser->skip_stack->type != SKIP_NO_SKIP &&
|
|
|
|
|
! parser->lexing_directive)
|
|
|
|
|
{
|
|
|
|
|
parser->skipping = 1;
|
|
|
|
|
} else {
|
|
|
|
|
parser->skipping = 0;
|
2011-03-01 12:24:58 -08:00
|
|
|
}
|
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 */
|
2014-06-19 11:57:06 -07:00
|
|
|
<DEFINE,INITIAL>"/*" { yy_push_state(COMMENT, yyscanner); }
|
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>"*"+[^*/\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)
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (SPACE);
|
2010-06-01 12:18:43 -07:00
|
|
|
}
|
|
|
|
|
|
2012-11-20 17:23:42 -08:00
|
|
|
{HASH}version{HSPACE}+ {
|
2010-07-28 16:58:39 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (HASH_VERSION);
|
2010-07-28 16:58:39 -07:00
|
|
|
}
|
|
|
|
|
|
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]* {
|
2010-06-18 15:23:50 -07:00
|
|
|
yylineno++;
|
|
|
|
|
yycolumn = 0;
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (OTHER);
|
2010-06-16 12:21:17 -07:00
|
|
|
}
|
|
|
|
|
|
2012-11-20 17:23:42 -08:00
|
|
|
{HASH}line{HSPACE}+ {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (HASH_LINE);
|
2010-08-18 17:38:05 -07:00
|
|
|
}
|
|
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
/* For the pre-processor directives, we return these tokens
|
|
|
|
|
* even when we are otherwise skipping. */
|
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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (HASH_IFDEF);
|
2010-06-17 14:36:34 -07:00
|
|
|
}
|
|
|
|
|
|
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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (HASH_IFNDEF);
|
2010-06-17 14:36:34 -07:00
|
|
|
}
|
|
|
|
|
|
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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (HASH_ENDIF);
|
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
|
|
|
}
|
2011-03-01 12:24:58 -08:00
|
|
|
|
2010-07-02 15:31:26 -07:00
|
|
|
{HASH}error.* {
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
if (! parser->skipping) {
|
|
|
|
|
char *p;
|
|
|
|
|
for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
|
|
|
|
|
p += 5; /* skip "error" */
|
|
|
|
|
glcpp_error(yylloc, yyextra, "#error%s", p);
|
|
|
|
|
}
|
2010-07-02 15:31:26 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 11:57:06 -07:00
|
|
|
/* After we see a "#define" we enter the <DEFINE> start state
|
|
|
|
|
* for the lexer. Within <DEFINE> we are looking for the first
|
|
|
|
|
* identifier and specifically checking whether the identifier
|
|
|
|
|
* is followed by a '(' or not, (to lex either a
|
|
|
|
|
* FUNC_IDENTIFIER or an OBJ_IDENITIFIER token).
|
|
|
|
|
*
|
|
|
|
|
* While in the <DEFINE> state we also need to explicitly
|
|
|
|
|
* handle a few other things that may appear before the
|
|
|
|
|
* identifier:
|
|
|
|
|
*
|
|
|
|
|
* * Comments, (handled above with the main support for
|
|
|
|
|
* comments).
|
|
|
|
|
*
|
|
|
|
|
* * Whitespace (simply ignored)
|
|
|
|
|
*
|
|
|
|
|
* * Anything else, (not an identifier, not a comment,
|
|
|
|
|
* and not whitespace). This will generate an error.
|
|
|
|
|
*/
|
2012-10-22 10:56:46 -07:00
|
|
|
{HASH}define{HSPACE}+ {
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
if (! parser->skipping) {
|
|
|
|
|
yyextra->space_tokens = 0;
|
|
|
|
|
yy_push_state(DEFINE, yyscanner);
|
|
|
|
|
RETURN_TOKEN (HASH_DEFINE);
|
|
|
|
|
}
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 11:57:06 -07:00
|
|
|
/* An identifier immediately followed by '(' */
|
2012-10-22 10:56:46 -07:00
|
|
|
<DEFINE>{IDENTIFIER}/"(" {
|
|
|
|
|
yy_pop_state(yyscanner);
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (FUNC_IDENTIFIER);
|
2012-10-22 10:56:46 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 11:57:06 -07:00
|
|
|
/* An identifier not immediately followed by '(' */
|
2012-10-22 10:56:46 -07:00
|
|
|
<DEFINE>{IDENTIFIER} {
|
|
|
|
|
yy_pop_state(yyscanner);
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (OBJ_IDENTIFIER);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 11:57:06 -07:00
|
|
|
/* Whitespace */
|
|
|
|
|
<DEFINE>{HSPACE}+ {
|
|
|
|
|
/* Just ignore it. Nothing to do here. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* '/' not followed by '*', so not a comment. This is an error. */
|
|
|
|
|
<DEFINE>[/][^*]{NONSPACE}* {
|
|
|
|
|
BEGIN INITIAL;
|
|
|
|
|
glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (INTEGER_STRING);
|
2014-06-19 11:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* A character that can't start an identifier, comment, or
|
|
|
|
|
* space. This is an error. */
|
|
|
|
|
<DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
|
2014-06-17 11:55:07 -07:00
|
|
|
BEGIN INITIAL;
|
|
|
|
|
glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (INTEGER_STRING);
|
2014-06-17 11:55:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 11:20:18 -07:00
|
|
|
{HASH}undef {
|
2010-05-26 09:32:12 -07:00
|
|
|
yyextra->space_tokens = 0;
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (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;
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (HASH);
|
2010-05-20 22:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:32:12 -07:00
|
|
|
{DECIMAL_INTEGER} {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (INTEGER_STRING);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{OCTAL_INTEGER} {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (INTEGER_STRING);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{HEXADECIMAL_INTEGER} {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (INTEGER_STRING);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"<<" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (LEFT_SHIFT);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
">>" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (RIGHT_SHIFT);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"<=" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (LESS_OR_EQUAL);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
">=" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (GREATER_OR_EQUAL);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"==" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (EQUAL);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"!=" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (NOT_EQUAL);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"&&" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (AND);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"||" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (OR);
|
2010-05-24 10:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
"##" {
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
if (! parser->skipping) {
|
|
|
|
|
if (parser->is_gles)
|
|
|
|
|
glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
|
|
|
|
|
RETURN_TOKEN (PASTE);
|
|
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:32:12 -07:00
|
|
|
"defined" {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (DEFINED);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:35:34 -07:00
|
|
|
{IDENTIFIER} {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (IDENTIFIER);
|
2010-05-26 09:35:34 -07:00
|
|
|
}
|
|
|
|
|
|
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} {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (OTHER);
|
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
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:59:02 -07:00
|
|
|
{PUNCTUATION} {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (yytext[0]);
|
2010-05-19 13:28:24 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 15:04:32 -07:00
|
|
|
{OTHER}+ {
|
2014-06-20 15:30:21 -07:00
|
|
|
RETURN_STRING_TOKEN (OTHER);
|
2010-05-25 15:04:32 -07:00
|
|
|
}
|
|
|
|
|
|
2014-02-05 20:15:56 +06:00
|
|
|
{HSPACE} {
|
2010-05-25 16:59:02 -07:00
|
|
|
if (yyextra->space_tokens) {
|
2014-06-20 14:58:48 -07:00
|
|
|
RETURN_TOKEN (SPACE);
|
2010-05-25 16:59:02 -07:00
|
|
|
}
|
2010-05-12 13:19:23 -07:00
|
|
|
}
|
|
|
|
|
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
/* We preserve all newlines, even between #if 0..#endif, so no
|
|
|
|
|
skipping.. */
|
|
|
|
|
\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;
|
glsl/glcpp: Stop using a lexer start condition (<SKIP>) for token skipping.
Here, "skipping" refers to the lexer not emitting any tokens for portions of
the file within an #if condition (or similar) that evaluates to false.
Previously, the lexer had a special <SKIP> start condition used to control
this skipping. This start condition was not handled like a normal start
condition. Instead, there was a particularly ugly block of code set to be
included at the top of the generated lexing loop that would change from
<INITIAL> to <SKIP> or from <SKIP> to <INITIAL> depending on various pieces of
parser state, (such as parser->skip_state and parser->lexing_directive).
Not only was that an ugly approach, but the <SKIP> start condition was
complicating several glcpp bug fixes I attempted recently that want to use
start conditions for other purposes, (such as a new <HASH> start condition).
The recently added RETURN_TOKEN macro gives us a convenient way to implement
skipping without using a lexer start condition. Now, at the top of the
generated lexer, we examine all the necessary parser state and set a new
parser->skipping bit. Then, in RETURN_TOKEN, we examine parser->skipping to
determine whether to actually emit the token or not.
Besides this, there are only a couple of other places where we need to examine
the skipping bit (other than when returning a token):
* To avoid emitting an error for #error if skipped.
* To avoid entering the <DEFINE> start condition for a #define that is
skipped.
With all of this in place in the present commit, there are hopefully no
behavioral changes with this patch, ("make check" still passes all of the
glcpp tests at least).
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
2014-06-20 16:18:23 -07:00
|
|
|
RETURN_TOKEN_NEVER_SKIP (NEWLINE);
|
2010-05-25 15:04:32 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-20 14:28:20 -07:00
|
|
|
<INITIAL,COMMENT,DEFINE><<EOF>> {
|
|
|
|
|
if (YY_START == COMMENT)
|
|
|
|
|
glcpp_error(yylloc, yyextra, "Unterminated comment");
|
|
|
|
|
if (YY_START == DEFINE)
|
|
|
|
|
glcpp_error(yylloc, yyextra, "#define without macro name");
|
2010-06-16 12:53:19 -07:00
|
|
|
BEGIN DONE; /* Don't keep matching this rule forever. */
|
2014-06-12 10:39:39 -07:00
|
|
|
yyextra->lexing_directive = 0;
|
2014-06-20 14:58:48 -07:00
|
|
|
if (! parser->last_token_was_newline)
|
|
|
|
|
RETURN_TOKEN (NEWLINE);
|
2010-06-16 12:53:19 -07:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|