File: d4_ce_parser.yy

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (449 lines) | stat: -rw-r--r-- 11,867 bytes parent folder | download | duplicates (3)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// A minor edit

%skeleton "lalr1.cc" /* -*- C++ -*- */
%require "2.5"
%defines

// The d4ce_parser.tab.cc and .hh files define and declare this class
%define parser_class_name {D4CEParser}
// %define api.parser.class {D4CEParser}
// D4CEParser is in this namespace
%define api.namespace {libdap}

%define parse.trace
%define parse.error verbose
%define parse.assert

// Could not get this to work with a C++ scanner built by flex. 8/10/13 jhrg
// %define api.token.constructor
%define api.value.type variant

// Because the code uses the C++ mode of flex, we don't use this. 8/8/13 jhrg
// %define api.prefix { d4_ce }

%code requires {
#define YYERROR_VERBOSE 0
#include "D4ConstraintEvaluator.h"
#include "escaping.h" // for www2id() used with WORD and STRING
namespace libdap {
    class D4CEScanner;
}

}

// Pass both the scanner and parser objects to both the automatically generated
// parser and scanner. Note that in the actions bound to the rules, 'driver' 
// means use the 'D4ConstraintEvaluator' instance.
%lex-param   { D4CEScanner  &scanner  }
%parse-param { D4CEScanner  &scanner  }

%lex-param   { D4ConstraintEvaluator  &driver  }
%parse-param { D4ConstraintEvaluator  &driver  }

%locations
%initial-action
{
    // Initialize the initial location. This is printed when the parser builds
    // its own error messages - when the parse fails as opposed to when the 
    // CE names a missing variables, ...

    @$.initialize (driver.expression());
};

%code {
   #include <iostream>
   #include <cstdlib>
   #include <fstream>
   
   #include "BaseType.h"
   #include "DMR.h"
   #include "D4Group.h"

   /* include for all driver functions */
   #include "D4ConstraintEvaluator.h"

   /* this is silly, but I can't figure out a way around it */
   static int yylex(libdap::D4CEParser::semantic_type *yylval,
                    libdap::location *loc,
                    libdap::D4CEScanner  &scanner,
                    libdap::D4ConstraintEvaluator   &driver);

}

// The strings used in the token definitions are used for error messages
%token <std::string> WORD "word"
%token <std::string> STRING "string"

// %type is used to set the return type of non-terminals; %token sets the
// return type for terminals.
%type <bool> filter predicate fields indexes subset clause clauses dimension dimensions
%type <std::string> id path group name op

%type <libdap::D4ConstraintEvaluator::index> index

%token
    END  0  "end of file"
  
%token 
    SEMICOLON ";"
    PIPE "|"

    LBRACKET "["
    RBRACKET "]"
    COLON ":"

    LBRACE "{"
    RBRACE "}"

    LESS "<"
    GREATER ">"
    LESS_EQUAL "<="
    GREATER_EQUAL ">="
    EQUAL "=="
    NOT_EQUAL "!="
    REGEX_MATCH "~="

    LESS_BBOX "<<"
    GREATER_BBOX ">>"

    MASK "@="
    ND "ND"

    COMMA ","

    ASSIGN "="

    GROUP_SEP "/"
    PATH_SEP "."
    
%%

%start expression;

expression : clauses { driver.set_result($1); }
| dimensions ";" clauses { driver.set_result($1 && $3); }
;

dimensions : dimension { $$ = $1; }
| dimensions ";" dimension { $$ = $1 && $3; }
;

dimension : id "=" index
{
    $$ = driver.slice_dimension($1, $3);
}
;

clauses : clause { $$ = $1; }
| clauses ";" clause { $$ = $1 && $3; }
;
    
// Change: I moved the pop_basetype() call out of the 'fields'
// actions in 'subset' so that I could push the basetype for
// all of the cases. That way I'm sure to have a top_baseype()
// when processing the 'filter' part of the grammar. I need the
// the top basetype so that I know which Sequence to use.
// jhrg 4/23/16
               
