5 #include "../lib/stringbuf.h"
9 #define YYSTYPE sstring *
12 extern char *parseStrError;
13 extern int parseStrErrorPos;
15 #define MAXSTACKSIZE 50
17 typedef struct parserlist {
19 struct parserlist *next;
23 static parserlist *stack[MAXSTACKSIZE];
24 static int stackcount[MAXSTACKSIZE];
25 static fnFinder functionfinder;
27 static parsertree **presult, sresult;
29 extern int lexerror, lexpos;
30 void yystrerror(const char *format, ...);
33 void resetparser(fnFinder fnf, void *arg, parsertree **result) {
37 sresult.exprlist = NULL;
38 sresult.strlist = NULL;
48 void yyerror(const char *str) {
51 yystrerror("lexical error");
55 parseStrError = (char *)str;
56 parseStrErrorPos = lexpos;
59 void yystrerror(const char *format, ...) {
66 vsnprintf(buf, sizeof(buf), format, va);
74 %token LPAREN RPAREN WHITESPACE IDENTIFIER STRING
77 invocation: LPAREN function argumentlist RPAREN
81 searchASTExpr *ap, *root;
87 count = stackcount[stackpos];
89 pfn = functionfinder(str->content, fnfarg);
91 yystrerror("unknown function: %s", str->content);
94 /* if we're at the top of the stack we're the root of the tree */
97 * we store this function containing its children in the stack list
98 * as there might be functions or other stuff after it, such as:
101 parserlist *rootp = malloc(sizeof(searchASTExpr) + sizeof(parserlist));
104 stackcount[stackpos-1]++;
105 rootp->next = stack[stackpos-1];
106 stack[stackpos-1] = rootp;
108 /* need space for the final result and real root */
109 *presult = malloc(sizeof(parsertree) + sizeof(searchASTExpr));
111 memcpy(*presult, &sresult, sizeof(parsertree));
112 (*presult)->finished = 1;
114 root = (*presult)->root;
117 xl = malloc(sizeof(expressionlist) + sizeof(searchASTExpr) * count);
118 xl->next = (*presult)->exprlist;
119 (*presult)->exprlist = xl;
122 for(i=count-1,pp=stack[stackpos];i>=0;i--,pp=npp) {
124 memcpy(&ap[i], &pp->expr, sizeof(searchASTExpr));
127 *root = NSASTManualNode(pfn, count, ap);
134 if(stackpos >= MAXSTACKSIZE - 1) {
135 yyerror("function stack overflow");
139 stackcount[stackpos] = 0;
140 stack[stackpos] = NULL;
145 argumentlist: /* empty */ | WHITESPACE argument argumentlist;
151 parserlist *l = malloc(sizeof(parserlist));
152 stringlist *sl = malloc(sizeof(stringlist));
154 l->expr = NSASTLiteral(str->content);
155 l->next = stack[stackpos - 1];
156 stack[stackpos - 1] = l;
157 stackcount[stackpos - 1]++;
160 sl->next = (*presult)->strlist;
161 (*presult)->strlist = sl;
165 literal: IDENTIFIER | STRING;