This repository was archived by the owner on Dec 27, 2020. It is now read-only.
forked from df7cb/postgresql-unit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitparse.y
150 lines (132 loc) · 3.11 KB
/
unitparse.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright (C) 2016-2018 Christoph Berg
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
%{
#include <strings.h> /* bzero */
#include <math.h> /* pow */
#include "unit.h"
/* flex/bison prototypes */
int yylex (void);
struct yyunit_buffer_state *yyunit_scan_string(char *str);
void yyunit_delete_buffer(struct yyunit_buffer_state *buffer);
void yyerror (char const *s);
static UnitShift *unit_parse_result; /* parsing result gets stored here */
%}
%define parse.error verbose
%define api.prefix {yyunit}
%define api.value.type union
%token <double> DOUBLE
%token <UnitShift> UNIT_SHIFT
%token <int> EXPONENT SUPER_SIGN SUPER
%token ERR
%type <UnitShift> input expr simple_expr
%type <double> number
%type <int> exponent super
%left '+' '-'
%left '/'
%left '*' /* * binds stronger than / */
%left UMINUS /* unary minus */
%%
input:
expr {
*unit_parse_result = $1;
}
;
expr:
simple_expr
/* accept only simple expressions after unary sign to disambiguate
* (-N)N from -(NN): (-273.15)°C is not the same as -(273.15°C) */
| '+' simple_expr %prec UMINUS {
$$ = $2;
$$.shift = 0.0;
}
| '-' simple_expr %prec UMINUS {
$$ = $2;
$$.unit.value = -$$.unit.value;
$$.shift = 0.0;
}
| expr exponent {
int i;
if ($2 != 1) {
$$.unit.value = pow($1.unit.value, $2);
for (i = 0; i < N_UNITS; i++)
$$.unit.units[i] = $1.unit.units[i] * $2;
} else {
$$ = $1;
}
$$.shift = 0.0;
}
| expr '+' expr {
unit_add_internal(&$1.unit, &$3.unit, &$$.unit);
$$.shift = 0.0;
}
| expr '-' expr {
unit_sub_internal(&$1.unit, &$3.unit, &$$.unit);
$$.shift = 0.0;
}
| expr expr %prec '*' {
unit_mult_internal(&$1.unit, &$2.unit, &$$.unit);
if ($2.shift != 0.0) /* avoid shift to not destroy -0 */
$$.unit.value += $2.shift; /* shift is evaluated exactly here */
$$.shift = 0.0;
}
| expr '*' expr {
unit_mult_internal(&$1.unit, &$3.unit, &$$.unit);
$$.shift = 0.0;
}
| expr '/' expr {
unit_div_internal(&$1.unit, &$3.unit, &$$.unit);
$$.shift = 0.0;
}
| '/' expr {
Unit nominator = { 1.0, {0} };
unit_div_internal(&nominator, &$2.unit, &$$.unit);
$$.shift = 0.0;
}
;
simple_expr:
number {
$$.unit.value = $1;
memset(&$$.unit.units, 0, N_UNITS);
$$.shift = 0.0;
}
| UNIT_SHIFT
| '(' expr ')' {
$$ = $2;
$$.shift = 0.0;
}
number:
DOUBLE
| DOUBLE '|' DOUBLE {
$$ = $1 / $3;
}
exponent:
EXPONENT
| SUPER_SIGN super { $$ = $1 * $2; }
| super
;
super:
SUPER
| SUPER super { $$ = 10 * $1 + $2; }
;
%%
/* parse a given string and return the result via the second argument */
int
unit_parse (char *s, UnitShift *unit_shift)
{
struct yyunit_buffer_state *buf;
int ret;
unit_parse_result = unit_shift;
buf = yyunit_scan_string(s);
ret = yyunitparse();
yyunit_delete_buffer(buf);
return ret;
}