~ubuntu-branches/ubuntu/breezy/pmccabe/breezy

« back to all changes in this revision

Viewing changes to gettoken.c

  • Committer: Bazaar Package Importer
  • Author(s): Paul Bame
  • Date: 2003-03-12 14:05:33 UTC
  • Revision ID: james.westby@ubuntu.com-20030312140533-moud629bi4ryp48y
Tags: 2.2-3
tweek for running under buildd and whenever pmccabe not installed
and . not in $PATH

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2002 Hewlett-Packard under GPL version 2 or later */
 
2
#include <stdio.h>
 
3
#include "pmccabe.h"
 
4
 
 
5
#include "dmain.h"
 
6
 
 
7
int
 
8
matchcurly()
 
9
{
 
10
    int c;
 
11
    int nest = 1;
 
12
 
 
13
    while (nest > 0 && (c = ncss_Getchar()) != EOF)
 
14
    {
 
15
        switch (c)
 
16
        {
 
17
        case '{':
 
18
            nest++;
 
19
            break;
 
20
 
 
21
        case '}':
 
22
            nest--;
 
23
            break;
 
24
        }
 
25
    }
 
26
 
 
27
    if (nest > 0 /* && c == EOF */ )
 
28
    {
 
29
        Exit = 7;
 
30
        fileerror("not enough }'s");
 
31
    }
 
32
 
 
33
   return c;
 
34
}
 
35
 
 
36
int
 
37
matchparen()
 
38
{
 
39
    int c;
 
40
    int nest = 1;
 
41
 
 
42
    while (nest > 0 && (c = ncss_Getchar()) != EOF)
 
43
    {
 
44
        switch (c)
 
45
        {
 
46
        case '(':
 
47
            nest++;
 
48
            break;
 
49
 
 
50
        case ')':
 
51
            nest--;
 
52
            break;
 
53
        }
 
54
    }
 
55
 
 
56
    if (nest > 0 /* && c == EOF */ )
 
57
    {
 
58
        Exit = 8;
 
59
        fileerror("not enough )'s");
 
60
    }
 
61
 
 
62
    return c;
 
63
}
 
64
 
 
65
int
 
66
skipws()
 
67
{
 
68
    int c;
 
69
 
 
70
    /* skip whitespace */
 
71
    while ((c = ncss_Getchar()) != EOF && ISSPACE(c))
 
72
    {
 
73
    }
 
74
 
 
75
    return c;
 
76
}
 
77
 
 
78
int
 
79
getsimpleident(char *buf)
 
80
{
 
81
    int c = 0;
 
82
 
 
83
    while (c != EOF)
 
84
    {
 
85
        c = ncss_Getchar();
 
86
 
 
87
        if (ISIDENT(c))
 
88
        {
 
89
            *buf++ = c;
 
90
        }
 
91
        else
 
92
        {
 
93
            *buf++ = '\0';
 
94
            break;
 
95
        }
 
96
    }
 
97
 
 
98
    return c;
 
99
}
 
100
 
 
101
int
 
102
identify(const char *ident)
 
103
{
 
104
    int r = T_IDENT;
 
105
 
 
106
    switch(*ident)
 
107
    {
 
108
    case 'i':
 
109
        if (STREQUAL(ident, "if"))
 
110
            r = T_IF;
 
111
        break;
 
112
    case 'w':
 
113
        if (STREQUAL(ident, "while"))
 
114
            r = T_WHILE;
 
115
        break;
 
116
    case 'c':
 
117
        if (STREQUAL(ident, "case"))
 
118
            r = T_CASE;
 
119
        else if (STREQUAL(ident, "class"))
 
120
            r = T_CLASS;
 
121
        else if (STREQUAL(ident, "const"))
 
122
            r = T_CONST;
 
123
        break;
 
124
    case 's':
 
125
        if (STREQUAL(ident, "switch"))
 
126
            r = T_SWITCH;
 
127
        else if (STREQUAL(ident, "struct"))
 
128
            r = T_STRUCT;
 
129
        break;
 
130
    case 'f':
 
131
        if (STREQUAL(ident, "for"))
 
132
            r = T_FOR;
 
133
        break;
 
134
    case 'u':
 
135
        if (STREQUAL(ident, "union"))
 
136
            r = T_UNION;
 
137
        break;
 
138
    case 'o':
 
139
        if (STREQUAL(ident, "operator"))
 
140
            r = T_OPERATOR;
 
141
        break;
 
142
    }
 
143
 
 
144
    return r;
 
145
}
 
146
 
 
147
void
 
148
ungettoken(int c, char *s)
 
149
{
 
150
    if (c >= T_WORDS)
 
151
    {
 
152
        ncss_Ungets(s);
 
153
    }
 
154
    else
 
155
    {
 
156
        ncss_Ungetc(c);
 
157
    }
 
158
}
 
159
 
 
160
int
 
