Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / contrib / seg / segparse.y
blob47e3d398aaef24af015a06d2e41377aaa6977615
1 %{
2 #define YYPARSE_PARAM result /* need this to pass a pointer (void *) to yyparse */
4 #include "postgres.h"
6 #include <math.h>
8 #include "fmgr.h"
9 #include "utils/builtins.h"
10 #include "segdata.h"
13 * Bison doesn't allocate anything that needs to live across parser calls,
14 * so we can easily have it use palloc instead of malloc. This prevents
15 * memory leaks if we error out during parsing. Note this only works with
16 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
17 * if possible, so there's not really much problem anyhow, at least if
18 * you're building with gcc.
20 #define YYMALLOC palloc
21 #define YYFREE pfree
23 extern int seg_yylex(void);
25 extern int significant_digits(char *str); /* defined in seg.c */
27 void seg_yyerror(const char *message);
28 int seg_yyparse(void *result);
30 static float seg_atof(char *value);
32 static char strbuf[25] = {
33 '0', '0', '0', '0', '0',
34 '0', '0', '0', '0', '0',
35 '0', '0', '0', '0', '0',
36 '0', '0', '0', '0', '0',
37 '0', '0', '0', '0', '\0'
42 /* BISON Declarations */
43 %name-prefix="seg_yy"
45 %union {
46 struct BND {
47 float val;
48 char ext;
49 char sigd;
50 } bnd;
51 char * text;
53 %token <text> SEGFLOAT
54 %token <text> RANGE
55 %token <text> PLUMIN
56 %token <text> EXTENSION
57 %type <bnd> boundary
58 %type <bnd> deviation
59 %start range
61 /* Grammar follows */
65 range:
66 boundary PLUMIN deviation {
67 ((SEG *)result)->lower = $1.val - $3.val;
68 ((SEG *)result)->upper = $1.val + $3.val;
69 sprintf(strbuf, "%g", ((SEG *)result)->lower);
70 ((SEG *)result)->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
71 sprintf(strbuf, "%g", ((SEG *)result)->upper);
72 ((SEG *)result)->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
73 ((SEG *)result)->l_ext = '\0';
74 ((SEG *)result)->u_ext = '\0';
77 boundary RANGE boundary {
78 ((SEG *)result)->lower = $1.val;
79 ((SEG *)result)->upper = $3.val;
80 if ( ((SEG *)result)->lower > ((SEG *)result)->upper ) {
81 ereport(ERROR,
82 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
83 errmsg("swapped boundaries: %g is greater than %g",
84 ((SEG *)result)->lower, ((SEG *)result)->upper)));
86 YYERROR;
88 ((SEG *)result)->l_sigd = $1.sigd;
89 ((SEG *)result)->u_sigd = $3.sigd;
90 ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
91 ((SEG *)result)->u_ext = ( $3.ext ? $3.ext : '\0' );
94 boundary RANGE {
95 ((SEG *)result)->lower = $1.val;
96 ((SEG *)result)->upper = HUGE_VAL;
97 ((SEG *)result)->l_sigd = $1.sigd;
98 ((SEG *)result)->u_sigd = 0;
99 ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
100 ((SEG *)result)->u_ext = '-';
103 RANGE boundary {
104 ((SEG *)result)->lower = -HUGE_VAL;
105 ((SEG *)result)->upper = $2.val;
106 ((SEG *)result)->l_sigd = 0;
107 ((SEG *)result)->u_sigd = $2.sigd;
108 ((SEG *)result)->l_ext = '-';
109 ((SEG *)result)->u_ext = ( $2.ext ? $2.ext : '\0' );
112 boundary {
113 ((SEG *)result)->lower = ((SEG *)result)->upper = $1.val;
114 ((SEG *)result)->l_sigd = ((SEG *)result)->u_sigd = $1.sigd;
115 ((SEG *)result)->l_ext = ((SEG *)result)->u_ext = ( $1.ext ? $1.ext : '\0' );
119 boundary:
120 SEGFLOAT {
121 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
122 float val = seg_atof($1);
124 $$.ext = '\0';
125 $$.sigd = significant_digits($1);
126 $$.val = val;
129 EXTENSION SEGFLOAT {
130 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
131 float val = seg_atof($2);
133 $$.ext = $1[0];
134 $$.sigd = significant_digits($2);
135 $$.val = val;
139 deviation:
140 SEGFLOAT {
141 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
142 float val = seg_atof($1);
144 $$.ext = '\0';
145 $$.sigd = significant_digits($1);
146 $$.val = val;
153 static float
154 seg_atof(char *value)
156 Datum datum;
158 datum = DirectFunctionCall1(float4in, CStringGetDatum(value));
159 return DatumGetFloat4(datum);
163 #include "segscan.c"