| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* MALOC = < Minimal Abstraction Layer for Object-oriented C > |
| 4 |
* Copyright (C) 1994-- Michael Holst |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* |
| 20 |
* rcsid="$Id: vlex.l,v 1.8 2010/08/12 05:40:25 fetk Exp $" |
| 21 |
* *************************************************************************** |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* *************************************************************************** |
| 26 |
* File: vlex.l |
| 27 |
* |
| 28 |
* Purpose: Class Vsh: LEX Specification |
| 29 |
* |
| 30 |
* Author: Michael Holst |
| 31 |
* *************************************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
UND [_] |
| 35 |
LET [_A-Za-z] |
| 36 |
DIG [0-9] |
| 37 |
ULD [_A-Za-z0-9] |
| 38 |
SYM [-+$*.:/\\{}~^|@<>%#] |
| 39 |
|
| 40 |
%{ |
| 41 |
|
| 42 |
#include "vsh_p.h" |
| 43 |
|
| 44 |
VEMBED(rcsid="$Id: vlex.l,v 1.8 2010/08/12 05:40:25 fetk Exp $") |
| 45 |
|
| 46 |
#define YY_NO_UNPUT 1 |
| 47 |
#undef YY_INPUT |
| 48 |
#define YY_INPUT(buf,result,max_size) VSH_INPUT(buf,result,max_size) |
| 49 |
|
| 50 |
#include "vyacc.h" |
| 51 |
|
| 52 |
%} |
| 53 |
|
| 54 |
/* |
| 55 |
* *************************************************************************** |
| 56 |
* Initial states |
| 57 |
* *************************************************************************** |
| 58 |
*/ |
| 59 |
|
| 60 |
/* |
| 61 |
* *************************************************************************** |
| 62 |
* LEX Analyzer rules |
| 63 |
* *************************************************************************** |
| 64 |
*/ |
| 65 |
|
| 66 |
%% |
| 67 |
|
| 68 |
/* |
| 69 |
* *********************************************************************** |
| 70 |
* Whitespace |
| 71 |
* *********************************************************************** |
| 72 |
*/ |
| 73 |
|
| 74 |
[ \t]+ { /* no-op: ignore white space between tokens */ } |
| 75 |
|
| 76 |
^[ ]*\n { Vsh_trace("Lex","line of zero or more blanks with a newline"); } |
| 77 |
|
| 78 |
\\\n { Vsh_trace("Lex","an escaped newline"); } |
| 79 |
|
| 80 |
/* |
| 81 |
* *********************************************************************** |
| 82 |
* Comments |
| 83 |
* *********************************************************************** |
| 84 |
*/ |
| 85 |
|
| 86 |
/* ignore shell-style "to-end-of-line" comments (OReilly, page 31) */ |
| 87 |
#.*\n { Vsh_trace("Lex","single-line ``#'' comment"); } |
| 88 |
|
| 89 |
/* |
| 90 |
* *********************************************************************** |
| 91 |
* Legal 2-character symbol tokens (must encode them...) |
| 92 |
* *********************************************************************** |
| 93 |
*/ |
| 94 |
|
| 95 |
";;" { |
| 96 |
Vsh_trace("Lex",yytext); |
| 97 |
return SEMI_SEMI; |
| 98 |
} |
| 99 |
"&&" { |
| 100 |
Vsh_trace("Lex",yytext); |
| 101 |
return AND_AND; |
| 102 |
} |
| 103 |
"||" { |
| 104 |
Vsh_trace("Lex",yytext); |
| 105 |
return OR_OR; |
| 106 |
} |
| 107 |
"<<" { |
| 108 |
Vsh_trace("Lex",yytext); |
| 109 |
return LESS_LESS; |
| 110 |
} |
| 111 |
">>" { |
| 112 |
Vsh_trace("Lex",yytext); |
| 113 |
return GREATER_GREATER; |
| 114 |
} |
| 115 |
"<&" { |
| 116 |
Vsh_trace("Lex",yytext); |
| 117 |
return LESS_AND; |
| 118 |
} |
| 119 |
">&" { |
| 120 |
Vsh_trace("Lex",yytext); |
| 121 |
return GREATER_AND; |
| 122 |
} |
| 123 |
"&>" { |
| 124 |
Vsh_trace("Lex",yytext); |
| 125 |
return AND_GREATER; |
| 126 |
} |
| 127 |
"<>" { |
| 128 |
Vsh_trace("Lex",yytext); |
| 129 |
return LESS_GREATER; |
| 130 |
} |
| 131 |
">|" { |
| 132 |
Vsh_trace("Lex",yytext); |
| 133 |
return GREATER_BAR; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* *********************************************************************** |
| 138 |
* Legal 3-character symbol tokens (must encode them...) |
| 139 |
* *********************************************************************** |
| 140 |
*/ |
| 141 |
|
| 142 |
"<<-" { |
| 143 |
Vsh_trace("Lex",yytext); |
| 144 |
return LESS_LESS_MINUS; |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* *********************************************************************** |
| 149 |
* Legal 1-character symbol tokens (just forward them to the parser...) |
| 150 |
* *********************************************************************** |
| 151 |
*/ |
| 152 |
|
| 153 |
"=" | |
| 154 |
"(" | |
| 155 |
")" | |
| 156 |
"<" | |
| 157 |
">" | |
| 158 |
"|" | |
| 159 |
"&" | |
| 160 |
";" | |
| 161 |
"\n" { |
| 162 |
Vsh_trace("Lex",yytext); |
| 163 |
return yytext[0]; |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* *********************************************************************** |
| 168 |
* Reserved words |
| 169 |
* *********************************************************************** |
| 170 |
*/ |
| 171 |
|
| 172 |
"if" { |
| 173 |
Vsh_trace("Lex","IF"); |
| 174 |
return IF; |
| 175 |
} |
| 176 |
"then" { |
| 177 |
Vsh_trace("Lex","THEN"); |
| 178 |
return THEN; |
| 179 |
} |
| 180 |
"else" { |
| 181 |
Vsh_trace("Lex","ELSE"); |
| 182 |
return ELSE; |
| 183 |
} |
| 184 |
"elif" { |
| 185 |
Vsh_trace("Lex","ELIF"); |
| 186 |
return ELIF; |
| 187 |
} |
| 188 |
"fi" { |
| 189 |
Vsh_trace("Lex","FI"); |
| 190 |
return FI; |
| 191 |
} |
| 192 |
"case" { |
| 193 |
Vsh_trace("Lex","CASE"); |
| 194 |
return CASE; |
| 195 |
} |
| 196 |
"in" { |
| 197 |
Vsh_trace("Lex","IN"); |
| 198 |
return IN; |
| 199 |
} |
| 200 |
"esac" { |
| 201 |
Vsh_trace("Lex","ESAC"); |
| 202 |
return ESAC; |
| 203 |
} |
| 204 |
"for" { |
| 205 |
Vsh_trace("Lex","FOR"); |
| 206 |
return FOR; |
| 207 |
} |
| 208 |
"while" { |
| 209 |
Vsh_trace("Lex","WHILE"); |
| 210 |
return WHILE; |
| 211 |
} |
| 212 |
"until" { |
| 213 |
Vsh_trace("Lex","UNTIL"); |
| 214 |
return UNTIL; |
| 215 |
} |
| 216 |
"do" { |
| 217 |
Vsh_trace("Lex","DO"); |
| 218 |
return DO; |
| 219 |
} |
| 220 |
"done" { |
| 221 |
Vsh_trace("Lex","DONE"); |
| 222 |
return DONE; |
| 223 |
} |
| 224 |
"{" { |
| 225 |
Vsh_trace("Lex",yytext); |
| 226 |
return yytext[0]; |
| 227 |
} |
| 228 |
"}" { |
| 229 |
Vsh_trace("Lex",yytext); |
| 230 |
return yytext[0]; |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* *********************************************************************** |
| 235 |
* Tokens |
| 236 |
* *********************************************************************** |
| 237 |
*/ |
| 238 |
|
| 239 |
\'([^']|\\\')*\' { |
| 240 |
Vsh_trace("Lex","WORD-SSTRING"); |
| 241 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 242 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 243 |
strcpy(yylval.word->word,yytext); |
| 244 |
yylval.word->dollar_present = 0; |
| 245 |
yylval.word->quoted = 1; |
| 246 |
yylval.word->assignment = 0; |
| 247 |
return WORD; |
| 248 |
} |
| 249 |
|
| 250 |
\"([^"]|\\\")*\" { |
| 251 |
Vsh_trace("Lex","WORD-DSTRING"); |
| 252 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 253 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 254 |
strcpy(yylval.word->word,yytext); |
| 255 |
yylval.word->dollar_present = 0; |
| 256 |
yylval.word->quoted = 1; |
| 257 |
yylval.word->assignment = 0; |
| 258 |
return WORD; |
| 259 |
} |
| 260 |
|
| 261 |
\`([^`]|\\\`)*\` { |
| 262 |
Vsh_trace("Lex","WORD-EXEC"); |
| 263 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 264 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 265 |
strcpy(yylval.word->word,yytext); |
| 266 |
yylval.word->dollar_present = 0; |
| 267 |
yylval.word->quoted = 1; |
| 268 |
yylval.word->assignment = 0; |
| 269 |
return WORD; |
| 270 |
} |
| 271 |
|
| 272 |
"["([^[]|\\\[)*"]" { |
| 273 |
Vsh_trace("Lex","WORD-CONDITION"); |
| 274 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 275 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 276 |
strcpy(yylval.word->word,yytext); |
| 277 |
yylval.word->dollar_present = 0; |
| 278 |
yylval.word->quoted = 0; |
| 279 |
yylval.word->assignment = 0; |
| 280 |
return WORD; |
| 281 |
} |
| 282 |
|
| 283 |
{LET}+{ULD}*[=] { |
| 284 |
/* struct symtab *sp = symlook(yytext); */ |
| 285 |
/* yylval.symp = sp; */ |
| 286 |
Vsh_trace("Lex","WORD-ASSIGN"); |
| 287 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 288 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 289 |
strcpy(yylval.word->word,yytext); |
| 290 |
VASSERT( yylval.word->word[strlen(yylval.word->word)-1] == '=' ); |
| 291 |
yylval.word->word[strlen(yylval.word->word)-1] = '\0'; |
| 292 |
yylval.word->dollar_present = 0; |
| 293 |
yylval.word->quoted = 0; |
| 294 |
yylval.word->assignment = 1; |
| 295 |
return ASSIGNMENT_WORD; |
| 296 |
} |
| 297 |
|
| 298 |
({SYM}|{ULD})+ { |
| 299 |
/* struct symtab *sp = symlook(yytext); */ |
| 300 |
/* yylval.symp = sp; */ |
| 301 |
Vsh_trace("Lex","WORD"); |
| 302 |
yylval.word = (WORD_DESC*)malloc(sizeof(WORD_DESC)); |
| 303 |
yylval.word->word = (char*)malloc(1024*sizeof(char)); |
| 304 |
strcpy(yylval.word->word,yytext); |
| 305 |
yylval.word->dollar_present = 0; |
| 306 |
yylval.word->quoted = 0; |
| 307 |
yylval.word->assignment = 0; |
| 308 |
return WORD; |
| 309 |
} |
| 310 |
|
| 311 |
<<EOF>> { |
| 312 |
Vsh_trace("Lex","vsh_EOF"); |
| 313 |
return vsh_EOF; |
| 314 |
} |
| 315 |
|
| 316 |
/* |
| 317 |
* *********************************************************************** |
| 318 |
* Catchall (error exit) |
| 319 |
* *********************************************************************** |
| 320 |
*/ |
| 321 |
|
| 322 |
. { |
| 323 |
yyerror("bad token"); |
| 324 |
} |
| 325 |
|
| 326 |
%% |
| 327 |
|