]> granicus.if.org Git - postgresql/blob - contrib/seg/segparse.y
Make all our flex and bison files use %option prefix or %name-prefix
[postgresql] / contrib / seg / segparse.y
1 %{
2 #define YYPARSE_PARAM result  /* need this to pass a pointer (void *) to yyparse */
3   
4 #include "postgres.h"
5
6 #include <math.h>
7
8 #include "fmgr.h"
9 #include "utils/builtins.h"
10 #include "segdata.h"
11
12   extern int seg_yylex(void);
13
14   extern int significant_digits( char *str );    /* defined in seg.c */
15   
16   void seg_yyerror(const char *message);
17   int seg_yyparse(void *result);
18
19   float seg_atof( char *value );
20
21   long threshold;
22   char strbuf[25] = {
23     '0', '0', '0', '0', '0',
24     '0', '0', '0', '0', '0',
25     '0', '0', '0', '0', '0',
26     '0', '0', '0', '0', '0',
27     '0', '0', '0', '0', '\0'
28   };
29
30 %}
31
32 /* BISON Declarations */
33 %name-prefix="seg_yy"
34
35 %union {
36   struct BND {
37     float val;
38     char  ext;
39     char  sigd;
40   } bnd;
41   char * text;
42 }
43 %token <text> SEGFLOAT
44 %token <text> RANGE
45 %token <text> PLUMIN
46 %token <text> EXTENSION
47 %type  <bnd>  boundary
48 %type  <bnd>  deviation
49 %start range
50
51 /* Grammar follows */
52 %%
53
54
55 range:
56           boundary PLUMIN deviation {
57             ((SEG *)result)->lower = $1.val - $3.val;
58             ((SEG *)result)->upper = $1.val + $3.val;
59             sprintf(strbuf, "%g", ((SEG *)result)->lower);
60             ((SEG *)result)->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
61             sprintf(strbuf, "%g", ((SEG *)result)->upper);
62             ((SEG *)result)->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
63             ((SEG *)result)->l_ext = '\0';
64             ((SEG *)result)->u_ext = '\0';
65           }
66       |
67           boundary RANGE boundary {
68             ((SEG *)result)->lower = $1.val;
69             ((SEG *)result)->upper = $3.val;
70             if ( ((SEG *)result)->lower > ((SEG *)result)->upper ) {
71               ereport(ERROR,
72                                   (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
73                                    errmsg("swapped boundaries: %g is greater than %g",
74                                                   ((SEG *)result)->lower, ((SEG *)result)->upper)));
75
76               YYERROR;
77             }
78             ((SEG *)result)->l_sigd = $1.sigd;
79             ((SEG *)result)->u_sigd = $3.sigd;
80             ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
81             ((SEG *)result)->u_ext = ( $3.ext ? $3.ext : '\0' );
82           }
83       |
84           boundary RANGE {
85             ((SEG *)result)->lower = $1.val;
86             ((SEG *)result)->upper = HUGE_VAL;
87             ((SEG *)result)->l_sigd = $1.sigd;
88             ((SEG *)result)->u_sigd = 0;
89             ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
90             ((SEG *)result)->u_ext = '-';
91           }
92       |
93           RANGE boundary {
94             ((SEG *)result)->lower = -HUGE_VAL;
95             ((SEG *)result)->upper = $2.val;
96             ((SEG *)result)->l_sigd = 0;
97             ((SEG *)result)->u_sigd = $2.sigd;
98             ((SEG *)result)->l_ext = '-';
99             ((SEG *)result)->u_ext = ( $2.ext ? $2.ext : '\0' );
100           }
101       |
102           boundary {
103             ((SEG *)result)->lower = ((SEG *)result)->upper = $1.val;
104             ((SEG *)result)->l_sigd = ((SEG *)result)->u_sigd = $1.sigd;
105             ((SEG *)result)->l_ext = ((SEG *)result)->u_ext = ( $1.ext ? $1.ext : '\0' );
106           }
107       ;
108
109 boundary:
110           SEGFLOAT {
111              $$.ext = '\0';
112              $$.sigd = significant_digits($1);
113              $$.val = seg_atof($1);
114           }
115       | 
116           EXTENSION SEGFLOAT {
117              $$.ext = $1[0];
118              $$.sigd = significant_digits($2);
119              $$.val = seg_atof($2);
120           }
121       ;
122
123 deviation:
124           SEGFLOAT {
125              $$.ext = '\0';
126              $$.sigd = significant_digits($1);
127              $$.val = seg_atof($1);
128           }
129       ;
130
131 %%
132
133
134 float
135 seg_atof(char *value)
136 {
137         Datum datum;
138
139         datum = DirectFunctionCall1(float4in, CStringGetDatum(value));
140         return DatumGetFloat4(datum);
141 }
142
143
144 #include "segscan.c"