clause : subset { $$ = $1; driver.pop_basetype(); }

// For the DAP4 at this time (3/18/15) filters apply only to D4Sequences 

| subset "|" filter { driver.pop_basetype(); $$ = $1 && $3; }
;

// mark_variable returns a BaseType* or throws Error
// Note that this is a fairly long production rule with a number
// of different right hand sides spanning about 110 lines.
// jhrg 4/8/16
subset : id 
{
    BaseType *btp = 0;
    if (driver.top_basetype()) {
        btp = driver.top_basetype()->var($1);
    }
    else {
        btp = driver.dmr()->root()->find_var($1);
    }
    
    if (!btp)
        driver.throw_not_found($1, "id");

    $$ = driver.mark_variable(btp);
    
    // push the basetype so that it is
    // accessible if/while filters are parsed
    driver.push_basetype(btp);
}

| id indexes 
{
    BaseType *btp = 0;
    if (driver.top_basetype()) {
        btp = driver.top_basetype()->var($1);
    }
    else {
        btp = driver.dmr()->root()->find_var($1);
    }
    
    if (!btp)
        driver.throw_not_found($1, "id indexes");
        
    if (btp->type() != dods_array_c)
        driver.throw_not_array($1, "id indexes");
        
    $$ = driver.mark_variable(btp);
    
    // push the basetype so that it is
    // accessible if/while filters are parsed
    driver.push_basetype(btp);
}

// Note this case is '| id fields'
| id 
{
    BaseType *btp = 0;
    if (driver.top_basetype()) {
        btp = driver.top_basetype()->var($1);
    }
    else {
        btp = driver.dmr()->root()->find_var($1);
    }

    if (!btp)
        driver.throw_not_found($1, "id fields");
    
    if (btp->type() == dods_array_c) {
        if (btp->var() && !btp->var()->is_constructor_type())
            throw Error(no_such_variable, "The constraint expression referenced a variable that must be a Structure or Sequence to be used with {}.");
            
        // This call also tests the btp to make sure it's an array
        driver.mark_array_variable(btp);
    }
    else {
        // Don't mark the variable here because only some fields are to be sent and those
        // will be marked when the fields are parsed
        if (!btp->is_constructor_type())
            throw Error(no_such_variable, "The constraint expression referenced a variable that must be a Structure or Sequence to be used with {}.");
    }
    
    // push the basetype so that it is
    // accessible when fields and if/while filters are parsed
    driver.push_basetype(btp);
} 
fields 
{ 
    //driver.pop_basetype(); 
    $$ = true; 
}

// Note this case is '| id indexes fields'

| id indexes
{
    BaseType *btp = 0;
    if (driver.top_basetype()) {
        btp = driver.top_basetype()->var($1);
    }
    else {
        btp = driver.dmr()->root()->find_var($1);
    }

    if (!btp)
        driver.throw_not_found($1, "id indexes fields");
    
    if (btp->type() != dods_array_c)
        driver.throw_not_array($1, "id indexes fields");

    // This call also tests the btp to make sure it's an array
    driver.mark_array_variable(btp);
    
    if (!btp->var()->is_constructor_type())
        throw Error(no_such_variable, "The constraint expression referenced a variable that must be a Structure or Sequence to be used with {}.");
      
    driver.push_basetype(btp->var());       
} 
fields 
{
    $$ = true; 
}
;

// push_index stores the index in the D4ConstraintEvaluator
indexes : index 
{ 
    driver.push_index($1); 
    $$ = true; 
}
| index { driver.push_index($1); } indexes { $$ = $3; }
;

