Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 1 | %{ |
| 2 | |
| 3 | /* |
| 4 | * (C) Copyright 2014, Stephen M. Cameron. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include "y.tab.h" |
| 24 | |
| 25 | #define YYSTYPE PARSER_VALUE_TYPE |
| 26 | |
Jens Axboe | e2e7032 | 2014-10-14 21:12:22 -0600 | [diff] [blame^] | 27 | extern int lexer_input(char *buffer, unsigned int *nbytes, int buffersize); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 28 | |
| 29 | #undef YY_INPUT |
Jens Axboe | 4ce8aba | 2014-10-07 09:12:40 -0600 | [diff] [blame] | 30 | #define YY_INPUT(buffer, bytes_read, bytes_requested) \ |
| 31 | ({ \ |
| 32 | int __ret; \ |
Jens Axboe | e2e7032 | 2014-10-14 21:12:22 -0600 | [diff] [blame^] | 33 | unsigned int __bread = bytes_read; \ |
Jens Axboe | e340426 | 2014-10-07 09:09:57 -0600 | [diff] [blame] | 34 | __ret = lexer_input((buffer), &__bread, (bytes_requested)); \ |
| 35 | bytes_read = __bread; \ |
Jens Axboe | 4ce8aba | 2014-10-07 09:12:40 -0600 | [diff] [blame] | 36 | __ret; \ |
Jens Axboe | e340426 | 2014-10-07 09:09:57 -0600 | [diff] [blame] | 37 | }) |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 38 | |
| 39 | extern int yyerror(long long *result, double *dresult, |
Stephen M. Cameron | a22867a | 2014-09-30 11:21:20 -0500 | [diff] [blame] | 40 | int *has_error, int *units_specified, const char *msg); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 41 | |
Jens Axboe | 4ce8aba | 2014-10-07 09:12:40 -0600 | [diff] [blame] | 42 | static void __attribute__((unused)) yyunput(int c, char *buf_ptr); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 43 | static int __attribute__((unused)) input(void); |
| 44 | |
Stephen M. Cameron | c3805eb | 2014-09-29 12:15:35 -0600 | [diff] [blame] | 45 | #define set_suffix_value(yylval, i_val, d_val, has_d_val) \ |
| 46 | (yylval).v.dval = (d_val); \ |
| 47 | (yylval).v.ival = (i_val); \ |
| 48 | (yylval).v.has_dval = (has_d_val); \ |
| 49 | (yylval).v.has_error = 0; |
| 50 | |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 51 | %} |
| 52 | |
| 53 | %% |
| 54 | |
| 55 | |
Stephen M. Cameron | c3805eb | 2014-09-29 12:15:35 -0600 | [diff] [blame] | 56 | [kK]|[kK][bB] { |
| 57 | set_suffix_value(yylval, 1024, 1024.0, 0); |
| 58 | return SUFFIX; |
| 59 | } |
| 60 | [Mm]|[Mm][bB] { |
| 61 | set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0); |
| 62 | return SUFFIX; |
| 63 | } |
| 64 | [mM][sS] { |
| 65 | set_suffix_value(yylval, 1000, 1000.0, 1); |
| 66 | return SUFFIX; |
| 67 | } |
| 68 | [uU][sS] { |
| 69 | set_suffix_value(yylval, 1, 1.0, 1); |
| 70 | return SUFFIX; |
| 71 | } |
| 72 | [gG]|[Gg][Bb] { |
| 73 | set_suffix_value(yylval, 1024LL * 1024 * 1024, 1024.0 * 1024.0 * 1024, 0); |
| 74 | return SUFFIX; |
| 75 | } |
| 76 | [tT]|[tT][bB] { |
| 77 | set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024, |
| 78 | 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024, 0); |
| 79 | return SUFFIX; |
| 80 | } |
| 81 | [pP]|[pP][bB] { |
| 82 | set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024 * 1024, |
| 83 | 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0, 0); |
| 84 | return SUFFIX; |
| 85 | } |
| 86 | [kK][iI][Bb] { |
| 87 | set_suffix_value(yylval, 1000LL, 1000.0, 0); |
| 88 | return SUFFIX; |
| 89 | } |
| 90 | [mM][Ii][bB] { |
| 91 | set_suffix_value(yylval, 1000000LL, 1000000.0 , 0); |
| 92 | return SUFFIX; |
| 93 | } |
| 94 | [gG][iI][Bb] { |
| 95 | set_suffix_value(yylval, 1000000000LL, 1000000000.0 , 0); |
| 96 | return SUFFIX; |
| 97 | } |
| 98 | [pP][iI][Bb] { |
| 99 | set_suffix_value(yylval, 1000000000000LL, 1000000000000.0 , 0); |
| 100 | return SUFFIX; |
| 101 | } |
| 102 | [sS] { |
| 103 | set_suffix_value(yylval, 1000000LL, 1000000.0 , 0); |
| 104 | return SUFFIX; |
| 105 | } |
| 106 | [dD] { |
| 107 | set_suffix_value(yylval, 60LL * 60LL * 24LL * 1000000LL, |
| 108 | 60.0 * 60.0 * 24.0 * 1000000.0, 0); |
| 109 | return SUFFIX; |
| 110 | } |
| 111 | [hH] { |
| 112 | set_suffix_value(yylval, 60LL * 60LL * 1000000LL, |
| 113 | 60.0 * 60.0 * 1000000.0, 0); |
| 114 | return SUFFIX; |
| 115 | } |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 116 | [ \t] ; /* ignore whitespace */ |
Stephen M. Cameron | 303b5ca | 2014-09-30 08:44:24 -0500 | [diff] [blame] | 117 | [#:,].* ; /* ignore comments, and everything after colons and commas */ |
Stephen M. Cameron | 5abaf3e | 2014-10-02 20:31:11 -0500 | [diff] [blame] | 118 | [0-9]*[.][0-9]+|[0-9]*[.]?[0-9]+[eE][-+]*[0-9]+ { |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 119 | int rc; |
| 120 | double dval; |
| 121 | |
| 122 | rc = sscanf(yytext, "%lf", &dval); |
| 123 | if (rc == 1) { |
| 124 | yylval.v.dval = dval; |
| 125 | yylval.v.ival = (long long) dval; |
| 126 | yylval.v.has_dval = 1; |
| 127 | yylval.v.has_error = 0; |
| 128 | return NUMBER; |
| 129 | } else { |
Stephen M. Cameron | a22867a | 2014-09-30 11:21:20 -0500 | [diff] [blame] | 130 | yyerror(0, 0, 0, 0, "bad number\n"); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 131 | yylval.v.has_error = 1; |
| 132 | return NUMBER; |
| 133 | } |
| 134 | } |
| 135 | 0x[0-9a-fA-F]+ { |
| 136 | int rc, intval; |
| 137 | rc = sscanf(yytext, "%x", &intval); |
| 138 | if (rc == 1) { |
| 139 | yylval.v.ival = intval; |
| 140 | yylval.v.dval = (double) intval; |
| 141 | yylval.v.has_dval = 0; |
| 142 | yylval.v.has_error = 0; |
| 143 | return NUMBER; |
| 144 | } else { |
Stephen M. Cameron | a22867a | 2014-09-30 11:21:20 -0500 | [diff] [blame] | 145 | yyerror(0, 0, 0, 0, "bad number\n"); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 146 | yylval.v.has_error = 1; |
| 147 | return NUMBER; |
| 148 | } |
| 149 | } |
| 150 | [0-9]+ { |
| 151 | int rc, intval; |
| 152 | rc = sscanf(yytext, "%d", &intval); |
| 153 | if (rc == 1) { |
| 154 | yylval.v.ival = intval; |
| 155 | yylval.v.dval = (double) intval; |
| 156 | yylval.v.has_dval = 0; |
| 157 | yylval.v.has_error = 0; |
| 158 | return NUMBER; |
| 159 | } else { |
Stephen M. Cameron | a22867a | 2014-09-30 11:21:20 -0500 | [diff] [blame] | 160 | yyerror(0, 0, 0, 0, "bad number\n"); |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 161 | yylval.v.has_error = 1; |
| 162 | return NUMBER; |
| 163 | } |
| 164 | } |
| 165 | \n return 0; |
Jens Axboe | ded6ec2 | 2014-09-29 13:20:40 -0600 | [diff] [blame] | 166 | [+-/*()^%] return yytext[0]; |
Stephen M. Cameron | 4fe690e | 2014-09-29 12:16:54 -0600 | [diff] [blame] | 167 | |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 168 | . { |
| 169 | yylval.v.has_error = 1; |
| 170 | return NUMBER; |
| 171 | } |
| 172 | %% |
| 173 | |