]> mj.ucw.cz Git - umpf.git/commitdiff
reegx comparisons addded
authorAnicka Bernathova <anicka@anicka.net>
Wed, 2 Jul 2008 14:30:30 +0000 (16:30 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Wed, 2 Jul 2008 14:30:30 +0000 (16:30 +0200)
Makefile
cond.y

index 5734f8f44f3cbe866e66c0822229ffb4780fa722..458e8e4a13a236b1adbc8e0c13de4124e848b460 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 all: bison
-       gcc -Wall -O2 -o cond -g cond.tab.c
+       gcc -Wall -O2 -o cond -g -lpcre cond.tab.c
 
 bison:
        bison cond.y
diff --git a/cond.y b/cond.y
index 962fcb04c8728f9ba15f69f196432323258eb1ed..090cfec5e964746ba3fa845cd78a7f36998797ad 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -2,6 +2,9 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <pcre.h>
+
+#define OVECCOUNT 3
 
 int yylex (void);
 void yyerror (char const *);
@@ -15,8 +18,9 @@ void yyerror (char const *);
 }
 
 %token <str> CONST
+%token <str> REGEX
 %token <n> NUM
-%left EQ NEQ GE LE GT LT
+%left EQ NEQ GE LE GT LT RE NRE
 %left '|' '&' '^'
 %left '!'
 %type <b> boo
@@ -33,6 +37,40 @@ line:        '\n'
 
 boo:    CONST EQ CONST         { $$ = ! strcmp($1, $3); }
        | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
+       | CONST RE REGEX        {       
+                                       pcre *brum;
+                                               int erroroffset;
+                                               const char* error;
+                                       int ovector[OVECCOUNT];
+
+                                       brum=pcre_compile($3,0,&error,&erroroffset,NULL);
+                                       if (!brum){
+                                               puts("Mnau");
+                                               return 1;
+                                               }
+
+                                       int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
+                                       
+                                       $$ = res >= 0;
+                                       pcre_free(brum);
+                               }
+       | CONST NRE REGEX       {       
+                                       pcre *brum;
+                                               int erroroffset;
+                                               const char* error;
+                                       int ovector[OVECCOUNT];
+
+                                       brum=pcre_compile($3,0,&error,&erroroffset,NULL);
+                                       if (!brum){
+                                               puts("Mnau");
+                                               return 1;
+                                               }
+
+                                       int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
+                                       
+                                       $$ = res < 0;
+                                       pcre_free(brum);
+                               }
        | NUM EQ NUM            { $$ = $1 == $3 }
        | NUM NEQ NUM           { $$ = $1 != $3 }
        | NUM GE NUM            { $$ = $1 >= $3 }
@@ -69,17 +107,67 @@ yylex(void)
                        exit(0);
                }
                while ((c = getchar()) != '"' || last == '\\'){
-                       yylval.str[i] = c;
+                       if (last=='\\' && c != '"')
+                               yylval.str[i-1] = c;
+                       else {          
+                               yylval.str[i] = c;
+                               i++;
+                       }
                        last = c;
-                       i++;
                        if (i >= BUFSIZE-1)
                                break;
                }       
                yylval.str[i] = '\0';
-       
+
+               return CONST;   
+       }
+
+       if (c == '\''){
+               last = '\'';
+               i = 0;
+               if (!(yylval.str = malloc(BUFSIZE))){
+                       puts("Low memory");
+                       exit(0);
+               }
+               while ((c = getchar()) != '\'' || last == '\\'){
+                       if (last=='\\' && c != '\'')
+                               yylval.str[i-1] = c;
+                       else {          
+                               yylval.str[i] = c;
+                               i++;
+                       }
+                       last = c;
+                       if (i >= BUFSIZE-1)
+                               break;
+               }       
+               yylval.str[i] = '\0';
+
                return CONST;   
        }
 
+       if (c == '/'){
+               last = '/';
+               i = 0;
+               if (!(yylval.str = malloc(BUFSIZE))){
+                       puts("Low memory");
+                       exit(0);
+               }
+               while ((c = getchar()) != '/' || last == '\\'){
+                       if (last=='\\' && c != '/')
+                               yylval.str[i-1] = c;
+                       else {          
+                               yylval.str[i] = c;
+                               i++;
+                       }
+                       last = c;
+                       if (i >= BUFSIZE-1)
+                               break;
+               }       
+               yylval.str[i] = '\0';
+       
+               return REGEX;   
+       }
+
        if (isdigit(c)){
                ungetc(c,stdin);
                scanf("%d",&yylval.n);
@@ -89,6 +177,10 @@ yylex(void)
        if (c == '!'){
                if ((c = getchar ()) == '=')
                        return NEQ;
+               else if (c == '~')
+                       return NRE;
+               else
+                       ungetc(c,stdin);
        }
 
        if (c == '<'){
@@ -108,6 +200,15 @@ yylex(void)
        if (c == '='){
                if ((c = getchar ()) == '=')
                        return EQ;
+               else
+                       ungetc(c,stdin);
+       }
+
+       if (c == '~'){
+               if ((c = getchar ()) == '~')
+                       return RE;
+               else
+                       ungetc(c,stdin);
        }
        
        if (c == EOF)