2005-08-22 [paul] 1.9.13cvs50
[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 #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                         /*
61                          * a keyword consists of alpha and underscore 
62                          * characters, possibly preceded by a tilde (~)
63                          */
64
65 (~|[a-z])[a-z_]*        {
66                                 gint id;
67
68                                 if (-1 == (id = get_matchparser_tab_id(yytext))) { 
69                                         REJECT;
70                                 } else                                  
71                                         return id;
72                         }
73 [ \t]+
74 "\n"            return MATCHER_EOL;
75 "&"             return MATCHER_AND;
76 "|"             return MATCHER_OR;
77 \"              {
78                 BEGIN(string);
79                 string_buf_ptr = string_buf;
80                 }
81 <string>\"      {
82                 /* get out of the state: string ends. */
83                 BEGIN(0);
84                 *string_buf_ptr = '\0';
85                 if (!g_utf8_validate(string_buf, -1, NULL)) {
86                         gchar *tmp = conv_codeset_strdup(string_buf, conv_get_locale_charset_str(), CS_INTERNAL);
87                         if (tmp) {
88                                 strcpy(string_buf, tmp);
89                                 g_free(tmp);
90                         }
91                 }
92                 yylval.str = string_buf;
93                 return MATCHER_STRING;
94                 }
95 <string>\\.     {
96                 /* take care of quoted characters */
97                 add_char(yytext[1]);
98                 }
99 <string>.       {
100                 add_char(yytext[0]);
101                 }
102 \[[^\[\]]*\]    {
103                 /* for section name in configuration file */
104                 BEGIN(0);
105                 yylval.str = yytext + 1;
106                 yytext[strlen(yytext) - 1] = '\0';
107                 return MATCHER_SECTION;
108                 }
109 [-+]?[0-9]+     {
110                 yylval.str = yytext;
111                 return MATCHER_INTEGER;
112                 }
113 rulename        {
114                 return MATCHER_RULENAME;
115                 }
116
117 %%