1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* Motif Tools Library, Version 3.1
* $Id$
*
* Written by David Flanagan.
* Copyright (c) 1992-2001 by David Flanagan.
* All Rights Reserved. See the file COPYRIGHT for details.
* This is open source software. See the file LICENSE for details.
* There is no warranty for this software. See NO_WARRANTY for details.
*
* $Log$
* Revision 1.1.1.1 2001/07/18 11:06:02 root
* Initial checkin.
*
* Revision 1.2 2001/06/12 16:25:28 andre
* *** empty log message ***
*
*
*/
#include <ctype.h>
#include <Xmt/Xmt.h>
#include <Xmt/Lexer.h>
#if NeedFunctionPrototypes
XmtLexer XmtLexerCreate(String *keywords, int num_keywords)
#else
XmtLexer XmtLexerCreate(keywords, num_keywords)
String *keywords;
int num_keywords;
#endif
{
XmtLexer lexer = XtNew(XmtLexerRec);
lexer->c = NULL;
lexer->token = XmtLexerNone;
lexer->intval = 0;
lexer->strval = NULL;
lexer->keywords = keywords;
lexer->num_keywords = num_keywords;
return lexer;
}
#if NeedFunctionPrototypes
void XmtLexerDestroy(XmtLexer lexer)
#else
void XmtLexerDestroy(lexer)
XmtLexer lexer;
#endif
{
XtFree((char *)lexer);
}
#if NeedFunctionPrototypes
void XmtLexerInit(XmtLexer lexer, StringConst str)
#else
void XmtLexerInit(lexer, str)
XmtLexer lexer;
StringConst str;
#endif
{
lexer->c = str;
lexer->token = XmtLexerNone;
lexer->intval = 0;
lexer->strval = NULL;
}
#if NeedFunctionPrototypes
XmtLexerToken _XmtLexerGetToken(XmtLexer l)
#else
XmtLexerToken _XmtLexerGetToken(l)
XmtLexer l;
#endif
{
int total;
int len;
_Xconst char *mark;
int keyword;
l->token = XmtLexerNone;
if (l->c == NULL) {
l->token = XmtLexerEndOfString;
return l->token;
}
/* skip whitespace */
while (isspace((int)*l->c)) l->c++;
/* check for end-of-string */
if (*l->c == '\0') {
l->token = XmtLexerEndOfString;
return l->token;
}
/* check punctuation */
switch (*l->c) {
case '(': l->token = XmtLexerLParen; break;
case ')': l->token = XmtLexerRParen; break;
case '[': l->token = XmtLexerLBracket; break;
case ']': l->token = XmtLexerRBracket; break;
case '{': l->token = XmtLexerLBrace; break;
case '}': l->token = XmtLexerRBrace; break;
case '<': l->token = XmtLexerLess; break;
case '>': l->token = XmtLexerGreater; break;
case '+': l->token = XmtLexerPlus; break;
case '-': l->token = XmtLexerMinus; break;
case '*': l->token = XmtLexerAsterisk; break;
case '/': l->token = XmtLexerSlash; break;
case '|': l->token = XmtLexerBar; break;
case '=': l->token = XmtLexerEqual; break;
case '#': l->token = XmtLexerSharp; break;
case '~': l->token = XmtLexerTwiddle; break;
case '%': l->token = XmtLexerPercent; break;
case '$': l->token = XmtLexerDollar; break;
case '.': l->token = XmtLexerPeriod; break;
case '^': l->token = XmtLexerCaret; break;
case ':': l->token = XmtLexerColon; break;
case ';': l->token = XmtLexerSemicolon; break;
case ',': l->token = XmtLexerComma; break;
}
if (l->token != XmtLexerNone) {
l->c++;
return l->token;
}
if (isdigit((int)*l->c)) {
total = 0;
while (isdigit((int)*l->c)) {
total *= 10;
total += (int)(*l->c - '0');
l->c++;
}
l->token = XmtLexerInteger;
l->intval = total;
}
else if (*l->c == '"') { /* its a string */
l->c++;
mark = l->c;
while((*l->c != '\0') && (*l->c != '"')) {
if ((*l->c == '\\') && (*(l->c+1) != '\0'))
l->c++;
l->c++;
}
if (*l->c == '\0') {
XmtWarningMsg("XmtLexer", "badString",
"unterminated string.");
l->token = XmtLexerError;
}
else {
len = l->c - mark;
l->c++;
l->strval = XtMalloc(len+1);
strncpy(l->strval, mark, len);
l->strval[len] = '\0';
l->intval = len;
l->token = XmtLexerString;
}
}
else if (isalnum(*l->c) || (*l->c == '_')) {
/* its an ident. Figure out how long and copy it. */
mark = l->c;
for(len=0; *l->c &&
(isalnum((int)*l->c) || (*l->c == '_'));
len++,l->c++);
l->strval = XtMalloc(len+1);
strncpy(l->strval, mark, len);
l->strval[len] = '\0';
l->intval = len;
/* now go see if it is a keyword. */
keyword = XmtBSearch(l->strval, l->keywords, l->num_keywords);
if (keyword != -1) {
l->intval = keyword;
l->token = XmtLexerKeyword;
XtFree(l->strval);
l->strval = l->keywords[keyword];
}
else
l->token = XmtLexerIdent;
}
else { /* otherwise it is an unrecognized character. */
XmtWarningMsg("XmtLexer", "badChar",
"unrecognized character `%c'.",
*l->c);
l->c++;
l->token = XmtLexerError;
}
return l->token;
}
/*
* Scan from the current lexer location until an unquoted character
* from the delimiters string is encountered. Copy the string and return
* XmtLexerString. If include is True, include the delimiter, otherwise
* don't. If the end of string is found return XmtLexerEndOfString.
*/
#if NeedFunctionPrototypes
XmtLexerToken XmtLexerScan(XmtLexer l, StringConst delimiters,
XmtWideBoolean include)
#else
XmtLexerToken XmtLexerScan(l, delimiters, include)
XmtLexer l;
StringConst delimiters;
int include;
#endif
{
_Xconst char *c, *d;
Boolean instring = False;
if (l->c == '\0') {
l->token = XmtLexerEndOfString;
return XmtLexerEndOfString;
}
for(c = l->c; *c; c++) {
if (!instring) {
for(d = delimiters; *d && (*d != *c); d++);
if (*d) break;
}
if ((*c == '\\') && *(c+1)) c++;
else if (*c == '"') instring = !instring;
}
if (*c == '\0') {
l->c = c;
l->token = XmtLexerEndOfString;
return XmtLexerEndOfString;
}
/*
* if include is True, we include the terminating delimiter.
*/
if (include) c++;
l->intval = c - l->c;
l->strval = XtMalloc(l->intval +1);
strncpy(l->strval, l->c, l->intval);
l->strval[l->intval] = '\0';
l->c = c;
l->token = XmtLexerString;
return XmtLexerString;
}
#if NeedFunctionPrototypes
static String GetArg(XmtLexer l)
#else
static String GetArg(l)
XmtLexer l;
#endif
{
_Xconst char *c, *s;
register char *t;
Boolean instring;
int parenlevel;
int len;
char *arg;
/*
* This procedure breaks the XmtLexer abstraction and scans the
* string directly. It should only be called when the last token
* has been consumed.
*/
/* skip whitespace */
XmtLexerSkipWhite(l);
/* special case for procedures with 0 args */
if (*l->c == ')') {
return NULL;
}
/* scan to an unquoted, unnested comma or right paren */
instring = False;
parenlevel = 0;
for(c = l->c; *c; c++) {
if (*c == '\0') break;
else if (!instring && parenlevel<=0 && (*c == ',' || *c == ')')) break;
else if (*c == '"') instring = !instring;
else if (!instring && *c == '(') parenlevel++;
else if (!instring && *c == ')') parenlevel--;
else if ((*c == '\\') && (*(c+1) != '\0')) c++;
}
/* back up over any whitespace */
while(isspace(*(c-1))) c--;
/* allocate space for a copy of the arg */
len = (int) (c - l->c);
arg = XtMalloc(len+1);
/* copy it, unquoting things */
for(s = l->c, t = arg; s < c; s++) {
if (*s == '\\') *t++ = *++s;
else *t++ = *s;
}
/* and terminate the string */
*t = '\0';
/* put the lexer back in a known state */
l->c = c;
l->token = XmtLexerNone;
return arg;
}
#if NeedFunctionPrototypes
Boolean XmtLexerGetArgList(XmtLexer l, String *args,
Cardinal max_args, Cardinal *num_args)
#else
Boolean XmtLexerGetArgList(l, args, max_args, num_args)
XmtLexer l;
String *args;
Cardinal max_args;
Cardinal *num_args;
#endif
{
XmtLexerToken tok;
/* if no '(', then no arguments, but this is not an error. */
if (XmtLexerGetToken(l) != XmtLexerLParen) {
*num_args = 0;
return True;
}
/* otherwise eat the lparen */
XmtLexerConsumeToken(l);
/* now loop through the arguments */
*num_args = 0;
while(1) {
/* make sure we haven't overflowed */
if (*num_args >= max_args) {
XmtWarningMsg("_XmtParseArgList", "tooMany",
"too many arguments in argument list.");
goto error;
}
/* get the next argument */
args[*num_args] = GetArg(l);
if (args[*num_args]) *num_args += 1;
/* consume a comma or a right paren */
tok = XmtLexerGetToken(l);
if (tok == XmtLexerRParen) {
XmtLexerConsumeToken(l);
return True;
}
else if (tok == XmtLexerComma)
XmtLexerConsumeToken(l);
else {
XmtWarningMsg("_XmtParseArgList", "syntax",
"syntax error in argument list");
goto error;
}
}
error:
/* read past closing ')' */
while(((tok = XmtLexerGetToken(l)) != XmtLexerEndOfString) &&
(tok != XmtLexerRParen))
XmtLexerConsumeToken(l);
XmtLexerConsumeToken(l);
return False;
}
|