Upstream tarball 20080304
[amule.git] / src / Scanner.l
blobf78f174013d1a4ed6b0f037d1338aa19b47a0147
1 %{
2 #include <stdio.h>
3 #include "SearchExpr.h"
4 #include "Parser.hpp"
5 #include "ED2KLink.h"
6 #include <wx/string.h>
8 #include "libs/common/StringFunctions.h"
10 #ifdef _DEBUG
11 #define new DEBUG_NEW
12 #undef THIS_FILE
13 static char THIS_FILE[] = __FILE__;
14 #endif
16 #define YY_NEVER_INTERACTIVE 1
18 extern int yyerror(const char* errstr);
19 extern int yyerror(wxString errstr);
21 #define YY_INPUT                        ReadLexBuff
22 #define YY_FATAL_ERROR          FatalLexError
24 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize);
25 static void FatalLexError(yyconst char msg[]);
27 static char* _pszLexBuff;
28 static char* _pszLexStr;
32 %option noyywrap
34 keywordchar             [^ \"()]
38 [ ]                             { /* Skip blanks. */ }
39 "OR"                    { return TOK_OR; }
40 "AND"                   { return TOK_AND; }
41 "NOT"                   { return TOK_NOT; }
43 "ed2k::"[a-fA-F0-9]{32} {
44                                         yylval.pstr = new wxString(UTF82unicode(yytext));
45                                         return TOK_ED2K_LINK;
46                                 }
48 {keywordchar}*  {
49                                         yylval.pstr = new wxString(UTF82unicode(yytext));
50                                         return TOK_STRING;
51                 }
53 "\""                    {
54                                         int l = 128;
55                                         char* psz = (char*)malloc(l);
56                                         int i = 0;
57                                         int c;
58                                         while ((c = yyinput()) != '\"')
59                                         {
60                                                 if (c == EOF || c == '\n'){
61                                                         unput(c);
62                                                         yyerror(wxT("Search expression error: unterminated string"));
63                                                         break;
64                                                 }
65                                                 if (c == '\\'){         /*Escape sequence*/
66                                                         switch (c = yyinput())
67                                                         {
68                                                         case '\n':
69                                                                 continue;
70                                                         case 't':               /*Tab*/
71                                                                 c = '\t';
72                                                                 break;
73                                                         case 'n':               /*Linefeed*/
74                                                                 c = '\n';
75                                                                 break;
76                                                         case 'f':               /*Formfeed*/
77                                                                 c = '\f';
78                                                                 break;
79                                                         case 'r':               /*Carriage return*/
80                                                                 c = '\r';
81                                                                 break;
82                                                         case '\\':              /*Backslash*/
83                                                                 c = '\\';
84                                                                 break;
85                                                         case '"':               /*Double quotation mark*/
86                                                                 c = '\"';
87                                                                 break;
88                                                         case '\'':              /*Single quotation mark*/
89                                                                 c = '\'';
90                                                                 break;
91                                                         case '?':               /*Question mark*/
92                                                                 c = '\?';
93                                                                 break;
94                                                         case 'v':               /*Vertical Tab*/
95                                                                 c = '\v';
96                                                                 break;
97                                                         case 'a':               /*Alert*/
98                                                                 c = '\a';
99                                                                 break;
100                                                         case 'b':               /*Backspace*/
101                                                                 c = '\b';
102                                                                 break;
103                                                         case 'x':               /*Hexadecimal number*/
104                                                                 {
105                                                                         int n, octv;
106                                                                         for (n = 1, octv = 0; n <= 3; n++) {
107                                                                                 if ((c = yyinput()) >= '0' && c <= '9')
108                                                                                         c -= '0';
109                                                                                 else if (c >= 'a' && c <= 'f')
110                                                                                         c = (c - 'a') + 10;
111                                                                                 else if (c >= 'A' && c <= 'F')
112                                                                                         c = (c - 'A') + 10;
113                                                                                 else
114                                                                                         break;
115                                                                                 octv = octv * 16 + c;
116                                                                         }
117                                                                         unput(c);
118                                                                         if (n == 1)
119                                                                                 c = 'x';
120                                                                         else
121                                                                                 c = octv;
122                                                                 }
123                                                                 break;
124                                                         }
125                                                 } else {
126                                                         if ((unsigned char)c >= 0x80/* && IsDBCSLeadByte(yytext[0]) */){
127                                                                 psz[i++] = (unsigned char)c;
128                                                                 if (i >= l){
129                                                                         psz = (char*)realloc(psz, l += 128);
130                                                                         if (psz == NULL){
131                                                                                 yyerror("Less memory for string");
132                                                                                 break;
133                                                                         }
134                                                                 }
135                                                                 c = yyinput();
136                                                         }
137                                                 }
139                                                 psz[i++] = (unsigned char)c;
140                                                 if (i >= l){
141                                                         psz = (char*)realloc(psz, l += 128);
142                                                         if (psz == NULL){
143                                                                 yyerror("Less memory for string");
144                                                                 break;
145                                                         }
146                                                 }
147                                         }
148                                         psz[i] = '\0';
149                                         yylval.pstr = new wxString(UTF82unicode(psz));
150                                         free(psz);
151                                         return TOK_STRING;
152                                 }
154 .                               { return yytext[0]; }
158 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize)
160         wxASSERT( _pszLexBuff != NULL );
161         
162         if (_pszLexBuff == NULL) {
163                 YY_FATAL_ERROR("Input in flex scanner failed");
164         }
166         wxASSERT( sizeof(YY_CHAR) == sizeof(char) );
167         size_t uCharsInBuff = strlen(_pszLexBuff);
168         size_t uCharsRead = min(uMaxSize, uCharsInBuff);
169         riResult = uCharsRead;
170         memcpy(pcBuff, _pszLexBuff, uCharsRead);
171         _pszLexBuff += uCharsRead;
174 static void FatalLexError(yyconst char msg[])
176 #ifdef _CONSOLE
177         printf("Fatal error in flex scanner: %s\n", msg);
178 #else
179         printf("Fatal error in flex scanner: %s\n", msg);
180 #endif
183 void LexInit(const wxString& pszInput)
185         _pszLexStr = strdup(unicode2UTF8(pszInput));
186         _pszLexBuff = _pszLexStr;
189 void LexFree()
191         yylex_destroy();
192         
193         yyleng = 0;
194         yytext = NULL;
195         yyin = NULL;
196         yyout = NULL;
197         yy_hold_char = '\0';
198         yy_n_chars = 0;
199         yy_c_buf_p = NULL;
200         yy_start = 0;
201         yy_did_buffer_switch_on_eof = 0;
202         yy_last_accepting_state = 0;
203         yy_last_accepting_cpos = NULL;
204         
205 #if YY_STACK_USED
206         yy_start_stack_ptr = 0;
207         yy_start_stack_depth = 0;
208         yy_start_stack = NULL;
209 #endif
211         free(_pszLexStr);