1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
See the COPYRIGHT file for more information. */
/* Parser actions for constraint expressions */
/* Since oc does not use the constraint parser,
they functions all just abort if called.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "netcdf.h"
#include "nclist.h"
#include "ncbytes.h"
#include "dceconstraints.h"
#include "dceparselex.h"
static Object collectlist(Object list0, Object decl);
void
projections(DCEparsestate* state, Object list0)
{
NClist* list = (NClist*)list0;
if(list != NULL) {
nclistfree(state->constraint->projections);
state->constraint->projections = list;
}
#ifdef DEBUG
fprintf(stderr," ce.projections: %s\n",
dcetostring((DCEnode*)state->constraint->projections));
#endif
}
void
selections(DCEparsestate* state, Object list0)
{
NClist* list = (NClist*)list0;
if(list != NULL) {
nclistfree(state->constraint->selections);
state->constraint->selections = list;
}
#ifdef DEBUG
fprintf(stderr," ce.selections: %s\n",
dcetostring((DCEnode*)state->constraint->selections));
#endif
}
Object
projectionlist(DCEparsestate* state, Object list0, Object decl)
{
return collectlist(list0,decl);
}
Object
projection(DCEparsestate* state, Object varorfcn)
{
DCEprojection* p = (DCEprojection*)dcecreate(CES_PROJECT);
CEsort tag = *(CEsort*)varorfcn;
if(tag == CES_FCN)
p->fcn = varorfcn;
else
p->var = varorfcn;
p->discrim = tag;
#ifdef DEBUG
fprintf(stderr," ce.projection: %s\n",
dcetostring((DCEnode*)p));
#endif
return p;
}
Object
segmentlist(DCEparsestate* state, Object var0, Object decl)
{
/* watch out: this is non-standard */
NClist* list;
DCEvar* v = (DCEvar*)var0;
if(v==NULL) v = (DCEvar*)dcecreate(CES_VAR);
list = v->segments;
if(list == NULL) list = nclistnew();
nclistpush(list,(void*)decl);
v->segments = list;
return v;
}
Object
segment(DCEparsestate* state, Object name, Object slices0)
{
int i;
DCEsegment* segment = (DCEsegment*)dcecreate(CES_SEGMENT);
NClist* slices = (NClist*)slices0;
segment->name = strdup((char*)name);
if(slices != NULL && nclistlength(slices) > 0) {
segment->rank = nclistlength(slices);
segment->slicesdefined = 1; /* but not declsizes */
for(i=0;i<nclistlength(slices);i++) {
DCEslice* slice = (DCEslice*)nclistget(slices,i);
segment->slices[i] = *slice;
free(slice);
}
nclistfree(slices);
} else
segment->slicesdefined = 0;
#ifdef DEBUG
fprintf(stderr," ce.segment: %s\n",
dumpsegment(segment));
#endif
return segment;
}
Object
rangelist(DCEparsestate* state, Object list0, Object decl)
{
return collectlist(list0,decl);
}
Object
range(DCEparsestate* state, Object sfirst, Object sstride, Object slast)
{
DCEslice* slice = (DCEslice*)dcecreate(CES_SLICE);
unsigned long first=0,stride=0,last=0;
/* Note: that incoming arguments are strings; we must convert to size_t;
but we do know they are legal integers or NULL */
if(sscanf((char*)sfirst,"%lu",&first) != 1) /* always defined */
return NULL;
if(slast != NULL) {
if(sscanf((char*)slast,"%lu",&last) != 1)
return NULL;
} else
last = first;
if(sstride != NULL) {
if(sscanf((char*)sstride,"%lu",&stride) != 1)
return NULL;
} else
stride = 1; /* default */
if(stride == 0)
dceerror(state,"Illegal index for range stride");
if(last < first)
dceerror(state,"Illegal index for range last index");
slice->first = first;
slice->stride = (stride == 0 ? 1 : stride);
slice->last = last;
slice->length = (slice->last - slice->first) + 1;
slice->count = slice->length / slice->stride;
#ifdef DEBUG
fprintf(stderr," ce.slice: %s\n",
dumpslice(slice));
#endif
return slice;
}
Object
range1(DCEparsestate* state, Object rangenumber)
{
int range = -1;
if(sscanf((char*)rangenumber,"%u",&range) != 1)
range = -1;
if(range < 0) {
dceerror(state,"Illegal range index");
}
return rangenumber;
}
Object
clauselist(DCEparsestate* state, Object list0, Object decl)
{
return collectlist(list0,decl);
}
Object
sel_clause(DCEparsestate* state, int selcase,
Object lhs, Object relop0, Object values)
{
DCEselection* sel = (DCEselection*)dcecreate(CES_SELECT);
sel->operator = (CEsort)relop0;
sel->lhs = (DCEvalue*)lhs;
if(selcase == 2) {/*singleton value*/
sel->rhs = nclistnew();
nclistpush(sel->rhs,(void*)values);
} else
sel->rhs = (NClist*)values;
return sel;
}
Object
indexpath(DCEparsestate* state, Object list0, Object index)
{
return collectlist(list0,index);
}
Object
array_indices(DCEparsestate* state, Object list0, Object indexno)
{
DCEslice* slice;
long long start = -1;
NClist* list = (NClist*)list0;
if(list == NULL) list = nclistnew();
if(sscanf((char*)indexno,"%lld",&start) != 1)
start = -1;
if(start < 0) {
dceerror(state,"Illegal array index");
start = 1;
}
slice = (DCEslice*)dcecreate(CES_SLICE);
slice->first = start;
slice->stride = 1;
slice->length = 1;
slice->last = start;
slice->count = 1;
nclistpush(list,(void*)slice);
return list;
}
Object
indexer(DCEparsestate* state, Object name, Object indices)
{
int i;
NClist* list = (NClist*)indices;
DCEsegment* seg = (DCEsegment*)dcecreate(CES_SEGMENT);
seg->name = strdup((char*)name);
for(i=0;i<nclistlength(list);i++) {
DCEslice* slice = (DCEslice*)nclistget(list,i);
seg->slices[i] = *slice;
free(slice);
}
nclistfree(indices);
return seg;
}
Object
function(DCEparsestate* state, Object fcnname, Object args)
{
DCEfcn* fcn = (DCEfcn*)dcecreate(CES_FCN);
fcn->name = nulldup((char*)fcnname);
fcn->args = args;
return fcn;
}
Object
arg_list(DCEparsestate* state, Object list0, Object decl)
{
return collectlist(list0,decl);
}
Object
value_list(DCEparsestate* state, Object list0, Object decl)
{
return collectlist(list0,decl);
}
Object
value(DCEparsestate* state, Object val)
{
DCEvalue* ncvalue = (DCEvalue*)dcecreate(CES_VALUE);
CEsort tag = *(CEsort*)val;
switch (tag) {
case CES_VAR: ncvalue->var = (DCEvar*)val; break;
case CES_FCN: ncvalue->fcn = (DCEfcn*)val; break;
case CES_CONST: ncvalue->constant = (DCEconstant*)val; break;
default: abort(); break;
}
ncvalue->discrim = tag;
return ncvalue;
}
Object
var(DCEparsestate* state, Object indexpath)
{
DCEvar* v = (DCEvar*)dcecreate(CES_VAR);
v->segments = (NClist*)indexpath;
return v;
}
Object
constant(DCEparsestate* state, Object val, int tag)
{
DCEconstant* con = (DCEconstant*)dcecreate(CES_CONST);
char* text = (char*)val;
char* endpoint = NULL;
switch (tag) {
case SCAN_STRINGCONST:
con->discrim = CES_STR;
con->text = nulldup(text);
break;
case SCAN_NUMBERCONST:
con->intvalue = strtoll(text,&endpoint,10);
if(*text != '\0' && *endpoint == '\0') {
con->discrim = CES_INT;
} else {
con->floatvalue = strtod(text,&endpoint);
if(*text != '\0' && *endpoint == '\0')
con->discrim = CES_FLOAT;
else abort();
}
break;
default: abort(); break;
}
return con;
}
static Object
collectlist(Object list0, Object decl)
{
NClist* list = (NClist*)list0;
if(list == NULL) list = nclistnew();
nclistpush(list,(void*)decl);
return list;
}
Object
makeselectiontag(CEsort tag)
{
return (Object) tag;
}
int
dceerror(DCEparsestate* state, char* msg)
{
strncpy(state->errorbuf,msg,1023);
state->errorcode=NC_EDAPCONSTRAINT;
return 0;
}
static void
dce_parse_cleanup(DCEparsestate* state)
{
if(state == NULL) return;
dcelexcleanup(&state->lexstate); /* will free */
/* Free up the state */
free(state);
}
static DCEparsestate*
ce_parse_init(char* input, DCEconstraint* constraint)
{
DCEparsestate* state = (DCEparsestate*)calloc(1,sizeof(DCEparsestate));;
if(state==NULL) return (DCEparsestate*)NULL;
if(input==NULL) {
dceerror(state,"ce_parse_init: no input buffer");
} else {
state->errorbuf[0] = '\0';
state->errorcode = 0;
dcelexinit(input,&state->lexstate);
state->constraint = constraint;
}
return state;
}
#ifdef PARSEDEBUG
extern int dcedebug;
#endif
/* Wrapper for ceparse */
int
dapceparse(char* input, DCEconstraint* constraint, char** errmsgp)
{
DCEparsestate* state;
int errcode = 0;
#ifdef PARSEDEBUG
dcedebug = 1;
#endif
if(input != NULL) {
#ifdef DEBUG
fprintf(stderr,"dceeparse: input=%s\n",input);
#endif
state = ce_parse_init(input,constraint);
if(dceparse(state) == 0) {
#ifdef DEBUG
if(nclistlength(constraint->projections) > 0)
fprintf(stderr,"dceeparse: projections=%s\n",
dcetostring((DCEnode*)constraint->projections));
#endif
#ifdef DEBUG
if(nclistlength(constraint->selections) > 0)
fprintf(stderr,"dceeparse: selections=%s\n",
dumpselections(constraint->selections));
#endif
} else {
if(errmsgp) *errmsgp = nulldup(state->errorbuf);
}
errcode = state->errorcode;
dce_parse_cleanup(state);
}
return errcode;
}
#ifdef PARSEDEBUG
Object
debugobject(Object o)
{
return o;
}
#endif
|