summaryrefslogtreecommitdiff
path: root/src/nsgenbind-ast.c
blob: e192d742925b0eefec97032a47081d6ad5918369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* binding generator AST implementation for parser
 *
 * This file is part of nsgenbind.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 */

/** @todo this currently stuffs everything in one global tree, not very nice
 */

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "nsgenbind-ast.h"
#include "options.h"

/* parser and lexer interface */
extern int nsgenbind_debug;
extern int nsgenbind__flex_debug;
extern void nsgenbind_restart(FILE*);
extern int nsgenbind_parse(struct genbind_node **genbind_ast);

/* terminal nodes have a value only */
struct genbind_node {
	enum genbind_node_type type;
	struct genbind_node *l;
	union {
		void *value;
		struct genbind_node *node;
		char *text;
	} r;
};


char *genbind_strapp(char *a, char *b)
{
	char *fullstr;
	int fulllen;
	fulllen = strlen(a) + strlen(b) + 1;
	fullstr = malloc(fulllen);
	snprintf(fullstr, fulllen, "%s%s", a, b);
	free(a);
	free(b);
	return fullstr;
}

struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src)
{
	tgt->l = src;
	return tgt;
}


struct genbind_node *
genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r)
{
	struct genbind_node *nn;
	nn = calloc(1, sizeof(struct genbind_node));
	nn->type = type;
	nn->l = l;
	nn->r.value = r;
	return nn;
}

int
genbind_node_for_each_type(struct genbind_node *node,
			   enum genbind_node_type type,
			   genbind_callback_t *cb,
			   void *ctx)
{
	int ret;

	if (node == NULL) {
		return -1;
	}
	if (node->l != NULL) {
		ret = genbind_node_for_each_type(node->l, type, cb, ctx);
		if (ret != 0) {
			return ret;
		}
	}
	if (node->type == type) {
		return cb(node, ctx);
	}

	return 0;
}


/* exported interface defined in nsgenbind-ast.h */
struct genbind_node *
genbind_node_find(struct genbind_node *node,
		  struct genbind_node *prev,
		  genbind_callback_t *cb,
		  void *ctx)
{
	struct genbind_node *ret;

	if (node == NULL) {
		return NULL;
	}

	if (node->l != prev) {
		ret = genbind_node_find(node->l, prev, cb, ctx);
		if (ret != NULL) {
			return ret;
		}
	}

	if (cb(node, ctx) != 0) {
		return node;
	}

	return NULL;
}

/* exported interface defined in nsgenbind-ast.h */
struct genbind_node *
genbind_node_find_type(struct genbind_node *node,
		       struct genbind_node *prev,
		       enum genbind_node_type type)
{
	return genbind_node_find(node,
				 prev,
				 genbind_cmp_node_type,
				 (void *)type);
}

struct genbind_node *
genbind_node_find_type_ident(struct genbind_node *node,
			     struct genbind_node *prev,
			     enum genbind_node_type type,
			     const char *ident)
{
	struct genbind_node *found_node;
	struct genbind_node *ident_node;

	found_node = genbind_node_find(node,
				       prev,
				       genbind_cmp_node_type,
				       (void *)type);

	while (found_node != NULL) {

		ident_node = genbind_node_find(genbind_node_getnode(found_node),
					       NULL,
					       genbind_cmp_node_type,
					       (void *)GENBIND_NODE_TYPE_IDENT);
		if (ident_node != NULL) {
			if (strcmp(ident_node->r.text, ident) == 0)
				break;
		}

		/* look for next matching node */
		found_node = genbind_node_find(node,
					       found_node,
					       genbind_cmp_node_type,
					       (void *)type);

	}
	return found_node;
}

int genbind_cmp_node_type(struct genbind_node *node, void *ctx)
{
	if (node->type == (enum genbind_node_type)ctx)
		return 1;
	return 0;
}

char *genbind_node_gettext(struct genbind_node *node)
{
	switch(node->type) {
	case GENBIND_NODE_TYPE_WEBIDLFILE:
	case GENBIND_NODE_TYPE_STRING:
	case GENBIND_NODE_TYPE_PREAMBLE:
	case GENBIND_NODE_TYPE_IDENT:
	case GENBIND_NODE_TYPE_BINDING_TYPE:
	case GENBIND_NODE_TYPE_BINDING_INTERFACE:
	case GENBIND_NODE_TYPE_CBLOCK:
		return node->r.text;

	default:
		return NULL;
	}
}

