Added manual re-replication trigger.
[iwhd.git] / query.c
blob9ec0e73c8aa048d7442b103eedc80477918142af
1 /* A recursive-descent parser generated by peg 0.1.2 */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #define YYRULECOUNT 24
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <time.h>
11 #include "query.h"
13 char *arg_buf;
14 int arg_off;
15 int arg_len;
16 value_t **cur_expr;
18 /* TBD: use separate function to parse dates differently */
19 value_t *
20 make_number (char *text)
22 value_t *tmp = malloc(sizeof(*tmp));
24 if (tmp) {
25 tmp->type = T_NUMBER;
26 tmp->as_num = strtoll(text,NULL,10);
29 return tmp;
32 value_t *
33 make_string (char *text, type_t t)
35 value_t *tmp = malloc(sizeof(*tmp));
37 if (tmp) {
38 tmp->type = t;
39 tmp->as_str = strdup(text);
42 return tmp;
45 value_t *
46 make_tree (type_t t, value_t *left, value_t *right)
48 value_t *tmp = malloc(sizeof(*tmp));
50 if (tmp) {
51 tmp->type = t;
52 tmp->as_tree.left = left;
53 tmp->as_tree.right = right;
56 return tmp;
59 value_t *
60 make_comp (comp_t c, value_t *left, value_t *right)
62 value_t *tmp = make_tree(T_COMP,left,right);
64 if (tmp) {
65 tmp->as_tree.op = c;
68 return tmp;
71 #if defined(UNIT_TEST)
72 struct { char *name; char *value; } hacked_obj_fields[] = {
73 { "a", "2" }, { "b", "7" }, { "c", "11" },
74 { NULL }
77 char *
78 unit_oget (char *text)
80 int i;
82 for (i = 0; hacked_obj_fields[i].name; ++i) {
83 if (!strcmp(hacked_obj_fields[i].name,text)) {
84 return hacked_obj_fields[i].value;
88 return NULL;
91 char *
92 unit_sget (char *text)
94 return "never";
96 #endif
98 char *
99 string_value (value_t *v, getter_t *oget, getter_t *sget)
101 switch (v->type) {
102 case T_STRING:
103 return v->as_str;
104 case T_OFIELD:
105 return oget ? (*oget)(v->as_str) : "";
106 case T_SFIELD:
107 return oget ? (*sget)(v->as_str) : "";
108 default:
109 return NULL;
114 compare (value_t *left, comp_t op, value_t *right,
115 getter_t *oget, getter_t *sget)
117 char *lstr;
118 char *rstr;
119 int lval;
120 int rval;
122 lstr = string_value(left,oget,sget);
123 rstr = string_value(right,oget,sget);
125 if (lstr && rstr) {
126 lval = strcmp(lstr,rstr);
127 rval = 0;
129 else {
130 if (lstr) {
131 lval = strtoll(lstr,NULL,0);
133 else if (left->type == T_NUMBER) {
134 lval = left->as_num;
136 else {
137 lval = eval(left,oget,sget);
138 if (lval < 0) {
139 return lval;
142 if (rstr) {
143 rval = strtoll(rstr,NULL,0);
145 else if (right->type == T_NUMBER) {
146 rval = right->as_num;
148 else {
149 rval = eval(right,oget,sget);
150 if (rval < 0) {
151 return rval;
156 switch (op) {
157 case C_LESSTHAN: return (lval < rval);
158 case C_LESSOREQ: return (lval <= rval);
159 case C_EQUAL: return (lval == rval);
160 case C_DIFFERENT: return (lval != rval);
161 case C_GREATEROREQ: return (lval >= rval);
162 case C_GREATERTHAN: return (lval > rval);
163 default:
164 return -1;
168 void
169 _print_value (value_t *v, int level)
171 if (!v) {
172 printf("%*sNULL\n",level,"");
173 return;
176 switch (v->type) {
177 case T_NUMBER:
178 printf("%*sNUMBER %lld\n",level,"",v->as_num);
179 break;
180 case T_STRING:
181 printf("%*sSTRING %s\n",level,"",v->as_str);
182 break;
183 case T_OFIELD:
184 #if defined(UNIT_TEST)
185 printf("%*sOBJECT FIELD %s (%s)\n",level,"",v->as_str,
186 unit_oget(v->as_str));
187 #else
188 printf("%*sOBJECT FIELD %s\n",level,"",v->as_str);
189 #endif
190 break;
191 case T_SFIELD:
192 #if defined(UNIT_TEST)
193 printf("%*sSERVER FIELD %s (%s)\n",level,"",v->as_str,
194 unit_sget(v->as_str));
195 #else
196 printf("%*sSERVER FIELD %s\n",level,"",v->as_str);
197 #endif
198 break;
199 case T_COMP:
200 printf("%*sCOMPARISON (%d)\n",level,"",v->as_num);
201 _print_value(v->as_tree.left,level+2);
202 _print_value(v->as_tree.right,level+2);
203 break;
204 case T_NOT:
205 printf("%*sNOT\n",level,"");
206 _print_value(v->as_tree.left,level+2);
207 break;
208 case T_AND:
209 printf("%*sAND\n",level,"");
210 _print_value(v->as_tree.left,level+2);
211 _print_value(v->as_tree.right,level+2);
212 break;
213 case T_OR:
214 printf("%*sOR\n",level,"");
215 _print_value(v->as_tree.left,level+2);
216 _print_value(v->as_tree.right,level+2);
217 break;
218 default:
219 printf("%*sUNKNOWN %d\n",v->type,level,"");
223 void
224 print_value (value_t *v)
226 _print_value(v,0);
229 void
230 free_value (value_t *v)
232 if (!v) {
233 return;
236 switch (v->type) {
237 case T_STRING:
238 case T_OFIELD:
239 case T_SFIELD:
240 if (v->as_str) {
241 free(v->as_str);
243 break;
244 case T_COMP:
245 case T_AND:
246 case T_OR:
247 free_value(v->as_tree.right);
248 /* Fall through. */
249 case T_NOT:
250 free_value(v->as_tree.left);
251 /* Fall through. */
252 default:
253 free(v);
258 eval (value_t *v, getter_t *oget, getter_t *sget)
260 int res;
262 switch (v->type) {
263 case T_NUMBER:
264 return v->as_num != 0;
265 case T_STRING:
266 case T_OFIELD:
267 case T_SFIELD:
268 return -1;
269 case T_COMP:
270 return compare(v->as_tree.left,(comp_t)v->as_tree.op,
271 v->as_tree.right, oget, sget);
272 case T_NOT:
273 res = eval(v->as_tree.left,oget,sget);
274 return (res >= 0) ? !res : res;
275 case T_AND:
276 res = eval(v->as_tree.left,oget,sget);
277 if (res > 0) {
278 res = eval(v->as_tree.right,oget,sget);
280 return res;
281 case T_OR:
282 res = eval(v->as_tree.left,oget,sget);
283 if (res > 0) {
284 return res;
286 return eval(v->as_tree.right,oget,sget);
287 default:
288 return -1;
292 #define YY_INPUT(buf,result,max) { \
293 result = (arg_off < arg_len) ? (*buf = arg_buf[arg_off++], 1) \
294 : (arg_off == arg_len) ? (*buf = '\n', ++arg_off, 1) : 0; \
297 #define YYSTYPE value_t *
299 #ifndef YY_VARIABLE
300 #define YY_VARIABLE(T) static T
301 #endif
302 #ifndef YY_LOCAL
303 #define YY_LOCAL(T) static T
304 #endif
305 #ifndef YY_ACTION
306 #define YY_ACTION(T) static T
307 #endif
308 #ifndef YY_RULE
309 #define YY_RULE(T) static T
310 #endif
311 #ifndef YY_PARSE
312 #define YY_PARSE(T) T
313 #endif
314 #ifndef YYPARSE
315 #define YYPARSE yyparse
316 #endif
317 #ifndef YYPARSEFROM
318 #define YYPARSEFROM yyparsefrom
319 #endif
320 #ifndef YY_INPUT
321 #define YY_INPUT(buf, result, max_size) \
323 int yyc= getchar(); \
324 result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \
325 yyprintf((stderr, "<%c>", yyc)); \
327 #endif
328 #ifndef YY_BEGIN
329 #define YY_BEGIN ( yybegin= yypos, 1)
330 #endif
331 #ifndef YY_END
332 #define YY_END ( yyend= yypos, 1)
333 #endif
334 #ifdef YY_DEBUG
335 # define yyprintf(args) fprintf args
336 #else
337 # define yyprintf(args)
338 #endif
339 #ifndef YYSTYPE
340 #define YYSTYPE int
341 #endif
343 #ifndef YY_PART
345 typedef void (*yyaction)(char *yytext, int yyleng);
346 typedef struct _yythunk { int begin, end; yyaction action; struct _yythunk *next; } yythunk;
348 YY_VARIABLE(char * ) yybuf= 0;
349 YY_VARIABLE(int ) yybuflen= 0;
350 YY_VARIABLE(int ) yypos= 0;
351 YY_VARIABLE(int ) yylimit= 0;
352 YY_VARIABLE(char * ) yytext= 0;
353 YY_VARIABLE(int ) yytextlen= 0;
354 YY_VARIABLE(int ) yybegin= 0;
355 YY_VARIABLE(int ) yyend= 0;
356 YY_VARIABLE(int ) yytextmax= 0;
357 YY_VARIABLE(yythunk *) yythunks= 0;
358 YY_VARIABLE(int ) yythunkslen= 0;
359 YY_VARIABLE(int ) yythunkpos= 0;
360 YY_VARIABLE(YYSTYPE ) yy;
361 YY_VARIABLE(YYSTYPE *) yyval= 0;
362 YY_VARIABLE(YYSTYPE *) yyvals= 0;
363 YY_VARIABLE(int ) yyvalslen= 0;
365 YY_LOCAL(int) yyrefill(void)
367 int yyn;
368 while (yybuflen - yypos < 512)
370 yybuflen *= 2;
371 yybuf= realloc(yybuf, yybuflen);
373 YY_INPUT((yybuf + yypos), yyn, (yybuflen - yypos));
374 if (!yyn) return 0;
375 yylimit += yyn;
376 return 1;
379 YY_LOCAL(int) yymatchDot(void)
381 if (yypos >= yylimit && !yyrefill()) return 0;
382 ++yypos;
383 return 1;
386 YY_LOCAL(int) yymatchChar(int c)
388 if (yypos >= yylimit && !yyrefill()) return 0;
389 if (yybuf[yypos] == c)
391 ++yypos;
392 yyprintf((stderr, " ok yymatchChar(%c) @ %s\n", c, yybuf+yypos));
393 return 1;
395 yyprintf((stderr, " fail yymatchChar(%c) @ %s\n", c, yybuf+yypos));
396 return 0;
399 YY_LOCAL(int) yymatchString(char *s)
401 int yysav= yypos;
402 while (*s)
404 if (yypos >= yylimit && !yyrefill()) return 0;
405 if (yybuf[yypos] != *s)
407 yypos= yysav;
408 return 0;
410 ++s;
411 ++yypos;
413 return 1;
416 YY_LOCAL(int) yymatchClass(unsigned char *bits)
418 int c;
419 if (yypos >= yylimit && !yyrefill()) return 0;
420 c= yybuf[yypos];
421 if (bits[c >> 3] & (1 << (c & 7)))
423 ++yypos;
424 yyprintf((stderr, " ok yymatchClass @ %s\n", yybuf+yypos));
425 return 1;
427 yyprintf((stderr, " fail yymatchClass @ %s\n", yybuf+yypos));
428 return 0;
431 YY_LOCAL(void) yyDo(yyaction action, int begin, int end)
433 while (yythunkpos >= yythunkslen)
435 yythunkslen *= 2;
436 yythunks= realloc(yythunks, sizeof(yythunk) * yythunkslen);
438 yythunks[yythunkpos].begin= begin;
439 yythunks[yythunkpos].end= end;
440 yythunks[yythunkpos].action= action;
441 ++yythunkpos;
444 YY_LOCAL(int) yyText(int begin, int end)
446 int yyleng= end - begin;
447 if (yyleng <= 0)
448 yyleng= 0;
449 else
451 while (yytextlen < (yyleng - 1))
453 yytextlen *= 2;
454 yytext= realloc(yytext, yytextlen);
456 memcpy(yytext, yybuf + begin, yyleng);
458 yytext[yyleng]= '\0';
459 return yyleng;
462 YY_LOCAL(void) yyDone(void)
464 int pos;
465 for (pos= 0; pos < yythunkpos; ++pos)
467 yythunk *thunk= &yythunks[pos];
468 int yyleng= thunk->end ? yyText(thunk->begin, thunk->end) : thunk->begin;
469 yyprintf((stderr, "DO [%d] %p %s\n", pos, thunk->action, yytext));
470 thunk->action(yytext, yyleng);
472 yythunkpos= 0;
475 YY_LOCAL(void) yyCommit()
477 if ((yylimit -= yypos))
479 memmove(yybuf, yybuf + yypos, yylimit);
481 yybegin -= yypos;
482 yyend -= yypos;
483 yypos= yythunkpos= 0;
486 YY_LOCAL(int) yyAccept(int tp0)
488 if (tp0)
490 fprintf(stderr, "accept denied at %d\n", tp0);
491 return 0;
493 else
495 yyDone();
496 yyCommit();
498 return 1;
501 YY_LOCAL(void) yyPush(char *text, int count) { yyval += count; }
502 YY_LOCAL(void) yyPop(char *text, int count) { yyval -= count; }
503 YY_LOCAL(void) yySet(char *text, int count) { yyval[count]= yy; }
505 #endif /* YY_PART */
507 #define YYACCEPT yyAccept(yythunkpos0)
509 YY_RULE(int) yy_CLOSE(); /* 24 */
510 YY_RULE(int) yy_OPEN(); /* 23 */
511 YY_RULE(int) yy_WAFFLE(); /* 22 */
512 YY_RULE(int) yy_ID(); /* 21 */
513 YY_RULE(int) yy_DOLLAR(); /* 20 */
514 YY_RULE(int) yy_TIME(); /* 19 */
515 YY_RULE(int) yy_STRING(); /* 18 */
516 YY_RULE(int) yy_NUMBER(); /* 17 */
517 YY_RULE(int) yy_ParenExpr(); /* 16 */
518 YY_RULE(int) yy_Field(); /* 15 */
519 YY_RULE(int) yy_Literal(); /* 14 */
520 YY_RULE(int) yy_Atom(); /* 13 */
521 YY_RULE(int) yy_GREATER(); /* 12 */
522 YY_RULE(int) yy_NOT(); /* 11 */
523 YY_RULE(int) yy_EQUAL(); /* 10 */
524 YY_RULE(int) yy_LESS(); /* 9 */
525 YY_RULE(int) yy_Unary(); /* 8 */
526 YY_RULE(int) yy_OR(); /* 7 */
527 YY_RULE(int) yy_AND(); /* 6 */
528 YY_RULE(int) yy_CompExpr(); /* 5 */
529 YY_RULE(int) yy_EOL(); /* 4 */
530 YY_RULE(int) yy_BoolExpr(); /* 3 */
531 YY_RULE(int) yy__(); /* 2 */
532 YY_RULE(int) yy_Stmt(); /* 1 */
534 YY_ACTION(void) yy_1_ID(char *yytext, int yyleng)
536 yyprintf((stderr, "do yy_1_ID\n"));
537 yy = (YYSTYPE)yytext; ;
539 YY_ACTION(void) yy_1_TIME(char *yytext, int yyleng)
541 yyprintf((stderr, "do yy_1_TIME\n"));
542 yy = make_number(yytext); ;
544 YY_ACTION(void) yy_1_STRING(char *yytext, int yyleng)
546 yyprintf((stderr, "do yy_1_STRING\n"));
547 yy = make_string(yytext,T_STRING); ;
549 YY_ACTION(void) yy_1_NUMBER(char *yytext, int yyleng)
551 yyprintf((stderr, "do yy_1_NUMBER\n"));
552 yy= make_number(yytext); ;
554 YY_ACTION(void) yy_1_ParenExpr(char *yytext, int yyleng)
556 #define v yyval[-1]
557 yyprintf((stderr, "do yy_1_ParenExpr\n"));
558 yy = v; ;
559 #undef v
561 YY_ACTION(void) yy_2_Field(char *yytext, int yyleng)
563 #define i yyval[-1]
564 yyprintf((stderr, "do yy_2_Field\n"));
565 yy = make_string((char *)yy,T_SFIELD); ;
566 #undef i
568 YY_ACTION(void) yy_1_Field(char *yytext, int yyleng)
570 #define i yyval[-1]
571 yyprintf((stderr, "do yy_1_Field\n"));
572 yy = make_string((char *)yy,T_OFIELD); ;
573 #undef i
575 YY_ACTION(void) yy_2_Unary(char *yytext, int yyleng)
577 #define e yyval[-1]
578 yyprintf((stderr, "do yy_2_Unary\n"));
579 yy = make_tree(T_NOT,e,NULL); ;
580 #undef e
582 YY_ACTION(void) yy_1_Unary(char *yytext, int yyleng)
584 #define e yyval[-1]
585 yyprintf((stderr, "do yy_1_Unary\n"));
586 yy = make_tree(T_NOT,e,NULL); ;
587 #undef e
589 YY_ACTION(void) yy_6_CompExpr(char *yytext, int yyleng)
591 #define r yyval[-1]
592 #define l yyval[-2]
593 yyprintf((stderr, "do yy_6_CompExpr\n"));
594 yy = make_comp(C_GREATERTHAN,l,r); ;
595 #undef r
596 #undef l
598 YY_ACTION(void) yy_5_CompExpr(char *yytext, int yyleng)
600 #define r yyval[-1]
601 #define l yyval[-2]
602 yyprintf((stderr, "do yy_5_CompExpr\n"));
603 yy = make_comp(C_GREATEROREQ,l,r); ;
604 #undef r
605 #undef l
607 YY_ACTION(void) yy_4_CompExpr(char *yytext, int yyleng)
609 #define r yyval[-1]
610 #define l yyval[-2]
611 yyprintf((stderr, "do yy_4_CompExpr\n"));
612 yy = make_comp(C_DIFFERENT,l,r); ;
613 #undef r
614 #undef l
616 YY_ACTION(void) yy_3_CompExpr(char *yytext, int yyleng)
618 #define r yyval[-1]
619 #define l yyval[-2]
620 yyprintf((stderr, "do yy_3_CompExpr\n"));
621 yy = make_comp(C_EQUAL,l,r); ;
622 #undef r
623 #undef l
625 YY_ACTION(void) yy_2_CompExpr(char *yytext, int yyleng)
627 #define r yyval[-1]
628 #define l yyval[-2]
629 yyprintf((stderr, "do yy_2_CompExpr\n"));
630 yy = make_comp(C_LESSOREQ,l,r); ;
631 #undef r
632 #undef l
634 YY_ACTION(void) yy_1_CompExpr(char *yytext, int yyleng)
636 #define r yyval[-1]
637 #define l yyval[-2]
638 yyprintf((stderr, "do yy_1_CompExpr\n"));
639 yy = make_comp(C_LESSTHAN,l,r); ;
640 #undef r
641 #undef l
643 YY_ACTION(void) yy_2_BoolExpr(char *yytext, int yyleng)
645 #define r yyval[-1]
646 #define l yyval[-2]
647 yyprintf((stderr, "do yy_2_BoolExpr\n"));
648 yy = make_tree(T_OR,l,r); ;
649 #undef r
650 #undef l
652 YY_ACTION(void) yy_1_BoolExpr(char *yytext, int yyleng)
654 #define r yyval[-1]
655 #define l yyval[-2]
656 yyprintf((stderr, "do yy_1_BoolExpr\n"));
657 yy = make_tree(T_AND,l,r); ;
658 #undef r
659 #undef l
661 YY_ACTION(void) yy_1_Stmt(char *yytext, int yyleng)
663 yyprintf((stderr, "do yy_1_Stmt\n"));
664 *cur_expr = yy; ;
667 YY_RULE(int) yy_CLOSE()
668 { int yypos0= yypos, yythunkpos0= yythunkpos;
669 yyprintf((stderr, "%s\n", "CLOSE")); if (!yymatchChar(')')) goto l1;
670 yyprintf((stderr, " ok %s @ %s\n", "CLOSE", yybuf+yypos));
671 return 1;
672 l1:; yypos= yypos0; yythunkpos= yythunkpos0;
673 yyprintf((stderr, " fail %s @ %s\n", "CLOSE", yybuf+yypos));
674 return 0;
676 YY_RULE(int) yy_OPEN()
677 { int yypos0= yypos, yythunkpos0= yythunkpos;
678 yyprintf((stderr, "%s\n", "OPEN")); if (!yymatchChar('(')) goto l2;
679 yyprintf((stderr, " ok %s @ %s\n", "OPEN", yybuf+yypos));
680 return 1;
681 l2:; yypos= yypos0; yythunkpos= yythunkpos0;
682 yyprintf((stderr, " fail %s @ %s\n", "OPEN", yybuf+yypos));
683 return 0;
685 YY_RULE(int) yy_WAFFLE()
686 { int yypos0= yypos, yythunkpos0= yythunkpos;
687 yyprintf((stderr, "%s\n", "WAFFLE")); if (!yymatchChar('#')) goto l3;
688 yyprintf((stderr, " ok %s @ %s\n", "WAFFLE", yybuf+yypos));
689 return 1;
690 l3:; yypos= yypos0; yythunkpos= yythunkpos0;
691 yyprintf((stderr, " fail %s @ %s\n", "WAFFLE", yybuf+yypos));
692 return 0;
694 YY_RULE(int) yy_ID()
695 { int yypos0= yypos, yythunkpos0= yythunkpos;
696 yyprintf((stderr, "%s\n", "ID")); yyText(yybegin, yyend); if (!(YY_BEGIN)) goto l4; if (!yymatchClass((unsigned char *)"\000\000\000\000\000\000\000\000\000\000\000\200\376\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l4;
697 l5:;
698 { int yypos6= yypos, yythunkpos6= yythunkpos; if (!yymatchClass((unsigned char *)"\000\000\000\000\000\000\000\000\000\000\000\200\376\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l6; goto l5;
699 l6:; yypos= yypos6; yythunkpos= yythunkpos6;
700 } yyText(yybegin, yyend); if (!(YY_END)) goto l4; yyDo(yy_1_ID, yybegin, yyend);
701 yyprintf((stderr, " ok %s @ %s\n", "ID", yybuf+yypos));
702 return 1;
703 l4:; yypos= yypos0; yythunkpos= yythunkpos0;
704 yyprintf((stderr, " fail %s @ %s\n", "ID", yybuf+yypos));
705 return 0;
707 YY_RULE(int) yy_DOLLAR()
708 { int yypos0= yypos, yythunkpos0= yythunkpos;
709 yyprintf((stderr, "%s\n", "DOLLAR")); if (!yymatchChar('$')) goto l7;
710 yyprintf((stderr, " ok %s @ %s\n", "DOLLAR", yybuf+yypos));
711 return 1;
712 l7:; yypos= yypos0; yythunkpos= yythunkpos0;
713 yyprintf((stderr, " fail %s @ %s\n", "DOLLAR", yybuf+yypos));
714 return 0;
716 YY_RULE(int) yy_TIME()
717 { int yypos0= yypos, yythunkpos0= yythunkpos;
718 yyprintf((stderr, "%s\n", "TIME")); if (!yymatchChar('~')) goto l8; yyText(yybegin, yyend); if (!(YY_BEGIN)) goto l8;
719 l9:;
720 { int yypos10= yypos, yythunkpos10= yythunkpos; if (!yymatchClass((unsigned char *)"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\277\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377")) goto l10; goto l9;
721 l10:; yypos= yypos10; yythunkpos= yythunkpos10;
722 } yyText(yybegin, yyend); if (!(YY_END)) goto l8; if (!yymatchChar('~')) goto l8; yyDo(yy_1_TIME, yybegin, yyend);
723 yyprintf((stderr, " ok %s @ %s\n", "TIME", yybuf+yypos));
724 return 1;
725 l8:; yypos= yypos0; yythunkpos= yythunkpos0;
726 yyprintf((stderr, " fail %s @ %s\n", "TIME", yybuf+yypos));
727 return 0;
729 YY_RULE(int) yy_STRING()
730 { int yypos0= yypos, yythunkpos0= yythunkpos;
731 yyprintf((stderr, "%s\n", "STRING")); if (!yymatchChar('"')) goto l11; yyText(yybegin, yyend); if (!(YY_BEGIN)) goto l11;
732 l12:;
733 { int yypos13= yypos, yythunkpos13= yythunkpos; if (!yymatchClass((unsigned char *)"\377\377\377\377\373\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377")) goto l13; goto l12;
734 l13:; yypos= yypos13; yythunkpos= yythunkpos13;
735 } yyText(yybegin, yyend); if (!(YY_END)) goto l11; if (!yymatchChar('"')) goto l11; yyDo(yy_1_STRING, yybegin, yyend);
736 yyprintf((stderr, " ok %s @ %s\n", "STRING", yybuf+yypos));
737 return 1;
738 l11:; yypos= yypos0; yythunkpos= yythunkpos0;
739 yyprintf((stderr, " fail %s @ %s\n", "STRING", yybuf+yypos));
740 return 0;
742 YY_RULE(int) yy_NUMBER()
743 { int yypos0= yypos, yythunkpos0= yythunkpos;
744 yyprintf((stderr, "%s\n", "NUMBER")); yyText(yybegin, yyend); if (!(YY_BEGIN)) goto l14; if (!yymatchClass((unsigned char *)"\000\000\000\000\000\000\377\003\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l14;
745 l15:;
746 { int yypos16= yypos, yythunkpos16= yythunkpos; if (!yymatchClass((unsigned char *)"\000\000\000\000\000\000\377\003\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l16; goto l15;
747 l16:; yypos= yypos16; yythunkpos= yythunkpos16;
748 } yyText(yybegin, yyend); if (!(YY_END)) goto l14; yyDo(yy_1_NUMBER, yybegin, yyend);
749 yyprintf((stderr, " ok %s @ %s\n", "NUMBER", yybuf+yypos));
750 return 1;
751 l14:; yypos= yypos0; yythunkpos= yythunkpos0;
752 yyprintf((stderr, " fail %s @ %s\n", "NUMBER", yybuf+yypos));
753 return 0;
755 YY_RULE(int) yy_ParenExpr()
756 { int yypos0= yypos, yythunkpos0= yythunkpos; yyDo(yyPush, 1, 0);
757 yyprintf((stderr, "%s\n", "ParenExpr")); if (!yy_OPEN()) goto l17; if (!yy_BoolExpr()) goto l17; yyDo(yySet, -1, 0); if (!yy_CLOSE()) goto l17; if (!yy__()) goto l17; yyDo(yy_1_ParenExpr, yybegin, yyend);
758 yyprintf((stderr, " ok %s @ %s\n", "ParenExpr", yybuf+yypos)); yyDo(yyPop, 1, 0);
759 return 1;
760 l17:; yypos= yypos0; yythunkpos= yythunkpos0;
761 yyprintf((stderr, " fail %s @ %s\n", "ParenExpr", yybuf+yypos));
762 return 0;
764 YY_RULE(int) yy_Field()
765 { int yypos0= yypos, yythunkpos0= yythunkpos; yyDo(yyPush, 1, 0);
766 yyprintf((stderr, "%s\n", "Field"));
767 { int yypos19= yypos, yythunkpos19= yythunkpos; if (!yy_DOLLAR()) goto l20; if (!yy_ID()) goto l20; yyDo(yySet, -1, 0); yyDo(yy_1_Field, yybegin, yyend); goto l19;
768 l20:; yypos= yypos19; yythunkpos= yythunkpos19; if (!yy_WAFFLE()) goto l18; if (!yy_ID()) goto l18; yyDo(yySet, -1, 0); yyDo(yy_2_Field, yybegin, yyend);
770 l19:;
771 yyprintf((stderr, " ok %s @ %s\n", "Field", yybuf+yypos)); yyDo(yyPop, 1, 0);
772 return 1;
773 l18:; yypos= yypos0; yythunkpos= yythunkpos0;
774 yyprintf((stderr, " fail %s @ %s\n", "Field", yybuf+yypos));
775 return 0;
777 YY_RULE(int) yy_Literal()
778 { int yypos0= yypos, yythunkpos0= yythunkpos;
779 yyprintf((stderr, "%s\n", "Literal"));
780 { int yypos22= yypos, yythunkpos22= yythunkpos; if (!yy_NUMBER()) goto l23; goto l22;
781 l23:; yypos= yypos22; yythunkpos= yythunkpos22; if (!yy_STRING()) goto l24; goto l22;
782 l24:; yypos= yypos22; yythunkpos= yythunkpos22; if (!yy_TIME()) goto l21;
784 l22:;
785 yyprintf((stderr, " ok %s @ %s\n", "Literal", yybuf+yypos));
786 return 1;
787 l21:; yypos= yypos0; yythunkpos= yythunkpos0;
788 yyprintf((stderr, " fail %s @ %s\n", "Literal", yybuf+yypos));
789 return 0;
791 YY_RULE(int) yy_Atom()
792 { int yypos0= yypos, yythunkpos0= yythunkpos;
793 yyprintf((stderr, "%s\n", "Atom"));
794 { int yypos26= yypos, yythunkpos26= yythunkpos; if (!yy_Literal()) goto l27; goto l26;
795 l27:; yypos= yypos26; yythunkpos= yythunkpos26; if (!yy_Field()) goto l28; goto l26;
796 l28:; yypos= yypos26; yythunkpos= yythunkpos26; if (!yy_ParenExpr()) goto l25;
798 l26:; if (!yy__()) goto l25;
799 yyprintf((stderr, " ok %s @ %s\n", "Atom", yybuf+yypos));
800 return 1;
801 l25:; yypos= yypos0; yythunkpos= yythunkpos0;
802 yyprintf((stderr, " fail %s @ %s\n", "Atom", yybuf+yypos));
803 return 0;
805 YY_RULE(int) yy_GREATER()
806 { int yypos0= yypos, yythunkpos0= yythunkpos;
807 yyprintf((stderr, "%s\n", "GREATER")); if (!yymatchChar('>')) goto l29;
808 yyprintf((stderr, " ok %s @ %s\n", "GREATER", yybuf+yypos));
809 return 1;
810 l29:; yypos= yypos0; yythunkpos= yythunkpos0;
811 yyprintf((stderr, " fail %s @ %s\n", "GREATER", yybuf+yypos));
812 return 0;
814 YY_RULE(int) yy_NOT()
815 { int yypos0= yypos, yythunkpos0= yythunkpos;
816 yyprintf((stderr, "%s\n", "NOT")); if (!yymatchChar('!')) goto l30;
817 yyprintf((stderr, " ok %s @ %s\n", "NOT", yybuf+yypos));
818 return 1;
819 l30:; yypos= yypos0; yythunkpos= yythunkpos0;
820 yyprintf((stderr, " fail %s @ %s\n", "NOT", yybuf+yypos));
821 return 0;
823 YY_RULE(int) yy_EQUAL()
824 { int yypos0= yypos, yythunkpos0= yythunkpos;
825 yyprintf((stderr, "%s\n", "EQUAL")); if (!yymatchChar('=')) goto l31;
826 yyprintf((stderr, " ok %s @ %s\n", "EQUAL", yybuf+yypos));
827 return 1;
828 l31:; yypos= yypos0; yythunkpos= yythunkpos0;
829 yyprintf((stderr, " fail %s @ %s\n", "EQUAL", yybuf+yypos));
830 return 0;
832 YY_RULE(int) yy_LESS()
833 { int yypos0= yypos, yythunkpos0= yythunkpos;
834 yyprintf((stderr, "%s\n", "LESS")); if (!yymatchChar('<')) goto l32;
835 yyprintf((stderr, " ok %s @ %s\n", "LESS", yybuf+yypos));
836 return 1;
837 l32:; yypos= yypos0; yythunkpos= yythunkpos0;
838 yyprintf((stderr, " fail %s @ %s\n", "LESS", yybuf+yypos));
839 return 0;
841 YY_RULE(int) yy_Unary()
842 { int yypos0= yypos, yythunkpos0= yythunkpos; yyDo(yyPush, 1, 0);
843 yyprintf((stderr, "%s\n", "Unary"));
844 { int yypos34= yypos, yythunkpos34= yythunkpos; if (!yy_Atom()) goto l35; goto l34;
845 l35:; yypos= yypos34; yythunkpos= yythunkpos34; if (!yy_NOT()) goto l36; if (!yy__()) goto l36; if (!yy_Atom()) goto l36; yyDo(yySet, -1, 0); if (!yy__()) goto l36; yyDo(yy_1_Unary, yybegin, yyend); goto l34;
846 l36:; yypos= yypos34; yythunkpos= yythunkpos34; if (!yy_NOT()) goto l33; if (!yy__()) goto l33; if (!yy_Unary()) goto l33; yyDo(yySet, -1, 0); if (!yy__()) goto l33; yyDo(yy_2_Unary, yybegin, yyend);
848 l34:;
849 yyprintf((stderr, " ok %s @ %s\n", "Unary", yybuf+yypos)); yyDo(yyPop, 1, 0);
850 return 1;
851 l33:; yypos= yypos0; yythunkpos= yythunkpos0;
852 yyprintf((stderr, " fail %s @ %s\n", "Unary", yybuf+yypos));
853 return 0;
855 YY_RULE(int) yy_OR()
856 { int yypos0= yypos, yythunkpos0= yythunkpos;
857 yyprintf((stderr, "%s\n", "OR")); if (!yymatchString("||")) goto l37;
858 yyprintf((stderr, " ok %s @ %s\n", "OR", yybuf+yypos));
859 return 1;
860 l37:; yypos= yypos0; yythunkpos= yythunkpos0;
861 yyprintf((stderr, " fail %s @ %s\n", "OR", yybuf+yypos));
862 return 0;
864 YY_RULE(int) yy_AND()
865 { int yypos0= yypos, yythunkpos0= yythunkpos;
866 yyprintf((stderr, "%s\n", "AND")); if (!yymatchString("&&")) goto l38;
867 yyprintf((stderr, " ok %s @ %s\n", "AND", yybuf+yypos));
868 return 1;
869 l38:; yypos= yypos0; yythunkpos= yythunkpos0;
870 yyprintf((stderr, " fail %s @ %s\n", "AND", yybuf+yypos));
871 return 0;
873 YY_RULE(int) yy_CompExpr()
874 { int yypos0= yypos, yythunkpos0= yythunkpos; yyDo(yyPush, 2, 0);
875 yyprintf((stderr, "%s\n", "CompExpr")); if (!yy_Unary()) goto l39; yyDo(yySet, -2, 0);
876 { int yypos40= yypos, yythunkpos40= yythunkpos;
877 { int yypos42= yypos, yythunkpos42= yythunkpos; if (!yy_LESS()) goto l43; if (!yy__()) goto l43; if (!yy_Unary()) goto l43; yyDo(yySet, -1, 0); yyDo(yy_1_CompExpr, yybegin, yyend); goto l42;
878 l43:; yypos= yypos42; yythunkpos= yythunkpos42; if (!yy_LESS()) goto l44; if (!yy_EQUAL()) goto l44; if (!yy__()) goto l44; if (!yy_Unary()) goto l44; yyDo(yySet, -1, 0); yyDo(yy_2_CompExpr, yybegin, yyend); goto l42;
879 l44:; yypos= yypos42; yythunkpos= yythunkpos42; if (!yy_EQUAL()) goto l45; if (!yy_EQUAL()) goto l45; if (!yy__()) goto l45; if (!yy_Unary()) goto l45; yyDo(yySet, -1, 0); yyDo(yy_3_CompExpr, yybegin, yyend); goto l42;
880 l45:; yypos= yypos42; yythunkpos= yythunkpos42; if (!yy_NOT()) goto l46; if (!yy_EQUAL()) goto l46; if (!yy__()) goto l46; if (!yy_Unary()) goto l46; yyDo(yySet, -1, 0); yyDo(yy_4_CompExpr, yybegin, yyend); goto l42;
881 l46:; yypos= yypos42; yythunkpos= yythunkpos42; if (!yy_GREATER()) goto l47; if (!yy_EQUAL()) goto l47; if (!yy__()) goto l47; if (!yy_Unary()) goto l47; yyDo(yySet, -1, 0); yyDo(yy_5_CompExpr, yybegin, yyend); goto l42;
882 l47:; yypos= yypos42; yythunkpos= yythunkpos42; if (!yy_GREATER()) goto l40; if (!yy__()) goto l40; if (!yy_Unary()) goto l40; yyDo(yySet, -1, 0); yyDo(yy_6_CompExpr, yybegin, yyend);
884 l42:; goto l41;
885 l40:; yypos= yypos40; yythunkpos= yythunkpos40;
887 l41:; if (!yy__()) goto l39;
888 yyprintf((stderr, " ok %s @ %s\n", "CompExpr", yybuf+yypos)); yyDo(yyPop, 2, 0);
889 return 1;
890 l39:; yypos= yypos0; yythunkpos= yythunkpos0;
891 yyprintf((stderr, " fail %s @ %s\n", "CompExpr", yybuf+yypos));
892 return 0;
894 YY_RULE(int) yy_EOL()
895 { int yypos0= yypos, yythunkpos0= yythunkpos;
896 yyprintf((stderr, "%s\n", "EOL"));
897 { int yypos49= yypos, yythunkpos49= yythunkpos; if (!yymatchChar('\n')) goto l50; goto l49;
898 l50:; yypos= yypos49; yythunkpos= yythunkpos49; if (!yymatchString("\r\n")) goto l51; goto l49;
899 l51:; yypos= yypos49; yythunkpos= yythunkpos49; if (!yymatchChar('\r')) goto l52; goto l49;
900 l52:; yypos= yypos49; yythunkpos= yythunkpos49; if (!yymatchChar(';')) goto l48;
902 l49:;
903 yyprintf((stderr, " ok %s @ %s\n", "EOL", yybuf+yypos));
904 return 1;
905 l48:; yypos= yypos0; yythunkpos= yythunkpos0;
906 yyprintf((stderr, " fail %s @ %s\n", "EOL", yybuf+yypos));
907 return 0;
909 YY_RULE(int) yy_BoolExpr()
910 { int yypos0= yypos, yythunkpos0= yythunkpos; yyDo(yyPush, 2, 0);
911 yyprintf((stderr, "%s\n", "BoolExpr")); if (!yy_CompExpr()) goto l53; yyDo(yySet, -2, 0);
912 l54:;
913 { int yypos55= yypos, yythunkpos55= yythunkpos;
914 { int yypos56= yypos, yythunkpos56= yythunkpos; if (!yy__()) goto l57; if (!yy_AND()) goto l57; if (!yy__()) goto l57; if (!yy_CompExpr()) goto l57; yyDo(yySet, -1, 0); yyDo(yy_1_BoolExpr, yybegin, yyend); goto l56;
915 l57:; yypos= yypos56; yythunkpos= yythunkpos56; if (!yy__()) goto l55; if (!yy_OR()) goto l55; if (!yy__()) goto l55; if (!yy_CompExpr()) goto l55; yyDo(yySet, -1, 0); yyDo(yy_2_BoolExpr, yybegin, yyend);
917 l56:; goto l54;
918 l55:; yypos= yypos55; yythunkpos= yythunkpos55;
919 } if (!yy__()) goto l53;
920 yyprintf((stderr, " ok %s @ %s\n", "BoolExpr", yybuf+yypos)); yyDo(yyPop, 2, 0);
921 return 1;
922 l53:; yypos= yypos0; yythunkpos= yythunkpos0;
923 yyprintf((stderr, " fail %s @ %s\n", "BoolExpr", yybuf+yypos));
924 return 0;
926 YY_RULE(int) yy__()
928 yyprintf((stderr, "%s\n", "_"));
929 l59:;
930 { int yypos60= yypos, yythunkpos60= yythunkpos; if (!yymatchClass((unsigned char *)"\000\002\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l60; goto l59;
931 l60:; yypos= yypos60; yythunkpos= yythunkpos60;
933 yyprintf((stderr, " ok %s @ %s\n", "_", yybuf+yypos));
934 return 1;
936 YY_RULE(int) yy_Stmt()
937 { int yypos0= yypos, yythunkpos0= yythunkpos;
938 yyprintf((stderr, "%s\n", "Stmt"));
939 { int yypos62= yypos, yythunkpos62= yythunkpos; if (!yy__()) goto l63; if (!yy_BoolExpr()) goto l63; if (!yy_EOL()) goto l63; yyDo(yy_1_Stmt, yybegin, yyend); goto l62;
940 l63:; yypos= yypos62; yythunkpos= yythunkpos62;
941 l64:;
942 { int yypos65= yypos, yythunkpos65= yythunkpos;
943 { int yypos66= yypos, yythunkpos66= yythunkpos; if (!yy_EOL()) goto l66; goto l65;
944 l66:; yypos= yypos66; yythunkpos= yythunkpos66;
945 } if (!yymatchDot()) goto l65; goto l64;
946 l65:; yypos= yypos65; yythunkpos= yythunkpos65;
947 } if (!yy_EOL()) goto l61;
949 l62:;
950 yyprintf((stderr, " ok %s @ %s\n", "Stmt", yybuf+yypos));
951 return 1;
952 l61:; yypos= yypos0; yythunkpos= yythunkpos0;
953 yyprintf((stderr, " fail %s @ %s\n", "Stmt", yybuf+yypos));
954 return 0;
957 #ifndef YY_PART
959 typedef int (*yyrule)();
961 YY_PARSE(int) YYPARSEFROM(yyrule yystart)
963 int yyok;
964 if (!yybuflen)
966 yybuflen= 1024;
967 yybuf= malloc(yybuflen);
968 yytextlen= 1024;
969 yytext= malloc(yytextlen);
970 yythunkslen= 32;
971 yythunks= malloc(sizeof(yythunk) * yythunkslen);
972 yyvalslen= 32;
973 yyvals= malloc(sizeof(YYSTYPE) * yyvalslen);
974 yybegin= yyend= yypos= yylimit= yythunkpos= 0;
976 yybegin= yyend= yypos;
977 yythunkpos= 0;
978 yyval= yyvals;
979 yyok= yystart();
980 if (yyok) yyDone();
981 yyCommit();
982 return yyok;
983 (void)yyrefill;
984 (void)yymatchDot;
985 (void)yymatchChar;
986 (void)yymatchString;
987 (void)yymatchClass;
988 (void)yyDo;
989 (void)yyText;
990 (void)yyDone;
991 (void)yyCommit;
992 (void)yyAccept;
993 (void)yyPush;
994 (void)yyPop;
995 (void)yySet;
996 (void)yytextmax;
999 YY_PARSE(int) YYPARSE(void)
1001 return YYPARSEFROM(yy_Stmt);
1004 #endif
1007 value_t *
1008 parse (char *text)
1010 value_t *expr = NULL;
1012 arg_buf = text;
1013 arg_len = strlen(text);
1014 arg_off = 0;
1015 cur_expr = &expr;
1017 while (yyparse()) {
1020 return expr;
1023 #if defined(UNIT_TEST)
1025 main (int argc, char **argv)
1027 int i;
1028 value_t *expr;
1030 for (i = 1; i < argc; ++i) {
1031 expr = parse(argv[i]);
1032 if (expr) {
1033 print_value(expr);
1034 printf("= %d\n",eval(expr,unit_oget,unit_sget));
1036 else {
1037 printf("could not parse '%s'\n",argv[i]);
1041 return 0;
1043 #endif