16e38732360076a49f44b40a9d0f7f05438e0c92
[fio.git] / exp / expression-parser.l
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, size_t *nbytes, int buffersize);
28
29 #undef YY_INPUT
30 #define YY_INPUT(buffer, bytes_read, bytes_requested)                   \
31 ({                                                                      \
32         int __ret;                                                      \
33         size_t __bread = bytes_read;                                    \
34         __ret = lexer_input((buffer), &__bread, (bytes_requested));     \
35         bytes_read = __bread;                                           \
36         __ret;                                                          \
37 })
38
39 extern int yyerror(long long *result, double *dresult,
40                 int *has_error, int *units_specified, const char *msg);
41
42 static void __attribute__((unused)) yyunput(int c, char *buf_ptr);
43 static int __attribute__((unused)) input(void);
44
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
51 %}
52
53 %%
54
55
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                 }
116 [ \t] ; /* ignore whitespace */
117 [#:,].* ; /* ignore comments, and everything after colons and commas */
118 [0-9]*[.][0-9]+|[0-9]*[.]?[0-9]+[eE][-+]*[0-9]+ {
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 {
130                                 yyerror(0, 0, 0, 0, "bad number\n");
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 {
145                         yyerror(0, 0, 0, 0, "bad number\n");
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 {
160                         yyerror(0, 0, 0, 0, "bad number\n");
161                         yylval.v.has_error = 1;
162                         return NUMBER;
163                 }
164         }
165 \n      return 0;
166 [+-/*()^%]      return yytext[0];
167
168 .       {
169                 yylval.v.has_error = 1;
170                 return NUMBER;  
171         }
172 %%
173