]> mj.ucw.cz Git - umpf.git/blob - cond.y
reegx comparisons addded
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <pcre.h>
6
7 #define OVECCOUNT 3
8
9 int yylex (void);
10 void yyerror (char const *);
11
12 %}
13
14 %union {
15         int b;  
16         int n;
17         char* str;
18 }
19
20 %token <str> CONST
21 %token <str> REGEX
22 %token <n> NUM
23 %left EQ NEQ GE LE GT LT RE NRE
24 %left '|' '&' '^'
25 %left '!'
26 %type <b> boo
27
28 %%
29 input:  /* empty */
30         | input line
31 ;
32
33 line:   '\n'
34         | boo '\n'              { printf("%s\n",$1?"true":"false"); }
35         | error '\n'            { yyerrok; }
36 ;
37
38 boo:     CONST EQ CONST         { $$ = ! strcmp($1, $3); }
39         | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
40         | CONST RE REGEX        {       
41                                         pcre *brum;
42                                         int erroroffset;
43                                         const char* error;
44                                         int ovector[OVECCOUNT];
45
46                                         brum=pcre_compile($3,0,&error,&erroroffset,NULL);
47                                         if (!brum){
48                                                 puts("Mnau");
49                                                 return 1;
50                                         }
51
52                                         int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
53                                         
54                                         $$ = res >= 0;
55                                         pcre_free(brum);
56                                 }
57         | CONST NRE REGEX       {       
58                                         pcre *brum;
59                                         int erroroffset;
60                                         const char* error;
61                                         int ovector[OVECCOUNT];
62
63                                         brum=pcre_compile($3,0,&error,&erroroffset,NULL);
64                                         if (!brum){
65                                                 puts("Mnau");
66                                                 return 1;
67                                         }
68
69                                         int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
70                                         
71                                         $$ = res < 0;
72                                         pcre_free(brum);
73                                 }
74         | NUM EQ NUM            { $$ = $1 == $3 }
75         | NUM NEQ NUM           { $$ = $1 != $3 }
76         | NUM GE NUM            { $$ = $1 >= $3 }
77         | NUM LE NUM            { $$ = $1 <= $3 }
78         | NUM GT NUM            { $$ = $1 > $3 }
79         | NUM LT NUM            { $$ = $1 < $3 }
80         | boo '|' boo           { $$ = $1 || $3 }
81         | boo '&' boo           { $$ = $1 && $3 }
82         | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
83         | '!' boo               { $$ = ! $2 }
84 ;
85
86 ;
87 %%
88
89 #include <ctype.h>
90 #include <stdlib.h>
91
92 #define BUFSIZE 4096
93
94 int
95 yylex(void)
96 {
97
98         int i, c, last;
99         
100         while ((c = getchar ()) == ' ' || c == '\t');
101         
102         if (c == '"'){
103                 last = '"';
104                 i = 0;
105                 if (!(yylval.str = malloc(BUFSIZE))){
106                         puts("Low memory");
107                         exit(0);
108                 }
109                 while ((c = getchar()) != '"' || last == '\\'){
110                         if (last=='\\' && c != '"')
111                                 yylval.str[i-1] = c;
112                         else {          
113                                 yylval.str[i] = c;
114                                 i++;
115                         }
116                         last = c;
117                         if (i >= BUFSIZE-1)
118                                 break;
119                 }       
120                 yylval.str[i] = '\0';
121
122                 return CONST;   
123         }
124
125         if (c == '\''){
126                 last = '\'';
127                 i = 0;
128                 if (!(yylval.str = malloc(BUFSIZE))){
129                         puts("Low memory");
130                         exit(0);
131                 }
132                 while ((c = getchar()) != '\'' || last == '\\'){
133                         if (last=='\\' && c != '\'')
134                                 yylval.str[i-1] = c;
135                         else {          
136                                 yylval.str[i] = c;
137                                 i++;
138                         }
139                         last = c;
140                         if (i >= BUFSIZE-1)
141                                 break;
142                 }       
143                 yylval.str[i] = '\0';
144
145                 return CONST;   
146         }
147
148         if (c == '/'){
149                 last = '/';
150                 i = 0;
151                 if (!(yylval.str = malloc(BUFSIZE))){
152                         puts("Low memory");
153                         exit(0);
154                 }
155                 while ((c = getchar()) != '/' || last == '\\'){
156                         if (last=='\\' && c != '/')
157                                 yylval.str[i-1] = c;
158                         else {          
159                                 yylval.str[i] = c;
160                                 i++;
161                         }
162                         last = c;
163                         if (i >= BUFSIZE-1)
164                                 break;
165                 }       
166                 yylval.str[i] = '\0';
167         
168                 return REGEX;   
169         }
170
171         if (isdigit(c)){
172                 ungetc(c,stdin);
173                 scanf("%d",&yylval.n);
174                 return NUM;
175         }
176
177         if (c == '!'){
178                 if ((c = getchar ()) == '=')
179                         return NEQ;
180                 else if (c == '~')
181                         return NRE;
182                 else
183                         ungetc(c,stdin);
184         }
185
186         if (c == '<'){
187                 if ((c = getchar ()) == '=')
188                         return LE;
189                 else
190                         return LT;
191         }
192
193         if (c == '>'){
194                 if ((c = getchar ()) == '=')
195                         return GE;
196                 else
197                         return GT;
198         }
199
200         if (c == '='){
201                 if ((c = getchar ()) == '=')
202                         return EQ;
203                 else
204                         ungetc(c,stdin);
205         }
206
207         if (c == '~'){
208                 if ((c = getchar ()) == '~')
209                         return RE;
210                 else
211                         ungetc(c,stdin);
212         }
213         
214         if (c == EOF)
215                 return 0;
216         
217         return c;
218 }
219
220 void
221 yyerror (char const *s)
222 {
223         fprintf (stderr, "%s\n", s);
224 }
225
226 int
227 main(void)
228 {
229         return yyparse ();
230 }