mesa/glcpp-lex.l
Carl Worth 81f01432bd Don't return SPACE tokens unless strictly needed.
This reverts the unconditional return of SPACE tokens from the lexer
from commit 48b94da099 .

That commit seemed useful because it kept the lexer simpler, but the
presence of SPACE tokens is causing lots of extra complication for the
parser itself, (redundant productions other than whitespace
differences, several productions buggy in the case of extra
whitespace, etc.)

Of course, we'd prefer to never have any whitespace token, but that's
not possible with the need to distinguish between "#define foo()" and
"#define foo ()". So we'll accept a little bit of pain in the lexer,
(enough state to support this special-case token), in exchange for
keeping most of the parser blissffully ignorant of whether tokens are
separated by whitespace or not.

This change does mean that our output now differs from that of "gcc -E",
but only in whitespace. So we test with "diff -w now to ignore those
differences.
2010-05-14 17:13:00 -07:00

114 lines
2.5 KiB
Text

%{
/*
* 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>
#include "glcpp.h"
#include "glcpp-parse.h"
%}
%option reentrant noyywrap
%option extra-type="glcpp_parser_t *"
%x ST_DEFINE
%x ST_DEFVAL
SPACE [[:space:]]
NONSPACE [^[:space:]]
NEWLINE [\n]
HSPACE [ \t]
HASH ^{HSPACE}*#
IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
TOKEN [^[:space:](),]+
%%
{HASH}undef{HSPACE}* {
return UNDEF;
}
/* We use the ST_DEFINE and ST_DEFVAL states so that we can
* pass a space token, (yes, a token for whitespace!), since
* the preprocessor specification requires distinguishing
* "#define foo()" from "#define foo ()".
*/
{HASH}define{HSPACE}* {
BEGIN ST_DEFINE;
return DEFINE;
}
<ST_DEFINE>{IDENTIFIER} {
BEGIN ST_DEFVAL;
yylval.str = xtalloc_strdup (yyextra, yytext);
return IDENTIFIER;
}
<ST_DEFVAL>\n {
BEGIN INITIAL;
return NEWLINE;
}
<ST_DEFVAL>{HSPACE}+ {
BEGIN INITIAL;
return SPACE;
}
<ST_DEFVAL>"(" {
BEGIN INITIAL;
return '(';
}
{IDENTIFIER} {
yylval.str = xtalloc_strdup (yyextra, yytext);
switch (glcpp_parser_macro_type (yyextra, yylval.str))
{
case MACRO_TYPE_UNDEFINED:
return IDENTIFIER;
break;
case MACRO_TYPE_OBJECT:
return OBJ_MACRO;
break;
case MACRO_TYPE_FUNCTION:
return FUNC_MACRO;
break;
}
}
[(),] {
return yytext[0];
}
{TOKEN} {
yylval.str = xtalloc_strdup (yyextra, yytext);
return TOKEN;
}
\n {
return NEWLINE;
}
{SPACE}+
%%