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-26 09:32:12 -07:00
|
|
|
#include <inttypes.h>
|
2010-05-10 11:44:09 -07:00
|
|
|
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_object_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-13 09:36:23 -07:00
|
|
|
_define_function_macro (glcpp_parser_t *parser,
|
|
|
|
|
const char *macro,
|
2010-05-14 10:12:21 -07:00
|
|
|
string_list_t *parameters,
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static string_list_t *
|
2010-05-14 10:05:11 -07:00
|
|
|
_string_list_create (void *ctx);
|
2010-05-12 12:17:10 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_push (string_list_t *list, const char *str);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_pop (string_list_t *list);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static 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-25 18:39:43 -07:00
|
|
|
static argument_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_create (void *ctx);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-19 13:54:37 -07:00
|
|
|
_argument_list_append (argument_list_t *list, token_list_t *argument);
|
2010-05-14 10:44:19 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static int
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_length (argument_list_t *list);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_argument_list_member_at (argument_list_t *list, int index);
|
|
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
/* Note: This function talloc_steal()s the str pointer. */
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_t *
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_create_str (void *ctx, int type, char *str);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_t *
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_create_ival (void *ctx, int type, int ival);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static token_list_t *
|
2010-05-19 13:54:37 -07:00
|
|
|
_token_list_create (void *ctx);
|
|
|
|
|
|
2010-05-26 08:10:38 -07:00
|
|
|
/* Note: This function adds a talloc_reference() to token.
|
2010-05-25 14:52:43 -07:00
|
|
|
*
|
|
|
|
|
* You may want to talloc_unlink any current reference if you no
|
|
|
|
|
* longer need it. */
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_list_append (token_list_t *list, token_t *token);
|
2010-05-19 13:54:37 -07:00
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-19 13:54:37 -07:00
|
|
|
_token_list_append_list (token_list_t *list, token_list_t *tail);
|
|
|
|
|
|
2010-05-26 11:15:21 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list);
|
|
|
|
|
|
2010-05-25 18:39:43 -07:00
|
|
|
static void
|
2010-05-25 15:24:59 -07:00
|
|
|
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list);
|
2010-05-25 14:52:43 -07:00
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list,
|
|
|
|
|
token_list_t *result);
|
|
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
|
|
|
|
|
int condition);
|
2010-05-25 14:42:00 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
|
|
|
|
|
|
2010-05-19 10:05:40 -07:00
|
|
|
#define yylex glcpp_parser_lex
|
|
|
|
|
|
2010-05-19 10:01:29 -07:00
|
|
|
static int
|
2010-05-19 10:05:40 -07:00
|
|
|
glcpp_parser_lex (glcpp_parser_t *parser);
|
2010-05-19 10:01:29 -07:00
|
|
|
|
2010-05-26 11:15:21 -07:00
|
|
|
static void
|
|
|
|
|
glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
|
|
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%}
|
|
|
|
|
|
2010-05-10 16:16:06 -07:00
|
|
|
%parse-param {glcpp_parser_t *parser}
|
2010-05-19 10:05:40 -07:00
|
|
|
%lex-param {glcpp_parser_t *parser}
|
2010-05-10 11:52:29 -07:00
|
|
|
|
2010-05-27 10:12:33 -07:00
|
|
|
%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF IDENTIFIER IF_EXPANDED INTEGER NEWLINE OTHER SPACE
|
2010-05-26 09:32:12 -07:00
|
|
|
%token PASTE
|
2010-05-26 11:15:21 -07:00
|
|
|
%type <ival> expression INTEGER operator SPACE
|
2010-05-25 17:08:07 -07:00
|
|
|
%type <str> IDENTIFIER OTHER
|
2010-05-25 16:28:26 -07:00
|
|
|
%type <string_list> identifier_list
|
2010-05-25 14:52:43 -07:00
|
|
|
%type <token> preprocessing_token
|
|
|
|
|
%type <token_list> pp_tokens replacement_list text_line
|
2010-05-26 09:32:12 -07:00
|
|
|
%left OR
|
|
|
|
|
%left AND
|
|
|
|
|
%left '|'
|
|
|
|
|
%left '^'
|
|
|
|
|
%left '&'
|
|
|
|
|
%left EQUAL NOT_EQUAL
|
|
|
|
|
%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
|
|
|
|
|
%left LEFT_SHIFT RIGHT_SHIFT
|
|
|
|
|
%left '+' '-'
|
|
|
|
|
%left '*' '/' '%'
|
|
|
|
|
%right UNARY
|
2010-05-25 13:09:03 -07:00
|
|
|
|
2010-05-10 11:44:09 -07:00
|
|
|
%%
|
|
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
input:
|
2010-05-25 13:09:03 -07:00
|
|
|
/* empty */
|
2010-05-26 11:15:21 -07:00
|
|
|
| input line
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
line:
|
|
|
|
|
control_line {
|
2010-05-26 09:32:12 -07:00
|
|
|
if (parser->skip_stack == NULL ||
|
|
|
|
|
parser->skip_stack->type == SKIP_NO_SKIP)
|
|
|
|
|
{
|
|
|
|
|
printf ("\n");
|
|
|
|
|
}
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
2010-05-25 14:52:43 -07:00
|
|
|
| text_line {
|
2010-05-26 09:32:12 -07:00
|
|
|
if (parser->skip_stack == NULL ||
|
|
|
|
|
parser->skip_stack->type == SKIP_NO_SKIP)
|
|
|
|
|
{
|
|
|
|
|
_glcpp_parser_print_expanded_token_list (parser, $1);
|
2010-05-26 11:15:21 -07:00
|
|
|
printf ("\n");
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
2010-05-25 14:52:43 -07:00
|
|
|
talloc_free ($1);
|
|
|
|
|
}
|
2010-05-26 11:15:21 -07:00
|
|
|
| expanded_line
|
2010-05-25 13:09:03 -07:00
|
|
|
| HASH non_directive
|
2010-05-12 13:11:50 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-26 11:15:21 -07:00
|
|
|
expanded_line:
|
|
|
|
|
IF_EXPANDED expression NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, $2);
|
|
|
|
|
}
|
|
|
|
|
| ELIF_EXPANDED expression NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (parser, "elif", $2);
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
control_line:
|
2010-05-25 16:59:02 -07:00
|
|
|
HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
|
2010-05-25 15:24:59 -07:00
|
|
|
_define_object_macro (parser, $2, $3);
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
|
|
|
|
|
_define_function_macro (parser, $2, NULL, $5);
|
|
|
|
|
}
|
|
|
|
|
| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
|
|
|
|
|
_define_function_macro (parser, $2, $4, $6);
|
|
|
|
|
}
|
2010-05-25 15:28:58 -07:00
|
|
|
| HASH_UNDEF IDENTIFIER NEWLINE {
|
2010-05-26 15:53:05 -07:00
|
|
|
macro_t *macro = hash_table_find (parser->defines, $2);
|
2010-05-25 15:28:58 -07:00
|
|
|
if (macro) {
|
|
|
|
|
/* 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 (macro);
|
|
|
|
|
}
|
|
|
|
|
talloc_free ($2);
|
|
|
|
|
}
|
2010-05-26 11:15:21 -07:00
|
|
|
| HASH_IF pp_tokens NEWLINE {
|
|
|
|
|
token_list_t *expanded;
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
expanded = _token_list_create (parser);
|
|
|
|
|
token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
|
|
|
|
|
_token_list_append (expanded, token);
|
|
|
|
|
talloc_unlink (parser, token);
|
|
|
|
|
_glcpp_parser_evaluate_defined (parser, $2);
|
|
|
|
|
_glcpp_parser_expand_token_list_onto (parser, $2, expanded);
|
|
|
|
|
glcpp_parser_lex_from (parser, expanded);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
| HASH_IFDEF IDENTIFIER NEWLINE {
|
2010-05-26 15:53:05 -07:00
|
|
|
macro_t *macro = hash_table_find (parser->defines, $2);
|
2010-05-26 09:32:12 -07:00
|
|
|
talloc_free ($2);
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, macro != NULL);
|
|
|
|
|
}
|
|
|
|
|
| HASH_IFNDEF IDENTIFIER NEWLINE {
|
2010-05-26 15:53:05 -07:00
|
|
|
macro_t *macro = hash_table_find (parser->defines, $2);
|
2010-05-26 09:32:12 -07:00
|
|
|
talloc_free ($2);
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (parser, macro == NULL);
|
|
|
|
|
}
|
2010-05-26 11:15:21 -07:00
|
|
|
| HASH_ELIF pp_tokens NEWLINE {
|
|
|
|
|
token_list_t *expanded;
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
expanded = _token_list_create (parser);
|
|
|
|
|
token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
|
|
|
|
|
_token_list_append (expanded, token);
|
|
|
|
|
talloc_unlink (parser, token);
|
|
|
|
|
_glcpp_parser_evaluate_defined (parser, $2);
|
|
|
|
|
_glcpp_parser_expand_token_list_onto (parser, $2, expanded);
|
|
|
|
|
glcpp_parser_lex_from (parser, expanded);
|
2010-05-26 09:32:12 -07:00
|
|
|
}
|
|
|
|
|
| HASH_ELSE NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (parser, "else", 1);
|
|
|
|
|
}
|
|
|
|
|
| HASH_ENDIF NEWLINE {
|
|
|
|
|
_glcpp_parser_skip_stack_pop (parser);
|
|
|
|
|
}
|
2010-05-25 13:09:03 -07:00
|
|
|
| HASH NEWLINE
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-26 09:32:12 -07:00
|
|
|
expression:
|
|
|
|
|
INTEGER {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
}
|
|
|
|
|
| expression OR expression {
|
|
|
|
|
$$ = $1 || $3;
|
|
|
|
|
}
|
|
|
|
|
| expression AND expression {
|
|
|
|
|
$$ = $1 && $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '|' expression {
|
|
|
|
|
$$ = $1 | $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '^' expression {
|
|
|
|
|
$$ = $1 ^ $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '&' expression {
|
|
|
|
|
$$ = $1 & $3;
|
|
|
|
|
}
|
|
|
|
|
| expression NOT_EQUAL expression {
|
|
|
|
|
$$ = $1 != $3;
|
|
|
|
|
}
|
|
|
|
|
| expression EQUAL expression {
|
|
|
|
|
$$ = $1 == $3;
|
|
|
|
|
}
|
|
|
|
|
| expression GREATER_OR_EQUAL expression {
|
|
|
|
|
$$ = $1 >= $3;
|
|
|
|
|
}
|
|
|
|
|
| expression LESS_OR_EQUAL expression {
|
|
|
|
|
$$ = $1 <= $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '>' expression {
|
|
|
|
|
$$ = $1 > $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '<' expression {
|
|
|
|
|
$$ = $1 < $3;
|
|
|
|
|
}
|
|
|
|
|
| expression RIGHT_SHIFT expression {
|
|
|
|
|
$$ = $1 >> $3;
|
|
|
|
|
}
|
|
|
|
|
| expression LEFT_SHIFT expression {
|
|
|
|
|
$$ = $1 << $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '-' expression {
|
|
|
|
|
$$ = $1 - $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '+' expression {
|
|
|
|
|
$$ = $1 + $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '%' expression {
|
|
|
|
|
$$ = $1 % $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '/' expression {
|
|
|
|
|
$$ = $1 / $3;
|
|
|
|
|
}
|
|
|
|
|
| expression '*' expression {
|
|
|
|
|
$$ = $1 * $3;
|
|
|
|
|
}
|
|
|
|
|
| '!' expression %prec UNARY {
|
|
|
|
|
$$ = ! $2;
|
|
|
|
|
}
|
|
|
|
|
| '~' expression %prec UNARY {
|
|
|
|
|
$$ = ~ $2;
|
|
|
|
|
}
|
|
|
|
|
| '-' expression %prec UNARY {
|
|
|
|
|
$$ = - $2;
|
|
|
|
|
}
|
|
|
|
|
| '+' expression %prec UNARY {
|
|
|
|
|
$$ = + $2;
|
|
|
|
|
}
|
|
|
|
|
| '(' expression ')' {
|
|
|
|
|
$$ = $2;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
identifier_list:
|
2010-05-25 16:28:26 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
$$ = _string_list_create (parser);
|
|
|
|
|
_string_list_append_item ($$, $1);
|
|
|
|
|
talloc_steal ($$, $1);
|
|
|
|
|
}
|
|
|
|
|
| identifier_list ',' IDENTIFIER {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
_string_list_append_item ($$, $3);
|
|
|
|
|
talloc_steal ($$, $3);
|
|
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
text_line:
|
2010-05-25 14:52:43 -07:00
|
|
|
NEWLINE { $$ = NULL; }
|
2010-05-25 13:09:03 -07:00
|
|
|
| pp_tokens NEWLINE
|
2010-05-19 07:49:47 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
non_directive:
|
|
|
|
|
pp_tokens NEWLINE
|
2010-05-20 08:42:02 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
replacement_list:
|
2010-05-25 14:52:43 -07:00
|
|
|
/* empty */ { $$ = NULL; }
|
2010-05-25 13:09:03 -07:00
|
|
|
| pp_tokens
|
2010-05-10 11:44:09 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
pp_tokens:
|
2010-05-25 14:52:43 -07:00
|
|
|
preprocessing_token {
|
2010-05-25 16:59:02 -07:00
|
|
|
parser->space_tokens = 1;
|
2010-05-25 14:52:43 -07:00
|
|
|
$$ = _token_list_create (parser);
|
|
|
|
|
_token_list_append ($$, $1);
|
|
|
|
|
talloc_unlink (parser, $1);
|
|
|
|
|
}
|
|
|
|
|
| pp_tokens preprocessing_token {
|
|
|
|
|
$$ = $1;
|
|
|
|
|
_token_list_append ($$, $2);
|
|
|
|
|
talloc_unlink (parser, $2);
|
|
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-25 13:09:03 -07:00
|
|
|
preprocessing_token:
|
2010-05-25 14:52:43 -07:00
|
|
|
IDENTIFIER {
|
|
|
|
|
$$ = _token_create_str (parser, IDENTIFIER, $1);
|
|
|
|
|
}
|
2010-05-26 09:32:12 -07:00
|
|
|
| INTEGER {
|
|
|
|
|
$$ = _token_create_ival (parser, INTEGER, $1);
|
|
|
|
|
}
|
2010-05-26 11:15:21 -07:00
|
|
|
| operator {
|
2010-05-25 14:52:43 -07:00
|
|
|
$$ = _token_create_ival (parser, $1, $1);
|
|
|
|
|
}
|
|
|
|
|
| OTHER {
|
|
|
|
|
$$ = _token_create_str (parser, OTHER, $1);
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
| SPACE {
|
2010-05-25 17:08:07 -07:00
|
|
|
$$ = _token_create_ival (parser, SPACE, SPACE);
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-13 09:36:23 -07:00
|
|
|
;
|
|
|
|
|
|
2010-05-26 11:15:21 -07:00
|
|
|
operator:
|
2010-05-25 14:52:43 -07:00
|
|
|
'[' { $$ = '['; }
|
|
|
|
|
| ']' { $$ = ']'; }
|
|
|
|
|
| '(' { $$ = '('; }
|
|
|
|
|
| ')' { $$ = ')'; }
|
|
|
|
|
| '{' { $$ = '{'; }
|
|
|
|
|
| '}' { $$ = '}'; }
|
|
|
|
|
| '.' { $$ = '.'; }
|
|
|
|
|
| '&' { $$ = '&'; }
|
|
|
|
|
| '*' { $$ = '*'; }
|
|
|
|
|
| '+' { $$ = '+'; }
|
|
|
|
|
| '-' { $$ = '-'; }
|
|
|
|
|
| '~' { $$ = '~'; }
|
|
|
|
|
| '!' { $$ = '!'; }
|
|
|
|
|
| '/' { $$ = '/'; }
|
|
|
|
|
| '%' { $$ = '%'; }
|
|
|
|
|
| LEFT_SHIFT { $$ = LEFT_SHIFT; }
|
|
|
|
|
| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
|
|
|
|
|
| '<' { $$ = '<'; }
|
|
|
|
|
| '>' { $$ = '>'; }
|
|
|
|
|
| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
|
|
|
|
|
| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
|
|
|
|
|
| EQUAL { $$ = EQUAL; }
|
|
|
|
|
| NOT_EQUAL { $$ = NOT_EQUAL; }
|
|
|
|
|
| '^' { $$ = '^'; }
|
|
|
|
|
| '|' { $$ = '|'; }
|
|
|
|
|
| AND { $$ = AND; }
|
|
|
|
|
| OR { $$ = OR; }
|
|
|
|
|
| ';' { $$ = ';'; }
|
|
|
|
|
| ',' { $$ = ','; }
|
|
|
|
|
| PASTE { $$ = PASTE; }
|
2010-05-26 11:15:21 -07:00
|
|
|
| DEFINED { $$ = DEFINED; }
|
2010-05-19 13:28:24 -07:00
|
|
|
;
|
|
|
|
|
|
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-25 14:42:00 -07:00
|
|
|
|
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
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
void
|
|
|
|
|
_string_list_push (string_list_t *list, const char *str)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, string_node_t);
|
|
|
|
|
node->str = xtalloc_strdup (node, str);
|
|
|
|
|
node->next = list->head;
|
|
|
|
|
|
|
|
|
|
if (list->tail == NULL) {
|
|
|
|
|
list->tail = node;
|
|
|
|
|
}
|
|
|
|
|
list->head = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_string_list_pop (string_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
string_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = list->head;
|
|
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->head = node->next;
|
|
|
|
|
if (list->tail == node) {
|
|
|
|
|
assert (node->next == NULL);
|
|
|
|
|
list->tail = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
talloc_free (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: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
|
2010-05-19 13:54:37 -07:00
|
|
|
_argument_list_append (argument_list_t *list, token_list_t *argument)
|
2010-05-14 10:44:19 -07:00
|
|
|
{
|
|
|
|
|
argument_node_t *node;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *
|
2010-05-14 10:44:19 -07:00
|
|
|
_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-19 13:54:37 -07:00
|
|
|
|
2010-05-25 14:52:43 -07:00
|
|
|
/* Note: This function talloc_steal()s the str pointer. */
|
|
|
|
|
token_t *
|
|
|
|
|
_token_create_str (void *ctx, int type, char *str)
|
|
|
|
|
{
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
token = xtalloc (ctx, token_t);
|
|
|
|
|
token->type = type;
|
|
|
|
|
token->value.str = talloc_steal (token, str);
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token_t *
|
|
|
|
|
_token_create_ival (void *ctx, int type, int ival)
|
|
|
|
|
{
|
|
|
|
|
token_t *token;
|
|
|
|
|
|
|
|
|
|
token = xtalloc (ctx, token_t);
|
|
|
|
|
token->type = type;
|
|
|
|
|
token->value.ival = ival;
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 13:54:37 -07:00
|
|
|
token_list_t *
|
|
|
|
|
_token_list_create (void *ctx)
|
|
|
|
|
{
|
|
|
|
|
token_list_t *list;
|
|
|
|
|
|
|
|
|
|
list = xtalloc (ctx, token_list_t);
|
|
|
|
|
list->head = NULL;
|
|
|
|
|
list->tail = NULL;
|
2010-05-25 20:35:01 -07:00
|
|
|
list->non_space_tail = NULL;
|
2010-05-19 13:54:37 -07:00
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2010-05-25 14:52:43 -07:00
|
|
|
_token_list_append (token_list_t *list, token_t *token)
|
2010-05-19 13:54:37 -07:00
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (list, token_node_t);
|
2010-05-25 14:52:43 -07:00
|
|
|
node->token = xtalloc_reference (list, token);
|
2010-05-19 13:54:37 -07:00
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = node;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = node;
|
2010-05-25 20:35:01 -07:00
|
|
|
if (token->type != SPACE)
|
|
|
|
|
list->non_space_tail = node;
|
2010-05-19 13:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_append_list (token_list_t *list, token_list_t *tail)
|
|
|
|
|
{
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
|
list->head = tail->head;
|
|
|
|
|
} else {
|
|
|
|
|
list->tail->next = tail->head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list->tail = tail->tail;
|
2010-05-25 20:35:01 -07:00
|
|
|
list->non_space_tail = tail->non_space_tail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_token_list_trim_trailing_space (token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *tail, *next;
|
|
|
|
|
|
|
|
|
|
if (list->non_space_tail) {
|
|
|
|
|
tail = list->non_space_tail->next;
|
|
|
|
|
list->non_space_tail->next = NULL;
|
|
|
|
|
list->tail = list->non_space_tail;
|
|
|
|
|
|
|
|
|
|
while (tail) {
|
|
|
|
|
next = tail->next;
|
|
|
|
|
talloc_free (tail);
|
|
|
|
|
tail = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-19 13:54:37 -07:00
|
|
|
}
|
2010-05-25 14:42:00 -07:00
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
static void
|
|
|
|
|
_token_print (token_t *token)
|
|
|
|
|
{
|
|
|
|
|
if (token->type < 256) {
|
|
|
|
|
printf ("%c", token->type);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (token->type) {
|
2010-05-26 09:32:12 -07:00
|
|
|
case INTEGER:
|
|
|
|
|
printf ("%" PRIxMAX, token->value.ival);
|
|
|
|
|
break;
|
2010-05-26 08:05:19 -07:00
|
|
|
case IDENTIFIER:
|
|
|
|
|
case OTHER:
|
|
|
|
|
printf ("%s", token->value.str);
|
|
|
|
|
break;
|
|
|
|
|
case SPACE:
|
|
|
|
|
printf (" ");
|
|
|
|
|
break;
|
|
|
|
|
case LEFT_SHIFT:
|
|
|
|
|
printf ("<<");
|
|
|
|
|
break;
|
|
|
|
|
case RIGHT_SHIFT:
|
|
|
|
|
printf (">>");
|
|
|
|
|
break;
|
|
|
|
|
case LESS_OR_EQUAL:
|
|
|
|
|
printf ("<=");
|
|
|
|
|
break;
|
|
|
|
|
case GREATER_OR_EQUAL:
|
|
|
|
|
printf (">=");
|
|
|
|
|
break;
|
|
|
|
|
case EQUAL:
|
|
|
|
|
printf ("==");
|
|
|
|
|
break;
|
|
|
|
|
case NOT_EQUAL:
|
|
|
|
|
printf ("!=");
|
|
|
|
|
break;
|
|
|
|
|
case AND:
|
|
|
|
|
printf ("&&");
|
|
|
|
|
break;
|
|
|
|
|
case OR:
|
|
|
|
|
printf ("||");
|
|
|
|
|
break;
|
|
|
|
|
case PASTE:
|
|
|
|
|
printf ("##");
|
|
|
|
|
break;
|
2010-05-27 10:12:33 -07:00
|
|
|
case COMMA_FINAL:
|
|
|
|
|
printf (",");
|
|
|
|
|
break;
|
2010-05-26 08:05:19 -07:00
|
|
|
default:
|
|
|
|
|
fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:04:50 -07:00
|
|
|
/* Change 'token' into a new token formed by pasting 'other'. */
|
|
|
|
|
static void
|
|
|
|
|
_token_paste (token_t *token, token_t *other)
|
|
|
|
|
{
|
|
|
|
|
/* A very few single-character punctuators can be combined
|
|
|
|
|
* with another to form a multi-character punctuator. */
|
|
|
|
|
switch (token->type) {
|
|
|
|
|
case '<':
|
|
|
|
|
if (other->type == '<') {
|
|
|
|
|
token->type = LEFT_SHIFT;
|
|
|
|
|
token->value.ival = LEFT_SHIFT;
|
|
|
|
|
return;
|
|
|
|
|
} else if (other->type == '=') {
|
|
|
|
|
token->type = LESS_OR_EQUAL;
|
|
|
|
|
token->value.ival = LESS_OR_EQUAL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
if (other->type == '>') {
|
|
|
|
|
token->type = RIGHT_SHIFT;
|
|
|
|
|
token->value.ival = RIGHT_SHIFT;
|
|
|
|
|
return;
|
|
|
|
|
} else if (other->type == '=') {
|
|
|
|
|
token->type = GREATER_OR_EQUAL;
|
|
|
|
|
token->value.ival = GREATER_OR_EQUAL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '=':
|
|
|
|
|
if (other->type == '=') {
|
|
|
|
|
token->type = EQUAL;
|
|
|
|
|
token->value.ival = EQUAL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '!':
|
|
|
|
|
if (other->type == '=') {
|
|
|
|
|
token->type = NOT_EQUAL;
|
|
|
|
|
token->value.ival = NOT_EQUAL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '&':
|
|
|
|
|
if (other->type == '&') {
|
|
|
|
|
token->type = AND;
|
|
|
|
|
token->value.ival = AND;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '|':
|
|
|
|
|
if (other->type == '|') {
|
|
|
|
|
token->type = OR;
|
|
|
|
|
token->value.ival = OR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Two string-valued tokens can usually just be mashed
|
|
|
|
|
* together.
|
|
|
|
|
*
|
|
|
|
|
* XXX: Since our 'OTHER' case is currently so loose, this may
|
|
|
|
|
* allow some things thruogh that should be treated as
|
|
|
|
|
* errors. */
|
|
|
|
|
if ((token->type == IDENTIFIER || token->type == OTHER) &&
|
|
|
|
|
(other->type == IDENTIFIER || other->type == OTHER))
|
|
|
|
|
{
|
|
|
|
|
token->value.str = talloc_strdup_append (token->value.str,
|
|
|
|
|
other->value.str);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf ("Error: Pasting \"");
|
|
|
|
|
_token_print (token);
|
|
|
|
|
printf ("\" and \"");
|
|
|
|
|
_token_print (other);
|
|
|
|
|
printf ("\" does not give a valid preprocessing token.\n");
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
static void
|
|
|
|
|
_token_list_print (token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
|
|
|
|
_token_print (node->token);
|
|
|
|
|
}
|
|
|
|
|
|
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-25 15:24:59 -07:00
|
|
|
parser->active = _string_list_create (parser);
|
2010-05-25 16:59:02 -07:00
|
|
|
parser->space_tokens = 1;
|
Treat newlines as space when invoking a function-like macro invocation.
This adds three new pieces of state to the parser, (is_control_line,
newline_as_space, and paren_count), and a large amount of messy
code. I'd definitely like to see a cleaner solution for this.
With this fix, the "define-func-extra-newlines" now passes so we put
it back to test #26 where it was originally (lately it has been known
as test #55).
Also, we tweak test 25 slightly. Previously this test was ending a
file function-like macro name that was not actually a macro (not
followed by a left parenthesis). As is, this fix was making that test
fail because the text_line production expects to see a terminating
NEWLINE, but that NEWLINE is now getting turned into a SPACE here.
This seems unlikely to be a problem in the wild, (function macros
being used in a non-macro sense seems rare enough---but more than
likely they won't happen at the end of a file). Still, we document
this shortcoming in the README.
2010-05-26 15:57:10 -07:00
|
|
|
parser->newline_as_space = 0;
|
|
|
|
|
parser->in_control_line = 0;
|
|
|
|
|
parser->paren_count = 0;
|
2010-05-20 14:29:43 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
parser->skip_stack = NULL;
|
|
|
|
|
|
2010-05-26 11:15:21 -07:00
|
|
|
parser->lex_from_list = NULL;
|
|
|
|
|
parser->lex_from_node = 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-20 22:27:07 -07:00
|
|
|
if (parser->skip_stack)
|
|
|
|
|
fprintf (stderr, "Error: Unterminated #if\n");
|
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-26 11:15:21 -07:00
|
|
|
/* Replace any occurences of DEFINED tokens in 'list' with either a
|
|
|
|
|
* '0' or '1' INTEGER token depending on whether the next token in the
|
|
|
|
|
* list is defined or not. */
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node, *next;
|
2010-05-26 15:53:05 -07:00
|
|
|
macro_t *macro;
|
2010-05-26 11:15:21 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next) {
|
|
|
|
|
if (node->token->type != DEFINED)
|
|
|
|
|
continue;
|
|
|
|
|
next = node->next;
|
|
|
|
|
while (next && next->token->type == SPACE)
|
|
|
|
|
next = next->next;
|
|
|
|
|
if (next == NULL || next->token->type != IDENTIFIER) {
|
|
|
|
|
fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
macro = hash_table_find (parser->defines,
|
|
|
|
|
next->token->value.str);
|
|
|
|
|
|
|
|
|
|
node->token->type = INTEGER;
|
|
|
|
|
node->token->value.ival = (macro != NULL);
|
|
|
|
|
node->next = next->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
/* Appends onto 'expansion' a non-macro token or the expansion of an
|
|
|
|
|
* object-like macro.
|
2010-05-25 16:28:26 -07:00
|
|
|
*
|
2010-05-26 08:05:19 -07:00
|
|
|
* Returns 0 if this token is completely processed.
|
2010-05-25 16:28:26 -07:00
|
|
|
*
|
|
|
|
|
* Returns 1 in the case that 'token' is a function-like macro that
|
|
|
|
|
* needs further expansion.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2010-05-26 17:01:57 -07:00
|
|
|
_expand_token_onto (glcpp_parser_t *parser,
|
|
|
|
|
token_t *token,
|
|
|
|
|
token_list_t *result)
|
2010-05-25 15:24:59 -07:00
|
|
|
{
|
|
|
|
|
const char *identifier;
|
|
|
|
|
macro_t *macro;
|
2010-05-26 08:05:19 -07:00
|
|
|
token_list_t *expansion;
|
2010-05-25 15:24:59 -07:00
|
|
|
|
|
|
|
|
/* We only expand identifiers */
|
|
|
|
|
if (token->type != IDENTIFIER) {
|
2010-05-27 10:12:33 -07:00
|
|
|
/* We change any COMMA into a COMMA_FINAL to prevent
|
|
|
|
|
* it being mistaken for an argument separator
|
|
|
|
|
* later. */
|
|
|
|
|
if (token->type == ',') {
|
|
|
|
|
token_t *new_token;
|
|
|
|
|
|
|
|
|
|
new_token = _token_create_ival (result, COMMA_FINAL,
|
|
|
|
|
COMMA_FINAL);
|
|
|
|
|
_token_list_append (result, new_token);
|
|
|
|
|
} else {
|
|
|
|
|
_token_list_append (result, token);
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
return 0;
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Look up this identifier in the hash table. */
|
|
|
|
|
identifier = token->value.str;
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
/* Not a macro, so just append. */
|
2010-05-25 15:24:59 -07:00
|
|
|
if (macro == NULL) {
|
2010-05-26 08:05:19 -07:00
|
|
|
_token_list_append (result, token);
|
2010-05-25 16:28:26 -07:00
|
|
|
return 0;
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finally, don't expand this macro if we're already actively
|
|
|
|
|
* expanding it, (to avoid infinite recursion). */
|
2010-05-26 08:15:49 -07:00
|
|
|
if (_string_list_contains (parser->active, identifier, NULL))
|
|
|
|
|
{
|
|
|
|
|
/* We change the token type here from IDENTIFIER to
|
|
|
|
|
* OTHER to prevent any future expansion of this
|
|
|
|
|
* unexpanded token. */
|
|
|
|
|
char *str;
|
|
|
|
|
token_t *new_token;
|
|
|
|
|
|
|
|
|
|
str = xtalloc_strdup (result, token->value.str);
|
|
|
|
|
new_token = _token_create_str (result, OTHER, str);
|
|
|
|
|
_token_list_append (result, new_token);
|
2010-05-25 16:28:26 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 08:01:42 -07:00
|
|
|
/* For function-like macros return 1 for further processing. */
|
|
|
|
|
if (macro->is_function) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 16:28:26 -07:00
|
|
|
_string_list_push (parser->active, identifier);
|
2010-05-26 08:05:19 -07:00
|
|
|
_glcpp_parser_expand_token_list_onto (parser,
|
|
|
|
|
macro->replacements,
|
|
|
|
|
result);
|
2010-05-25 16:28:26 -07:00
|
|
|
_string_list_pop (parser->active);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef enum function_status
|
|
|
|
|
{
|
|
|
|
|
FUNCTION_STATUS_SUCCESS,
|
|
|
|
|
FUNCTION_NOT_A_FUNCTION,
|
|
|
|
|
FUNCTION_UNBALANCED_PARENTHESES
|
|
|
|
|
} function_status_t;
|
|
|
|
|
|
|
|
|
|
/* Find a set of function-like macro arguments by looking for a
|
|
|
|
|
* balanced set of parentheses. Upon return *node will be the last
|
|
|
|
|
* consumed node, such that further processing can continue with
|
|
|
|
|
* node->next.
|
|
|
|
|
*
|
|
|
|
|
* Return values:
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_STATUS_SUCCESS:
|
|
|
|
|
*
|
|
|
|
|
* Successfully parsed a set of function arguments.
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_NOT_A_FUNCTION:
|
|
|
|
|
*
|
|
|
|
|
* Macro name not followed by a '('. This is not an error, but
|
|
|
|
|
* simply that the macro name should be treated as a non-macro.
|
|
|
|
|
*
|
|
|
|
|
* FUNCTION_UNBLANCED_PARENTHESES
|
|
|
|
|
*
|
|
|
|
|
* Macro name is not followed by a balanced set of parentheses.
|
|
|
|
|
*/
|
|
|
|
|
static function_status_t
|
2010-05-25 17:32:21 -07:00
|
|
|
_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
|
2010-05-25 16:28:26 -07:00
|
|
|
{
|
2010-05-25 17:32:21 -07:00
|
|
|
token_list_t *argument;
|
2010-05-25 16:28:26 -07:00
|
|
|
token_node_t *node = *node_ret, *last;
|
|
|
|
|
int paren_count;
|
|
|
|
|
|
|
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
|
|
/* Ignore whitespace before first parenthesis. */
|
|
|
|
|
while (node && node->token->type == SPACE)
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
|
|
|
|
if (node == NULL || node->token->type != '(')
|
|
|
|
|
return FUNCTION_NOT_A_FUNCTION;
|
|
|
|
|
|
2010-05-25 17:45:22 -07:00
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = NULL;
|
|
|
|
|
|
2010-05-25 17:45:22 -07:00
|
|
|
for (paren_count = 1; node; last = node, node = node->next) {
|
2010-05-25 16:28:26 -07:00
|
|
|
if (node->token->type == '(')
|
|
|
|
|
{
|
|
|
|
|
paren_count++;
|
|
|
|
|
}
|
|
|
|
|
else if (node->token->type == ')')
|
|
|
|
|
{
|
|
|
|
|
paren_count--;
|
2010-05-25 17:41:07 -07:00
|
|
|
if (paren_count == 0) {
|
|
|
|
|
last = node;
|
|
|
|
|
node = node->next;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-25 17:45:22 -07:00
|
|
|
|
|
|
|
|
if (node->token->type == ',' &&
|
2010-05-25 16:28:26 -07:00
|
|
|
paren_count == 1)
|
|
|
|
|
{
|
2010-05-25 20:35:01 -07:00
|
|
|
if (argument)
|
|
|
|
|
_token_list_trim_trailing_space (argument);
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = NULL;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (argument == NULL) {
|
2010-05-25 17:41:07 -07:00
|
|
|
/* Don't treat initial whitespace as
|
|
|
|
|
* part of the arguement. */
|
|
|
|
|
if (node->token->type == SPACE)
|
|
|
|
|
continue;
|
2010-05-25 17:32:21 -07:00
|
|
|
argument = _token_list_create (arguments);
|
|
|
|
|
_argument_list_append (arguments, argument);
|
|
|
|
|
}
|
|
|
|
|
_token_list_append (argument, node->token);
|
2010-05-25 16:28:26 -07:00
|
|
|
}
|
2010-05-25 17:41:07 -07:00
|
|
|
}
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
if (node && paren_count)
|
|
|
|
|
return FUNCTION_UNBALANCED_PARENTHESES;
|
|
|
|
|
|
|
|
|
|
*node_ret = last;
|
|
|
|
|
|
|
|
|
|
return FUNCTION_STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prints the expansion of *node (consuming further tokens from the
|
|
|
|
|
* list as necessary). Upon return *node will be the last consumed
|
|
|
|
|
* node, such that further processing can continue with node->next. */
|
2010-05-26 17:01:57 -07:00
|
|
|
static function_status_t
|
|
|
|
|
_expand_function_onto (glcpp_parser_t *parser,
|
|
|
|
|
token_node_t **node_ret,
|
|
|
|
|
token_list_t *result)
|
2010-05-25 16:28:26 -07:00
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
const char *identifier;
|
|
|
|
|
argument_list_t *arguments;
|
|
|
|
|
function_status_t status;
|
2010-05-26 08:05:19 -07:00
|
|
|
token_list_t *substituted;
|
2010-05-25 17:32:21 -07:00
|
|
|
int parameter_index;
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
node = *node_ret;
|
|
|
|
|
identifier = node->token->value.str;
|
|
|
|
|
|
|
|
|
|
macro = hash_table_find (parser->defines, identifier);
|
|
|
|
|
|
|
|
|
|
assert (macro->is_function);
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
arguments = _argument_list_create (parser);
|
|
|
|
|
status = _arguments_parse (arguments, node_ret);
|
2010-05-25 16:28:26 -07:00
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
case FUNCTION_STATUS_SUCCESS:
|
|
|
|
|
break;
|
|
|
|
|
case FUNCTION_NOT_A_FUNCTION:
|
2010-05-26 08:05:19 -07:00
|
|
|
_token_list_append (result, node->token);
|
2010-05-26 17:01:57 -07:00
|
|
|
return FUNCTION_NOT_A_FUNCTION;
|
2010-05-25 16:28:26 -07:00
|
|
|
case FUNCTION_UNBALANCED_PARENTHESES:
|
|
|
|
|
fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
|
|
|
|
|
identifier);
|
|
|
|
|
exit (1);
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 17:32:21 -07:00
|
|
|
if (macro->replacements == NULL) {
|
|
|
|
|
talloc_free (arguments);
|
2010-05-26 17:01:57 -07:00
|
|
|
return FUNCTION_STATUS_SUCCESS;
|
2010-05-25 17:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_argument_list_length (arguments) !=
|
|
|
|
|
_string_list_length (macro->parameters))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr,
|
|
|
|
|
"Error: macro %s invoked with %d arguments (expected %d)\n",
|
|
|
|
|
identifier,
|
|
|
|
|
_argument_list_length (arguments),
|
|
|
|
|
_string_list_length (macro->parameters));
|
2010-05-26 17:01:57 -07:00
|
|
|
exit (1);
|
2010-05-25 17:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
/* Perform argument substitution on the replacement list. */
|
|
|
|
|
substituted = _token_list_create (arguments);
|
2010-05-25 17:32:21 -07:00
|
|
|
|
2010-05-26 08:25:44 -07:00
|
|
|
for (node = macro->replacements->head; node; node = node->next)
|
|
|
|
|
{
|
|
|
|
|
if (node->token->type == IDENTIFIER &&
|
2010-05-25 17:32:21 -07:00
|
|
|
_string_list_contains (macro->parameters,
|
2010-05-26 08:25:44 -07:00
|
|
|
node->token->value.str,
|
2010-05-25 17:32:21 -07:00
|
|
|
¶meter_index))
|
|
|
|
|
{
|
|
|
|
|
token_list_t *argument;
|
|
|
|
|
argument = _argument_list_member_at (arguments,
|
|
|
|
|
parameter_index);
|
2010-05-26 08:09:29 -07:00
|
|
|
/* Before substituting, we expand the argument
|
|
|
|
|
* tokens. */
|
|
|
|
|
_glcpp_parser_expand_token_list_onto (parser,
|
|
|
|
|
argument,
|
|
|
|
|
substituted);
|
2010-05-25 17:32:21 -07:00
|
|
|
} else {
|
2010-05-26 08:25:44 -07:00
|
|
|
_token_list_append (substituted, node->token);
|
2010-05-25 17:32:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 09:04:50 -07:00
|
|
|
/* After argument substitution, and before further expansion
|
|
|
|
|
* below, implement token pasting. */
|
|
|
|
|
|
|
|
|
|
node = substituted->head;
|
|
|
|
|
while (node)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *next_non_space;
|
|
|
|
|
|
|
|
|
|
/* Look ahead for a PASTE token, skipping space. */
|
|
|
|
|
next_non_space = node->next;
|
|
|
|
|
while (next_non_space && next_non_space->token->type == SPACE)
|
|
|
|
|
next_non_space = next_non_space->next;
|
|
|
|
|
|
|
|
|
|
if (next_non_space == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (next_non_space->token->type != PASTE) {
|
|
|
|
|
node = next_non_space;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now find the next non-space token after the PASTE. */
|
|
|
|
|
next_non_space = next_non_space->next;
|
|
|
|
|
while (next_non_space && next_non_space->token->type == SPACE)
|
|
|
|
|
next_non_space = next_non_space->next;
|
|
|
|
|
|
|
|
|
|
if (next_non_space == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_token_paste (node->token, next_non_space->token);
|
|
|
|
|
node->next = next_non_space->next;
|
|
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_push (parser->active, identifier);
|
2010-05-26 08:05:19 -07:00
|
|
|
_glcpp_parser_expand_token_list_onto (parser, substituted, result);
|
2010-05-25 15:24:59 -07:00
|
|
|
_string_list_pop (parser->active);
|
2010-05-25 17:32:21 -07:00
|
|
|
|
|
|
|
|
talloc_free (arguments);
|
2010-05-26 17:01:57 -07:00
|
|
|
|
|
|
|
|
return FUNCTION_STATUS_SUCCESS;
|
2010-05-25 15:24:59 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list,
|
|
|
|
|
token_list_t *result)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
2010-05-26 17:01:57 -07:00
|
|
|
token_list_t *intermediate, *list_orig = list;
|
|
|
|
|
int i, need_rescan = 0;
|
2010-05-26 08:05:19 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-26 17:01:57 -07:00
|
|
|
intermediate = _token_list_create (parser);
|
|
|
|
|
|
|
|
|
|
/* XXX: The two-pass expansion here is really ugly. The
|
|
|
|
|
* problem this is solving is that we can expand a macro into
|
|
|
|
|
* a function-like macro name, and then we need to recognize
|
|
|
|
|
* that as a function-like macro, but perhaps the parentheses
|
|
|
|
|
* and arguments aren't on the token list yet, (since they are
|
|
|
|
|
* in the actual content so they are part of what we are
|
|
|
|
|
* expanding.
|
|
|
|
|
*
|
|
|
|
|
* This ugly hack works, but is messy, fragile, and hard to
|
|
|
|
|
* maintain. I think a cleaner solution would separate the
|
|
|
|
|
* notions of expanding and appending and avoid this problem
|
|
|
|
|
* altogether.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
|
if (i == 1) {
|
|
|
|
|
list = intermediate;
|
|
|
|
|
intermediate = _token_list_create (parser);
|
|
|
|
|
}
|
|
|
|
|
for (node = list->head; node; node = node->next)
|
2010-05-26 08:05:19 -07:00
|
|
|
{
|
2010-05-26 17:01:57 -07:00
|
|
|
if (_expand_token_onto (parser, node->token,
|
|
|
|
|
intermediate))
|
|
|
|
|
{
|
|
|
|
|
if (_expand_function_onto (parser, &node,
|
|
|
|
|
intermediate))
|
|
|
|
|
{
|
|
|
|
|
need_rescan = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-26 08:05:19 -07:00
|
|
|
}
|
2010-05-26 17:01:57 -07:00
|
|
|
if (list != list_orig)
|
|
|
|
|
talloc_free (list);
|
2010-05-26 08:05:19 -07:00
|
|
|
}
|
2010-05-26 17:01:57 -07:00
|
|
|
|
|
|
|
|
_token_list_append_list (result, intermediate);
|
2010-05-26 08:05:19 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-25 15:24:59 -07:00
|
|
|
void
|
|
|
|
|
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
|
|
|
|
|
token_list_t *list)
|
|
|
|
|
{
|
2010-05-26 08:05:19 -07:00
|
|
|
token_list_t *expanded;
|
2010-05-25 15:24:59 -07:00
|
|
|
token_node_t *node;
|
2010-05-25 16:28:26 -07:00
|
|
|
function_status_t function_status;
|
2010-05-25 15:24:59 -07:00
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
expanded = _token_list_create (parser);
|
2010-05-25 20:35:01 -07:00
|
|
|
|
2010-05-26 08:05:19 -07:00
|
|
|
_glcpp_parser_expand_token_list_onto (parser, list, expanded);
|
|
|
|
|
|
|
|
|
|
_token_list_trim_trailing_space (expanded);
|
|
|
|
|
|
|
|
|
|
_token_list_print (expanded);
|
|
|
|
|
|
|
|
|
|
talloc_free (expanded);
|
2010-05-25 15:24:59 -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-19 13:54:37 -07:00
|
|
|
token_list_t *replacements)
|
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);
|
2010-05-19 13:28:24 -07:00
|
|
|
macro->replacements = talloc_steal (macro, replacements);
|
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-19 13:54:37 -07:00
|
|
|
token_list_t *replacements)
|
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);
|
2010-05-19 13:28:24 -07:00
|
|
|
macro->replacements = talloc_steal (macro, replacements);
|
2010-05-13 09:36:23 -07:00
|
|
|
|
|
|
|
|
hash_table_insert (parser->defines, macro, identifier);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-19 10:01:29 -07:00
|
|
|
static int
|
2010-05-19 10:05:40 -07:00
|
|
|
glcpp_parser_lex (glcpp_parser_t *parser)
|
2010-05-19 10:01:29 -07:00
|
|
|
{
|
2010-05-26 11:15:21 -07:00
|
|
|
token_node_t *node;
|
|
|
|
|
int ret;
|
|
|
|
|
|
Treat newlines as space when invoking a function-like macro invocation.
This adds three new pieces of state to the parser, (is_control_line,
newline_as_space, and paren_count), and a large amount of messy
code. I'd definitely like to see a cleaner solution for this.
With this fix, the "define-func-extra-newlines" now passes so we put
it back to test #26 where it was originally (lately it has been known
as test #55).
Also, we tweak test 25 slightly. Previously this test was ending a
file function-like macro name that was not actually a macro (not
followed by a left parenthesis). As is, this fix was making that test
fail because the text_line production expects to see a terminating
NEWLINE, but that NEWLINE is now getting turned into a SPACE here.
This seems unlikely to be a problem in the wild, (function macros
being used in a non-macro sense seems rare enough---but more than
likely they won't happen at the end of a file). Still, we document
this shortcoming in the README.
2010-05-26 15:57:10 -07:00
|
|
|
if (parser->lex_from_list == NULL) {
|
|
|
|
|
ret = glcpp_lex (parser->scanner);
|
|
|
|
|
|
|
|
|
|
/* XXX: This ugly block of code exists for the sole
|
|
|
|
|
* purpose of converting a NEWLINE token into a SPACE
|
|
|
|
|
* token, but only in the case where we have seen a
|
|
|
|
|
* function-like macro name, but have not yet seen its
|
|
|
|
|
* closing parenthesis.
|
|
|
|
|
*
|
|
|
|
|
* There's perhaps a more compact way to do this with
|
|
|
|
|
* mid-rule actions in the grammar.
|
|
|
|
|
*
|
|
|
|
|
* I'm definitely not pleased with the complexity of
|
|
|
|
|
* this code here.
|
|
|
|
|
*/
|
|
|
|
|
if (parser->newline_as_space)
|
|
|
|
|
{
|
|
|
|
|
if (ret == '(') {
|
|
|
|
|
parser->paren_count++;
|
|
|
|
|
} else if (ret == ')') {
|
|
|
|
|
parser->paren_count--;
|
|
|
|
|
if (parser->paren_count == 0)
|
|
|
|
|
parser->newline_as_space = 0;
|
|
|
|
|
} else if (ret == NEWLINE) {
|
|
|
|
|
ret = SPACE;
|
|
|
|
|
} else if (ret != SPACE) {
|
|
|
|
|
if (parser->paren_count == 0)
|
|
|
|
|
parser->newline_as_space = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (parser->in_control_line)
|
|
|
|
|
{
|
|
|
|
|
if (ret == NEWLINE)
|
|
|
|
|
parser->in_control_line = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
|
|
|
|
|
ret == HASH_UNDEF || ret == HASH_IF ||
|
|
|
|
|
ret == HASH_IFDEF || ret == HASH_IFNDEF ||
|
|
|
|
|
ret == HASH_ELIF || ret == HASH_ELSE ||
|
|
|
|
|
ret == HASH_ENDIF || ret == HASH)
|
|
|
|
|
{
|
|
|
|
|
parser->in_control_line = 1;
|
|
|
|
|
}
|
|
|
|
|
else if (ret == IDENTIFIER)
|
|
|
|
|
{
|
|
|
|
|
macro_t *macro;
|
|
|
|
|
macro = hash_table_find (parser->defines,
|
|
|
|
|
yylval.str);
|
|
|
|
|
if (macro && macro->is_function) {
|
|
|
|
|
parser->newline_as_space = 1;
|
|
|
|
|
parser->paren_count = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2010-05-26 11:15:21 -07:00
|
|
|
|
|
|
|
|
node = parser->lex_from_node;
|
|
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
talloc_free (parser->lex_from_list);
|
|
|
|
|
parser->lex_from_list = NULL;
|
|
|
|
|
return NEWLINE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yylval = node->token->value;
|
|
|
|
|
ret = node->token->type;
|
|
|
|
|
|
|
|
|
|
parser->lex_from_node = node->next;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
|
|
|
|
|
{
|
|
|
|
|
token_node_t *node;
|
|
|
|
|
|
|
|
|
|
assert (parser->lex_from_list == NULL);
|
|
|
|
|
|
|
|
|
|
/* Copy list, eliminating any space tokens. */
|
|
|
|
|
parser->lex_from_list = _token_list_create (parser);
|
|
|
|
|
|
|
|
|
|
for (node = list->head; node; node = node->next) {
|
|
|
|
|
if (node->token->type == SPACE)
|
|
|
|
|
continue;
|
|
|
|
|
_token_list_append (parser->lex_from_list, node->token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
talloc_free (list);
|
|
|
|
|
|
|
|
|
|
parser->lex_from_node = parser->lex_from_list->head;
|
|
|
|
|
|
|
|
|
|
/* It's possible the list consisted of nothing but whitespace. */
|
|
|
|
|
if (parser->lex_from_node == NULL) {
|
|
|
|
|
talloc_free (parser->lex_from_list);
|
|
|
|
|
parser->lex_from_list = NULL;
|
|
|
|
|
}
|
2010-05-19 10:01:29 -07:00
|
|
|
}
|
2010-05-20 22:27:07 -07:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
|
|
|
|
|
{
|
|
|
|
|
skip_type_t current = SKIP_NO_SKIP;
|
|
|
|
|
skip_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack)
|
|
|
|
|
current = parser->skip_stack->type;
|
|
|
|
|
|
|
|
|
|
node = xtalloc (parser, skip_node_t);
|
|
|
|
|
|
|
|
|
|
if (current == SKIP_NO_SKIP) {
|
|
|
|
|
if (condition)
|
|
|
|
|
node->type = SKIP_NO_SKIP;
|
|
|
|
|
else
|
|
|
|
|
node->type = SKIP_TO_ELSE;
|
|
|
|
|
} else {
|
|
|
|
|
node->type = SKIP_TO_ENDIF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node->next = parser->skip_stack;
|
|
|
|
|
parser->skip_stack = node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
|
|
|
|
|
int condition)
|
|
|
|
|
{
|
|
|
|
|
if (parser->skip_stack == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: %s without #if\n", type);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack->type == SKIP_TO_ELSE) {
|
|
|
|
|
if (condition)
|
|
|
|
|
parser->skip_stack->type = SKIP_NO_SKIP;
|
|
|
|
|
} else {
|
|
|
|
|
parser->skip_stack->type = SKIP_TO_ENDIF;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-25 14:42:00 -07:00
|
|
|
|
2010-05-20 22:27:07 -07:00
|
|
|
static void
|
|
|
|
|
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
skip_node_t *node;
|
|
|
|
|
|
|
|
|
|
if (parser->skip_stack == NULL) {
|
|
|
|
|
fprintf (stderr, "Error: #endif without #if\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = parser->skip_stack;
|
|
|
|
|
parser->skip_stack = node->next;
|
|
|
|
|
talloc_free (node);
|
|
|
|
|
}
|