struct genbind_node *genbind_node_getnode(struct genbind_node *node)
{
	switch(node->type) {
	case GENBIND_NODE_TYPE_HDRCOMMENT:
	case GENBIND_NODE_TYPE_BINDING:
	case GENBIND_NODE_TYPE_BINDING_PRIVATE:
	case GENBIND_NODE_TYPE_BINDING_INTERNAL:
	case GENBIND_NODE_TYPE_OPERATION:
	case GENBIND_NODE_TYPE_API:
		return node->r.node;

	default:
		return NULL;
	}
}

static const char *genbind_node_type_to_str(enum genbind_node_type type)
{
	switch(type) {
	case GENBIND_NODE_TYPE_IDENT:
		return "Ident";

	case GENBIND_NODE_TYPE_ROOT:
		return "Root";

	case GENBIND_NODE_TYPE_WEBIDLFILE:
		return "webidlfile";

	case GENBIND_NODE_TYPE_HDRCOMMENT:
		return "HdrComment";

	case GENBIND_NODE_TYPE_STRING:
		return "String";

	case GENBIND_NODE_TYPE_PREAMBLE:
		return "Preamble";

	case GENBIND_NODE_TYPE_BINDING:
		return "Binding";

	case GENBIND_NODE_TYPE_BINDING_TYPE:
		return "Type";

	case GENBIND_NODE_TYPE_BINDING_PRIVATE:
		return "Private";

	case GENBIND_NODE_TYPE_BINDING_INTERNAL:
		return "Internal";

	case GENBIND_NODE_TYPE_BINDING_INTERFACE:
		return "Interface";

	case GENBIND_NODE_TYPE_OPERATION:
		return "Operation";

	case GENBIND_NODE_TYPE_API:
		return "API";

	case GENBIND_NODE_TYPE_CBLOCK:
		return "CBlock";

	default:
		return "Unknown";
	}
}

int genbind_ast_dump(struct genbind_node *node, int indent)
{
	const char *SPACES="                                                                               ";
	char *txt;

	while (node != NULL) {
		printf("%.*s%s", indent, SPACES,  genbind_node_type_to_str(node->type));

		txt = genbind_node_gettext(node);
		if (txt == NULL) {
			printf("\n");
			genbind_ast_dump(genbind_node_getnode(node), indent + 2);
		} else {
			printf(": \"%.*s\"\n", 75 - indent, txt);
		}
		node = node->l;
	}
	return 0;
}

FILE *genbindopen(const char *filename)
{
	FILE *genfile;
	char *fullname;
	int fulllen;
	static char *prevfilepath = NULL;

	/* try filename raw */
	genfile = fopen(filename, "r");
	if (genfile != NULL) {
		if (options->verbose) {
			printf("Opened Genbind file %s\n", filename);
		}
		if (prevfilepath == NULL) {
			fullname = strrchr(filename, '/');
			if (fullname == NULL) {
				fulllen = strlen(filename);
			} else {
				fulllen = fullname - filename;
			}
			prevfilepath = strndup(filename,fulllen);
		}
		return genfile;
	}

	/* try based on previous filename */
	if (prevfilepath != NULL) {
		fulllen = strlen(prevfilepath) + strlen(filename) + 2;
		fullname = malloc(fulllen);
		snprintf(fullname, fulllen, "%s/%s", prevfilepath, filename);
		if (options->debug) {
			printf("Attempting to open Genbind file %s\n", fullname);
		}
		genfile = fopen(fullname, "r");
		if (genfile != NULL) {
			if (options->verbose) {
				printf("Opened Genbind file %s\n", fullname);
			}
			free(fullname);
			return genfile;
		}
		free(fullname);
	}

	/* try on idl path */
	if (options->idlpath != NULL) {
		fulllen = strlen(options->idlpath) + strlen(filename) + 2;
		fullname = malloc(fulllen);
		snprintf(fullname, fulllen, "%s/%s", options->idlpath, filename);
		genfile = fopen(fullname, "r");
		if ((genfile != NULL) && options->verbose) {
			printf("Opend Genbind file %s\n", fullname);
		}

		free(fullname);
	}

	return genfile;
}

int genbind_parsefile(char *infilename, struct genbind_node **ast)
{
	FILE *infile;

	/* open input file */
	if ((infilename[0] == '-') &&
	    (infilename[1] == 0)) {
		if (options->verbose) {
			printf("Using stdin for input\n");
		}
		infile = stdin;
	} else {
		infile = genbindopen(infilename);
	}

	if (!infile) {
		fprintf(stderr, "Error opening %s: %s\n",
			infilename,
			strerror(errno));
		return 3;
	}

	if (options->debug) {
		nsgenbind_debug = 1;
		nsgenbind__flex_debug = 1;
	}

	/* set flex to read from file */
	nsgenbind_restart(infile);

	/* process binding */
	return nsgenbind_parse(ast);

}