2006-08-26 [mones] 2.4.0cvs95
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <string.h>
22 #include <glib.h>
23
24 #include "codeconv.h"
25 #include "matcher.h"
26 #include "matcher_parser_lex.h"
27 #ifndef YYSTYPE
28 #include "matcher_parser_parse.h"
29 #endif
30 #define MAX_STR_CONST 512
31 #define YY_NO_UNPUT 1
32
33 static char string_buf[MAX_STR_CONST];
34 static char *string_buf_ptr;
35
36 static void add_char(char ch)
37 {
38         if (string_buf_ptr - string_buf < sizeof(string_buf))
39                 *string_buf_ptr++ = ch;
40 }
41
42
43 /* this function will reinitializes the parser */
44
45 void matcher_parser_init(void)
46 {
47         BEGIN(0);
48 }
49 %}
50
51 %option prefix="matcher_parser"
52 %option outfile="lex.yy.c"
53 %option yylineno
54
55 %x string
56 %x section
57
58 %%
59                         
60 "in"    return MATCHER_IN; 
61
62                         /*
63                          * a keyword consists of alpha and underscore 
64                          * characters, possibly preceded by a tilde (~)
65                          */
66
67 (~|[a-z])[a-z_]*        {
68                                 gint id;
69
70                                 if (-1 == (id = get_matchparser_tab_id(yytext))) { 
71                                         REJECT;
72                                 } else                                  
73                                         return id;
74                         }
75 [ \t]+
76 "\n"            return MATCHER_EOL;
77 "&"             return MATCHER_AND;
78 "|"             return MATCHER_OR;
79 \"              {
80                 BEGIN(string);
81                 string_buf_ptr = string_buf;
82                 }
83 <string>\"      {
84                 /* get out of the state: string ends. */
85                 BEGIN(0);
86                 *string_buf_ptr = '\0';
87                 if (!g_utf8_validate(string_buf, -1, NULL)) {
88                         gchar *tmp = conv_codeset_strdup(string_buf, conv_get_locale_charset_str(), CS_INTERNAL);
89                         if (tmp) {
90                                 strcpy(string_buf, tmp);
91                                 g_free(tmp);
92                         }
93                 }
94                 yylval.str = string_buf;
95                 return MATCHER_STRING;
96                 }
97 <string>\\.     {
98                 /* take care of quoted characters */
99                 add_char(yytext[1]);
100                 }
101 <string>.       {
102                 add_char(yytext[0]);
103                 }
104 \[[^\[\]]*\]    {
105                 /* for section name in configuration file */
106                 BEGIN(0);
107                 yylval.str = yytext + 1;
108                 yytext[strlen(yytext) - 1] = '\0';
109                 return MATCHER_SECTION;
110                 }
111 [-+]?[0-9]+     {
112                 yylval.str = yytext;
113                 return MATCHER_INTEGER;
114                 }
115 rulename        {
116                 return MATCHER_RULENAME;
117                 }
118 disabled        {
119                 return MATCHER_DISABLED;
120                 }
121 account {
122                 return MATCHER_ACCOUNT;
123                 }
124 enabled {
125                 return MATCHER_ENABLED;
126                 }
127
128 %%