// Note that the index values are scanned as WORDs but the web escaping is
// not supported as it is for identifiers. jhrg 10/20/16
index   : "[" "]" { $$ = driver.make_index(); }
| "[" WORD "]" { $$ = driver.make_index($2); }
| "[" WORD ":" WORD "]" { $$ = driver.make_index($2, 1, $4); }
| "[" WORD ":" WORD ":" WORD "]" { $$ = driver.make_index($2, $4, $6); }
| "[" WORD ":" "]" { $$ = driver.make_index($2, 1); }
| "[" WORD ":" WORD ":" "]" { $$ = driver.make_index($2, $4); }
;
        
fields : "{" clauses "}" { $$ = $2; }
;

// A filter should return a FilterClauseList; a predicate should return a single
// FilterClause.

filter : predicate { $$ = true; }
| filter "," predicate { $$ = $1 && $3; }
;

// Here we use a grammar that is overly general: id op id is not really
// supported by the CE evaluator. However, id op constant, which captures
// the intent of the evaluator design introduces a number of reduce/reduce
// conflicts because any sensible definition of 'constant' will be the
// same as the definition of 'name'. This happens because we must make 'name'
// far more general than ideal (it must include tokens that start with digits,
// odd characters that clash with the operators, et cetera).

predicate : id op id
{ driver.add_filter_clause($2, $1, $3); $$ = true; }
          
| id op id op id 
{ 
    driver.add_filter_clause($2, $1, $3); 
    driver.add_filter_clause($4, $3, $5); 
    $$ = true; 

}
| "ND" "=" id { throw Error(malformed_expr, "The 'ND' operator is not currently supported."); }
;

// See http://docs.opendap.org/index.php/DAP4:_Constraint_Expressions,_v2
// for a discussion of filters that's quite a bit longer than the current
// draft spec. << and >> are the 'less than bbox' and '> bbox' operations
// that I'm not so sure about now; @= is the same as *= and is the mapping
// operation. jhrg 3/18/15 
op : "<" {$$ = "<";}
   | ">" {$$ = ">";}
   | "<=" {$$ = "<=";}
   | ">=" {$$ = ">=";}
   | "==" {$$ = "==";}
   | "!=" {$$ = "!=";}
   | "~=" {$$ = "~=";}

   | "<<" {$$ = "<<";}
   | ">>" {$$ = ">>";}

   | "@=" {$$ = "@=";}
;

id : path
{
    $$ = $1;
}
| "/" path
{
    $$.append("/");
    $$.append($2);
}
| group "/" path
{
    $1.append("/");
    $1.append($3);
    $$ = $1;
}
;

group : "/" name
{
    $$.append("/");
    $$.append($2);
}
| group "/" name
{
    $1.append("/");
    $1.append($3);
    $$ = $1;
}
;

path : name 
{
    $$ = $1;
}
| path "." name
{
    $1.append(".");
    $1.append($3);
    $$ = $1;
}
;

// Because some formats/datasets allow 'any' name for a variable, it's possible
// that a variable name will be a number, etc. The grammar also allows STRING
// to support "name"."name with spaces and dots (.)".x
//
// I added calls here to remove the double quotes because they were breaking
// the parse for STRINGs and also added www2id() for WORDs (so that %20, etc.
// can be used for escaping stuff). However, the two cannot be mixed - if the
// parser id passed"Point%20Break" the %20 will remain as a literal in the STRING.
// jhrg 10/20/16
name : WORD 
{
    $$=www2id($1);
}
| STRING 
{
    $$=driver.remove_quotes($1);
}
;

%%

// Forward the error to the driver for handling. The location parameter
// provides the line number and character position of the error.
void
libdap::D4CEParser::error(const location_type &l, const std::string &m)
{
    driver.error(l, m);
}

/* include for access to scanner.yylex */
#include "D4CEScanner.h"

static int yylex(libdap::D4CEParser::semantic_type *yylval,
                 libdap::location *loc,
                 libdap::D4CEScanner &scanner,
                 libdap::D4ConstraintEvaluator &driver)
{
    if (driver.trace_scanning())
        scanner.set_debug(true);
    
    return( scanner.yylex(yylval, loc) );
}