]> jfr.im git - irc/quakenet/newserv.git/blobdiff - newsearch/newsearch.y
LUA: add function for channel chanop notice
[irc/quakenet/newserv.git] / newsearch / newsearch.y
index 7f70dcdeea7ffdbc02a1e9477660d8abc873db33..bcf26cb731627d047c1c46111503985dc182ffb2 100644 (file)
@@ -1,15 +1,16 @@
 %{
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
 #include "../lib/stringbuf.h"
 #include "newsearch.h"
 #include "parser.h"
 
-#define YYSTYPE char *
+#define YYSTYPE sstring *
 
 int yylex(void);
 extern char *parseStrError;
-int position;
+extern int parseStrErrorPos;
 
 #define MAXSTACKSIZE 50
 
@@ -18,10 +19,6 @@ typedef struct parserlist {
   struct parserlist *next;
 } parserlist;
 
-void yyerror(const char *str) {
-  parseStrError = (char *)str;
-}
-
 static int stackpos;
 static parserlist *stack[MAXSTACKSIZE];
 static int stackcount[MAXSTACKSIZE];
@@ -29,17 +26,47 @@ static fnFinder functionfinder;
 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);
 }
 
 %}
@@ -49,7 +76,7 @@ void resetparser(fnFinder fnf, void *arg, parsertree **result) {
 %%
 invocation: LPAREN function argumentlist RPAREN
        {
-    char *str = $2;
+    sstring *str = $2;
     int i, count;
     searchASTExpr *ap, *root;
     parserlist *pp, *npp;
@@ -59,9 +86,9 @@ invocation: LPAREN function argumentlist RPAREN
     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 */
@@ -99,14 +126,16 @@ invocation: LPAREN function argumentlist RPAREN
     }
     *root = NSASTManualNode(pfn, count, ap);
    
-    free(str);
+    freesstring(str);
        }
 
 function: IDENTIFIER
   {
-    if(stackpos >= MAXSTACKSIZE - 1)
+    if(stackpos >= MAXSTACKSIZE - 1) {
+      yyerror("function stack overflow");
       YYERROR;
-      
+    }
+
     stackcount[stackpos] = 0;
     stack[stackpos] = NULL;
     
@@ -118,21 +147,26 @@ argumentlist: /* empty */ | WHITESPACE argument argumentlist;
 argument:
        invocation | literal
        {
-    char *str = $1;
-    parserlist *l = malloc(sizeof(parserlist) + sizeof(stringlist));
-    stringlist *sl;
-    
-    l->expr = NSASTLiteral(str);
+    sstring *str = $1;
+    parserlist *l = malloc(sizeof(parserlist));
+
+    if(str) {
+      stringlist *sl;
+      l->expr = NSASTLiteral(str->content);
+
+      sl = malloc(sizeof(stringlist));
+      sl->data = str;
+      sl->next = (*presult)->strlist;
+      (*presult)->strlist = sl;
+    } else {
+      l->expr = NSASTLiteral("");
+    }
+
     l->next = stack[stackpos - 1];
     stack[stackpos - 1] = l;
     stackcount[stackpos - 1]++;
 
-    /* HACK */
-    sl = (stringlist *)(l + 1);
-    
-    sl->next = (*presult)->strlist;
-    (*presult)->strlist = sl;
        }
        ;
 
-literal: IDENTIFIER | STRING;
\ No newline at end of file
+literal: IDENTIFIER | STRING;