161
gettoken(char *buf, int *line, int *nLine)
 
162
/*
 
163
 * Callers depend on the fact that gettoken() doesn't modify buf except
 
164
 * when T_IDENT is parsed.
 
165
 */
 
166
{
 
167
    int c;
 
168
    int colon = FALSE;
 
169
    char *startbuf = buf;
 
170
 
 
171
    /* skip whitespace */
 
172
    c = skipws();
 
173
 
 
174
    if (line != NULL)
 
175
        *line = Line;
 
176
 
 
177
    if (nLine != NULL)
 
178
        *nLine = ncss_Line;
 
179
 
 
180
    if (ISIDENT1(c))
 
181
    {
 
182
        *buf++ = c;
 
183
        ncss_Ungetc(getsimpleident(buf));
 
184
        c = identify(buf - 1);
 
185
    }
 
186
 
 
187
    return c;
 
188
}
 
189
 
 
190
int
 
191
gettoken2(char *buf, int *line, int *nLine)
 
192
/*
 
193
 * This one can additionally return T_ASSIGN and T_LOGICAL.  But note
 
194
 * that the caller isn't given enough data to know what specifically
 
195
 * was parsed.
 
196
 */
 
197
{
 
198
    int c, c1, c2;
 
199
    int colon = FALSE;
 
200
    char *startbuf = buf;
 
201
 
 
202
    /* skip whitespace */
 
203
    c = skipws();
 
204
 
 
205
    if (line != NULL)
 
206
        *line = Line;
 
207
 
 
208
    if (nLine != NULL)
 
209
        *nLine = ncss_Line;
 
210
 
 
211
    switch(c)
 
212
    {
 
213
    case '*':   /* OP= */
 
214
    case '/':
 
215
    case '%':
 
216
    case '^':
 
217
        c1 = ncss_Getchar();
 
218
        if (c1 == '=')
 
219
        {
 
220
            c = T_ASSIGN;
 
221
        }
 
222
        else
 
223
        {
 
224
            ncss_Ungetc(c1);
 
225
        }
 
226
        break;
 
227
 
 
228
    case '+':   /* +=, ++ */
 
229
    case '-':   /* -=, -- */
 
230
        c1 = ncss_Getchar();
 
231
        if (c1 == '=' || c1 == c)
 
232
        {
 
233
            c = T_ASSIGN;
 
234
        }
 
235
        else
 
236
        {
 
237
            ncss_Ungetc(c1);
 
238
        }
 
239
        break;
 
240
 
 
241
    case '&':   /* &=, && */
 
242
    case '|':   /* |=, || */
 
243
        c1 = ncss_Getchar();
 
244
        if (c1 == '=')
 
245
        {
 
246
            c = T_ASSIGN;
 
247
        }
 
248
        else if (c1 == c)
 
249
        {
 
250
            c = T_LOGICAL;
 
251
        }
 
252
        else
 
253
        {
 
254
            ncss_Ungetc(c1);
 
255
        }
 
256
        break;
 
257
 
 
258
    case '<':   /* >>= */
 
259
    case '>':   /* <<= */
 
260
        c1 = ncss_Getchar();
 
261
        if (c1 == c)
 
262
        {
 
263
            c2 = ncss_Getchar();
 
264
            if (c2 == '=')
 
265
            {
 
266
                c = T_ASSIGN;
 
267
            }
 
268
            else
 
269
            {
 
270
                ncss_Ungetc(c2);
 
271
            }
 
272
        }
 
273
        else
 
274
        {
 
275
            ncss_Ungetc(c1);
 
276
        }
 
277
        break;
 
278
    }
 
279
 
 
280
    if (ISIDENT1(c))
 
281
    {
 
282
        *buf++ = c;
 
283
        ncss_Ungetc(getsimpleident(buf));
 
284
        c = identify(buf - 1);
 
285
    }
 
286
 
 
287
    return c;
 
288
}
 
289
 
 
290
void
 
291
operatorident(char *s, int c)
 
292
/*
 
293
 * We're in an operator-overloaded C++ identifier.  This isn't
 
294
 * perfect but we read until either ( or ; to guess the identifier's
 
295
 * name.  In this pass we also replace whitespace with _ for printing.
 
296
 */
 
297
{
 
298
    while (c != EOF)
 
299
    {
 
300
        if (ISSPACE(c))
 
301
        {
 
302
            ncss_Ungetc(skipws());
 
303
            *s++ = '_';
 
304
        }
 
305
        else if (c == '(' || c == ';')
 
306
        {
 
307
            ncss_Ungetc(c);
 
308
            break;
 
309
        }
 
310
        else
 
311
        {
 
312
            *s++ = c;
 
313
        }
 
314
        c = ncss_Getchar();
 
315
    }
 
316
 
 
317
    if (s[-1] == '_')
 
318
        s[-1] = '\0';
 
319
    else
 
320
        s[0] = '\0';
 
321
}