blob: 89dc46497f5f09f49b3473edaf1438a0a5b3c977 [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
35%}
36
Carl Worth0b27b5f2010-05-10 16:16:06 -070037%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -070038%lex-param {void *scanner}
39
Carl Worth0b27b5f2010-05-10 16:16:06 -070040%token DEFINE
41%token DEFVAL
42%token IDENTIFIER
Carl Worth3a37b872010-05-10 11:44:09 -070043%token TOKEN
44
45%%
46
47input: /* empty */
Carl Worth0b27b5f2010-05-10 16:16:06 -070048 | content
Carl Worth3a37b872010-05-10 11:44:09 -070049;
50
Carl Worth0b27b5f2010-05-10 16:16:06 -070051content: token
52 | directive
53 | content token
54 | content directive
Carl Worth3a37b872010-05-10 11:44:09 -070055;
56
Carl Worth0b27b5f2010-05-10 16:16:06 -070057directive: DEFINE IDENTIFIER DEFVAL {
58 hash_table_insert (parser->defines, $3, $2);
59}
60;
61
62token: TOKEN {
63 char *value = hash_table_find (parser->defines, $1);
64 if (value)
65 printf ("%s", value);
66 else
67 printf ("%s", $1);
68 free ($1);
69}
Carl Worth3a37b872010-05-10 11:44:09 -070070;
71
72%%
73
74void
Carl Wortha1e32bc2010-05-10 13:17:25 -070075yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -070076{
77 fprintf (stderr, "Parse error: %s\n", error);
78}
Carl Worth0b27b5f2010-05-10 16:16:06 -070079
80void
81glcpp_parser_init (glcpp_parser_t *parser)
82{
83 yylex_init (&parser->scanner);
84 parser->defines = hash_table_ctor (32, hash_table_string_hash,
85 hash_table_string_compare);
86}
87
88int
89glcpp_parser_parse (glcpp_parser_t *parser)
90{
91 return yyparse (parser);
92}
93
94void
95glcpp_parser_fini (glcpp_parser_t *parser)
96{
97 yylex_destroy (parser->scanner);
98 hash_table_dtor (parser->defines);
99}