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-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
|
|
|
|
|
|
|
|
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,
|
2010-05-18 22:10:04 -07:00
|
|
|
const char *replacement);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
2010-05-18 22:10:04 -07:00
|
|
|
const char *replacement);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_expand_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_print_string_list (string_list_t *list);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_string_list_create (void *ctx);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item (string_list_t *list, const char *str);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_list (string_list_t *list, string_list_t *tail);
|
2010-05-11 12:30:09 -07:00
|
|
|
|
2010-05-13 12:56:42 -07:00
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_contains (string_list_t *list, const char *member, int *index);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_length (string_list_t *list);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *
|
|
|
|
|
_argument_list_create (void *ctx);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_argument_list_append (argument_list_t *list, string_list_t *argument);
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_argument_list_length (argument_list_t *list);
|
|
|
|
|
|
|
|
|
|
string_list_t *
|
|
|
|
|
_argument_list_member_at (argument_list_t *list, int index);
|
|
|
|
|
|
2010-05-19 10:01:29 -07:00
|
|
|
static int
|
|
|
|
|
yylex (yyscan_t scanner);
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
%union {
|
|
|
|
|
char *str;
|
2010-05-14 10:44:19 -07:00
|
|
|
string_list_t *string_list;
|
|
|
|
|
argument_list_t *argument_list;
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
|
|
|
|
|
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-18 22:10:04 -07:00
|
|
|
%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO REPLACEMENT TOKEN UNDEF
|
2010-05-19 07:49:47 -07:00
|
|
|
%type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO REPLACEMENT TOKEN
|
2010-05-18 22:10:04 -07:00
|
|
|
%type <string_list> argument macro parameter_list
|
2010-05-14 10:44:19 -07:00
|
|
|
%type <argument_list> argument_list
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-17 12:45:16 -07:00
|
|
|
/* Hard to remove shift/reduce conflicts documented as follows:
|
|
|
|
|
*
|
|
|
|
|
* 1. '(' after FUNC_MACRO name which is correctly resolved to shift
|
|
|
|
|
* to form macro invocation rather than reducing directly to
|
|
|
|
|
* content.
|
2010-05-19 07:42:42 -07:00
|
|
|
*
|
|
|
|
|
* 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
|
|
|
|
|
* shift to form macro invocation rather than reducing directly to
|
|
|
|
|
* argument.
|
2010-05-17 12:45:16 -07:00
|
|
|
*/
|
2010-05-19 07:42:42 -07:00
|
|
|
%expect 2
|
2010-05-17 12:45:16 -07:00
|
|
|
|
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-14 10:17:38 -07:00
|
|
|
/* We do all printing at the content level */
|
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-17 10:34:29 -07:00
|
|
|
| FUNC_MACRO {
|
|
|
|
|
printf ("%s", $1);
|
|
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| directive {
|
|
|
|
|
printf ("\n");
|
2010-05-14 10:31:43 -07:00
|
|
|
}
|
2010-05-13 10:29:07 -07:00
|
|
|
| '(' { printf ("("); }
|
|
|
|
|
| ')' { printf (")"); }
|
|
|
|
|
| ',' { printf (","); }
|
2010-05-18 22:10:04 -07:00
|
|
|
| macro
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro:
|
|
|
|
|
FUNC_MACRO '(' argument_list ')' {
|
2010-05-18 22:10:04 -07:00
|
|
|
_expand_function_macro (parser, $1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
| OBJ_MACRO {
|
2010-05-18 22:10:04 -07:00
|
|
|
_expand_object_macro (parser, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument_list:
|
2010-05-14 11:33:00 -07:00
|
|
|
/* empty */ {
|
|
|
|
|
$$ = _argument_list_create (parser);
|
|
|
|
|
}
|
|
|
|
|
| argument {
|
2010-05-14 10:44:19 -07:00
|
|
|
$$ = _argument_list_create (parser);
|
|
|
|
|
_argument_list_append ($$, $1);
|
|
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
| argument_list ',' argument {
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_append ($1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
argument:
|
2010-05-19 07:49:47 -07:00
|
|
|
argument_word {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
2010-05-14 11:33:00 -07:00
|
|
|
_string_list_append_item ($$, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-19 07:49:47 -07:00
|
|
|
| argument argument_word {
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item ($1, $2);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($2);
|
2010-05-14 16:53:52 -07:00
|
|
|
$$ = $1;
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-14 16:53:52 -07:00
|
|
|
| argument '(' argument ')' {
|
|
|
|
|
_string_list_append_item ($1, "(");
|
|
|
|
|
_string_list_append_list ($1, $3);
|
|
|
|
|
_string_list_append_item ($1, ")");
|
|
|
|
|
$$ = $1;
|
2010-05-14 10:44:19 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-19 07:49:47 -07:00
|
|
|
argument_word:
|
|
|
|
|
IDENTIFIER { $$ = $1; }
|
|
|
|
|
| TOKEN { $$ = $1; }
|
|
|
|
|
| FUNC_MACRO { $$ = $1; }
|
2010-05-19 07:57:03 -07:00
|
|
|
| macro { $$ = xtalloc_strdup (parser, ""); }
|
2010-05-19 07:49:47 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
directive:
|
2010-05-18 22:10:04 -07:00
|
|
|
DEFINE IDENTIFIER REPLACEMENT {
|
|
|
|
|
_define_object_macro (parser, $2, $3);
|
2010-05-13 10:29:07 -07:00
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| DEFINE IDENTIFIER '(' parameter_list ')' REPLACEMENT {
|
2010-05-14 17:08:45 -07:00
|
|
|
_define_function_macro (parser, $2, $4, $6);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| UNDEF IDENTIFIER {
|
|
|
|
|
string_list_t *macro = hash_table_find (parser->defines, $2);
|
|
|
|
|
if (macro) {
|
2010-05-12 13:11:50 -07:00
|
|
|
/* 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);
|
2010-05-18 22:10:04 -07:00
|
|
|
talloc_free (macro);
|
2010-05-12 13:11:50 -07:00
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
parameter_list:
|
|
|
|
|
/* empty */ {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| IDENTIFIER {
|
2010-05-14 10:05:11 -07:00
|
|
|
$$ = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item ($$, $1);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-18 22:10:04 -07:00
|
|
|
| parameter_list ',' IDENTIFIER {
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item ($1, $3);
|
2010-05-13 09:36:23 -07:00
|
|
|
talloc_free ($3);
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
|
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *
|
|
|
|
|
_string_list_create (void *ctx)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_list_t *list;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
list = xtalloc (ctx, string_list_t);
|
2010-05-12 12:17:10 -07:00
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_list (string_list_t *list, string_list_t *tail)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = tail->head;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = tail->head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = tail->tail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_append_item (string_list_t *list, const char *str)
|
2010-05-12 12:17:10 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-14 10:05:11 -07:00
|
|
|
node = xtalloc (list, string_node_t);
|
2010-05-12 12:45:33 -07:00
|
|
|
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-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_contains (string_list_t *list, const char *member, int *index)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0, node = list->head; node; i++, node = node->next) {
|
|
|
|
|
if (strcmp (node->str, member) == 0) {
|
2010-05-17 10:15:23 -07:00
|
|
|
if (index)
|
|
|
|
|
*index = i;
|
2010-05-13 12:56:42 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_length (string_list_t *list)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
|
|
|
|
int length = 0;
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
|
|
|
|
length++;
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:31:43 -07:00
|
|
|
void
|
|
|
|
|
_print_string_list (string_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-14 17:08:45 -07:00
|
|
|
for (node = list->head; node; node = node->next) {
|
2010-05-14 10:31:43 -07:00
|
|
|
printf ("%s", node->str);
|
2010-05-14 17:08:45 -07:00
|
|
|
if (node->next)
|
|
|
|
|
printf (" ");
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *
|
|
|
|
|
_argument_list_create (void *ctx)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = xtalloc (ctx, argument_list_t);
|
|
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_argument_list_append (argument_list_t *list, string_list_t *argument)
|
|
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (argument == NULL || argument->head == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, argument_node_t);
|
|
|
|
|
node->argument = argument;
|
|
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_argument_list_length (argument_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
int length = 0;
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
|
|
|
|
length++;
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string_list_t *
|
|
|
|
|
_argument_list_member_at (argument_list_t *list, int index)
|
|
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
2010-05-13 12:56:42 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
node = list->head;
|
|
|
|
|
for (i = 0; i < index; i++) {
|
|
|
|
|
node = node->next;
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node)
|
2010-05-14 10:44:19 -07:00
|
|
|
return node->argument;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-05-12 12:17:10 -07:00
|
|
|
|
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-19 10:01:29 -07:00
|
|
|
glcpp_lex_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-18 22:10:04 -07:00
|
|
|
parser->expansions = NULL;
|
|
|
|
|
|
|
|
|
|
parser->lex_stack = xtalloc (parser, glcpp_lex_stack_t);
|
|
|
|
|
parser->lex_stack->parser = parser;
|
|
|
|
|
parser->lex_stack->head = NULL;
|
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
|
|
|
{
|
2010-05-19 10:01:29 -07:00
|
|
|
glcpp_lex_destroy (parser->scanner);
|
2010-05-10 16:16:06 -07:00
|
|
|
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-19 07:29:22 -07:00
|
|
|
static int
|
|
|
|
|
glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
|
|
|
|
|
{
|
|
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
for (node = parser->expansions; node; node = node->next) {
|
|
|
|
|
if (node->macro &&
|
|
|
|
|
strcmp (node->macro->identifier, member) == 0)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
token_class_t
|
|
|
|
|
glcpp_parser_classify_token (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
|
|
|
|
int *parameter_index)
|
2010-05-13 07:38:29 -07:00
|
|
|
{
|
2010-05-13 09:36:23 -07:00
|
|
|
macro_t *macro;
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
/* First we check if we are currently expanding a
|
|
|
|
|
* function-like macro, and if so, whether the parameter list
|
|
|
|
|
* contains a parameter matching this token name. */
|
|
|
|
|
if (parser->expansions &&
|
|
|
|
|
parser->expansions->macro &&
|
|
|
|
|
parser->expansions->macro->parameters)
|
|
|
|
|
{
|
|
|
|
|
string_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = parser->expansions->macro->parameters;
|
|
|
|
|
|
|
|
|
|
if (_string_list_contains (list, identifier, parameter_index))
|
|
|
|
|
return TOKEN_CLASS_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If not a function-like macro parameter, we next check if
|
|
|
|
|
* this token is a macro itself. */
|
|
|
|
|
|
2010-05-13 09:36:23 -07:00
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
if (macro == NULL)
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_IDENTIFIER;
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-19 07:29:22 -07:00
|
|
|
/* Don't consider this a macro if we are already actively
|
|
|
|
|
* expanding this macro. */
|
|
|
|
|
if (glcpp_parser_is_expanding (parser, identifier))
|
|
|
|
|
return TOKEN_CLASS_IDENTIFIER;
|
|
|
|
|
|
|
|
|
|
/* Definitely a macro. Just need to check if it's function-like. */
|
2010-05-13 09:36:23 -07:00
|
|
|
if (macro->is_function)
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_FUNC_MACRO;
|
2010-05-13 09:36:23 -07:00
|
|
|
else
|
2010-05-18 22:10:04 -07:00
|
|
|
return TOKEN_CLASS_OBJ_MACRO;
|
2010-05-13 07:38:29 -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,
|
2010-05-18 22:10:04 -07:00
|
|
|
const char *replacement)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 0;
|
2010-05-14 10:12:21 -07:00
|
|
|
macro->parameters = NULL;
|
2010-05-18 22:10:04 -07:00
|
|
|
macro->identifier = talloc_strdup (macro, identifier);
|
|
|
|
|
macro->replacement = talloc_steal (macro, replacement);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
2010-05-18 22:10:04 -07:00
|
|
|
const char *replacement)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = xtalloc (parser, macro_t);
|
|
|
|
|
|
|
|
|
|
macro->is_function = 1;
|
2010-05-14 10:12:21 -07:00
|
|
|
macro->parameters = talloc_steal (macro, parameters);
|
2010-05-18 22:10:04 -07:00
|
|
|
macro->identifier = talloc_strdup (macro, identifier);
|
|
|
|
|
macro->replacement = talloc_steal (macro, replacement);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
|
|
|
|
|
macro_t *macro,
|
|
|
|
|
argument_list_t *arguments,
|
|
|
|
|
const char * replacement)
|
|
|
|
|
{
|
|
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (parser, expansion_node_t);
|
|
|
|
|
|
|
|
|
|
node->macro = macro;
|
|
|
|
|
node->arguments = arguments;
|
|
|
|
|
|
|
|
|
|
node->next = parser->expansions;
|
|
|
|
|
parser->expansions = node;
|
|
|
|
|
|
|
|
|
|
glcpp_lex_stack_push (parser->lex_stack, replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
|
|
|
|
|
macro_t *macro,
|
|
|
|
|
argument_list_t *arguments)
|
|
|
|
|
{
|
|
|
|
|
_glcpp_parser_push_expansion_internal (parser, macro, arguments,
|
|
|
|
|
macro->replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
|
|
|
|
|
int argument_index)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-18 22:10:04 -07:00
|
|
|
argument_list_t *arguments;
|
|
|
|
|
string_list_t *argument;
|
2010-05-14 10:05:11 -07:00
|
|
|
string_node_t *node;
|
2010-05-18 22:10:04 -07:00
|
|
|
char *argument_str, *s;
|
|
|
|
|
int length;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
arguments = parser->expansions->arguments;
|
2010-05-14 10:31:43 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
argument = _argument_list_member_at (arguments, argument_index);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
length = 0;
|
|
|
|
|
for (node = argument->head; node; node = node->next)
|
|
|
|
|
length += strlen (node->str) + 1;
|
|
|
|
|
|
|
|
|
|
argument_str = xtalloc_size (parser, length);
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
*argument_str = '\0';
|
|
|
|
|
s = argument_str;
|
|
|
|
|
for (node = argument->head; node; node = node->next) {
|
|
|
|
|
strcpy (s, node->str);
|
|
|
|
|
s += strlen (node->str);
|
|
|
|
|
if (node->next) {
|
|
|
|
|
*s = ' ';
|
|
|
|
|
s++;
|
|
|
|
|
*s = '\0';
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
_glcpp_parser_push_expansion_internal (parser, NULL, NULL,
|
|
|
|
|
argument_str);
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
/* The lexer calls this when it exhausts a string. */
|
|
|
|
|
void
|
|
|
|
|
glcpp_parser_pop_expansion (glcpp_parser_t *parser)
|
2010-05-13 12:56:42 -07:00
|
|
|
{
|
2010-05-18 22:10:04 -07:00
|
|
|
expansion_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = parser->expansions;
|
2010-05-17 10:15:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
if (node == NULL) {
|
|
|
|
|
fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
2010-05-17 10:15:23 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
parser->expansions = node->next;
|
2010-05-13 12:56:42 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
talloc_free (node);
|
|
|
|
|
}
|
2010-05-14 10:31:43 -07:00
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_expand_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-19 07:29:22 -07:00
|
|
|
assert (! glcpp_parser_is_expanding (parser, identifier));
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-19 07:29:22 -07:00
|
|
|
glcpp_parser_push_expansion_macro (parser, macro, NULL);
|
2010-05-13 09:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-18 22:10:04 -07:00
|
|
|
void
|
2010-05-14 10:31:43 -07:00
|
|
|
_expand_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
argument_list_t *arguments)
|
2010-05-13 09:36:23 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
assert (macro->is_function);
|
2010-05-19 07:29:22 -07:00
|
|
|
assert (! glcpp_parser_is_expanding (parser, identifier));
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-14 10:44:19 -07:00
|
|
|
if (_argument_list_length (arguments) !=
|
2010-05-14 10:31:43 -07:00
|
|
|
_string_list_length (macro->parameters))
|
|
|
|
|
{
|
2010-05-13 12:56:42 -07:00
|
|
|
fprintf (stderr,
|
|
|
|
|
"Error: macro %s invoked with %d arguments (expected %d)\n",
|
|
|
|
|
identifier,
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_length (arguments),
|
2010-05-14 10:12:21 -07:00
|
|
|
_string_list_length (macro->parameters));
|
2010-05-18 22:10:04 -07:00
|
|
|
return;
|
2010-05-13 12:56:42 -07:00
|
|
|
}
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-19 07:29:22 -07:00
|
|
|
glcpp_parser_push_expansion_macro (parser, macro, arguments);
|
2010-05-12 12:17:10 -07:00
|
|
|
}
|
2010-05-19 10:01:29 -07:00
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
yylex (yyscan_t scanner)
|
|
|
|
|
{
|
|
|
|
|
return glcpp_lex (scanner);
|
|
|
|
|
}
|