blob: 50bd38320bbaedc2eec487d0e8d09b95d95faa7b [file] [log] [blame]
Stephen M. Cameron88b635b2014-09-29 12:10:49 -06001%{
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 Axboee2e70322014-10-14 21:12:22 -060027extern int lexer_input(char *buffer, unsigned int *nbytes, int buffersize);
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060028
29#undef YY_INPUT
Jens Axboe4ce8aba2014-10-07 09:12:40 -060030#define YY_INPUT(buffer, bytes_read, bytes_requested) \
31({ \
32 int __ret; \
Jens Axboee2e70322014-10-14 21:12:22 -060033 unsigned int __bread = bytes_read; \
Jens Axboee3404262014-10-07 09:09:57 -060034 __ret = lexer_input((buffer), &__bread, (bytes_requested)); \
35 bytes_read = __bread; \
Jens Axboe4ce8aba2014-10-07 09:12:40 -060036 __ret; \
Jens Axboee3404262014-10-07 09:09:57 -060037})
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060038
39extern int yyerror(long long *result, double *dresult,
Stephen M. Camerona22867a2014-09-30 11:21:20 -050040 int *has_error, int *units_specified, const char *msg);
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060041
Jens Axboe4ce8aba2014-10-07 09:12:40 -060042static void __attribute__((unused)) yyunput(int c, char *buf_ptr);
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060043static int __attribute__((unused)) input(void);
44
Stephen M. Cameron79dc9142014-11-10 20:31:26 -070045/* set by parser -- this is another thing which makes the parser thread-unsafe :(. */
46int lexer_value_is_time = 0; /* for determining if "m" suffix means mega- or minutes */
47
Stephen M. Cameronc3805eb2014-09-29 12:15:35 -060048#define set_suffix_value(yylval, i_val, d_val, has_d_val) \
49 (yylval).v.dval = (d_val); \
50 (yylval).v.ival = (i_val); \
51 (yylval).v.has_dval = (has_d_val); \
52 (yylval).v.has_error = 0;
53
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060054%}
55
56%%
57
58
Stephen M. Cameronc3805eb2014-09-29 12:15:35 -060059[kK]|[kK][bB] {
60 set_suffix_value(yylval, 1024, 1024.0, 0);
61 return SUFFIX;
62 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -070063[Mm][bB] {
Stephen M. Cameronc3805eb2014-09-29 12:15:35 -060064 set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
65 return SUFFIX;
66 }
67[mM][sS] {
68 set_suffix_value(yylval, 1000, 1000.0, 1);
69 return SUFFIX;
70 }
71[uU][sS] {
72 set_suffix_value(yylval, 1, 1.0, 1);
73 return SUFFIX;
74 }
75[gG]|[Gg][Bb] {
76 set_suffix_value(yylval, 1024LL * 1024 * 1024, 1024.0 * 1024.0 * 1024, 0);
77 return SUFFIX;
78 }
79[tT]|[tT][bB] {
80 set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024,
81 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024, 0);
82 return SUFFIX;
83 }
84[pP]|[pP][bB] {
85 set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024 * 1024,
86 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0, 0);
87 return SUFFIX;
88 }
89[kK][iI][Bb] {
90 set_suffix_value(yylval, 1000LL, 1000.0, 0);
91 return SUFFIX;
92 }
93[mM][Ii][bB] {
94 set_suffix_value(yylval, 1000000LL, 1000000.0 , 0);
95 return SUFFIX;
96 }
97[gG][iI][Bb] {
98 set_suffix_value(yylval, 1000000000LL, 1000000000.0 , 0);
99 return SUFFIX;
100 }
101[pP][iI][Bb] {
102 set_suffix_value(yylval, 1000000000000LL, 1000000000000.0 , 0);
103 return SUFFIX;
104 }
105[sS] {
106 set_suffix_value(yylval, 1000000LL, 1000000.0 , 0);
107 return SUFFIX;
108 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700109[mM] {
110 if (!lexer_value_is_time) {
111 set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
112 } else {
113 set_suffix_value(yylval, 60LL * 1000000LL, 60.0 * 1000000.0, 0);
114 }
115 return SUFFIX;
116 }
Stephen M. Cameronc3805eb2014-09-29 12:15:35 -0600117[dD] {
118 set_suffix_value(yylval, 60LL * 60LL * 24LL * 1000000LL,
119 60.0 * 60.0 * 24.0 * 1000000.0, 0);
120 return SUFFIX;
121 }
122[hH] {
123 set_suffix_value(yylval, 60LL * 60LL * 1000000LL,
124 60.0 * 60.0 * 1000000.0, 0);
125 return SUFFIX;
126 }
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600127[ \t] ; /* ignore whitespace */
Stephen M. Cameron303b5ca2014-09-30 08:44:24 -0500128[#:,].* ; /* ignore comments, and everything after colons and commas */
Stephen M. Cameron5abaf3e2014-10-02 20:31:11 -0500129[0-9]*[.][0-9]+|[0-9]*[.]?[0-9]+[eE][-+]*[0-9]+ {
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600130 int rc;
131 double dval;
132
133 rc = sscanf(yytext, "%lf", &dval);
134 if (rc == 1) {
135 yylval.v.dval = dval;
136 yylval.v.ival = (long long) dval;
137 yylval.v.has_dval = 1;
138 yylval.v.has_error = 0;
139 return NUMBER;
140 } else {
Stephen M. Camerona22867a2014-09-30 11:21:20 -0500141 yyerror(0, 0, 0, 0, "bad number\n");
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600142 yylval.v.has_error = 1;
143 return NUMBER;
144 }
145 }
1460x[0-9a-fA-F]+ {
147 int rc, intval;
148 rc = sscanf(yytext, "%x", &intval);
149 if (rc == 1) {
150 yylval.v.ival = intval;
151 yylval.v.dval = (double) intval;
152 yylval.v.has_dval = 0;
153 yylval.v.has_error = 0;
154 return NUMBER;
155 } else {
Stephen M. Camerona22867a2014-09-30 11:21:20 -0500156 yyerror(0, 0, 0, 0, "bad number\n");
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600157 yylval.v.has_error = 1;
158 return NUMBER;
159 }
160 }
161[0-9]+ {
162 int rc, intval;
163 rc = sscanf(yytext, "%d", &intval);
164 if (rc == 1) {
165 yylval.v.ival = intval;
166 yylval.v.dval = (double) intval;
167 yylval.v.has_dval = 0;
168 yylval.v.has_error = 0;
169 return NUMBER;
170 } else {
Stephen M. Camerona22867a2014-09-30 11:21:20 -0500171 yyerror(0, 0, 0, 0, "bad number\n");
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600172 yylval.v.has_error = 1;
173 return NUMBER;
174 }
175 }
176\n return 0;
Jens Axboeded6ec22014-09-29 13:20:40 -0600177[+-/*()^%] return yytext[0];
Stephen M. Cameron4fe690e2014-09-29 12:16:54 -0600178
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600179. {
180 yylval.v.has_error = 1;
181 return NUMBER;
182 }
183%%
184