fixed a new bison warning in Ubuntu 13.4
[amule.git] / src / Scanner.l
blob73017410d789c517e5697f42172f04c753beefc9
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                                                                         char *tmp = (char*)realloc(psz, l += 128);
134                                                                         if (tmp == NULL){
135                                                                                 yyerror("Less memory for string");
136                                                                                 break;
137                                                                         } else {
138                                                                                 psz = tmp;
139                                                                         }
140                                                                 }
141                                                                 c = yyinput();
142                                                         }
143                                                 }
145                                                 psz[i++] = (unsigned char)c;
146                                                 if (i >= l) {
147                                                         char *tmp = (char*)realloc(psz, l += 128);
148                                                         if (tmp == NULL){
149                                                                 yyerror("Less memory for string");
150                                                                 break;
151                                                         } else {
152                                                                 psz = tmp;
153                                                         }
154                                                 }
155                                         }
156                                         psz[i] = '\0';
157                                         yylval.pstr = new wxString(UTF82unicode(psz));
158                                         free(psz);
159                                         return TOK_STRING;
160                                 }
162 .                               { return yytext[0]; }
166 static void ReadLexBuff(char* pcBuff, int& riResult, size_t uMaxSize)
168         wxASSERT( _pszLexBuff != NULL );
170         if (_pszLexBuff == NULL) {
171                 YY_FATAL_ERROR("Input in flex scanner failed");
172         }
174         wxASSERT( sizeof(YY_CHAR) == sizeof(char) );
175         size_t uCharsInBuff = strlen(_pszLexBuff);
176         size_t uCharsRead = min(uMaxSize, uCharsInBuff);
177         riResult = uCharsRead;
178         memcpy(pcBuff, _pszLexBuff, uCharsRead);
179         _pszLexBuff += uCharsRead;
182 static void FatalLexError(yyconst char msg[])
184 #ifdef _CONSOLE
185         printf("Fatal error in flex scanner: %s\n", msg);
186 #else
187         printf("Fatal error in flex scanner: %s\n", msg);
188 #endif
191 void LexInit(const wxString& pszInput)
193         _pszLexStr = strdup(unicode2UTF8(pszInput));
194         _pszLexBuff = _pszLexStr;
197 void LexFree()
199         yylex_destroy();
201         yyleng = 0;
202         yytext = NULL;
203         yyin = NULL;
204         yyout = NULL;
205         yy_hold_char = '\0';
206         yy_n_chars = 0;
207         yy_c_buf_p = NULL;
208         yy_start = 0;
209         yy_did_buffer_switch_on_eof = 0;
210         yy_last_accepting_state = 0;
211         yy_last_accepting_cpos = NULL;
213 #if YY_STACK_USED
214         yy_start_stack_ptr = 0;
215         yy_start_stack_depth = 0;
216         yy_start_stack = NULL;
217 #endif
219         free(_pszLexStr);