1fcdb70b76d07e1265bff34bfbb858005ef0152b
[claws.git] / src / matcher_parser_lex.l
1 %{
2 /*
3  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4  * Copyright (c) 2001-2002 by Hiroyuki Yamamoto & The Sylpheed Claws Team.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <string.h>
22 #include <glib.h>
23
24 #include "matcher.h"
25 #include "matcher_parser_lex.h"
26 #include "matcher_parser_parse.h"
27
28 #define MAX_STR_CONST 512
29 #define YY_NO_UNPUT 1
30
31 static char string_buf[MAX_STR_CONST];
32 static char *string_buf_ptr;
33
34 static void add_char(char ch)
35 {
36         if (string_buf_ptr - string_buf < sizeof(string_buf))
37                 *string_buf_ptr++ = ch;
38 }
39
40
41 /* this function will reinitializes the parser */
42
43 void matcher_parser_init(void)
44 {
45         BEGIN(0);
46 }
47 %}
48
49 %option prefix="matcher_parser"
50 %option outfile="lex.yy.c"
51 %option yylineno
52
53 %x string
54 %x section
55
56 %%
57                         
58                         /*
59                          * a keyword consists of alpha and underscore 
60                          * characters, possibly preceded by a tilde (~)
61                          */
62
63 (~|[a-z])[a-z_]*        {
64                                 gint id;
65
66                                 if (-1 == (id = get_matchparser_tab_id(yytext))) { 
67                                         REJECT;
68                                 } else                                  
69                                         return id;
70                         }
71 [ \t]+
72 "\n"            return MATCHER_EOL;
73 "&"             return MATCHER_AND;
74 "|"             return MATCHER_OR;
75 \"              {
76                 BEGIN(string);
77                 string_buf_ptr = string_buf;
78                 }
79                 /* alfons - OK, the new attempt is to just swallow 
80                  * *EVERYTHING* and make sure everything is escaped
81                  * when actually performing things. */
82 <string>\\\"    {
83                 /* take care of escaped \" because this means the
84                  * quote char should be skipped */
85                 add_char('\\');
86                 add_char('\"');
87                 }
88 <string>\"      {
89                 /* get out of the state: string ends. */
90                 BEGIN(0);
91                 *string_buf_ptr = '\0';
92                 yylval.str = string_buf;
93                 return MATCHER_STRING;
94                 }
95                 /* put everything else in the output. */
96 <string>.       {
97                 add_char(yytext[0]);
98                 }
99 \[[^\[\]]*\]    {
100                 BEGIN(0);
101                 yylval.str = yytext + 1;
102                 yytext[strlen(yytext) - 1] = '\0';
103                 return MATCHER_SECTION;
104                 }
105 [-+]?[0-9]+     {
106                 yylval.str = yytext;
107                 return MATCHER_INTEGER;
108                 }
109
110 %%