%{
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#include "../lib/stringbuf.h"
#include "newsearch.h"
#include "parser.h"
int yylex(void);
extern char *parseStrError;
-int position;
+extern int parseStrErrorPos;
#define MAXSTACKSIZE 50
struct parserlist *next;
} parserlist;
-void yyerror(const char *str) {
- parseStrError = (char *)str;
-}
-
static int stackpos;
static parserlist *stack[MAXSTACKSIZE];
static int stackcount[MAXSTACKSIZE];
static void *fnfarg;
static parsertree **presult, sresult;
+extern int lexerror, lexpos;
+void yystrerror(const char *format, ...);
+void lexreset(void);
+
void resetparser(fnFinder fnf, void *arg, parsertree **result) {
presult = result;
+ *presult = &sresult;
sresult.exprlist = NULL;
sresult.strlist = NULL;
sresult.finished = 0;
-
+
functionfinder = fnf;
fnfarg = arg;
-
+
stackpos = 0;
+ lexreset();
+}
+
+void yyerror(const char *str) {
+ if(lexerror) {
+ lexerror = 0;
+ yystrerror("lexical error");
+ return;
+ }
+
+ parseStrError = (char *)str;
+ parseStrErrorPos = lexpos;
+}
+
+void yystrerror(const char *format, ...) {
+ va_list va;
+ static char buf[512];
+
+ parse_free(&sresult);
+
+ va_start(va, format);
+ vsnprintf(buf, sizeof(buf), format, va);
+ va_end(va);
+
+ yyerror(buf);
}
%}
stackpos--;
count = stackcount[stackpos];
- pfn = functionfinder(str, fnfarg);
+ pfn = functionfinder(str->content, fnfarg);
if(!pfn) {
- parse_free(&sresult);
+ yystrerror("unknown function: %s", str->content);
YYERROR;
}
/* if we're at the top of the stack we're the root of the tree */
function: IDENTIFIER
{
- if(stackpos >= MAXSTACKSIZE - 1)
+ if(stackpos >= MAXSTACKSIZE - 1) {
+ yyerror("function stack overflow");
YYERROR;
-
+ }
+
stackcount[stackpos] = 0;
stack[stackpos] = NULL;
invocation | literal
{
sstring *str = $1;
- parserlist *l = malloc(sizeof(parserlist) + sizeof(stringlist));
- stringlist *sl;
+ parserlist *l = malloc(sizeof(parserlist));
+ stringlist *sl = malloc(sizeof(stringlist));
l->expr = NSASTLiteral(str->content);
l->next = stack[stackpos - 1];
stack[stackpos - 1] = l;
stackcount[stackpos - 1]++;
- /* HACK */
- sl = (stringlist *)(l + 1);
-
sl->data = str;
sl->next = (*presult)->strlist;
(*presult)->strlist = sl;