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 | |
| 27 | extern int lexer_input(char* buffer, int *nbytes, int buffersize); |
| 28 | |
| 29 | #undef YY_INPUT |
| 30 | #define YY_INPUT(buffer, bytes_read, bytes_requested) \ |
| 31 | lexer_input((buffer), &(bytes_read), (bytes_requested)) |
| 32 | |
| 33 | extern int yyerror(long long *result, double *dresult, |
| 34 | int *has_error, int *bye, const char *msg); |
| 35 | |
| 36 | static void __attribute__((unused)) yyunput(int c,char *buf_ptr); |
| 37 | static int __attribute__((unused)) input(void); |
| 38 | |
Stephen M. Cameron | c3805eb | 2014-09-29 12:15:35 -0600 | [diff] [blame] | 39 | #define set_suffix_value(yylval, i_val, d_val, has_d_val) \ |
| 40 | (yylval).v.dval = (d_val); \ |
| 41 | (yylval).v.ival = (i_val); \ |
| 42 | (yylval).v.has_dval = (has_d_val); \ |
| 43 | (yylval).v.has_error = 0; |
| 44 | |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 45 | %} |
| 46 | |
| 47 | %% |
| 48 | |
| 49 | |
Stephen M. Cameron | c3805eb | 2014-09-29 12:15:35 -0600 | [diff] [blame] | 50 | bye return BYE; |
| 51 | [kK]|[kK][bB] { |
| 52 | set_suffix_value(yylval, 1024, 1024.0, 0); |
| 53 | return SUFFIX; |
| 54 | } |
| 55 | [Mm]|[Mm][bB] { |
| 56 | set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0); |
| 57 | return SUFFIX; |
| 58 | } |
| 59 | [mM][sS] { |
| 60 | set_suffix_value(yylval, 1000, 1000.0, 1); |
| 61 | return SUFFIX; |
| 62 | } |
| 63 | [uU][sS] { |
| 64 | set_suffix_value(yylval, 1, 1.0, 1); |
| 65 | return SUFFIX; |
| 66 | } |
| 67 | [gG]|[Gg][Bb] { |
| 68 | set_suffix_value(yylval, 1024LL * 1024 * 1024, 1024.0 * 1024.0 * 1024, 0); |
| 69 | return SUFFIX; |
| 70 | } |
| 71 | [tT]|[tT][bB] { |
| 72 | set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024, |
| 73 | 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024, 0); |
| 74 | return SUFFIX; |
| 75 | } |
| 76 | [pP]|[pP][bB] { |
| 77 | set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024 * 1024, |
| 78 | 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0, 0); |
| 79 | return SUFFIX; |
| 80 | } |
| 81 | [kK][iI][Bb] { |
| 82 | set_suffix_value(yylval, 1000LL, 1000.0, 0); |
| 83 | return SUFFIX; |
| 84 | } |
| 85 | [mM][Ii][bB] { |
| 86 | set_suffix_value(yylval, 1000000LL, 1000000.0 , 0); |
| 87 | return SUFFIX; |
| 88 | } |
| 89 | [gG][iI][Bb] { |
| 90 | set_suffix_value(yylval, 1000000000LL, 1000000000.0 , 0); |
| 91 | return SUFFIX; |
| 92 | } |
| 93 | [pP][iI][Bb] { |
| 94 | set_suffix_value(yylval, 1000000000000LL, 1000000000000.0 , 0); |
| 95 | return SUFFIX; |
| 96 | } |
| 97 | [sS] { |
| 98 | set_suffix_value(yylval, 1000000LL, 1000000.0 , 0); |
| 99 | return SUFFIX; |
| 100 | } |
| 101 | [dD] { |
| 102 | set_suffix_value(yylval, 60LL * 60LL * 24LL * 1000000LL, |
| 103 | 60.0 * 60.0 * 24.0 * 1000000.0, 0); |
| 104 | return SUFFIX; |
| 105 | } |
| 106 | [hH] { |
| 107 | set_suffix_value(yylval, 60LL * 60LL * 1000000LL, |
| 108 | 60.0 * 60.0 * 1000000.0, 0); |
| 109 | return SUFFIX; |
| 110 | } |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 111 | [ \t] ; /* ignore whitespace */ |
Stephen M. Cameron | 303b5ca | 2014-09-30 08:44:24 -0500 | [diff] [blame^] | 112 | [#:,].* ; /* ignore comments, and everything after colons and commas */ |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 113 | [0-9]*[.][0-9]+ { |
| 114 | int rc; |
| 115 | double dval; |
| 116 | |
| 117 | rc = sscanf(yytext, "%lf", &dval); |
| 118 | if (rc == 1) { |
| 119 | yylval.v.dval = dval; |
| 120 | yylval.v.ival = (long long) dval; |
| 121 | yylval.v.has_dval = 1; |
| 122 | yylval.v.has_error = 0; |
| 123 | return NUMBER; |
| 124 | } else { |
| 125 | yyerror(0, 0, 0, 0, "bad number\n"); |
| 126 | yylval.v.has_error = 1; |
| 127 | return NUMBER; |
| 128 | } |
| 129 | } |
| 130 | 0x[0-9a-fA-F]+ { |
| 131 | int rc, intval; |
| 132 | rc = sscanf(yytext, "%x", &intval); |
| 133 | if (rc == 1) { |
| 134 | yylval.v.ival = intval; |
| 135 | yylval.v.dval = (double) intval; |
| 136 | yylval.v.has_dval = 0; |
| 137 | yylval.v.has_error = 0; |
| 138 | return NUMBER; |
| 139 | } else { |
| 140 | yyerror(0, 0, 0, 0, "bad number\n"); |
| 141 | yylval.v.has_error = 1; |
| 142 | return NUMBER; |
| 143 | } |
| 144 | } |
| 145 | [0-9]+ { |
| 146 | int rc, intval; |
| 147 | rc = sscanf(yytext, "%d", &intval); |
| 148 | if (rc == 1) { |
| 149 | yylval.v.ival = intval; |
| 150 | yylval.v.dval = (double) intval; |
| 151 | yylval.v.has_dval = 0; |
| 152 | yylval.v.has_error = 0; |
| 153 | return NUMBER; |
| 154 | } else { |
| 155 | yyerror(0, 0, 0, 0, "bad number\n"); |
| 156 | yylval.v.has_error = 1; |
| 157 | return NUMBER; |
| 158 | } |
| 159 | } |
| 160 | \n return 0; |
Jens Axboe | ded6ec2 | 2014-09-29 13:20:40 -0600 | [diff] [blame] | 161 | [+-/*()^%] return yytext[0]; |
Stephen M. Cameron | 4fe690e | 2014-09-29 12:16:54 -0600 | [diff] [blame] | 162 | |
Stephen M. Cameron | 88b635b | 2014-09-29 12:10:49 -0600 | [diff] [blame] | 163 | . { |
| 164 | yylval.v.has_error = 1; |
| 165 | return NUMBER; |
| 166 | } |
| 167 | %% |
| 168 | |