More sensible character_octet_length
[PostgreSQL.git] / contrib / seg / segparse.y
blobca351c661b3ca25124bae1b238db39b6e0d6f825
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 %expect 0
44 %name-prefix="seg_yy"
46 %union {
47 struct BND {
48 float val;
49 char ext;
50 char sigd;
51 } bnd;
52 char * text;
54 %token <text> SEGFLOAT
55 %token <text> RANGE
56 %token <text> PLUMIN
57 %token <text> EXTENSION
58 %type <bnd> boundary
59 %type <bnd> deviation
60 %start range
62 /* Grammar follows */
66 range:
67 boundary PLUMIN deviation {
68 ((SEG *)result)->lower = $1.val - $3.val;
69 ((SEG *)result)->upper = $1.val + $3.val;
70 sprintf(strbuf, "%g", ((SEG *)result)->lower);
71 ((SEG *)result)->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
72 sprintf(strbuf, "%g", ((SEG *)result)->upper);
73 ((SEG *)result)->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
74 ((SEG *)result)->l_ext = '\0';
75 ((SEG *)result)->u_ext = '\0';
78 boundary RANGE boundary {
79 ((SEG *)result)->lower = $1.val;
80 ((SEG *)result)->upper = $3.val;
81 if ( ((SEG *)result)->lower > ((SEG *)result)->upper ) {
82 ereport(ERROR,
83 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
84 errmsg("swapped boundaries: %g is greater than %g",
85 ((SEG *)result)->lower, ((SEG *)result)->upper)));
87 YYERROR;
89 ((SEG *)result)->l_sigd = $1.sigd;
90 ((SEG *)result)->u_sigd = $3.sigd;
91 ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
92 ((SEG *)result)->u_ext = ( $3.ext ? $3.ext : '\0' );
95 boundary RANGE {
96 ((SEG *)result)->lower = $1.val;
97 ((SEG *)result)->upper = HUGE_VAL;
98 ((SEG *)result)->l_sigd = $1.sigd;
99 ((SEG *)result)->u_sigd = 0;
100 ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
101 ((SEG *)result)->u_ext = '-';
104 RANGE boundary {
105 ((SEG *)result)->lower = -HUGE_VAL;
106 ((SEG *)result)->upper = $2.val;
107 ((SEG *)result)->l_sigd = 0;
108 ((SEG *)result)->u_sigd = $2.sigd;
109 ((SEG *)result)->l_ext = '-';
110 ((SEG *)result)->u_ext = ( $2.ext ? $2.ext : '\0' );
113 boundary {
114 ((SEG *)result)->lower = ((SEG *)result)->upper = $1.val;
115 ((SEG *)result)->l_sigd = ((SEG *)result)->u_sigd = $1.sigd;
116 ((SEG *)result)->l_ext = ((SEG *)result)->u_ext = ( $1.ext ? $1.ext : '\0' );
120 boundary:
121 SEGFLOAT {
122 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
123 float val = seg_atof($1);
125 $$.ext = '\0';
126 $$.sigd = significant_digits($1);
127 $$.val = val;
130 EXTENSION SEGFLOAT {
131 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
132 float val = seg_atof($2);
134 $$.ext = $1[0];
135 $$.sigd = significant_digits($2);
136 $$.val = val;
140 deviation:
141 SEGFLOAT {
142 /* temp variable avoids a gcc 3.3.x bug on Sparc64 */
143 float val = seg_atof($1);
145 $$.ext = '\0';
146 $$.sigd = significant_digits($1);
147 $$.val = val;
154 static float
155 seg_atof(char *value)
157 Datum datum;
159 datum = DirectFunctionCall1(float4in, CStringGetDatum(value));
160 return DatumGetFloat4(datum);
164 #include "segscan.c"