aboutsummaryrefslogtreecommitdiffstats
path: root/src/glsl/glcpp/glcpp-lex.l
diff options
context:
space:
mode:
authorCarl Worth <[email protected]>2014-06-20 14:58:48 -0700
committerCarl Worth <[email protected]>2014-07-29 15:11:49 -0700
commit828686d4ebe055d1c6583192bc57742e101ff931 (patch)
treefe6825369bfd1e755dd8b4f83e7121bc8e3744dc /src/glsl/glcpp/glcpp-lex.l
parent5dbdc341e8e8502ab5d98784a24b54c6f3907472 (diff)
glsl/glcpp: Drop extra, final newline from most output
The glcpp parser is line-based, so it needs to see a NEWLINE token at the end of each line. This causes a trick for files that end without a final newline. Previously, the lexer for glcpp punted in this case by unconditionally returning a NEWLINE token at end-of-file, (causing most files to have an extra blank line at the end). Here, we refine this by lexing end-of-file as a NEWLINE token only if the immediately preceding token was not a NEWLINE token. The patch is a minor change that only looks huge for two reasons: 1. Almost all glcpp test result ".expected" files are updated to drop the extra newline. 2. All return statements from the lexer are adjusted to use a new RETURN_TOKEN macro that tracks the last-token-was-a-newline state. Reviewed-by: Jordan Justen <[email protected]>
Diffstat (limited to 'src/glsl/glcpp/glcpp-lex.l')
-rw-r--r--src/glsl/glcpp/glcpp-lex.l87
1 files changed, 49 insertions, 38 deletions
diff --git a/src/glsl/glcpp/glcpp-lex.l b/src/glsl/glcpp/glcpp-lex.l
index 44547fde0e9..980ab5d3ff1 100644
--- a/src/glsl/glcpp/glcpp-lex.l
+++ b/src/glsl/glcpp/glcpp-lex.l
@@ -60,6 +60,16 @@ void glcpp_set_column (int column_no , yyscan_t yyscanner);
yycolumn = 1; \
yylloc->source = 0; \
} while(0)
+
+#define RETURN_TOKEN(token) \
+ do { \
+ if (token == NEWLINE) \
+ parser->last_token_was_newline = 1; \
+ else \
+ parser->last_token_was_newline = 0; \
+ return (token); \
+ } while(0)
+
%}
%option bison-bridge bison-locations reentrant noyywrap
@@ -111,7 +121,7 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
parser->commented_newlines--;
if (parser->commented_newlines == 0)
BEGIN INITIAL;
- return NEWLINE;
+ RETURN_TOKEN (NEWLINE);
}
/* The handling of the SKIP vs INITIAL start states requires
@@ -169,13 +179,13 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
<COMMENT>"*"+"/" {
yy_pop_state(yyscanner);
if (yyextra->space_tokens)
- return SPACE;
+ RETURN_TOKEN (SPACE);
}
{HASH}version{HSPACE}+ {
yylval->str = ralloc_strdup (yyextra, yytext);
yyextra->space_tokens = 0;
- return HASH_VERSION;
+ RETURN_TOKEN (HASH_VERSION);
}
/* glcpp doesn't handle #extension, #version, or #pragma directives.
@@ -184,46 +194,46 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
yylval->str = ralloc_strdup (yyextra, yytext);
yylineno++;
yycolumn = 0;
- return OTHER;
+ RETURN_TOKEN (OTHER);
}
{HASH}line{HSPACE}+ {
- return HASH_LINE;
+ RETURN_TOKEN (HASH_LINE);
}
<SKIP,INITIAL>{
{HASH}ifdef {
yyextra->lexing_directive = 1;
yyextra->space_tokens = 0;
- return HASH_IFDEF;
+ RETURN_TOKEN (HASH_IFDEF);
}
{HASH}ifndef {
yyextra->lexing_directive = 1;
yyextra->space_tokens = 0;
- return HASH_IFNDEF;
+ RETURN_TOKEN (HASH_IFNDEF);
}
{HASH}if/[^_a-zA-Z0-9] {
yyextra->lexing_directive = 1;
yyextra->space_tokens = 0;
- return HASH_IF;
+ RETURN_TOKEN (HASH_IF);
}
{HASH}elif/[^_a-zA-Z0-9] {
yyextra->lexing_directive = 1;
yyextra->space_tokens = 0;
- return HASH_ELIF;
+ RETURN_TOKEN (HASH_ELIF);
}
{HASH}else {
yyextra->space_tokens = 0;
- return HASH_ELSE;
+ RETURN_TOKEN (HASH_ELSE);
}
{HASH}endif {
yyextra->space_tokens = 0;
- return HASH_ENDIF;
+ RETURN_TOKEN (HASH_ENDIF);
}
}
@@ -258,21 +268,21 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
{HASH}define{HSPACE}+ {
yyextra->space_tokens = 0;
yy_push_state(DEFINE, yyscanner);
- return HASH_DEFINE;
+ RETURN_TOKEN (HASH_DEFINE);
}
/* An identifier immediately followed by '(' */
<DEFINE>{IDENTIFIER}/"(" {
yy_pop_state(yyscanner);
yylval->str = ralloc_strdup (yyextra, yytext);
- return FUNC_IDENTIFIER;
+ RETURN_TOKEN (FUNC_IDENTIFIER);
}
/* An identifier not immediately followed by '(' */
<DEFINE>{IDENTIFIER} {
yy_pop_state(yyscanner);
yylval->str = ralloc_strdup (yyextra, yytext);
- return OBJ_IDENTIFIER;
+ RETURN_TOKEN (OBJ_IDENTIFIER);
}
/* Whitespace */
@@ -284,7 +294,7 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
<DEFINE>[/][^*]{NONSPACE}* {
BEGIN INITIAL;
glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
- return INTEGER_STRING;
+ RETURN_TOKEN (INTEGER_STRING);
}
/* A character that can't start an identifier, comment, or
@@ -292,98 +302,98 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
<DEFINE>[^_a-zA-Z/[:space:]]{NONSPACE}* {
BEGIN INITIAL;
glcpp_error(yylloc, yyextra, "#define followed by a non-identifier: %s", yytext);
- return INTEGER_STRING;
+ RETURN_TOKEN (INTEGER_STRING);
}
{HASH}undef {
yyextra->space_tokens = 0;
- return HASH_UNDEF;
+ RETURN_TOKEN (HASH_UNDEF);
}
{HASH} {
yyextra->space_tokens = 0;
- return HASH;
+ RETURN_TOKEN (HASH);
}
{DECIMAL_INTEGER} {
yylval->str = ralloc_strdup (yyextra, yytext);
- return INTEGER_STRING;
+ RETURN_TOKEN (INTEGER_STRING);
}
{OCTAL_INTEGER} {
yylval->str = ralloc_strdup (yyextra, yytext);
- return INTEGER_STRING;
+ RETURN_TOKEN (INTEGER_STRING);
}
{HEXADECIMAL_INTEGER} {
yylval->str = ralloc_strdup (yyextra, yytext);
- return INTEGER_STRING;
+ RETURN_TOKEN (INTEGER_STRING);
}
"<<" {
- return LEFT_SHIFT;
+ RETURN_TOKEN (LEFT_SHIFT);
}
">>" {
- return RIGHT_SHIFT;
+ RETURN_TOKEN (RIGHT_SHIFT);
}
"<=" {
- return LESS_OR_EQUAL;
+ RETURN_TOKEN (LESS_OR_EQUAL);
}
">=" {
- return GREATER_OR_EQUAL;
+ RETURN_TOKEN (GREATER_OR_EQUAL);
}
"==" {
- return EQUAL;
+ RETURN_TOKEN (EQUAL);
}
"!=" {
- return NOT_EQUAL;
+ RETURN_TOKEN (NOT_EQUAL);
}
"&&" {
- return AND;
+ RETURN_TOKEN (AND);
}
"||" {
- return OR;
+ RETURN_TOKEN (OR);
}
"##" {
if (parser->is_gles)
glcpp_error(yylloc, yyextra, "Token pasting (##) is illegal in GLES");
- return PASTE;
+ RETURN_TOKEN (PASTE);
}
"defined" {
- return DEFINED;
+ RETURN_TOKEN (DEFINED);
}
{IDENTIFIER} {
yylval->str = ralloc_strdup (yyextra, yytext);
- return IDENTIFIER;
+ RETURN_TOKEN (IDENTIFIER);
}
{PP_NUMBER} {
yylval->str = ralloc_strdup (yyextra, yytext);
- return OTHER;
+ RETURN_TOKEN (OTHER);
}
{PUNCTUATION} {
- return yytext[0];
+ RETURN_TOKEN (yytext[0]);
}
{OTHER}+ {
yylval->str = ralloc_strdup (yyextra, yytext);
- return OTHER;
+ RETURN_TOKEN (OTHER);
}
{HSPACE} {
if (yyextra->space_tokens) {
- return SPACE;
+ RETURN_TOKEN (SPACE);
}
}
@@ -395,7 +405,7 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
yyextra->lexing_directive = 0;
yylineno++;
yycolumn = 0;
- return NEWLINE;
+ RETURN_TOKEN (NEWLINE);
}
<INITIAL,COMMENT,DEFINE><<EOF>> {
@@ -405,7 +415,8 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
glcpp_error(yylloc, yyextra, "#define without macro name");
BEGIN DONE; /* Don't keep matching this rule forever. */
yyextra->lexing_directive = 0;
- return NEWLINE;
+ if (! parser->last_token_was_newline)
+ RETURN_TOKEN (NEWLINE);
}
/* We don't actually use the UNREACHABLE start condition. We