2 /* NdBox = [(lowerleft),(upperright)] */
3 /* [(xLL(1)...xLL(N)),(xUR(1)...xUR(n))] */
7 #define YYPARSE_PARAM result /* need this to pass a pointer (void *) to yyparse */
16 * Bison doesn't allocate anything that needs to live across parser calls,
17 * so we can easily have it use palloc instead of malloc. This prevents
18 * memory leaks if we error out during parsing. Note this only works with
19 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
20 * if possible, so there's not really much problem anyhow, at least if
21 * you're building with gcc.
23 #define YYMALLOC palloc
26 extern
int cube_yylex
(void);
29 static int scanbuflen
;
31 void cube_yyerror
(const char *message
);
32 int cube_yyparse
(void *result
);
34 static int delim_count
(char *s
, char delim
);
35 static NDBOX
* write_box
(unsigned int dim
, char *str1
, char *str2
);
36 static NDBOX
* write_point_as_box
(char *s
, int dim
);
40 /* BISON Declarations */
42 %name
-prefix
="cube_yy"
44 %token CUBEFLOAT O_PAREN C_PAREN O_BRACKET C_BRACKET COMMA
51 O_BRACKET paren_list COMMA paren_list C_BRACKET
{
55 dim
= delim_count
($2, ',') + 1;
56 if
( (delim_count
($4, ',') + 1) != dim
) {
58 (errcode
(ERRCODE_SYNTAX_ERROR
),
59 errmsg
("bad cube representation"),
60 errdetail
("Different point dimensions in (%s) and (%s).",
64 if
(dim
> CUBE_MAX_DIM
) {
66 (errcode
(ERRCODE_SYNTAX_ERROR
),
67 errmsg
("bad cube representation"),
68 errdetail
("A cube cannot have more than %d dimensions.",
73 *((void **)result
) = write_box
( dim
, $2, $4 );
77 paren_list COMMA paren_list
{
80 dim
= delim_count
($1, ',') + 1;
82 if
( (delim_count
($3, ',') + 1) != dim
) {
84 (errcode
(ERRCODE_SYNTAX_ERROR
),
85 errmsg
("bad cube representation"),
86 errdetail
("Different point dimensions in (%s) and (%s).",
90 if
(dim
> CUBE_MAX_DIM
) {
92 (errcode
(ERRCODE_SYNTAX_ERROR
),
93 errmsg
("bad cube representation"),
94 errdetail
("A cube cannot have more than %d dimensions.",
99 *((void **)result
) = write_box
( dim
, $1, $3 );
106 dim
= delim_count
($1, ',') + 1;
107 if
(dim
> CUBE_MAX_DIM
) {
109 (errcode
(ERRCODE_SYNTAX_ERROR
),
110 errmsg
("bad cube representation"),
111 errdetail
("A cube cannot have more than %d dimensions.",
116 *((void **)result
) = write_point_as_box
($1, dim
);
124 dim
= delim_count
($1, ',') + 1;
125 if
(dim
> CUBE_MAX_DIM
) {
127 (errcode
(ERRCODE_SYNTAX_ERROR
),
128 errmsg
("bad cube representation"),
129 errdetail
("A cube cannot have more than %d dimensions.",
133 *((void **)result
) = write_point_as_box
($1, dim
);
138 O_PAREN list C_PAREN
{
145 /* alloc enough space to be sure whole list will fit */
146 $$
= palloc
(scanbuflen
+ 1);
150 list COMMA CUBEFLOAT
{
160 delim_count
(char *s
, char delim
)
164 while
((s
= strchr
(s
, delim
)) != NULL
)
173 write_box
(unsigned int dim
, char *str1
, char *str2
)
178 int size
= offsetof
(NDBOX
, x
[0]) + sizeof
(double) * dim
* 2;
181 SET_VARSIZE
(bp
, size
);
185 bp
->x
[i
=0] = strtod
(s
, NULL
);
186 while
((s
= strchr
(s
, ',')) != NULL
) {
188 bp
->x
[i
] = strtod
(s
, NULL
);
192 bp
->x
[i
=dim
] = strtod
(s
, NULL
);
193 while
((s
= strchr
(s
, ',')) != NULL
) {
195 bp
->x
[i
] = strtod
(s
, NULL
);
203 write_point_as_box
(char *str
, int dim
)
210 size
= offsetof
(NDBOX
, x
[0]) + sizeof
(double) * dim
* 2;
213 SET_VARSIZE
(bp
, size
);
220 while
((s
= strchr
(s
, ',')) != NULL
) {
230 #include "cubescan.c"