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" |
| 30 | %} |
| 31 | |
Carl Worth | 38aa835 | 2010-05-10 11:52:29 -0700 | [diff] [blame] | 32 | %option reentrant noyywrap |
Carl Worth | 5070a20 | 2010-05-12 12:45:33 -0700 | [diff] [blame] | 33 | %option extra-type="glcpp_parser_t *" |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 34 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 35 | /* This lexer has two states: |
| 36 | * |
| 37 | * The CONTROL state is for control lines (directives) |
| 38 | * It lexes exactly as specified in the C99 specification. |
| 39 | * |
| 40 | * The INITIAL state is for input lines. In this state, we |
| 41 | * make the OTHER token much more broad in that it now |
| 42 | * includes tokens consisting entirely of whitespace. This |
| 43 | * allows us to pass text through verbatim. It avoids the |
| 44 | * "inadvertent token pasting" problem that would occur if we |
| 45 | * just printed tokens, while also avoiding excess whitespace |
| 46 | * insertion in the output.*/ |
| 47 | |
| 48 | %x CONTROL |
| 49 | |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 50 | SPACE [[:space:]] |
| 51 | NONSPACE [^[:space:]] |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 52 | NEWLINE [\n] |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 53 | HSPACE [ \t] |
Carl Worth | e36a4d5 | 2010-05-14 17:29:24 -0700 | [diff] [blame] | 54 | HASH ^{HSPACE}*#{HSPACE}* |
Carl Worth | 0b27b5f | 2010-05-10 16:16:06 -0700 | [diff] [blame] | 55 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 56 | PUNCTUATION [][(){}.&*~!/%<>^|;,+-] |
| 57 | OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+ |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 58 | |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 59 | DECIMAL_INTEGER [1-9][0-9]*[uU]? |
| 60 | OCTAL_INTEGER 0[0-7]*[uU]? |
| 61 | HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]? |
| 62 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 63 | %% |
| 64 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 65 | {HASH}define{HSPACE}+/{IDENTIFIER}"(" { |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 66 | BEGIN CONTROL; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 67 | return HASH_DEFINE_FUNC; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 70 | {HASH}define { |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 71 | BEGIN CONTROL; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 72 | return HASH_DEFINE_OBJ; |
Carl Worth | b20d33c | 2010-05-20 22:27:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 75 | {HASH}undef { |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 76 | BEGIN CONTROL; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 77 | return HASH_UNDEF; |
Carl Worth | 03f6d5d | 2010-05-24 11:29:02 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 80 | {HASH} { |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 81 | BEGIN CONTROL; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 82 | return HASH; |
Carl Worth | 81f0143 | 2010-05-14 17:08:45 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 85 | <CONTROL>{IDENTIFIER} { |
| 86 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 87 | return IDENTIFIER; |
| 88 | } |
| 89 | |
| 90 | <CONTROL>"<<" { |
| 91 | return LEFT_SHIFT; |
| 92 | } |
| 93 | |
| 94 | <CONTROL>">>" { |
| 95 | return RIGHT_SHIFT; |
| 96 | } |
| 97 | |
| 98 | <CONTROL>"<=" { |
| 99 | return LESS_OR_EQUAL; |
| 100 | } |
| 101 | |
| 102 | <CONTROL>">=" { |
| 103 | return GREATER_OR_EQUAL; |
| 104 | } |
| 105 | |
| 106 | <CONTROL>"==" { |
| 107 | return EQUAL; |
| 108 | } |
| 109 | |
| 110 | <CONTROL>"!=" { |
| 111 | return NOT_EQUAL; |
| 112 | } |
| 113 | |
| 114 | <CONTROL>"&&" { |
| 115 | return AND; |
| 116 | } |
| 117 | |
| 118 | <CONTROL>"||" { |
| 119 | return OR; |
| 120 | } |
| 121 | |
| 122 | <CONTROL>"##" { |
| 123 | return PASTE; |
| 124 | } |
| 125 | |
| 126 | <CONTROL>{PUNCTUATION} { |
| 127 | return yytext[0]; |
| 128 | } |
| 129 | |
| 130 | <CONTROL>{OTHER} { |
| 131 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 132 | return OTHER; |
| 133 | } |
| 134 | |
| 135 | <CONTROL>{HSPACE}+ |
| 136 | |
| 137 | <CONTROL>\n { |
| 138 | BEGIN INITIAL; |
| 139 | return NEWLINE; |
| 140 | } |
| 141 | |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 142 | {IDENTIFIER} { |
Carl Worth | a807fb7 | 2010-05-18 22:10:04 -0700 | [diff] [blame] | 143 | yylval.str = xtalloc_strdup (yyextra, yytext); |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 144 | return IDENTIFIER; |
Carl Worth | cd27e64 | 2010-05-12 13:11:50 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 147 | {OTHER}+ { |
| 148 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 149 | return OTHER; |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 152 | {HSPACE}+ { |
| 153 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 154 | return OTHER; |
Carl Worth | 0a93cbb | 2010-05-13 10:29:07 -0700 | [diff] [blame] | 155 | } |
Carl Worth | fcbbb46 | 2010-05-13 09:36:23 -0700 | [diff] [blame] | 156 | |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 157 | \n { |
| 158 | return NEWLINE; |
Carl Worth | 33cc400 | 2010-05-12 12:17:10 -0700 | [diff] [blame] | 159 | } |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 160 | |
Carl Worth | 9fb8b7a | 2010-05-25 15:04:32 -0700 | [diff] [blame^] | 161 | . { |
Carl Worth | 3ff8167 | 2010-05-25 13:09:03 -0700 | [diff] [blame] | 162 | yylval.str = xtalloc_strdup (yyextra, yytext); |
| 163 | return OTHER; |
Carl Worth | 012295f | 2010-05-12 13:19:23 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Carl Worth | 3a37b87 | 2010-05-10 11:44:09 -0700 | [diff] [blame] | 166 | %% |