glcpp: Refactor HASH_IF and HASH_ELIF expansion to reuse code.

This commit is contained in:
Kenneth Graunke
2010-08-04 16:10:03 -07:00
parent 046bef2357
commit 16b4eed59a
2 changed files with 142 additions and 144 deletions
+19 -20
View File
@@ -105,6 +105,9 @@ _active_list_pop (active_list_t *list);
int
_active_list_contains (active_list_t *list, const char *identifier);
static void
_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
static void
_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
token_list_t *list);
@@ -212,16 +215,7 @@ control_line:
talloc_free ($2);
}
| HASH_IF conditional_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_expand_token_list (parser, $2);
_token_list_append_list (expanded, $2);
glcpp_parser_lex_from (parser, expanded);
_glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
}
| HASH_IFDEF IDENTIFIER junk NEWLINE {
macro_t *macro = hash_table_find (parser->defines, $2);
@@ -234,16 +228,7 @@ control_line:
_glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
}
| HASH_ELIF conditional_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_expand_token_list (parser, $2);
_token_list_append_list (expanded, $2);
glcpp_parser_lex_from (parser, expanded);
_glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
}
| HASH_ELIF NEWLINE {
/* #elif without an expression results in a warning if the
@@ -1087,6 +1072,20 @@ _token_list_create_with_one_space (void *ctx)
return list;
}
static void
_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
{
token_list_t *expanded;
token_t *token;
expanded = _token_list_create (parser);
token = _token_create_ival (parser, type, type);
_token_list_append (expanded, token);
_glcpp_parser_expand_token_list (parser, list);
_token_list_append_list (expanded, list);
glcpp_parser_lex_from (parser, expanded);
}
/* This is a helper function that's essentially part of the
* implementation of _glcpp_parser_expand_node. It shouldn't be called
* except for by that function.