debian: fix build-deps for focal
[amule.git] / src / Scanner.l
blobc86ce1f07591370fa308f4c25a6f9fbb0f716d26
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 #if defined(_MSC_VER) && (_MSC_VER >= 1800) 
15 #include <algorithm> // for std::min and std::max 
16 #endif
18 #ifdef _DEBUG
19 #define new DEBUG_NEW
20 #undef THIS_FILE
21 static char THIS_FILE[] = __FILE__;
22 #endif
24 #define YY_NEVER_INTERACTIVE 1
26 extern int yyerror(const char* errstr);
27 extern int yyerror(wxString errstr);
29 #define YY_INPUT                        ReadLexBuff
30 #define YY_FATAL_ERROR          FatalLexError
32 static void ReadLexBuff(char* pcBuff, size_t& riResult, size_t uMaxSize);
33 static void FatalLexError(yyconst char msg[]);
35 static char* _pszLexBuff;
36 static char* _pszLexStr;
38 void LexInit(const wxString& pszInput);
39 void LexFree();
43 %option noyywrap
45 keywordchar             [^ \"()]
49 [ ]                             { /* Skip blanks. */ }
50 "OR"                    { return TOK_OR; }
51 "AND"                   { return TOK_AND; }
52 "NOT"                   { return TOK_NOT; }
54 "ed2k::"[a-fA-F0-9]{32} {
55                                         yylval.pstr = new wxString(UTF82unicode(yytext));
56                                         return TOK_ED2K_LINK;
57                                 }
59 {keywordchar}*  {
60                                         yylval.pstr = new wxString(UTF82unicode(yytext));
61                                         return TOK_STRING;
62                 }
64 "\""                    {
65                                         int l = 128;
66                                         char* psz = (char*)malloc(l);
67                                         int i = 0;
68                                         int c;
69                                         while ((c = yyinput()) != '\"')
70                                         {
71                                                 if (c == EOF || c == '\n'){
72                                                         unput(c);
73                                                         yyerror(wxT("Search expression error: unterminated string"));
74                                                         break;
75                                                 }
76                                                 if (c == '\\'){         /*Escape sequence*/
77                                                         switch (c = yyinput())
78                                                         {
79                                                         case '\n':
80                                                                 continue;
81                                                         case 't':               /*Tab*/
82                                                                 c = '\t';
83                                                                 break;
84                                                         case 'n':               /*Linefeed*/
85                                                                 c = '\n';
86                                                                 break;
87                                                         case 'f':               /*Formfeed*/
88                                                                 c = '\f';
89                                                                 break;
90                                                         case 'r':               /*Carriage return*/
91                                                                 c = '\r';
92                                                                 break;
93                                                         case '\\':              /*Backslash*/
94                                                                 c = '\\';
95                                                                 break;
96                                                         case '"':               /*Double quotation mark*/
97                                                                 c = '\"';
98                                                                 break;
99                                                         case '\'':              /*Single quotation mark*/
100                                                                 c = '\'';
101                                                                 break;
102                                                         case '?':               /*Question mark*/
103                                                                 c = '\?';
104                                                                 break;
105                                                         case 'v':               /*Vertical Tab*/
106                                                                 c = '\v';
107                                                                 break;
108                                                         case 'a':               /*Alert*/
109                                                                 c = '\a';
110                                                                 break;
111                                                         case 'b':               /*Backspace*/
112                                                                 c = '\b';
113                                                                 break;
114                                                         case 'x':               /*Hexadecimal number*/
115                                                                 {
116                                                                         int n, octv;
117                                                                         for (n = 1, octv = 0; n <= 3; n++) {
118                                                                                 if ((c = yyinput()) >= '0' && c <= '9')
119                                                                                         c -= '0';
120                                                                                 else if (c >= 'a' && c <= 'f')
121                                                                                         c = (c - 'a') + 10;
122                                                                                 else if (c >= 'A' && c <= 'F')
123                                                                                         c = (c - 'A') + 10;
124                                                                                 else
125                                                                                         break;
126                                                                                 octv = octv * 16 + c;
127                                                                         }
128                                                                         unput(c);
129                                                                         if (n == 1)
130                                                                                 c = 'x';
131                                                                         else
132                                                                                 c = octv;
133                                                                 }
134                                                                 break;
135                                                         }
136                                                 } else {
137                                                         if ((unsigned char)c >= 0x80/* && IsDBCSLeadByte(yytext[0]) */){
138                                                                 psz[i++] = (unsigned char)c;
139                                                                 if (i >= l) {
140                                                                         char *tmp = (char*)realloc(psz, l += 128);
141                                                                         if (tmp == NULL){
142                                                                                 yyerror("Less memory for string");
143                                                                                 break;
144                                                                         } else {
145                                                                                 psz = tmp;
146                                                                         }
147                                                                 }
148                                                                 c = yyinput();
149                                                         }
150                                                 }
152                                                 psz[i++] = (unsigned char)c;
153                                                 if (i >= l) {
154                                                         char *tmp = (char*)realloc(psz, l += 128);
155                                                         if (tmp == NULL){
156                                                                 yyerror("Less memory for string");
157                                                                 break;
158                                                         } else {
159                                                                 psz = tmp;
160                                                         }
161                                                 }
162                                         }
163                                         psz[i] = '\0';
164                                         yylval.pstr = new wxString(UTF82unicode(psz));
165                                         free(psz);
166                                         return TOK_STRING;
167                                 }
169 .                               { return yytext[0]; }
173 static void ReadLexBuff(char* pcBuff, size_t& riResult, size_t uMaxSize)
175         wxASSERT( _pszLexBuff != NULL );
177         if (_pszLexBuff == NULL) {
178                 YY_FATAL_ERROR("Input in flex scanner failed");
179         }
181         wxASSERT( sizeof(YY_CHAR) == sizeof(char) );
182         size_t uCharsInBuff = strlen(_pszLexBuff);
183         size_t uCharsRead = min(uMaxSize, uCharsInBuff);
184         riResult = uCharsRead;
185         memcpy(pcBuff, _pszLexBuff, uCharsRead);
186         _pszLexBuff += uCharsRead;
189 static void FatalLexError(yyconst char msg[])
191 #ifdef _CONSOLE
192         printf("Fatal error in flex scanner: %s\n", msg);
193 #else
194         printf("Fatal error in flex scanner: %s\n", msg);
195 #endif
198 void LexInit(const wxString& pszInput)
200         _pszLexStr = strdup(unicode2UTF8(pszInput));
201         _pszLexBuff = _pszLexStr;
204 void LexFree()
206         yylex_destroy();
208         yyleng = 0;
209         yytext = NULL;
210         yyin = NULL;
211         yyout = NULL;
212         yy_hold_char = '\0';
213         yy_n_chars = 0;
214         yy_c_buf_p = NULL;
215         yy_start = 0;
216         yy_did_buffer_switch_on_eof = 0;
217         yy_last_accepting_state = 0;
218         yy_last_accepting_cpos = NULL;
220         free(_pszLexStr);