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 | 3541909 | 2010-05-24 11:27:23 -0700 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 29 | #include <talloc.h> |
| 30 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 31 | #include "hash_table.h" |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 32 | |
| 33 | #define yyscan_t void* |
| 34 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 35 | /* Some data types used for parser values. */ |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 36 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 37 | typedef struct string_node { |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 38 | const char *str; |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 39 | struct string_node *next; |
| 40 | } string_node_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 41 | |
Carl Worth | 610053b | 2010-05-14 10:05:11 -0700 | [diff] [blame] | 42 | typedef struct string_list { |
| 43 | string_node_t *head; |
| 44 | string_node_t *tail; |
| 45 | } string_list_t; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 46 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame^] | 47 | typedef struct token token_t; |
| 48 | typedef struct token_list token_list_t; |
| 49 | |
| 50 | typedef union YYSTYPE |
| 51 | { |
| 52 | int ival; |
| 53 | char *str; |
| 54 | token_t *token; |
| 55 | token_list_t *token_list; |
| 56 | } YYSTYPE; |
| 57 | |
| 58 | # define YYSTYPE_IS_TRIVIAL 1 |
| 59 | # define YYSTYPE_IS_DECLARED 1 |
| 60 | |
| 61 | struct token { |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 62 | int type; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame^] | 63 | YYSTYPE value; |
| 64 | }; |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 65 | |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 66 | typedef struct token_node { |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame^] | 67 | token_t *token; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 68 | struct token_node *next; |
| 69 | } token_node_t; |
| 70 | |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame^] | 71 | struct token_list { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 72 | token_node_t *head; |
| 73 | token_node_t *tail; |
Carl Worth | 808401f | 2010-05-25 14:52:43 -0700 | [diff] [blame^] | 74 | }; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 75 | |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 76 | typedef struct argument_node { |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 77 | token_list_t *argument; |
Carl Worth | 8f6a828 | 2010-05-14 10:44:19 -0700 | [diff] [blame] | 78 | struct argument_node *next; |
| 79 | } argument_node_t; |
| 80 | |
| 81 | typedef struct argument_list { |
| 82 | argument_node_t *head; |
| 83 | argument_node_t *tail; |
| 84 | } argument_list_t; |
| 85 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 86 | typedef struct glcpp_parser glcpp_parser_t; |
| 87 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 88 | typedef enum { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 89 | TOKEN_CLASS_IDENTIFIER, |
Carl Worth | b569383 | 2010-05-20 08:01:44 -0700 | [diff] [blame] | 90 | TOKEN_CLASS_IDENTIFIER_FINALIZED, |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 91 | TOKEN_CLASS_FUNC_MACRO, |
| 92 | TOKEN_CLASS_OBJ_MACRO |
| 93 | } token_class_t; |
| 94 | |
| 95 | token_class_t |
| 96 | glcpp_parser_classify_token (glcpp_parser_t *parser, |
| 97 | const char *identifier, |
| 98 | int *parameter_index); |
| 99 | |
| 100 | typedef struct { |
| 101 | int is_function; |
| 102 | string_list_t *parameters; |
| 103 | const char *identifier; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 104 | token_list_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 105 | } macro_t; |
| 106 | |
| 107 | typedef struct expansion_node { |
| 108 | macro_t *macro; |
Carl Worth | 4725244 | 2010-05-19 13:54:37 -0700 | [diff] [blame] | 109 | token_node_t *replacements; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 110 | struct expansion_node *next; |
| 111 | } expansion_node_t; |
| 112 | |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 113 | typedef enum skip_type { |
| 114 | SKIP_NO_SKIP, |
| 115 | SKIP_TO_ELSE, |
| 116 | SKIP_TO_ENDIF |
| 117 | } skip_type_t; |
| 118 | |
| 119 | typedef struct skip_node { |
| 120 | skip_type_t type; |
| 121 | struct skip_node *next; |
| 122 | } skip_node_t; |
| 123 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 124 | struct glcpp_parser { |
| 125 | yyscan_t scanner; |
| 126 | struct hash_table *defines; |
| 127 | expansion_node_t *expansions; |
Carl Worth | 5a6b9a2 | 2010-05-20 14:29:43 -0700 | [diff] [blame] | 128 | int just_printed_separator; |
Carl Worth | 876e510 | 2010-05-20 14:38:06 -0700 | [diff] [blame] | 129 | int need_newline; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 130 | skip_node_t *skip_stack; |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Carl Worth | aaa9acb | 2010-05-19 13:28:24 -0700 | [diff] [blame] | 133 | void |
| 134 | glcpp_parser_push_expansion_argument (glcpp_parser_t *parser, |
| 135 | int argument_index); |
| 136 | |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 137 | glcpp_parser_t * |
| 138 | glcpp_parser_create (void); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 139 | |
| 140 | int |
| 141 | glcpp_parser_parse (glcpp_parser_t *parser); |
| 142 | |
| 143 | void |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 144 | glcpp_parser_destroy (glcpp_parser_t *parser); |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 145 | |
| 146 | /* Generated by glcpp-lex.l to glcpp-lex.c */ |
| 147 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 148 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 149 | glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 150 | |
| 151 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 152 | glcpp_lex (yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 153 | |
| 154 | int |
Carl Worth | 8f38aff | 2010-05-19 10:01:29 -0700 | [diff] [blame] | 155 | glcpp_lex_destroy (yyscan_t scanner); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 156 | |
| 157 | /* Generated by glcpp-parse.y to glcpp-parse.c */ |
| 158 | |
| 159 | int |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 160 | yyparse (glcpp_parser_t *parser); |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 161 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 162 | /* xtalloc - wrappers around talloc to check for out-of-memory */ |
| 163 | |
| 164 | #define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type) |
| 165 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 166 | #define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__) |
| 167 | |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 168 | void * |
| 169 | xtalloc_named_const (const void *context, size_t size, const char *name); |
| 170 | |
| 171 | char * |
| 172 | xtalloc_strdup (const void *t, const char *p); |
| 173 | |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 174 | char * |
| 175 | xtalloc_strndup (const void *t, const char *p, size_t n); |
| 176 | |
Carl Worth | b894583 | 2010-05-20 15:02:03 -0700 | [diff] [blame] | 177 | char * |
| 178 | xtalloc_asprintf (const void *t, const char *fmt, ...); |
| 179 | |
Carl Worth | 9bb796f | 2010-05-25 14:40:47 -0700 | [diff] [blame] | 180 | void * |
| 181 | _xtalloc_reference_loc (const void *context, |
| 182 | const void *ptr, const char *location); |
| 183 | |
| 184 | #define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__) |
| 185 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 186 | #endif |