blob: a3a661b8befcb1d5c9a22abf031c309b8a18b4dc [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27
Carl Wortha1e32bc2010-05-10 13:17:25 -070028#include "glcpp.h"
29
Carl Worth0b27b5f2010-05-10 16:16:06 -070030#define YYLEX_PARAM parser->scanner
Carl Worth3a37b872010-05-10 11:44:09 -070031
32void
Carl Wortha1e32bc2010-05-10 13:17:25 -070033yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070034
Carl Worthc6d5af32010-05-11 12:30:09 -070035const char *
36_resolve_token (glcpp_parser_t *parser, const char *token);
37
Carl Worth3a37b872010-05-10 11:44:09 -070038%}
39
Carl Worth0b27b5f2010-05-10 16:16:06 -070040%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -070041%lex-param {void *scanner}
42
Carl Worth0b27b5f2010-05-10 16:16:06 -070043%token DEFINE
44%token DEFVAL
45%token IDENTIFIER
Carl Worth3a37b872010-05-10 11:44:09 -070046%token TOKEN
47
48%%
49
50input: /* empty */
Carl Worth0b27b5f2010-05-10 16:16:06 -070051 | content
Carl Worth3a37b872010-05-10 11:44:09 -070052;
53
Carl Worth0b27b5f2010-05-10 16:16:06 -070054content: token
55 | directive
56 | content token
57 | content directive
Carl Worth3a37b872010-05-10 11:44:09 -070058;
59
Carl Worth0b27b5f2010-05-10 16:16:06 -070060directive: DEFINE IDENTIFIER DEFVAL {
61 hash_table_insert (parser->defines, $3, $2);
62}
63;
64
Carl Worthc6d5af32010-05-11 12:30:09 -070065token: TOKEN { printf ("%s", _resolve_token (parser, $1)); free ($1); }
Carl Worth3a37b872010-05-10 11:44:09 -070066;
67
68%%
69
70void
Carl Wortha1e32bc2010-05-10 13:17:25 -070071yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -070072{
73 fprintf (stderr, "Parse error: %s\n", error);
74}
Carl Worth0b27b5f2010-05-10 16:16:06 -070075
76void
77glcpp_parser_init (glcpp_parser_t *parser)
78{
79 yylex_init (&parser->scanner);
80 parser->defines = hash_table_ctor (32, hash_table_string_hash,
81 hash_table_string_compare);
82}
83
84int
85glcpp_parser_parse (glcpp_parser_t *parser)
86{
87 return yyparse (parser);
88}
89
90void
91glcpp_parser_fini (glcpp_parser_t *parser)
92{
93 yylex_destroy (parser->scanner);
94 hash_table_dtor (parser->defines);
95}
Carl Worthc6d5af32010-05-11 12:30:09 -070096
97const char *
98_resolve_token (glcpp_parser_t *parser, const char *token)
99{
100 const char *orig = token;
101 const char *replacement;
102
103 while (1) {
104 replacement = hash_table_find (parser->defines, token);
105 if (replacement == NULL)
106 break;
107 token = replacement;
108 if (strcmp (token, orig) == 0)
109 break;
110 }
111
112 return token;
113}
114