From 9f62a7e9e25efd79ebf46c64166876436f88f08a Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 13 May 2010 07:38:29 -0700 Subject: [PATCH] Make the lexer distinguish between identifiers and defined macros. This is just a minor style improvement for now. But the same mechanism, (having the lexer peek into the table of defined macros), will be essential when we add function-like macros in addition to the current object-like macros. --- glcpp-lex.l | 5 ++++- glcpp-parse.y | 51 ++++++++++++++++++++++++++++++++++----------------- glcpp.h | 4 ++++ 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/glcpp-lex.l b/glcpp-lex.l index 18d9050d715..3622db939e7 100644 --- a/glcpp-lex.l +++ b/glcpp-lex.l @@ -53,7 +53,10 @@ TOKEN {NONSPACE}+ {IDENTIFIER} { yylval.str = xtalloc_strdup (yyextra, yytext); - return IDENTIFIER; + if (glcpp_parser_macro_defined (yyextra, yylval.str)) + return MACRO; + else + return IDENTIFIER; } {TOKEN} { diff --git a/glcpp-parse.y b/glcpp-parse.y index 91fc5b98fc5..4d6475497bf 100644 --- a/glcpp-parse.y +++ b/glcpp-parse.y @@ -39,7 +39,7 @@ void yyerror (void *scanner, const char *error); void -_print_resolved_token (glcpp_parser_t *parser, const char *token); +_print_expanded_macro (glcpp_parser_t *parser, const char *macro); list_t * _list_create (void *ctx); @@ -57,8 +57,8 @@ _list_append (list_t *list, const char *str); %parse-param {glcpp_parser_t *parser} %lex-param {void *scanner} -%token DEFINE IDENTIFIER NEWLINE TOKEN UNDEF -%type token IDENTIFIER TOKEN +%token DEFINE IDENTIFIER MACRO NEWLINE TOKEN UNDEF +%type IDENTIFIER MACRO TOKEN string %type replacement_list %% @@ -69,8 +69,16 @@ input: ; content: - token { - _print_resolved_token (parser, $1); + IDENTIFIER { + printf ("%s", $1); + talloc_free ($1); + } +| TOKEN { + printf ("%s", $1); + talloc_free ($1); + } +| MACRO { + _print_expanded_macro (parser, $1); talloc_free ($1); } | directive_with_newline @@ -90,7 +98,7 @@ directive: talloc_steal ($3, $2); hash_table_insert (parser->defines, $3, $2); } -| UNDEF IDENTIFIER { +| UNDEF MACRO { list_t *replacement = hash_table_find (parser->defines, $2); if (replacement) { /* XXX: Need hash table to support a real way @@ -108,16 +116,17 @@ replacement_list: $$ = _list_create (parser); } -| replacement_list token { +| replacement_list string { _list_append ($1, $2); talloc_free ($2); $$ = $1; } ; -token: - TOKEN { $$ = $1; } -| IDENTIFIER { $$ = $1; } +string: + IDENTIFIER { $$ = $1; } +| MACRO { $$ = $1; } +| TOKEN { $$ = $1; } ; %% @@ -187,11 +196,17 @@ glcpp_parser_destroy (glcpp_parser_t *parser) talloc_free (parser); } +int +glcpp_parser_macro_defined (glcpp_parser_t *parser, const char *identifier) +{ + return (hash_table_find (parser->defines, identifier) != NULL); +} + static void -_print_resolved_recursive (glcpp_parser_t *parser, - const char *token, - const char *orig, - int *first) +_print_expanded_macro_recursive (glcpp_parser_t *parser, + const char *token, + const char *orig, + int *first) { list_t *replacement; node_t *node; @@ -207,16 +222,18 @@ _print_resolved_recursive (glcpp_parser_t *parser, printf ("%s%s", *first ? "" : " ", token); *first = 0; } else { - _print_resolved_recursive (parser, token, orig, first); + _print_expanded_macro_recursive (parser, + token, orig, + first); } } } } void -_print_resolved_token (glcpp_parser_t *parser, const char *token) +_print_expanded_macro (glcpp_parser_t *parser, const char *macro) { int first = 1; - _print_resolved_recursive (parser, token, token, &first); + _print_expanded_macro_recursive (parser, macro, macro, &first); } diff --git a/glcpp.h b/glcpp.h index 8472570ccb6..39d6d5d0ebb 100644 --- a/glcpp.h +++ b/glcpp.h @@ -52,6 +52,10 @@ glcpp_parser_parse (glcpp_parser_t *parser); void glcpp_parser_destroy (glcpp_parser_t *parser); +int +glcpp_parser_macro_defined (glcpp_parser_t *parser, + const char *identifier); + /* Generated by glcpp-lex.l to glcpp-lex.c */ int -- 2.30.2