Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 1 | %{ |
| 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 Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 28 | #include "glcpp.h" |
| 29 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 30 | #define YYLEX_PARAM parser->scanner |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 31 | |
| 32 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 33 | yyerror (void *scanner, const char *error); |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 34 | |
| 35 | %} |
| 36 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 37 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 38 | %lex-param {void *scanner} |
| 39 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 40 | %token DEFINE |
| 41 | %token DEFVAL |
| 42 | %token IDENTIFIER |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 43 | %token TOKEN |
| 44 | |
| 45 | %% |
| 46 | |
| 47 | input: /* empty */ |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 48 | | content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 49 | ; |
| 50 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 51 | content: token |
| 52 | | directive |
| 53 | | content token |
| 54 | | content directive |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 55 | ; |
| 56 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 57 | directive: DEFINE IDENTIFIER DEFVAL { |
| 58 | hash_table_insert (parser->defines, $3, $2); |
| 59 | } |
| 60 | ; |
| 61 | |
| 62 | token: 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 Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 70 | ; |
| 71 | |
| 72 | %% |
| 73 | |
| 74 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 75 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 76 | { |
| 77 | fprintf (stderr, "Parse error: %s\n", error); |
| 78 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame^] | 79 | |
| 80 | void |
| 81 | glcpp_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 | |
| 88 | int |
| 89 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 90 | { |
| 91 | return yyparse (parser); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | glcpp_parser_fini (glcpp_parser_t *parser) |
| 96 | { |
| 97 | yylex_destroy (parser->scanner); |
| 98 | hash_table_dtor (parser->defines); |
| 99 | } |