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 <string.h> |
| 27 | |
Carl Worth | a1e32bc | 2010-05-10 13:17:25 -0700 | [diff] [blame] | 28 | #include "glcpp.h" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 29 | #include "glcpp-parse.h" |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 30 | |
| 31 | /* Yes, a macro with a return statement in it is evil. But surely no |
| 32 | * more evil than all the code generation happening with flex in the |
| 33 | * first place. */ |
| 34 | #define LEXIFY_IDENTIFIER do { \ |
| 35 | yylval.str = xtalloc_strdup (yyextra, yytext); \ |
| 36 | switch (glcpp_parser_macro_type (yyextra, yylval.str)) \ |
| 37 | { \ |
| 38 | case MACRO_TYPE_UNDEFINED: \ |
| 39 | return IDENTIFIER; \ |
| 40 | break; \ |
| 41 | case MACRO_TYPE_OBJECT: \ |
| 42 | return OBJ_MACRO; \ |
| 43 | break; \ |
| 44 | case MACRO_TYPE_FUNCTION: \ |
| 45 | return FUNC_MACRO; \ |
| 46 | break; \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 50 | %} |
| 51 | |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 52 | %option reentrant noyywrap |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 53 | %option extra-type="glcpp_parser_t *" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 54 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 55 | %x ST_DEFINE |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 56 | %x ST_DEFVAL_START |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 57 | %x ST_DEFVAL |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 58 | %x ST_UNDEF |
| 59 | %x ST_UNDEF_END |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 60 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 61 | SPACE [[:space:]] |
| 62 | NONSPACE [^[:space:]] |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 63 | NEWLINE [\n] |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 64 | HSPACE [ \t] |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 65 | HASH ^{HSPACE}*#{HSPACE}* |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 66 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 67 | TOKEN [^[:space:](),]+ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 68 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 69 | %% |
| 70 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 71 | {HASH}undef{HSPACE}* { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 72 | BEGIN ST_UNDEF; |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 73 | return UNDEF; |
| 74 | } |
| 75 | |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 76 | <ST_UNDEF>{IDENTIFIER} { |
| 77 | BEGIN ST_UNDEF_END; |
| 78 | LEXIFY_IDENTIFIER; |
| 79 | } |
| 80 | |
| 81 | <ST_UNDEF_END>\n { |
| 82 | BEGIN INITIAL; |
| 83 | return NEWLINE; |
| 84 | } |
| 85 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 86 | /* We use the ST_DEFINE and ST_DEFVAL states so that we can |
| 87 | * pass a space token, (yes, a token for whitespace!), since |
| 88 | * the preprocessor specification requires distinguishing |
| 89 | * "#define foo()" from "#define foo ()". |
| 90 | */ |
| 91 | {HASH}define{HSPACE}* { |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 92 | BEGIN ST_DEFINE; |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 93 | return DEFINE; |
| 94 | } |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 95 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 96 | <ST_DEFINE>{IDENTIFIER} { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 97 | BEGIN ST_DEFVAL_START; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 98 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 99 | return IDENTIFIER; |
| 100 | } |
| 101 | |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 102 | <ST_DEFVAL_START>\n { |
| 103 | BEGIN INITIAL; |
| 104 | return NEWLINE; |
| 105 | } |
| 106 | |
| 107 | <ST_DEFVAL_START>{HSPACE}+ { |
| 108 | BEGIN ST_DEFVAL; |
| 109 | return SPACE; |
| 110 | } |
| 111 | |
| 112 | <ST_DEFVAL_START>"(" { |
| 113 | BEGIN ST_DEFVAL; |
| 114 | return '('; |
| 115 | } |
| 116 | |
| 117 | <ST_DEFVAL>{IDENTIFIER} { |
| 118 | LEXIFY_IDENTIFIER; |
| 119 | } |
| 120 | |
| 121 | <ST_DEFVAL>[(),] { |
| 122 | return yytext[0]; |
| 123 | } |
| 124 | |
| 125 | <ST_DEFVAL>{TOKEN} { |
| 126 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 127 | return TOKEN; |
| 128 | } |
| 129 | |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 130 | <ST_DEFVAL>\n { |
| 131 | BEGIN INITIAL; |
| 132 | return NEWLINE; |
| 133 | } |
| 134 | |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 135 | <ST_DEFVAL>{HSPACE}+ |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 136 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 137 | {IDENTIFIER} { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 138 | LEXIFY_IDENTIFIER; |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 141 | [(),] { |
| 142 | return yytext[0]; |
| 143 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 144 | |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 145 | {TOKEN} { |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 146 | yylval.str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 147 | return TOKEN; |
| 148 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 149 | |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 150 | \n { |
Carl Worth | 1a29500 | 2010-05-17 13:19:04 -0700 | [diff] [blame^] | 151 | printf ("\n"); |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 154 | {HSPACE}+ |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 155 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 156 | %% |