blob: 503731b85b3b8e7ef0df104f64dd6dc1a2b6d48c [file] [log] [blame]
Carl Wortha1e32bc2010-05-10 13:17:25 -07001/*
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 Worth35419092010-05-24 11:27:23 -070027#include <stdint.h>
28
Carl Wortha807fb72010-05-18 22:10:04 -070029#include <talloc.h>
30
Carl Worth0b27b5f2010-05-10 16:16:06 -070031#include "hash_table.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
33#define yyscan_t void*
34
Carl Wortha807fb72010-05-18 22:10:04 -070035/* Some data types used for parser values. */
Carl Worth0b27b5f2010-05-10 16:16:06 -070036
Carl Worth610053b2010-05-14 10:05:11 -070037typedef struct string_node {
Carl Worth33cc4002010-05-12 12:17:10 -070038 const char *str;
Carl Worth610053b2010-05-14 10:05:11 -070039 struct string_node *next;
40} string_node_t;
Carl Worth33cc4002010-05-12 12:17:10 -070041
Carl Worth610053b2010-05-14 10:05:11 -070042typedef struct string_list {
43 string_node_t *head;
44 string_node_t *tail;
45} string_list_t;
Carl Worth33cc4002010-05-12 12:17:10 -070046
Carl Worthb5693832010-05-20 08:01:44 -070047typedef struct token {
48 int type;
49 char *value;
50} token_t;
51
Carl Worth47252442010-05-19 13:54:37 -070052typedef struct token_node {
53 int type;
54 const char *value;
55 struct token_node *next;
56} token_node_t;
57
58typedef struct token_list {
59 token_node_t *head;
60 token_node_t *tail;
61} token_list_t;
62
Carl Worth8f6a8282010-05-14 10:44:19 -070063typedef struct argument_node {
Carl Worth47252442010-05-19 13:54:37 -070064 token_list_t *argument;
Carl Worth8f6a8282010-05-14 10:44:19 -070065 struct argument_node *next;
66} argument_node_t;
67
68typedef struct argument_list {
69 argument_node_t *head;
70 argument_node_t *tail;
71} argument_list_t;
72
Carl Worth33cc4002010-05-12 12:17:10 -070073typedef struct glcpp_parser glcpp_parser_t;
74
Carl Wortha807fb72010-05-18 22:10:04 -070075typedef enum {
Carl Wortha807fb72010-05-18 22:10:04 -070076 TOKEN_CLASS_IDENTIFIER,
Carl Worthb5693832010-05-20 08:01:44 -070077 TOKEN_CLASS_IDENTIFIER_FINALIZED,
Carl Wortha807fb72010-05-18 22:10:04 -070078 TOKEN_CLASS_FUNC_MACRO,
79 TOKEN_CLASS_OBJ_MACRO
80} token_class_t;
81
82token_class_t
83glcpp_parser_classify_token (glcpp_parser_t *parser,
84 const char *identifier,
85 int *parameter_index);
86
87typedef struct {
88 int is_function;
89 string_list_t *parameters;
90 const char *identifier;
Carl Worth47252442010-05-19 13:54:37 -070091 token_list_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -070092} macro_t;
93
94typedef struct expansion_node {
95 macro_t *macro;
Carl Worth47252442010-05-19 13:54:37 -070096 token_node_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -070097 struct expansion_node *next;
98} expansion_node_t;
99
Carl Worthb20d33c2010-05-20 22:27:07 -0700100typedef enum skip_type {
101 SKIP_NO_SKIP,
102 SKIP_TO_ELSE,
103 SKIP_TO_ENDIF
104} skip_type_t;
105
106typedef struct skip_node {
107 skip_type_t type;
108 struct skip_node *next;
109} skip_node_t;
110
Carl Wortha807fb72010-05-18 22:10:04 -0700111struct glcpp_parser {
112 yyscan_t scanner;
113 struct hash_table *defines;
114 expansion_node_t *expansions;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700115 int just_printed_separator;
Carl Worth876e5102010-05-20 14:38:06 -0700116 int need_newline;
Carl Worthb20d33c2010-05-20 22:27:07 -0700117 skip_node_t *skip_stack;
Carl Wortha807fb72010-05-18 22:10:04 -0700118};
119
Carl Worthaaa9acb2010-05-19 13:28:24 -0700120void
121glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
122 int argument_index);
123
Carl Worth33cc4002010-05-12 12:17:10 -0700124glcpp_parser_t *
125glcpp_parser_create (void);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700126
127int
128glcpp_parser_parse (glcpp_parser_t *parser);
129
130void
Carl Worth33cc4002010-05-12 12:17:10 -0700131glcpp_parser_destroy (glcpp_parser_t *parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700132
133/* Generated by glcpp-lex.l to glcpp-lex.c */
134
Carl Wortha1e32bc2010-05-10 13:17:25 -0700135int
Carl Worth8f38aff2010-05-19 10:01:29 -0700136glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700137
138int
Carl Worth8f38aff2010-05-19 10:01:29 -0700139glcpp_lex (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700140
141int
Carl Worth8f38aff2010-05-19 10:01:29 -0700142glcpp_lex_destroy (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700143
144/* Generated by glcpp-parse.y to glcpp-parse.c */
145
146int
Carl Worth0b27b5f2010-05-10 16:16:06 -0700147yyparse (glcpp_parser_t *parser);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700148
Carl Worth5070a202010-05-12 12:45:33 -0700149/* xtalloc - wrappers around talloc to check for out-of-memory */
150
151#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
152
Carl Wortha807fb72010-05-18 22:10:04 -0700153#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
154
Carl Worth5070a202010-05-12 12:45:33 -0700155void *
156xtalloc_named_const (const void *context, size_t size, const char *name);
157
158char *
159xtalloc_strdup (const void *t, const char *p);
160
Carl Wortha807fb72010-05-18 22:10:04 -0700161char *
162xtalloc_strndup (const void *t, const char *p, size_t n);
163
Carl Worthb8945832010-05-20 15:02:03 -0700164char *
165xtalloc_asprintf (const void *t, const char *fmt, ...);
166
Carl Wortha1e32bc2010-05-10 13:17:25 -0700167#endif