Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #ifndef GLCPP_H |
| 25 | #define GLCPP_H |
| 26 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 27 | #include <talloc.h> |
| 28 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 29 | #include "hash_table.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 30 | |
| 31 | #define yyscan_t void* |
| 32 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 33 | /* Some data types used for parser values. */ |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 34 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 35 | typedef struct string_node { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 36 | const char *str; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 37 | struct string_node *next; |
| 38 | } string_node_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 39 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 40 | typedef struct string_list { |
| 41 | string_node_t *head; |
| 42 | string_node_t *tail; |
| 43 | } string_list_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 44 | |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 45 | typedef struct token { |
| 46 | int type; |
| 47 | char *value; |
| 48 | } token_t; |
| 49 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 50 | typedef struct token_node { |
| 51 | int type; |
| 52 | const char *value; |
| 53 | struct token_node *next; |
| 54 | } token_node_t; |
| 55 | |
| 56 | typedef struct token_list { |
| 57 | token_node_t *head; |
| 58 | token_node_t *tail; |
| 59 | } token_list_t; |
| 60 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 61 | typedef struct argument_node { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 62 | token_list_t *argument; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 63 | struct argument_node *next; |
| 64 | } argument_node_t; |
| 65 | |
| 66 | typedef struct argument_list { |
| 67 | argument_node_t *head; |
| 68 | argument_node_t *tail; |
| 69 | } argument_list_t; |
| 70 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 71 | typedef struct glcpp_parser glcpp_parser_t; |
| 72 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 73 | typedef enum { |
| 74 | TOKEN_CLASS_ARGUMENT, |
| 75 | TOKEN_CLASS_IDENTIFIER, |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 76 | TOKEN_CLASS_IDENTIFIER_FINALIZED, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 77 | TOKEN_CLASS_FUNC_MACRO, |
| 78 | TOKEN_CLASS_OBJ_MACRO |
| 79 | } token_class_t; |
| 80 | |
| 81 | token_class_t |
| 82 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 83 | const char *identifier, |
| 84 | int *parameter_index); |
| 85 | |
| 86 | typedef struct { |
| 87 | int is_function; |
| 88 | string_list_t *parameters; |
| 89 | const char *identifier; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 90 | token_list_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 91 | } macro_t; |
| 92 | |
| 93 | typedef struct expansion_node { |
| 94 | macro_t *macro; |
| 95 | argument_list_t *arguments; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 96 | token_node_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 97 | struct expansion_node *next; |
| 98 | } expansion_node_t; |
| 99 | |
| 100 | struct glcpp_parser { |
| 101 | yyscan_t scanner; |
| 102 | struct hash_table *defines; |
| 103 | expansion_node_t *expansions; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame^] | 104 | int just_printed_separator; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 107 | void |
| 108 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 109 | int argument_index); |
| 110 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 111 | glcpp_parser_t * |
| 112 | glcpp_parser_create (void); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 113 | |
| 114 | int |
| 115 | glcpp_parser_parse (glcpp_parser_t *parser); |
| 116 | |
| 117 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 118 | glcpp_parser_destroy (glcpp_parser_t *parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 119 | |
| 120 | /* Generated by glcpp-lex.l to glcpp-lex.c */ |
| 121 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 122 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 123 | glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 124 | |
| 125 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 126 | glcpp_lex (yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 127 | |
| 128 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 129 | glcpp_lex_destroy (yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 130 | |
| 131 | /* Generated by glcpp-parse.y to glcpp-parse.c */ |
| 132 | |
| 133 | int |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 134 | yyparse (glcpp_parser_t *parser); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 135 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 136 | /* xtalloc - wrappers around talloc to check for out-of-memory */ |
| 137 | |
| 138 | #define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type) |
| 139 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 140 | #define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__) |
| 141 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 142 | void * |
| 143 | xtalloc_named_const (const void *context, size_t size, const char *name); |
| 144 | |
| 145 | char * |
| 146 | xtalloc_strdup (const void *t, const char *p); |
| 147 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 148 | char * |
| 149 | xtalloc_strndup (const void *t, const char *p, size_t n); |
| 150 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 151 | #endif |