]> granicus.if.org Git - postgresql/blob - contrib/cube/cubeparse.y
Make all our flex and bison files use %option prefix or %name-prefix
[postgresql] / contrib / cube / cubeparse.y
1 %{
2 /* NdBox = [(lowerleft),(upperright)] */
3 /* [(xLL(1)...xLL(N)),(xUR(1)...xUR(n))] */
4
5 #define YYPARSE_PARAM result  /* need this to pass a pointer (void *) to yyparse */
6 #define YYSTYPE char *
7 #define YYDEBUG 1
8
9 #include "postgres.h"
10
11 #include "cubedata.h"
12
13 extern int cube_yylex(void);
14
15 static char *scanbuf;
16 static int      scanbuflen;
17
18 void cube_yyerror(const char *message);
19 int cube_yyparse(void *result);
20
21 static int delim_count(char *s, char delim);
22 static NDBOX * write_box(unsigned int dim, char *str1, char *str2);
23 static NDBOX * write_point_as_box(char *s, int dim);
24
25 %}
26
27 /* BISON Declarations */
28 %name-prefix="cube_yy"
29
30 %token CUBEFLOAT O_PAREN C_PAREN O_BRACKET C_BRACKET COMMA
31 %start box
32
33 /* Grammar follows */
34 %%
35
36 box:
37           O_BRACKET paren_list COMMA paren_list C_BRACKET {
38
39             int dim;
40             
41             dim = delim_count($2, ',') + 1;
42             if ( (delim_count($4, ',') + 1) != dim ) {
43           ereport(ERROR,
44                   (errcode(ERRCODE_SYNTAX_ERROR),
45                    errmsg("bad cube representation"),
46                    errdetail("Different point dimensions in (%s) and (%s).",
47                              $2, $4)));
48               YYABORT;
49             }
50             if (dim > CUBE_MAX_DIM) {
51               ereport(ERROR,
52                       (errcode(ERRCODE_SYNTAX_ERROR),
53                        errmsg("bad cube representation"),
54                        errdetail("A cube cannot have more than %d dimensions.",
55                                                                  CUBE_MAX_DIM)));
56               YYABORT;
57             }
58             
59             *((void **)result) = write_box( dim, $2, $4 );
60     
61           }
62       |
63           paren_list COMMA paren_list {
64             int dim;
65
66             dim = delim_count($1, ',') + 1;
67             
68             if ( (delim_count($3, ',') + 1) != dim ) {
69           ereport(ERROR,
70                   (errcode(ERRCODE_SYNTAX_ERROR),
71                    errmsg("bad cube representation"),
72                    errdetail("Different point dimensions in (%s) and (%s).",
73                              $1, $3)));
74               YYABORT;
75             }
76             if (dim > CUBE_MAX_DIM) {
77               ereport(ERROR,
78                       (errcode(ERRCODE_SYNTAX_ERROR),
79                        errmsg("bad cube representation"),
80                        errdetail("A cube cannot have more than %d dimensions.",
81                                  CUBE_MAX_DIM)));
82               YYABORT;
83             }
84             
85             *((void **)result) = write_box( dim, $1, $3 );
86           }
87       |
88
89           paren_list {
90             int dim;
91
92             dim = delim_count($1, ',') + 1;
93             if (dim > CUBE_MAX_DIM) {
94               ereport(ERROR,
95                       (errcode(ERRCODE_SYNTAX_ERROR),
96                        errmsg("bad cube representation"),
97                        errdetail("A cube cannot have more than %d dimensions.",
98                                  CUBE_MAX_DIM)));
99               YYABORT;
100             }
101
102             *((void **)result) = write_point_as_box($1, dim);
103           }
104
105       |
106
107           list {
108             int dim;
109
110             dim = delim_count($1, ',') + 1;
111             if (dim > CUBE_MAX_DIM) {
112               ereport(ERROR,
113                       (errcode(ERRCODE_SYNTAX_ERROR),
114                        errmsg("bad cube representation"),
115                        errdetail("A cube cannot have more than %d dimensions.",
116                                  CUBE_MAX_DIM)));
117               YYABORT;
118             }
119             *((void **)result) = write_point_as_box($1, dim);
120           }
121       ;
122
123 paren_list:
124           O_PAREN list C_PAREN {
125              $$ = $2;
126           }
127       ;
128
129 list:
130           CUBEFLOAT {
131                          /* alloc enough space to be sure whole list will fit */
132              $$ = palloc(scanbuflen + 1);
133                          strcpy($$, $1);
134           }
135       | 
136           list COMMA CUBEFLOAT {
137              $$ = $1;
138              strcat($$, ",");
139              strcat($$, $3);
140           }
141       ;
142
143 %%
144
145 static int
146 delim_count(char *s, char delim)
147 {
148       int        ndelim = 0;
149
150       while ((s = strchr(s, delim)) != NULL)
151       {
152         ndelim++;
153         s++;
154       }
155       return (ndelim);
156 }
157
158 static NDBOX * 
159 write_box(unsigned int dim, char *str1, char *str2)
160 {
161   NDBOX * bp;
162   char * s;
163   int i; 
164   int size = offsetof(NDBOX, x[0]) + sizeof(double) * dim * 2;
165             
166   bp = palloc(size);
167   memset(bp, 0, size);
168   bp->size = size;
169   bp->dim = dim;
170             
171   s = str1;
172   bp->x[i=0] = strtod(s, NULL);
173   while ((s = strchr(s, ',')) != NULL) {
174     s++; i++;
175     bp->x[i] = strtod(s, NULL);
176   }     
177   
178   s = str2;
179   bp->x[i=dim] = strtod(s, NULL);
180   while ((s = strchr(s, ',')) != NULL) {
181     s++; i++;
182     bp->x[i] = strtod(s, NULL);
183   }     
184
185   return(bp);
186 }
187
188
189 static NDBOX *
190 write_point_as_box(char *str, int dim)
191 {
192   NDBOX * bp;
193   int i, size;
194   double x;
195   char * s = str;
196   
197   size = offsetof(NDBOX, x[0]) + sizeof(double) * dim * 2;
198
199   bp = palloc(size);
200   memset(bp, 0, size);
201   bp->size = size;
202   bp->dim = dim;
203   
204   i = 0;
205   x = strtod(s, NULL);
206   bp->x[0] = x;
207   bp->x[dim] = x;
208   while ((s = strchr(s, ',')) != NULL) {
209     s++; i++;
210     x = strtod(s, NULL);
211     bp->x[i] = x;
212     bp->x[i+dim] = x;
213   }     
214
215   return(bp);
216 }
217
218 #include "cubescan.c"