2005-01-21 [colin] 1.0.0cvs4.1
[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 "codeconv.h"
25 #include "matcher.h"
26 #include "matcher_parser_lex.h"
27 #include "matcher_parser_parse.h"
28
29 #define MAX_STR_CONST 512
30 #define YY_NO_UNPUT 1
31
32 static char string_buf[MAX_STR_CONST];
33 static char *string_buf_ptr;
34
35 static void add_char(char ch)
36 {
37         if (string_buf_ptr - string_buf < sizeof(string_buf))
38                 *string_buf_ptr++ = ch;
39 }
40
41
42 /* this function will reinitializes the parser */
43
44 void matcher_parser_init(void)
45 {
46         BEGIN(0);
47 }
48 %}
49
50 %option prefix="matcher_parser"
51 %option outfile="lex.yy.c"
52 %option yylineno
53
54 %x string
55 %x section
56
57 %%
58                         
59                         /*
60                          * a keyword consists of alpha and underscore 
61                          * characters, possibly preceded by a tilde (~)
62                          */
63
64 (~|[a-z])[a-z_]*        {
65                                 gint id;
66
67                                 if (-1 == (id = get_matchparser_tab_id(yytext))) { 
68                                         REJECT;
69                                 } else                                  
70                                         return id;
71                         }
72 [ \t]+
73 "\n"            return MATCHER_EOL;
74 "&"             return MATCHER_AND;
75 "|"             return MATCHER_OR;
76 \"              {
77                 BEGIN(string);
78                 string_buf_ptr = string_buf;
79                 }
80 <string>\"      {
81                 /* get out of the state: string ends. */
82                 BEGIN(0);
83                 *string_buf_ptr = '\0';
84                 if (!g_utf8_validate(string_buf, -1, NULL)) {
85                         gchar *tmp = conv_codeset_strdup(string_buf, conv_get_current_charset_str(), CS_UTF_8);
86                         if (tmp) {
87                                 strcpy(string_buf, tmp);
88                                 g_free(tmp);
89                         }
90                 }
91                 yylval.str = string_buf;
92                 return MATCHER_STRING;
93                 }
94 <string>\\.     {
95                 /* take care of quoted characters */
96                 add_char(yytext[1]);
97                 }
98 <string>.       {
99                 add_char(yytext[0]);
100                 }
101 \[[^\[\]]*\]    {
102                 /* for section name in configuration file */
103                 BEGIN(0);
104                 yylval.str = yytext + 1;
105                 yytext[strlen(yytext) - 1] = '\0';
106                 return MATCHER_SECTION;
107                 }
108 [-+]?[0-9]+     {
109                 yylval.str = yytext;
110                 return MATCHER_INTEGER;
111                 }
112
113 %%