Blame exp/expression-parser.l

Packit 8930e1
%{
Packit 8930e1
Packit 8930e1
/*
Packit 8930e1
 * (C) Copyright 2014, Stephen M. Cameron.
Packit 8930e1
 *
Packit 8930e1
 *  This program is free software; you can redistribute it and/or modify
Packit 8930e1
 *  it under the terms of the GNU General Public License version 2 as
Packit 8930e1
 *  published by the Free Software Foundation.
Packit 8930e1
 *
Packit 8930e1
 *  This program is distributed in the hope that it will be useful,
Packit 8930e1
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8930e1
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8930e1
 *  GNU General Public License for more details.
Packit 8930e1
 *
Packit 8930e1
 *  You should have received a copy of the GNU General Public License
Packit 8930e1
 *  along with this program; if not, write to the Free Software
Packit 8930e1
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 8930e1
 *
Packit 8930e1
 */
Packit 8930e1
Packit 8930e1
#include <stdio.h>
Packit 8930e1
#include <string.h>
Packit 8930e1
#include "y.tab.h"
Packit 8930e1
Packit 8930e1
#define YYSTYPE PARSER_VALUE_TYPE
Packit 8930e1
Packit 8930e1
extern int lexer_input(char *buffer, unsigned int *nbytes, int buffersize);
Packit 8930e1
Packit 8930e1
#undef YY_INPUT
Packit 8930e1
#define YY_INPUT(buffer, bytes_read, bytes_requested)			\
Packit 8930e1
({									\
Packit 8930e1
	int __ret;							\
Packit 8930e1
	unsigned int __bread = bytes_read;				\
Packit 8930e1
	__ret = lexer_input((buffer), &__bread, (bytes_requested));	\
Packit 8930e1
	bytes_read = __bread;						\
Packit 8930e1
	__ret;								\
Packit 8930e1
})
Packit 8930e1
Packit 8930e1
extern int yyerror(long long *result, double *dresult,
Packit 8930e1
		int *has_error, int *units_specified, const char *msg);
Packit 8930e1
Packit 8930e1
static void __attribute__((unused)) yyunput(int c, char *buf_ptr);
Packit 8930e1
static int __attribute__((unused)) input(void);
Packit 8930e1
Packit 8930e1
/* set by parser -- this is another thing which makes the parser thread-unsafe :(. */
Packit 8930e1
int lexer_value_is_time = 0; /* for determining if "m" suffix means mega- or minutes */
Packit 8930e1
Packit 8930e1
#define set_suffix_value(yylval, i_val, d_val, has_d_val) \
Packit 8930e1
	(yylval).v.dval = (d_val); \
Packit 8930e1
	(yylval).v.ival = (i_val); \
Packit 8930e1
	(yylval).v.has_dval = (has_d_val); \
Packit 8930e1
	(yylval).v.has_error = 0;
Packit 8930e1
Packit 8930e1
%}
Packit 8930e1
Packit 8930e1
%%
Packit 8930e1
Packit 8930e1
Packit 8930e1
[kK]|[kK][bB] 	{
Packit 8930e1
			set_suffix_value(yylval, 1024, 1024.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[Mm][bB]	{
Packit 8930e1
			set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[mM][sS]	{
Packit 8930e1
			set_suffix_value(yylval, 1000, 1000.0, 1);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[uU][sS]	{
Packit 8930e1
			set_suffix_value(yylval, 1, 1.0, 1);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[gG]|[Gg][Bb]	{
Packit 8930e1
			set_suffix_value(yylval, 1024LL * 1024 * 1024, 1024.0 * 1024.0 * 1024, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[tT]|[tT][bB]	{	
Packit 8930e1
			set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024,
Packit 8930e1
						1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[pP]|[pP][bB]	{	
Packit 8930e1
			set_suffix_value(yylval, 1024LL * 1024 * 1024 * 1024 * 1024,
Packit 8930e1
					1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[kK][iI][Bb]	{
Packit 8930e1
			set_suffix_value(yylval, 1000LL, 1000.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[mM][Ii][bB]	{
Packit 8930e1
			set_suffix_value(yylval, 1000000LL, 1000000.0 , 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[gG][iI][Bb]	{
Packit 8930e1
			set_suffix_value(yylval, 1000000000LL, 1000000000.0 , 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[pP][iI][Bb]	{	
Packit 8930e1
			set_suffix_value(yylval, 1000000000000LL, 1000000000000.0 , 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[sS]		{
Packit 8930e1
			set_suffix_value(yylval, 1000000LL, 1000000.0 , 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[mM]		{
Packit 8930e1
			if (!lexer_value_is_time) {
Packit 8930e1
				set_suffix_value(yylval, 1024 * 1024, 1024.0 * 1024.0, 0);
Packit 8930e1
			} else {
Packit 8930e1
				set_suffix_value(yylval, 60LL * 1000000LL, 60.0 * 1000000.0, 0);
Packit 8930e1
			}
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[dD]		{
Packit 8930e1
			set_suffix_value(yylval, 60LL * 60LL * 24LL * 1000000LL,
Packit 8930e1
						60.0 * 60.0 * 24.0 * 1000000.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[hH]		{	
Packit 8930e1
			set_suffix_value(yylval, 60LL * 60LL * 1000000LL,
Packit 8930e1
					60.0 * 60.0 * 1000000.0, 0);
Packit 8930e1
			return SUFFIX;
Packit 8930e1
		}
Packit 8930e1
[ \t] ; /* ignore whitespace */
Packit 8930e1
[#:,].* ; /* ignore comments, and everything after colons and commas */
Packit 8930e1
[0-9]*[.][0-9]+|[0-9]*[.]?[0-9]+[eE][-+]*[0-9]+ {
Packit 8930e1
			int rc;
Packit 8930e1
			double dval;
Packit 8930e1
Packit 8930e1
			rc = sscanf(yytext, "%lf", &dval);
Packit 8930e1
			if (rc == 1) {
Packit 8930e1
				yylval.v.dval = dval;
Packit 8930e1
				yylval.v.ival = (long long) dval;
Packit 8930e1
				yylval.v.has_dval = 1;
Packit 8930e1
				yylval.v.has_error = 0;
Packit 8930e1
				return NUMBER;
Packit 8930e1
			} else {
Packit 8930e1
				yyerror(0, 0, 0, 0, "bad number\n");
Packit 8930e1
				yylval.v.has_error = 1;
Packit 8930e1
				return NUMBER;
Packit 8930e1
			}
Packit 8930e1
		}
Packit 8930e1
0x[0-9a-fA-F]+ {
Packit 8930e1
		int rc, intval;
Packit 8930e1
		rc = sscanf(yytext, "%x", &intval);
Packit 8930e1
		if (rc == 1) {
Packit 8930e1
			yylval.v.ival = intval;
Packit 8930e1
			yylval.v.dval = (double) intval;
Packit 8930e1
			yylval.v.has_dval = 0;
Packit 8930e1
			yylval.v.has_error = 0;
Packit 8930e1
			return NUMBER;
Packit 8930e1
		} else {
Packit 8930e1
			yyerror(0, 0, 0, 0, "bad number\n");
Packit 8930e1
			yylval.v.has_error = 1;
Packit 8930e1
			return NUMBER;
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
[0-9]+	{
Packit 8930e1
		int rc, intval;
Packit 8930e1
		rc = sscanf(yytext, "%d", &intval);
Packit 8930e1
		if (rc == 1) {
Packit 8930e1
			yylval.v.ival = intval;
Packit 8930e1
			yylval.v.dval = (double) intval;
Packit 8930e1
			yylval.v.has_dval = 0;
Packit 8930e1
			yylval.v.has_error = 0;
Packit 8930e1
			return NUMBER;
Packit 8930e1
		} else {
Packit 8930e1
			yyerror(0, 0, 0, 0, "bad number\n");
Packit 8930e1
			yylval.v.has_error = 1;
Packit 8930e1
			return NUMBER;
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
\n	return 0;
Packit 8930e1
[+-/*()^%]	return yytext[0];
Packit 8930e1
Packit 8930e1
.	{
Packit 8930e1
		yylval.v.has_error = 1;
Packit 8930e1
		return NUMBER;	
Packit 8930e1
	}
Packit 8930e1
%%
Packit 8930e1