2000-12-11 21:40:33 +01:00
|
|
|
%{
|
2013-07-29 16:42:37 +02:00
|
|
|
/* contrib/seg/segparse.y */
|
2010-11-23 21:27:50 +01:00
|
|
|
|
2002-07-30 19:32:10 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-12-11 21:40:33 +01:00
|
|
|
#include <math.h>
|
2002-07-30 19:32:10 +02:00
|
|
|
|
2004-10-24 23:47:07 +02:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/builtins.h"
|
2015-03-26 19:03:19 +01:00
|
|
|
|
2002-07-30 19:32:10 +02:00
|
|
|
#include "segdata.h"
|
2001-06-18 23:38:02 +02:00
|
|
|
|
2008-09-02 22:37:55 +02:00
|
|
|
/*
|
|
|
|
* Bison doesn't allocate anything that needs to live across parser calls,
|
|
|
|
* so we can easily have it use palloc instead of malloc. This prevents
|
|
|
|
* memory leaks if we error out during parsing. Note this only works with
|
|
|
|
* bison >= 2.0. However, in bison 1.875 the default is to use alloca()
|
|
|
|
* if possible, so there's not really much problem anyhow, at least if
|
|
|
|
* you're building with gcc.
|
|
|
|
*/
|
|
|
|
#define YYMALLOC palloc
|
|
|
|
#define YYFREE pfree
|
|
|
|
|
2017-10-31 15:34:31 +01:00
|
|
|
static float seg_atof(const char *value);
|
2000-12-11 21:40:33 +01:00
|
|
|
|
2011-11-01 20:50:00 +01:00
|
|
|
static char strbuf[25] = {
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '0',
|
|
|
|
'0', '0', '0', '0', '\0'
|
|
|
|
};
|
2000-12-11 21:40:33 +01:00
|
|
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
/* BISON Declarations */
|
2013-07-29 16:42:37 +02:00
|
|
|
%parse-param {SEG *result}
|
2008-11-26 09:45:12 +01:00
|
|
|
%expect 0
|
2014-05-29 01:21:01 +02:00
|
|
|
%name-prefix="seg_yy"
|
2006-03-07 02:03:12 +01:00
|
|
|
|
2000-12-11 21:40:33 +01:00
|
|
|
%union {
|
2011-11-01 20:50:00 +01:00
|
|
|
struct BND {
|
|
|
|
float val;
|
|
|
|
char ext;
|
|
|
|
char sigd;
|
|
|
|
} bnd;
|
|
|
|
char * text;
|
2000-12-11 21:40:33 +01:00
|
|
|
}
|
2004-09-14 06:21:38 +02:00
|
|
|
%token <text> SEGFLOAT
|
2000-12-11 21:40:33 +01:00
|
|
|
%token <text> RANGE
|
|
|
|
%token <text> PLUMIN
|
|
|
|
%token <text> EXTENSION
|
|
|
|
%type <bnd> boundary
|
|
|
|
%type <bnd> deviation
|
|
|
|
%start range
|
|
|
|
|
|
|
|
/* Grammar follows */
|
|
|
|
%%
|
|
|
|
|
|
|
|
|
2011-11-01 20:50:00 +01:00
|
|
|
range: boundary PLUMIN deviation
|
|
|
|
{
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower = $1.val - $3.val;
|
|
|
|
result->upper = $1.val + $3.val;
|
|
|
|
sprintf(strbuf, "%g", result->lower);
|
|
|
|
result->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
|
|
|
|
sprintf(strbuf, "%g", result->upper);
|
|
|
|
result->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
|
|
|
|
result->l_ext = '\0';
|
|
|
|
result->u_ext = '\0';
|
2011-11-01 20:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
| boundary RANGE boundary
|
|
|
|
{
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower = $1.val;
|
|
|
|
result->upper = $3.val;
|
|
|
|
if ( result->lower > result->upper ) {
|
2011-11-01 20:50:00 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("swapped boundaries: %g is greater than %g",
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower, result->upper)));
|
2011-11-01 20:50:00 +01:00
|
|
|
|
|
|
|
YYERROR;
|
|
|
|
}
|
2013-07-29 16:42:37 +02:00
|
|
|
result->l_sigd = $1.sigd;
|
|
|
|
result->u_sigd = $3.sigd;
|
|
|
|
result->l_ext = ( $1.ext ? $1.ext : '\0' );
|
|
|
|
result->u_ext = ( $3.ext ? $3.ext : '\0' );
|
2011-11-01 20:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
| boundary RANGE
|
|
|
|
{
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower = $1.val;
|
|
|
|
result->upper = HUGE_VAL;
|
|
|
|
result->l_sigd = $1.sigd;
|
|
|
|
result->u_sigd = 0;
|
|
|
|
result->l_ext = ( $1.ext ? $1.ext : '\0' );
|
|
|
|
result->u_ext = '-';
|
2011-11-01 20:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
| RANGE boundary
|
|
|
|
{
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower = -HUGE_VAL;
|
|
|
|
result->upper = $2.val;
|
|
|
|
result->l_sigd = 0;
|
|
|
|
result->u_sigd = $2.sigd;
|
|
|
|
result->l_ext = '-';
|
|
|
|
result->u_ext = ( $2.ext ? $2.ext : '\0' );
|
2011-11-01 20:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
| boundary
|
|
|
|
{
|
2013-07-29 16:42:37 +02:00
|
|
|
result->lower = result->upper = $1.val;
|
|
|
|
result->l_sigd = result->u_sigd = $1.sigd;
|
|
|
|
result->l_ext = result->u_ext = ( $1.ext ? $1.ext : '\0' );
|
2011-11-01 20:50:00 +01:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
boundary: SEGFLOAT
|
|
|
|
{
|
|
|
|
/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
|
|
|
|
float val = seg_atof($1);
|
|
|
|
|
|
|
|
$$.ext = '\0';
|
|
|
|
$$.sigd = significant_digits($1);
|
|
|
|
$$.val = val;
|
|
|
|
}
|
|
|
|
| EXTENSION SEGFLOAT
|
|
|
|
{
|
|
|
|
/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
|
|
|
|
float val = seg_atof($2);
|
|
|
|
|
|
|
|
$$.ext = $1[0];
|
|
|
|
$$.sigd = significant_digits($2);
|
|
|
|
$$.val = val;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
deviation: SEGFLOAT
|
|
|
|
{
|
|
|
|
/* temp variable avoids a gcc 3.3.x bug on Sparc64 */
|
|
|
|
float val = seg_atof($1);
|
|
|
|
|
|
|
|
$$.ext = '\0';
|
|
|
|
$$.sigd = significant_digits($1);
|
|
|
|
$$.val = val;
|
|
|
|
}
|
|
|
|
;
|
2000-12-11 21:40:33 +01:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
|
2006-06-01 04:39:59 +02:00
|
|
|
static float
|
2017-10-31 15:34:31 +01:00
|
|
|
seg_atof(const char *value)
|
2004-10-24 23:47:07 +02:00
|
|
|
{
|
|
|
|
Datum datum;
|
2000-12-11 21:40:33 +01:00
|
|
|
|
2004-10-24 23:47:07 +02:00
|
|
|
datum = DirectFunctionCall1(float4in, CStringGetDatum(value));
|
|
|
|
return DatumGetFloat4(datum);
|
2000-12-11 21:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-01 23:52:34 +01:00
|
|
|
#include "segscan.c"
|