2 * Lexical scanner for command line parsing
4 * Copyright 1993 Eric Youngdale
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 %option noinput nounput always-interactive 8bit prefix="dbg_"
29 #define YY_NO_UNISTD_H
35 static char** local_lexemes /* = NULL */;
36 static int next_lexeme /* = 0 */;
37 static int alloc_lexeme /* = 0 */;
39 char* lexeme_alloc_size(int size)
41 assert(0 <= next_lexeme && next_lexeme < alloc_lexeme + 1);
42 if (next_lexeme >= alloc_lexeme)
45 local_lexemes = realloc(local_lexemes, alloc_lexeme * sizeof(local_lexemes[0]));
46 assert(local_lexemes);
48 return local_lexemes[next_lexeme++] = malloc(size + 1);
51 static char* lexeme_alloc(const char* lexeme)
53 char* ptr = lexeme_alloc_size(strlen(lexeme) + 1);
54 return strcpy(ptr, lexeme);
57 static char* lexeme_alloc_if(const char* lexeme, unsigned sz)
59 size_t len = strlen(lexeme);
61 if (len <= sz) return NULL;
63 ptr = lexeme_alloc_size(len + 1);
64 memcpy(ptr, lexeme, len);
69 void lexeme_flush(void)
71 while (--next_lexeme >= 0) free(local_lexemes[next_lexeme]);
75 /* called with quoted string, unescape all elements inside the quotes */
76 static char* unescape_string(const char* str)
78 size_t len = strlen(str) - 2;
79 char* ret = lexeme_alloc_size(len + 1);
82 for (ptr = str + 1; ptr < str + 1 + len; ptr++)
88 case 't': *dst++ = '\t'; break;
89 case 'r': *dst++ = '\r'; break;
90 case 'n': *dst++ = '\n'; break;
91 case '\\': *dst++ = '\\'; break;
92 case '"': *dst++ = '"'; break;
93 default: *dst++ = '\\'; *dst++ = *ptr; /* not handled, don't change */
103 static int resolve_identifier(const char* id, DBG_STYPE* lval)
105 if (types_find_type(id, SymTagTypedef, &lval->type)) return tTYPEDEF;
106 lval->string = lexeme_alloc(id);
110 #define YY_INPUT(buf,result,max_size) \
111 (result = input_lex_read_buffer(buf, max_size))
113 static int syntax_error;
120 IDENTIFIER [_a-zA-Z~?][_a-zA-Z0-9~?@]*
121 SCOPED_IDENTIFIER [_a-zA-Z~?][_a-zA-Z0-9~?@]*"::"
122 MODULE_IDENTIFIER [_a-zA-Z~?\*][_a-zA-Z0-9~?\*@\.]*"!"
123 PATHNAME [\\/_a-zA-Z0-9\.~@][\\/\-_a-zA-Z0-9\.~@]*
124 STRING \"(\\[^\n]|[^\\"\n])*\"
142 /* set to special state when no process is loaded. */
143 if (!dbg_num_processes() && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
145 <<EOF>> { return tEOF; }
146 <*>"#"[^\n]* /* Skip comments */
147 <*>\n { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
148 /* Indicates end of command. Reset state. */
150 "||" { return OP_LOR; }
151 "&&" { return OP_LAND; }
152 "==" { return OP_EQ; }
153 "!=" { return OP_NE; }
154 "<=" { return OP_LE; }
155 ">=" { return OP_GE; }
156 "<<" { return OP_SHL; }
157 ">>" { return OP_SHR; }
158 "->" { return OP_DRF; }
159 "[" { return *yytext; }
160 "]" { return *yytext; }
162 "0x"{HEXDIGIT}+ { sscanf(yytext, "%I64x", &dbg_lval.integer); return tNUM; }
163 {DIGIT}+ { sscanf(yytext, "%I64d", &dbg_lval.integer); return tNUM; }
164 "'\\''" { dbg_lval.integer = '\''; return tNUM;}
165 "'\\0"{OCTDIGIT}*"'" { sscanf(yytext + 3, "%I64o", &dbg_lval.integer); return tNUM;}
166 "'\\x"{HEXDIGIT}+"'" { sscanf(yytext + 3, "%I64x", &dbg_lval.integer); return tNUM;}
167 "'\\"[a-z]"'" { dbg_lval.integer = yytext[2] - 'a'; return tNUM;}
168 "'"."'" { dbg_lval.integer = yytext[1]; return tNUM;}
170 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT} { char* last;
171 dbg_lval.integer = strtol(yytext+1, &last, 0) << 8;
172 dbg_lval.integer |= *last;
175 <FORMAT_EXPECTED>"/"{FORMAT} { dbg_lval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
177 <*>{STRING} { dbg_lval.string = unescape_string(yytext); return tSTRING;}
178 <ASTRING_EXPECTED>[^\n]+ { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
179 dbg_lval.string = lexeme_alloc(p); return tSTRING; }
180 <AWORD_EXPECTED>[^ \t\n]+ { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
181 dbg_lval.string = lexeme_alloc(p); return tSTRING; }
182 <INITIAL,NOPROCESS>info|inf|in { BEGIN(INFO_CMD); return tINFO; }
183 <INITIAL>up { BEGIN(NOCMD); return tUP; }
184 <INITIAL>down|dow|do { BEGIN(NOCMD); return tDOWN; }
185 <INITIAL,INFO_CMD>frame|fram|fra|fr { BEGIN(NOCMD); return tFRAME; }
186 <INITIAL>list|lis|li|l { BEGIN(PATH_ACCEPTED); return tLIST; }
187 <INITIAL>enable|enabl|enab|ena { BEGIN(BD_CMD); return tENABLE;}
188 <INITIAL>disable|disabl|disab|disa|dis { BEGIN(BD_CMD); return tDISABLE; }
189 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
190 <INITIAL>locally|local { BEGIN(LOCAL_CMD); return tLOCAL; }
191 <INITIAL,LOCAL_CMD>display|displa|displ|disp { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
192 <INFO_CMD,BD_CMD>display|displa|displ|disp|dis|di|d { BEGIN(NOCMD); return tDISPLAY; }
193 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und { BEGIN(NOCMD); return tUNDISPLAY; }
194 <INITIAL>delete|delet|dele|del { BEGIN(BD_CMD); return tDELETE; }
195 <INITIAL>thread|threa|thre|thr|th { BEGIN(NOCMD); return tTHREAD; }
196 <INITIAL,NOPROCESS>quit|qui|qu|q { BEGIN(NOCMD); return tQUIT; }
197 <INITIAL>set|se { BEGIN(NOCMD); return tSET; }
198 <INITIAL>x { BEGIN(FORMAT_EXPECTED); return tEXAM; }
199 <INITIAL,NOPROCESS>help|hel|he|"?" { BEGIN(HELP_CMD); return tHELP; }
201 <INITIAL,NOPROCESS>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
202 <INITIAL,NOPROCESS>where|wher|whe { BEGIN(NOCMD); return tBACKTRACE; }
204 <INITIAL>continue|continu|contin|conti|cont|con|co|c { BEGIN(NOCMD); return tCONT; }
205 <INITIAL>pass|pas|pa { BEGIN(NOCMD); return tPASS; }
206 <INITIAL>condition|conditio|conditi|condit|condi|cond { BEGIN(NOCMD); return tCOND; }
207 <INITIAL>step|ste|st|s { BEGIN(NOCMD); return tSTEP; }
208 <INITIAL>next|nex|ne|n { BEGIN(NOCMD); return tNEXT; }
209 <INITIAL>stepi|si { BEGIN(NOCMD); return tSTEPI; }
210 <INITIAL>nexti|ni { BEGIN(NOCMD); return tNEXTI; }
211 <INITIAL>finish|finis|fini|fin|fi { BEGIN(NOCMD); return tFINISH; }
213 <INITIAL>abort|abor|abo { BEGIN(NOCMD); return tABORT; }
214 <INITIAL>print|prin|pri|pr|p { BEGIN(FORMAT_EXPECTED); return tPRINT; }
216 <INITIAL>show|sho|sh { BEGIN(SHOW_CMD); return tSHOW; }
217 <INITIAL,NOPROCESS>source|sourc|sour|src { BEGIN(PATH_EXPECTED); return tSOURCE; }
218 <INITIAL>symbolfile|symbols|symbol|sf { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
220 <INITIAL,INFO_CMD,BD_CMD>break|brea|bre|br|b { BEGIN(PATH_ACCEPTED); return tBREAK; }
221 <INITIAL,INFO_CMD,BD_CMD>hbreak|hbrea|hbre|hbr|hb { BEGIN(PATH_ACCEPTED); return tHBREAK; }
222 <INITIAL>watch|watc|wat { BEGIN(NOCMD); return tWATCH; }
223 <INITIAL>rwatch|rwatc|rwat { BEGIN(NOCMD); return tRWATCH; }
224 <INITIAL>whatis|whati|what { BEGIN(NOCMD); return tWHATIS; }
225 <INITIAL,NOPROCESS>run|ru|r { BEGIN(AWORD_EXPECTED); return tRUN;}
226 <INITIAL>detach|detac|deta|det { BEGIN(NOCMD); return tDETACH; }
227 <INITIAL>kill|kil|ki|k { BEGIN(NOCMD); return tKILL; }
228 <INITIAL,NOPROCESS>maintenance|maint { BEGIN(MAINT_CMD); return tMAINTENANCE; }
229 <INITIAL>minidump|mdmp { BEGIN(PATH_EXPECTED); return tMINIDUMP; }
230 <INITIAL>echo { BEGIN(ASTRING_EXPECTED); return tECHO; }
231 <NOPROCESS>attach|attac|atta|att { BEGIN(NOCMD); return tATTACH; }
232 <INFO_CMD>share|shar|sha { return tSHARE; }
233 <MAINT_CMD>module|modul|mod { BEGIN(ASTRING_EXPECTED); return tMODULE; }
234 <INFO_CMD>locals|local|loca|loc { return tLOCAL; }
235 <INFO_CMD>class|clas|cla { return tCLASS; }
236 <INFO_CMD>process|proces|proce|proc { return tPROCESS; }
237 <INFO_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
238 <INFO_CMD>exception|except|exc|ex { return tEXCEPTION; }
239 <INFO_CMD>registers|regs|reg|re { return tREGS; }
240 <INFO_CMD>allregs|allreg|allre { return tALLREGS; }
241 <INFO_CMD>"all-registers"|"all-regs"|"all-reg"|"all-re" { return tALLREGS; }
242 <INFO_CMD>segments|segment|segm|seg|se { return tSEGMENTS; }
243 <INFO_CMD>stack|stac|sta|st { return tSTACK; }
244 <INFO_CMD>symbol|symbo|symb|sym { BEGIN(ASTRING_EXPECTED); return tSYMBOL; }
245 <INFO_CMD>maps|map { return tMAPS; }
246 <INFO_CMD>window|windo|wind|win|wnd { return tWND; }
247 <HELP_CMD>info|inf|in { return tINFO; }
248 <MAINT_CMD>type { return tTYPE; }
250 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
251 BEGIN(PATH_EXPECTED); return tDIR; }
253 {MODULE_IDENTIFIER}?void { dbg_lval.string = lexeme_alloc_if(yytext, 5); return tVOID; } /* return modulename if present */
254 {MODULE_IDENTIFIER}?char { dbg_lval.string = lexeme_alloc_if(yytext, 5); return tCHAR; }
255 {MODULE_IDENTIFIER}?WCHAR { dbg_lval.string = lexeme_alloc_if(yytext, 6); return tWCHAR; }
256 {MODULE_IDENTIFIER}?short { dbg_lval.string = lexeme_alloc_if(yytext, 6); return tSHORT; }
257 {MODULE_IDENTIFIER}?int { dbg_lval.string = lexeme_alloc_if(yytext, 4); return tINT; }
258 {MODULE_IDENTIFIER}?long { dbg_lval.string = lexeme_alloc_if(yytext, 5); return tLONG; }
259 {MODULE_IDENTIFIER}?float { dbg_lval.string = lexeme_alloc_if(yytext, 6); return tFLOAT; }
260 {MODULE_IDENTIFIER}?double { dbg_lval.string = lexeme_alloc_if(yytext, 7); return tDOUBLE; }
261 {MODULE_IDENTIFIER}?unsigned { dbg_lval.string = lexeme_alloc_if(yytext, 9); return tUNSIGNED; }
262 {MODULE_IDENTIFIER}?signed { dbg_lval.string = lexeme_alloc_if(yytext, 7); return tSIGNED; }
263 struct { return tSTRUCT; }
264 union { return tUNION; }
265 enum { return tENUM; }
268 {MODULE_IDENTIFIER}?{SCOPED_IDENTIFIER}*{IDENTIFIER} { return resolve_identifier(yytext, &dbg_lval); }
269 "$"{IDENTIFIER} { dbg_lval.string = lexeme_alloc(yytext+1); return tINTVAR; }
271 <PATH_EXPECTED,PATH_ACCEPTED>{PATHNAME} { dbg_lval.string = lexeme_alloc(yytext); return tPATH; }
273 [-+<=>|&^()*/%:!~,\.] { return *yytext; }
275 <*>[ \t\r]+ /* Eat up whitespace and DOS LF */
277 <NOPROCESS>. { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
278 <*>. { if (syntax_error == 0) { syntax_error++; dbg_printf("Syntax Error (%s)\n", yytext); } }
282 int dbg_wrap(void) { return 1; }