blob: 81f7d14c5baf19f43e0e9971ae8e62fda48e339e [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 Wortha807fb72010-05-18 22:10:04 -070027#include <talloc.h>
28
Carl Worth0b27b5f2010-05-10 16:16:06 -070029#include "hash_table.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070030
31#define yyscan_t void*
32
Carl Wortha807fb72010-05-18 22:10:04 -070033/* Some data types used for parser values. */
Carl Worth0b27b5f2010-05-10 16:16:06 -070034
Carl Worth610053b2010-05-14 10:05:11 -070035typedef struct string_node {
Carl Worth33cc4002010-05-12 12:17:10 -070036 const char *str;
Carl Worth610053b2010-05-14 10:05:11 -070037 struct string_node *next;
38} string_node_t;
Carl Worth33cc4002010-05-12 12:17:10 -070039
Carl Worth610053b2010-05-14 10:05:11 -070040typedef struct string_list {
41 string_node_t *head;
42 string_node_t *tail;
43} string_list_t;
Carl Worth33cc4002010-05-12 12:17:10 -070044
Carl Worth8f6a8282010-05-14 10:44:19 -070045typedef struct argument_node {
46 string_list_t *argument;
47 struct argument_node *next;
48} argument_node_t;
49
50typedef struct argument_list {
51 argument_node_t *head;
52 argument_node_t *tail;
53} argument_list_t;
54
Carl Worth33cc4002010-05-12 12:17:10 -070055typedef struct glcpp_parser glcpp_parser_t;
56
Carl Wortha807fb72010-05-18 22:10:04 -070057/* Support for temporarily lexing/parsing tokens from a string. */
58
59typedef struct glcpp_lex_node {
60 void *buffer;
61 struct glcpp_lex_node *next;
62} glcpp_lex_node_t;
63
64typedef struct {
65 glcpp_parser_t *parser;
66 glcpp_lex_node_t *head;
67} glcpp_lex_stack_t;
68
69void
70glcpp_lex_stack_push (glcpp_lex_stack_t *stack, const char *string);
71
72int
73glcpp_lex_stack_pop (glcpp_lex_stack_t *stack);
74
75typedef enum {
76 TOKEN_CLASS_ARGUMENT,
77 TOKEN_CLASS_IDENTIFIER,
78 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;
91 const char *replacement;
92} macro_t;
93
94typedef struct expansion_node {
95 macro_t *macro;
96 argument_list_t *arguments;
97 struct expansion_node *next;
98} expansion_node_t;
99
100struct glcpp_parser {
101 yyscan_t scanner;
102 struct hash_table *defines;
103 expansion_node_t *expansions;
104 glcpp_lex_stack_t *lex_stack;
105};
106
Carl Worth33cc4002010-05-12 12:17:10 -0700107glcpp_parser_t *
108glcpp_parser_create (void);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700109
110int
111glcpp_parser_parse (glcpp_parser_t *parser);
112
113void
Carl Worth33cc4002010-05-12 12:17:10 -0700114glcpp_parser_destroy (glcpp_parser_t *parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700115
Carl Wortha807fb72010-05-18 22:10:04 -0700116void
117glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
118 macro_t *macro,
119 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -0700120
Carl Wortha807fb72010-05-18 22:10:04 -0700121void
122glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
123 int argument_index);
124
125void
126glcpp_parser_pop_expansion (glcpp_parser_t *parser);
Carl Worth9f62a7e2010-05-13 07:38:29 -0700127
Carl Worth0b27b5f2010-05-10 16:16:06 -0700128/* Generated by glcpp-lex.l to glcpp-lex.c */
129
Carl Wortha1e32bc2010-05-10 13:17:25 -0700130int
Carl Worth5070a202010-05-12 12:45:33 -0700131yylex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700132
133int
134yylex (yyscan_t scanner);
135
136int
137yylex_destroy (yyscan_t scanner);
138
139/* Generated by glcpp-parse.y to glcpp-parse.c */
140
141int
Carl Worth0b27b5f2010-05-10 16:16:06 -0700142yyparse (glcpp_parser_t *parser);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700143
Carl Worth5070a202010-05-12 12:45:33 -0700144/* xtalloc - wrappers around talloc to check for out-of-memory */
145
146#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
147
Carl Wortha807fb72010-05-18 22:10:04 -0700148#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
149
Carl Worth5070a202010-05-12 12:45:33 -0700150void *
151xtalloc_named_const (const void *context, size_t size, const char *name);
152
153char *
154xtalloc_strdup (const void *t, const char *p);
155
Carl Wortha807fb72010-05-18 22:10:04 -0700156char *
157xtalloc_strndup (const void *t, const char *p, size_t n);
158
Carl Wortha1e32bc2010-05-10 13:17:25 -0700159#endif