~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/qmon/Xmt310/Xmt/Lexer.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

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