Either the comment is wrong or it's not needed anymore
[amule.git] / src / Scanner.l
blob5130726436467603dd29c2d491ef30520dd79a1c
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 _MSC_VER
11 #define isatty(DUMMY) 0
12 #endif
14 #ifdef _DEBUG
15 #define new DEBUG_NEW
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
20 #define YY_NEVER_INTERACTIVE 1
22 extern int yyerror(const char* errstr);
23 extern int yyerror(wxString errstr);
25 #define YY_INPUT                        ReadLexBuff
26 #define YY_FATAL_ERROR          FatalLexError
28 static void ReadLexBuff(char* pcBuff, size_t& riResult, size_t uMaxSize);
29 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize);
30 static void FatalLexError(yyconst char msg[]);
32 static char* _pszLexBuff;
33 static char* _pszLexStr;
37 %option noyywrap
39 keywordchar             [^ \"()]
43 [ ]                             { /* Skip blanks. */ }
44 "OR"                    { return TOK_OR; }
45 "AND"                   { return TOK_AND; }
46 "NOT"                   { return TOK_NOT; }
48 "ed2k::"[a-fA-F0-9]{32} {
49                                         yylval.pstr = new wxString(UTF82unicode(yytext));
50                                         return TOK_ED2K_LINK;
51                                 }
53 {keywordchar}*  {
54                                         yylval.pstr = new wxString(UTF82unicode(yytext));
55                                         return TOK_STRING;
56                 }
58 "\""                    {
59                                         int l = 128;
60                                         char* psz = (char*)malloc(l);
61                                         int i = 0;
62                                         int c;
63                                         while ((c = yyinput()) != '\"')
64                                         {
65                                                 if (c == EOF || c == '\n'){
66                                                         unput(c);
67                                                         yyerror(wxT("Search expression error: unterminated string"));
68                                                         break;
69                                                 }
70                                                 if (c == '\\'){         /*Escape sequence*/
71                                                         switch (c = yyinput())
72                                                         {
73                                                         case '\n':
74                                                                 continue;
75                                                         case 't':               /*Tab*/
76                                                                 c = '\t';
77                                                                 break;
78                                                         case 'n':               /*Linefeed*/
79                                                                 c = '\n';
80                                                                 break;
81                                                         case 'f':               /*Formfeed*/
82                                                                 c = '\f';
83                                                                 break;
84                                                         case 'r':               /*Carriage return*/
85                                                                 c = '\r';
86                                                                 break;
87                                                         case '\\':              /*Backslash*/
88                                                                 c = '\\';
89                                                                 break;
90                                                         case '"':               /*Double quotation mark*/
91                                                                 c = '\"';
92                                                                 break;
93                                                         case '\'':              /*Single quotation mark*/
94                                                                 c = '\'';
95                                                                 break;
96                                                         case '?':               /*Question mark*/
97                                                                 c = '\?';
98                                                                 break;
99                                                         case 'v':               /*Vertical Tab*/
100                                                                 c = '\v';
101                                                                 break;
102                                                         case 'a':               /*Alert*/
103                                                                 c = '\a';
104                                                                 break;
105                                                         case 'b':               /*Backspace*/
106                                                                 c = '\b';
107                                                                 break;
108                                                         case 'x':               /*Hexadecimal number*/
109                                                                 {
110                                                                         int n, octv;
111                                                                         for (n = 1, octv = 0; n <= 3; n++) {
112                                                                                 if ((c = yyinput()) >= '0' && c <= '9')
113                                                                                         c -= '0';
114                                                                                 else if (c >= 'a' && c <= 'f')
115                                                                                         c = (c - 'a') + 10;
116                                                                                 else if (c >= 'A' && c <= 'F')
117                                                                                         c = (c - 'A') + 10;
118                                                                                 else
119                                                                                         break;
120                                                                                 octv = octv * 16 + c;
121                                                                         }
122                                                                         unput(c);
123                                                                         if (n == 1)
124                                                                                 c = 'x';
125                                                                         else
126                                                                                 c = octv;
127                                                                 }
128                                                                 break;
129                                                         }
130                                                 } else {
131                                                         if ((unsigned char)c >= 0x80/* && IsDBCSLeadByte(yytext[0]) */){
132                                                                 psz[i++] = (unsigned char)c;
133                                                                 if (i >= l) {
134                                                                         char *tmp = (char*)realloc(psz, l += 128);
135                                                                         if (tmp == NULL){
136                                                                                 yyerror("Less memory for string");
137                                                                                 break;
138                                                                         } else {
139                                                                                 psz = tmp;
140                                                                         }
141                                                                 }
142                                                                 c = yyinput();
143                                                         }
144                                                 }
146                                                 psz[i++] = (unsigned char)c;
147                                                 if (i >= l) {
148                                                         char *tmp = (char*)realloc(psz, l += 128);
149                                                         if (tmp == NULL){
150                                                                 yyerror("Less memory for string");
151                                                                 break;
152                                                         } else {
153                                                                 psz = tmp;
154                                                         }
155                                                 }
156                                         }
157                                         psz[i] = '\0';
158                                         yylval.pstr = new wxString(UTF82unicode(psz));
159                                         free(psz);
160                                         return TOK_STRING;
161                                 }
163 .                               { return yytext[0]; }
167 static void ReadLexBuff(char* pcBuff, size_t& riResult, size_t uMaxSize)
169         wxASSERT( _pszLexBuff != NULL );
171         if (_pszLexBuff == NULL) {
172                 YY_FATAL_ERROR("Input in flex scanner failed");
173         }
175         wxASSERT( sizeof(YY_CHAR) == sizeof(char) );
176         size_t uCharsInBuff = strlen(_pszLexBuff);
177         size_t uCharsRead = min(uMaxSize, uCharsInBuff);
178         riResult = uCharsRead;
179         memcpy(pcBuff, _pszLexBuff, uCharsRead);
180         _pszLexBuff += uCharsRead;
183 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize)
185         size_t st_result = static_cast<size_t>(riResult);
186         ReadLexBuff(pcBuff, st_result, uMaxSize);
187         riResult = static_cast<int>(st_result);
190 static void FatalLexError(yyconst char msg[])
192 #ifdef _CONSOLE
193         printf("Fatal error in flex scanner: %s\n", msg);
194 #else
195         printf("Fatal error in flex scanner: %s\n", msg);
196 #endif
199 void LexInit(const wxString& pszInput)
201         _pszLexStr = strdup(unicode2UTF8(pszInput));
202         _pszLexBuff = _pszLexStr;
205 void LexFree()
207         yylex_destroy();
209         yyleng = 0;
210         yytext = NULL;
211         yyin = NULL;
212         yyout = NULL;
213         yy_hold_char = '\0';
214         yy_n_chars = 0;
215         yy_c_buf_p = NULL;
216         yy_start = 0;
217         yy_did_buffer_switch_on_eof = 0;
218         yy_last_accepting_state = 0;
219         yy_last_accepting_cpos = NULL;
221 #if YY_STACK_USED
222         yy_start_stack_ptr = 0;
223         yy_start_stack_depth = 0;
224         yy_start_stack = NULL;
225 #endif
227         free(_pszLexStr);