Move definition of 'tPATH' token up, so that '/', '.' and '0xA' (etc)
[wine] / programs / winedbg / debug.l
1 /* -*-C-*-
2  * Lexical scanner for command line parsing
3  *
4  * Copyright 1993 Eric Youngdale
5  *           2000 Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 %{
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26
27 #include "debugger.h"
28 #include "y.tab.h"
29
30 #undef YY_INPUT
31
32 static int read_input(const char* pfx, char* buf, int size)
33 {
34     size_t      len;
35 static char*  last_line = NULL;
36 static size_t last_line_size = 0;
37 static size_t last_line_idx = 0;
38
39     /* first alloc of our current buffer */
40     if (!last_line)
41     {
42         last_line = HeapAlloc(GetProcessHeap(), 0, last_line_size = 2);
43         assert(last_line);
44         last_line[0] = '\n';
45         last_line[1] = '\0';
46     }
47
48     /* try first to fetch the remaining of an existing line */
49     if (last_line_idx == 0)
50     {
51         /* no remaining chars to be read from last line, grab a brand new line up to '\n' */
52         lexeme_flush();
53         input_fetch_entire_line(pfx, &last_line, &last_line_size, TRUE);
54     }
55
56     len = min(strlen(last_line + last_line_idx), size - 1);
57     memcpy(buf, last_line + last_line_idx, len);
58     buf[len] = '\0';
59     if ((last_line_idx += len) >= strlen(last_line))
60         last_line_idx = 0;
61     return len;
62 }
63
64 #define YY_INPUT(buf,result,max_size) \
65         if ((result = read_input("Wine-dbg>", buf, max_size)) < 0) \
66             YY_FATAL_ERROR("read_input in flex scanner failed");
67
68 #define YY_NO_UNPUT
69
70 static int syntax_error;
71 %}
72
73 DIGIT      [0-9]
74 HEXDIGIT   [0-9a-fA-F]
75 FORMAT     [ubcdgiswx]
76 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
77 PATHNAME   [-/_a-zA-Z\.~][-/_a-zA-Z0-9\.~@]*
78 STRING     \"[^\n"]+\"
79
80 %s FORMAT_EXPECTED
81 %s PATH_EXPECTED
82 %s INFO_CMD
83 %s HELP_CMD
84 %s BD_CMD
85 %s LOCAL_CMD
86 %s SHOW_CMD
87 %s MODE_CMD
88 %s MAINT_CMD
89 %s NOCMD
90
91 %x ASTRING_EXPECTED
92 %x NOPROCESS
93 %%
94                                         /* set to special state when no process is loaded. */
95                                         if (!dbg_curr_process && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
96
97 <<EOF>>                                 { return tEOF; }
98 <*>\n                                   { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
99                                         /* Indicates end of command. Reset state. */
100
101                                         /* This rule must precede the ones below, */
102                                         /* otherwise paths like '/' or '0x9' would */
103                                         /* get parsed as an operator or tNUM */
104 <PATH_EXPECTED>{PATHNAME}               { yylval.string = lexeme_alloc(yytext); return tPATH; }
105
106 "||"                                    { return OP_LOR; }
107 "&&"                                    { return OP_LAND; }
108 "=="                                    { return OP_EQ; }
109 "!="                                    { return OP_NE; }
110 "<="                                    { return OP_LE; }
111 ">="                                    { return OP_GE; }
112 "<<"                                    { return OP_SHL; }
113 ">>"                                    { return OP_SHR; }
114 "->"                                    { return OP_DRF; }
115 [-+<=>|&^()*/%:!~,\.]                   { return *yytext; }
116 "["                                     { return *yytext; }
117 "]"                                     { return *yytext; }
118
119 "0x"{HEXDIGIT}+                         { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
120 {DIGIT}+                                { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
121
122 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT}    { char* last;
123                                           yylval.integer = strtol(yytext+1, &last, 0) << 8;
124                                           yylval.integer |= *last;
125                                           return tFORMAT; }
126
127 <FORMAT_EXPECTED>"/"{FORMAT}            { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
128
129 {STRING}                                { yylval.string = lexeme_alloc(yytext); return tSTRING; }
130 <ASTRING_EXPECTED>[^\n]+                { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
131                                           yylval.string = lexeme_alloc(p); return tSTRING; }
132
133 <INITIAL,NOPROCESS>info|inf|in          { BEGIN(INFO_CMD); return tINFO; }
134 <INITIAL>up                             { BEGIN(NOCMD); return tUP; }
135 <INITIAL>down|dow|do                    { BEGIN(NOCMD); return tDOWN; }
136 <INITIAL>frame|fram|fra|fr              { BEGIN(NOCMD); return tFRAME; }
137 <INITIAL>list|lis|li|l                  { BEGIN(PATH_EXPECTED); return tLIST; }
138 <INITIAL>enable|enabl|enab|ena          { BEGIN(BD_CMD); return tENABLE;}
139 <INITIAL>disable|disabl|disab|disa|dis  { BEGIN(BD_CMD); return tDISABLE; }
140 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
141 <INITIAL>locally|local                  { BEGIN(LOCAL_CMD); return tLOCAL; }
142 <INITIAL,LOCAL_CMD>display|displa|displ|disp    { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
143 <INFO_CMD,BD_CMD>display|displa|displ|disp|dis|di|d     { BEGIN(NOCMD); return tDISPLAY; }
144 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und       { BEGIN(NOCMD); return tUNDISPLAY; }
145 <INITIAL>delete|delet|dele|del          { BEGIN(BD_CMD); return tDELETE; }
146 <INITIAL,NOPROCESS>quit|qui|qu|q        { BEGIN(NOCMD); return tQUIT; }
147 <INITIAL>set|se                         { BEGIN(NOCMD); return tSET; }
148 <INITIAL>x                              { BEGIN(FORMAT_EXPECTED); return tEXAM; }
149 <INITIAL,NOPROCESS>help|hel|he|"?"      { BEGIN(HELP_CMD); return tHELP; }
150
151 <INITIAL,NOPROCESS>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
152 <INITIAL,NOPROCESS>where|wher|whe       { BEGIN(NOCMD); return tBACKTRACE; }
153
154 <INITIAL>cont|con|co|c                  { BEGIN(NOCMD); return tCONT; }
155 <INITIAL>pass|pas|pa                    { BEGIN(NOCMD); return tPASS; }
156 <INITIAL>condition|conditio|conditi|condit|condi|cond   { BEGIN(NOCMD); return tCOND; }
157 <INITIAL>step|ste|st|s                  { BEGIN(NOCMD); return tSTEP; }
158 <INITIAL>next|nex|ne|n                  { BEGIN(NOCMD); return tNEXT; }
159 <INITIAL>stepi|si                       { BEGIN(NOCMD); return tSTEPI; }
160 <INITIAL>nexti|ni                       { BEGIN(NOCMD); return tNEXTI; }
161 <INITIAL>finish|finis|fini|fin|fi       { BEGIN(NOCMD); return tFINISH; }
162
163 <INITIAL>abort|abor|abo                 { BEGIN(NOCMD); return tABORT; }
164 <INITIAL>print|prin|pri|pr|p            { BEGIN(FORMAT_EXPECTED); return tPRINT; }
165
166 <INITIAL>mode                           { BEGIN(MODE_CMD); return tMODE; }
167 <INITIAL>show|sho|sh                    { BEGIN(SHOW_CMD); return tSHOW; }
168 <INITIAL,NOPROCESS>source|sourc|sour|src { BEGIN(PATH_EXPECTED); return tSOURCE; }
169 <INITIAL>symbolfile|symbols|symbol|sf   { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
170
171 <INITIAL,INFO_CMD,BD_CMD>break|brea|bre|br|b    { BEGIN(NOCMD); return tBREAK; }
172 <INITIAL>watch|watc|wat                 { BEGIN(NOCMD); return tWATCH; }
173 <INITIAL>whatis|whati|what              { BEGIN(NOCMD); return tWHATIS; }
174 <INITIAL,NOPROCESS>run|ru|r             { BEGIN(ASTRING_EXPECTED); return tRUN;}
175 <INITIAL>detach|detac|deta|det          { BEGIN(NOCMD); return tDETACH; }
176 <INITIAL>maintenance|maint              { BEGIN(MAINT_CMD); return tMAINTENANCE; }
177 <NOPROCESS>attach|attac|atta|att        { BEGIN(NOCMD); return tATTACH; }
178 <INFO_CMD>share|shar|sha                { return tSHARE; }
179 <INFO_CMD>locals|local|loca|loc         { return tLOCAL; }
180 <INFO_CMD>class|clas|cla                { return tCLASS; }
181 <INFO_CMD>process|proces|proce|proc     { return tPROCESS; }
182 <INFO_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
183 <INFO_CMD>exception|except|exc|ex       { return tEXCEPTION; }
184 <INFO_CMD>registers|regs|reg|re         { return tREGS; }
185 <INFO_CMD>segments|segment|segm|seg|se  { return tSEGMENTS; }
186 <INFO_CMD>stack|stac|sta|st             { return tSTACK; }
187 <INFO_CMD>symbol|symbo|symb|sym         { BEGIN(ASTRING_EXPECTED); return tSYMBOL; }
188 <INFO_CMD>maps|map                      { return tMAPS; }
189 <INFO_CMD>window|windo|wind|win|wnd     { return tWND; }
190 <HELP_CMD>info|inf|in                   { return tINFO; }
191 <MODE_CMD>vm86                          { return tVM86; }
192 <MAINT_CMD>type                         { return tTYPE; }
193
194 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
195                                           BEGIN(PATH_EXPECTED); return tDIR; }
196
197 char                                    { return tCHAR; }
198 short                                   { return tSHORT; }
199 int                                     { return tINT; }
200 long                                    { return tLONG; }
201 float                                   { return tFLOAT; }
202 double                                  { return tDOUBLE; }
203 unsigned                                { return tUNSIGNED; }
204 signed                                  { return tSIGNED; }
205 struct                                  { return tSTRUCT; }
206 union                                   { return tUNION; }
207 enum                                    { return tENUM; }
208 all                                     { return tALL; }
209
210 {IDENTIFIER}                            { yylval.string = lexeme_alloc(yytext); return tIDENTIFIER; }
211 "$"{IDENTIFIER}                         { yylval.string = lexeme_alloc(yytext+1); return tINTVAR; }
212
213 <*>[ \t\r]+                             /* Eat up whitespace and DOS LF */
214
215 <NOPROCESS>.                            { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
216 <*>.                                    { if (syntax_error == 0) { syntax_error++; dbg_printf("Syntax Error (%s)\n", yytext); } }
217 %%
218
219 #ifndef yywrap
220 int yywrap(void) { return 1; }
221 #endif
222
223 static char** local_lexemes /* = NULL */;
224 static int next_lexeme /* = 0 */;
225 static int alloc_lexeme /* = 0 */;
226
227 char* lexeme_alloc(const char* lexeme)
228 {
229     assert(0 <= next_lexeme && next_lexeme < alloc_lexeme + 1);
230     if (next_lexeme >= alloc_lexeme)
231     {
232         alloc_lexeme += 32;
233         local_lexemes = dbg_heap_realloc(local_lexemes, alloc_lexeme * sizeof(local_lexemes[0]));
234         assert(local_lexemes);
235     }
236     return local_lexemes[next_lexeme++] = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(lexeme) + 1), lexeme);
237 }
238
239 void lexeme_flush(void)
240 {
241     while (--next_lexeme >= 0) HeapFree(GetProcessHeap(), 0, local_lexemes[next_lexeme]);
242     next_lexeme = 0;
243 }