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 | |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame^] | 35 | const char * |
| 36 | _resolve_token (glcpp_parser_t *parser, const char *token); |
| 37 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 38 | %} |
| 39 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 40 | %parse-param {glcpp_parser_t *parser} |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 41 | %lex-param {void *scanner} |
| 42 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 43 | %token DEFINE |
| 44 | %token DEFVAL |
| 45 | %token IDENTIFIER |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 46 | %token TOKEN |
| 47 | |
| 48 | %% |
| 49 | |
| 50 | input: /* empty */ |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 51 | | content |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 52 | ; |
| 53 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 54 | content: token |
| 55 | | directive |
| 56 | | content token |
| 57 | | content directive |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 58 | ; |
| 59 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 60 | directive: DEFINE IDENTIFIER DEFVAL { |
| 61 | hash_table_insert (parser->defines, $3, $2); |
| 62 | } |
| 63 | ; |
| 64 | |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame^] | 65 | token: TOKEN { printf ("%s", _resolve_token (parser, $1)); free ($1); } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 66 | ; |
| 67 | |
| 68 | %% |
| 69 | |
| 70 | void |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 71 | yyerror (void *scanner, const char *error) |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 72 | { |
| 73 | fprintf (stderr, "Parse error: %s\n", error); |
| 74 | } |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 75 | |
| 76 | void |
| 77 | glcpp_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 | |
| 84 | int |
| 85 | glcpp_parser_parse (glcpp_parser_t *parser) |
| 86 | { |
| 87 | return yyparse (parser); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | glcpp_parser_fini (glcpp_parser_t *parser) |
| 92 | { |
| 93 | yylex_destroy (parser->scanner); |
| 94 | hash_table_dtor (parser->defines); |
| 95 | } |
Carl Worth | c6d5af3 | 2010-05-11 12:30:09 -0700 | [diff] [blame^] | 96 | |
| 97 | const 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 | |