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 <stdlib.h>
|
2010-05-13 09:36:23 -07:00
|
|
|
#include <assert.h>
|
2010-05-12 12:17:10 -07:00
|
|
|
#include <talloc.h>
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
#define YYLEX_PARAM parser->scanner
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
typedef struct {
|
|
|
|
|
int is_function;
|
|
|
|
|
list_t *parameter_list;
|
|
|
|
|
list_t *replacement_list;
|
|
|
|
|
} macro_t;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
struct glcpp_parser {
|
|
|
|
|
yyscan_t scanner;
|
|
|
|
|
struct hash_table *defines;
|
|
|
|
|
};
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
void
|
2010-05-10 13:17:25 -07:00
|
|
|
yyerror (void *scanner, const char *error);
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
|
|
|
|
list_t *replacement_list);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
|
|
|
|
list_t *parameter_list,
|
|
|
|
|
list_t *replacement_list);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_print_expanded_object_macro (glcpp_parser_t *parser, const char *macro);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_print_expanded_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
|
|
|
|
list_t *arguments);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
list_t *
|
|
|
|
|
_list_create (void *ctx);
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_list_append_item (list_t *list, const char *str);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_list_append_list (list_t *list, list_t *tail);
|
2010-05-11 12:30:09 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
%union {
|
|
|
|
|
char *str;
|
|
|
|
|
list_t *list;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
%parse-param {glcpp_parser_t *parser}
|
2010-05-10 11:52:29 -07:00
|
|
|
%lex-param {void *scanner}
|
|
|
|
|
|
2010-05-13 10:29:07 -07:00
|
|
|
%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF
|
|
|
|
|
%type <str> FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN word word_or_symbol
|
2010-05-13 09:36:23 -07:00
|
|
|
%type <list> argument argument_list parameter_list replacement_list
|
2010-05-10 11:44:09 -07:00
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
input:
|
|
|
|
|
/* empty */
|
2010-05-12 13:21:20 -07:00
|
|
|
| input content
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
content:
|
2010-05-13 07:38:29 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
printf ("%s", $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
| TOKEN {
|
|
|
|
|
printf ("%s", $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
| macro
|
2010-05-12 13:11:50 -07:00
|
|
|
| directive_with_newline
|
2010-05-13 10:29:07 -07:00
|
|
|
| NEWLINE { printf ("\n"); }
|
|
|
|
|
| '(' { printf ("("); }
|
|
|
|
|
| ')' { printf (")"); }
|
|
|
|
|
| ',' { printf (","); }
|
2010-05-13 10:46:29 -07:00
|
|
|
| SPACE { printf (" "); }
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro:
|
|
|
|
|
FUNC_MACRO '(' argument_list ')' {
|
|
|
|
|
_print_expanded_function_macro (parser, $1, $3);
|
|
|
|
|
}
|
|
|
|
|
| OBJ_MACRO {
|
|
|
|
|
_print_expanded_object_macro (parser, $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument_list:
|
2010-05-14 08:47:32 -07:00
|
|
|
argument {
|
2010-05-13 09:36:23 -07:00
|
|
|
$$ = _list_create (parser);
|
|
|
|
|
_list_append_list ($$, $1);
|
|
|
|
|
}
|
|
|
|
|
| argument_list ',' argument {
|
|
|
|
|
_list_append_list ($1, $3);
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument:
|
|
|
|
|
/* empty */ {
|
|
|
|
|
$$ = _list_create (parser);
|
|
|
|
|
}
|
2010-05-13 10:29:07 -07:00
|
|
|
| argument word {
|
2010-05-13 09:36:23 -07:00
|
|
|
_list_append_item ($1, $2);
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
}
|
|
|
|
|
| argument '(' argument ')'
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-12 13:11:50 -07:00
|
|
|
directive_with_newline:
|
|
|
|
|
directive NEWLINE {
|
|
|
|
|
printf ("\n");
|
|
|
|
|
}
|
2010-05-10 16:16:06 -07:00
|
|
|
;
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
directive:
|
2010-05-13 10:29:07 -07:00
|
|
|
DEFINE IDENTIFIER {
|
|
|
|
|
list_t *list = _list_create (parser);
|
|
|
|
|
_define_object_macro (parser, $2, list);
|
|
|
|
|
}
|
|
|
|
|
| DEFINE IDENTIFIER SPACE replacement_list {
|
|
|
|
|
_define_object_macro (parser, $2, $4);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-13 10:46:29 -07:00
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' {
|
|
|
|
|
list_t *list = _list_create (parser);
|
|
|
|
|
_define_function_macro (parser, $2, $4, list);
|
|
|
|
|
}
|
|
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' SPACE replacement_list {
|
|
|
|
|
_define_function_macro (parser, $2, $4, $7);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
| UNDEF FUNC_MACRO {
|
|
|
|
|
list_t *replacement = hash_table_find (parser->defines, $2);
|
|
|
|
|
if (replacement) {
|
|
|
|
|
/* XXX: Need hash table to support a real way
|
|
|
|
|
* to remove an element rather than prefixing
|
|
|
|
|
* a new node with data of NULL like this. */
|
|
|
|
|
hash_table_insert (parser->defines, NULL, $2);
|
|
|
|
|
talloc_free (replacement);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 13:11:50 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
| UNDEF OBJ_MACRO {
|
2010-05-12 13:11:50 -07:00
|
|
|
list_t *replacement = hash_table_find (parser->defines, $2);
|
|
|
|
|
if (replacement) {
|
|
|
|
|
/* XXX: Need hash table to support a real way
|
|
|
|
|
* to remove an element rather than prefixing
|
|
|
|
|
* a new node with data of NULL like this. */
|
|
|
|
|
hash_table_insert (parser->defines, NULL, $2);
|
|
|
|
|
talloc_free (replacement);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
replacement_list:
|
2010-05-13 10:46:29 -07:00
|
|
|
word_or_symbol {
|
2010-05-12 12:17:10 -07:00
|
|
|
$$ = _list_create (parser);
|
2010-05-13 10:46:29 -07:00
|
|
|
_list_append_item ($$, $1);
|
|
|
|
|
talloc_free ($1);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-13 10:29:07 -07:00
|
|
|
| replacement_list word_or_symbol {
|
2010-05-13 09:36:23 -07:00
|
|
|
_list_append_item ($1, $2);
|
2010-05-12 12:45:33 -07:00
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
parameter_list:
|
|
|
|
|
/* empty */ {
|
|
|
|
|
$$ = _list_create (parser);
|
|
|
|
|
}
|
|
|
|
|
| IDENTIFIER {
|
|
|
|
|
$$ = _list_create (parser);
|
|
|
|
|
_list_append_item ($$, $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
| parameter_list ',' IDENTIFIER {
|
|
|
|
|
_list_append_item ($1, $3);
|
|
|
|
|
talloc_free ($3);
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-13 10:29:07 -07:00
|
|
|
word_or_symbol:
|
|
|
|
|
word { $$ = $1; }
|
|
|
|
|
| '(' { $$ = xtalloc_strdup (parser, "("); }
|
|
|
|
|
| ')' { $$ = xtalloc_strdup (parser, ")"); }
|
|
|
|
|
| ',' { $$ = xtalloc_strdup (parser, ","); }
|
2010-05-13 10:46:29 -07:00
|
|
|
| SPACE { $$ = xtalloc_strdup (parser, " "); }
|
2010-05-13 10:29:07 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
word:
|
2010-05-13 07:38:29 -07:00
|
|
|
IDENTIFIER { $$ = $1; }
|
2010-05-13 09:36:23 -07:00
|
|
|
| FUNC_MACRO { $$ = $1; }
|
|
|
|
|
| OBJ_MACRO { $$ = $1; }
|
2010-05-13 07:38:29 -07:00
|
|
|
| TOKEN { $$ = $1; }
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
list_t *
|
|
|
|
|
_list_create (void *ctx)
|
|
|
|
|
{
|
|
|
|
|
list_t *list;
|
|
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
list = xtalloc (ctx, list_t);
|
2010-05-12 12:17:10 -07:00
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_list_append_list (list_t *list, list_t *tail)
|
|
|
|
|
{
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = tail->head;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = tail->head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = tail->tail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_list_append_item (list_t *list, const char *str)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
|
|
|
|
node_t *node;
|
|
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
node = xtalloc (list, node_t);
|
|
|
|
|
node->str = xtalloc_strdup (node, str);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
void
|
2010-05-10 13:17:25 -07:00
|
|
|
yyerror (void *scanner, const char *error)
|
2010-05-10 11:44:09 -07:00
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Parse error: %s\n", error);
|
|
|
|
|
}
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_t *
|
|
|
|
|
glcpp_parser_create (void)
|
2010-05-10 16:16:06 -07:00
|
|
|
{
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_t *parser;
|
|
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
parser = xtalloc (NULL, glcpp_parser_t);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-12 12:45:33 -07:00
|
|
|
yylex_init_extra (parser, &parser->scanner);
|
2010-05-10 16:16:06 -07:00
|
|
|
parser->defines = hash_table_ctor (32, hash_table_string_hash,
|
|
|
|
|
hash_table_string_compare);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
return parser;
|
2010-05-10 16:16:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
glcpp_parser_parse (glcpp_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
return yyparse (parser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-12 12:17:10 -07:00
|
|
|
glcpp_parser_destroy (glcpp_parser_t *parser)
|
2010-05-10 16:16:06 -07:00
|
|
|
{
|
|
|
|
|
yylex_destroy (parser->scanner);
|
|
|
|
|
hash_table_dtor (parser->defines);
|
2010-05-12 12:17:10 -07:00
|
|
|
talloc_free (parser);
|
2010-05-10 16:16:06 -07:00
|
|
|
}
|
2010-05-11 12:30:09 -07:00
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_type_t
|
|
|
|
|
glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier)
|
2010-05-13 07:38:29 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
if (macro == NULL)
|
|
|
|
|
return MACRO_TYPE_UNDEFINED;
|
|
|
|
|
|
|
|
|
|
if (macro->is_function)
|
|
|
|
|
return MACRO_TYPE_FUNCTION;
|
|
|
|
|
else
|
|
|
|
|
return MACRO_TYPE_OBJECT;
|
2010-05-13 07:38:29 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
static void
|
2010-05-13 07:38:29 -07:00
|
|
|
_print_expanded_macro_recursive (glcpp_parser_t *parser,
|
|
|
|
|
const char *token,
|
2010-05-13 10:46:29 -07:00
|
|
|
const char *orig)
|
2010-05-11 12:30:09 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
2010-05-12 12:17:10 -07:00
|
|
|
node_t *node;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro = hash_table_find (parser->defines, token);
|
|
|
|
|
if (macro == NULL) {
|
2010-05-13 10:46:29 -07:00
|
|
|
printf ("%s", token);
|
2010-05-12 12:17:10 -07:00
|
|
|
} else {
|
2010-05-13 09:36:23 -07:00
|
|
|
list_t *replacement_list = macro->replacement_list;
|
|
|
|
|
|
|
|
|
|
for (node = replacement_list->head ; node ; node = node->next) {
|
2010-05-12 12:17:10 -07:00
|
|
|
token = node->str;
|
2010-05-13 10:46:29 -07:00
|
|
|
if (strcmp (token, orig) == 0)
|
|
|
|
|
printf ("%s", token);
|
|
|
|
|
else
|
2010-05-13 07:38:29 -07:00
|
|
|
_print_expanded_macro_recursive (parser,
|
2010-05-13 10:46:29 -07:00
|
|
|
token, orig);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-11 12:30:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
list_t *replacement_list)
|
|
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 0;
|
|
|
|
|
macro->parameter_list = NULL;
|
|
|
|
|
macro->replacement_list = talloc_steal (macro, replacement_list);
|
|
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
list_t *parameter_list,
|
|
|
|
|
list_t *replacement_list)
|
|
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 1;
|
|
|
|
|
macro->parameter_list = talloc_steal (macro, parameter_list);
|
|
|
|
|
macro->replacement_list = talloc_steal (macro, replacement_list);
|
|
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_print_expanded_object_macro (glcpp_parser_t *parser, const char *identifier)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (! macro->is_function);
|
|
|
|
|
|
2010-05-13 10:46:29 -07:00
|
|
|
_print_expanded_macro_recursive (parser, identifier, identifier);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_print_expanded_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
list_t *arguments)
|
|
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (macro->is_function);
|
|
|
|
|
|
|
|
|
|
/* XXX: Need to use argument list here in the expansion. */
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-13 10:46:29 -07:00
|
|
|
_print_expanded_macro_recursive (parser, identifier, identifier);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|