Fix header inclusions
[amule.git] / src / Scanner.l
blobd4ccd2df03ecfefe109424e3bb239c800c349233
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, int& riResult, size_t uMaxSize);
29 static void FatalLexError(yyconst char msg[]);
31 static char* _pszLexBuff;
32 static char* _pszLexStr;
36 %option noyywrap
38 keywordchar             [^ \"()]
42 [ ]                             { /* Skip blanks. */ }
43 "OR"                    { return TOK_OR; }
44 "AND"                   { return TOK_AND; }
45 "NOT"                   { return TOK_NOT; }
47 "ed2k::"[a-fA-F0-9]{32} {
48                                         yylval.pstr = new wxString(UTF82unicode(yytext));
49                                         return TOK_ED2K_LINK;
50                                 }
52 {keywordchar}*  {
53                                         yylval.pstr = new wxString(UTF82unicode(yytext));
54                                         return TOK_STRING;
55                 }
57 "\""                    {
58                                         int l = 128;
59                                         char* psz = (char*)malloc(l);
60                                         int i = 0;
61                                         int c;
62                                         while ((c = yyinput()) != '\"')
63                                         {
64                                                 if (c == EOF || c == '\n'){
65                                                         unput(c);
66                                                         yyerror(wxT("Search expression error: unterminated string"));
67                                                         break;
68                                                 }
69                                                 if (c == '\\'){         /*Escape sequence*/
70                                                         switch (c = yyinput())
71                                                         {
72                                                         case '\n':
73                                                                 continue;
74                                                         case 't':               /*Tab*/
75                                                                 c = '\t';
76                                                                 break;
77                                                         case 'n':               /*Linefeed*/
78                                                                 c = '\n';
79                                                                 break;
80                                                         case 'f':               /*Formfeed*/
81                                                                 c = '\f';
82                                                                 break;
83                                                         case 'r':               /*Carriage return*/
84                                                                 c = '\r';
85                                                                 break;
86                                                         case '\\':              /*Backslash*/
87                                                                 c = '\\';
88                                                                 break;
89                                                         case '"':               /*Double quotation mark*/
90                                                                 c = '\"';
91                                                                 break;
92                                                         case '\'':              /*Single quotation mark*/
93                                                                 c = '\'';
94                                                                 break;
95                                                         case '?':               /*Question mark*/
96                                                                 c = '\?';
97                                                                 break;
98                                                         case 'v':               /*Vertical Tab*/
99                                                                 c = '\v';
100                                                                 break;
101                                                         case 'a':               /*Alert*/
102                                                                 c = '\a';
103                                                                 break;
104                                                         case 'b':               /*Backspace*/
105                                                                 c = '\b';
106                                                                 break;
107                                                         case 'x':               /*Hexadecimal number*/
108                                                                 {
109                                                                         int n, octv;
110                                                                         for (n = 1, octv = 0; n <= 3; n++) {
111                                                                                 if ((c = yyinput()) >= '0' && c <= '9')
112                                                                                         c -= '0';
113                                                                                 else if (c >= 'a' && c <= 'f')
114                                                                                         c = (c - 'a') + 10;
115                                                                                 else if (c >= 'A' && c <= 'F')
116                                                                                         c = (c - 'A') + 10;
117                                                                                 else
118                                                                                         break;
119                                                                                 octv = octv * 16 + c;
120                                                                         }
121                                                                         unput(c);
122                                                                         if (n == 1)
123                                                                                 c = 'x';
124                                                                         else
125                                                                                 c = octv;
126                                                                 }
127                                                                 break;
128                                                         }
129                                                 } else {
130                                                         if ((unsigned char)c >= 0x80/* && IsDBCSLeadByte(yytext[0]) */){
131                                                                 psz[i++] = (unsigned char)c;
132                                                                 if (i >= l){
133                                                                         psz = (char*)realloc(psz, l += 128);
134                                                                         if (psz == NULL){
135                                                                                 yyerror("Less memory for string");
136                                                                                 break;
137                                                                         }
138                                                                 }
139                                                                 c = yyinput();
140                                                         }
141                                                 }
143                                                 psz[i++] = (unsigned char)c;
144                                                 if (i >= l){
145                                                         psz = (char*)realloc(psz, l += 128);
146                                                         if (psz == NULL){
147                                                                 yyerror("Less memory for string");
148                                                                 break;
149                                                         }
150                                                 }
151                                         }
152                                         psz[i] = '\0';
153                                         yylval.pstr = new wxString(UTF82unicode(psz));
154                                         free(psz);
155                                         return TOK_STRING;
156                                 }
158 .                               { return yytext[0]; }
162 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize)
164         wxASSERT( _pszLexBuff != NULL );
166         if (_pszLexBuff == NULL) {
167                 YY_FATAL_ERROR("Input in flex scanner failed");
168         }
170         wxASSERT( sizeof(YY_CHAR) == sizeof(char) );
171         size_t uCharsInBuff = strlen(_pszLexBuff);
172         size_t uCharsRead = min(uMaxSize, uCharsInBuff);
173         riResult = uCharsRead;
174         memcpy(pcBuff, _pszLexBuff, uCharsRead);
175         _pszLexBuff += uCharsRead;
178 static void FatalLexError(yyconst char msg[])
180 #ifdef _CONSOLE
181         printf("Fatal error in flex scanner: %s\n", msg);
182 #else
183         printf("Fatal error in flex scanner: %s\n", msg);
184 #endif
187 void LexInit(const wxString& pszInput)
189         _pszLexStr = strdup(unicode2UTF8(pszInput));
190         _pszLexBuff = _pszLexStr;
193 void LexFree()
195         yylex_destroy();
197         yyleng = 0;
198         yytext = NULL;
199         yyin = NULL;
200         yyout = NULL;
201         yy_hold_char = '\0';
202         yy_n_chars = 0;
203         yy_c_buf_p = NULL;
204         yy_start = 0;
205         yy_did_buffer_switch_on_eof = 0;
206         yy_last_accepting_state = 0;
207         yy_last_accepting_cpos = NULL;
209 #if YY_STACK_USED
210         yy_start_stack_ptr = 0;
211         yy_start_stack_depth = 0;
212         yy_start_stack = NULL;
213 #endif
215         free(_pszLexStr);