1 /* @(#)rpc_scan.c 2.1 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
31 static char sccsid[] = "@(#)rpc_scan.c 1.6 87/06/24 (C) 1987 SMI";
35 * rpc_scan.c, Scanner for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #define startcomment(where) (where[0] == '/' && where[1] == '*')
45 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
47 static int pushed = 0; /* is a token pushed */
48 static token lasttok; /* last token, if pushed */
51 * scan expecting 1 given token
59 if (tokp->kind != expect) {
65 * scan expecting 2 given tokens
68 scan2(expect1, expect2, tokp)
74 if (tokp->kind != expect1 && tokp->kind != expect2) {
75 expected2(expect1, expect2);
80 * scan expecting 3 given token
83 scan3(expect1, expect2, expect3, tokp)
90 if (tokp->kind != expect1 && tokp->kind != expect2
91 && tokp->kind != expect3) {
92 expected3(expect1, expect2, expect3);
98 * scan expecting a constant, possibly symbolic
105 switch (tokp->kind) {
109 error("constant or identifier expected");
115 * Peek at the next token
127 * Peek at the next token and scan it if it matches what you expect
130 peekscan(expect, tokp)
135 if (tokp->kind == expect) {
145 * Get the next token, printing out any directive that are encountered.
162 if (!fgets(curline, MAXLINESIZE, fin)) {
163 tokp->kind = TOK_EOF;
170 } else if (cppline(curline)) {
171 docppline(curline, &linenum,
173 } else if (directive(curline)) {
174 printdirective(curline);
180 } else if (isspace(*where)) {
181 while (isspace(*where)) {
184 } else if (commenting) {
186 if (endcomment(where)) {
190 } else if (startcomment(where)) {
199 * 'where' is not whitespace, comment or directive Must be a token!
203 tokp->kind = TOK_COLON;
207 tokp->kind = TOK_SEMICOLON;
211 tokp->kind = TOK_COMMA;
215 tokp->kind = TOK_EQUAL;
219 tokp->kind = TOK_STAR;
223 tokp->kind = TOK_LBRACKET;
227 tokp->kind = TOK_RBRACKET;
231 tokp->kind = TOK_LBRACE;
235 tokp->kind = TOK_RBRACE;
239 tokp->kind = TOK_LPAREN;
243 tokp->kind = TOK_RPAREN;
247 tokp->kind = TOK_LANGLE;
251 tokp->kind = TOK_RANGLE;
256 tokp->kind = TOK_STRCONST;
257 findstrconst(&where, &tokp->str);
271 tokp->kind = TOK_IDENT;
272 findconst(&where, &tokp->str);
277 if (!(isalpha(*where) || *where == '_')) {
281 s_print(buf, "illegal character in file: ");
282 p = buf + strlen(buf);
283 if (isprint(*where)) {
284 s_print(p, "%c", *where);
286 s_print(p, "%d", *where);
290 findkind(&where, tokp);
307 findstrconst(str, val)
317 } while (*p && *p != '"');
319 error("unterminated string constant");
323 *val = alloc(size + 1);
324 (void) strncpy(*val, *str, size);
338 if (*p == '0' && *(p + 1) == 'x') {
342 } while (isxdigit(*p));
346 } while (isdigit(*p));
349 *val = alloc(size + 1);
350 (void) strncpy(*val, *str, size);
357 static token symbols[] = {
358 {TOK_CONST, "const"},
359 {TOK_UNION, "union"},
360 {TOK_SWITCH, "switch"},
362 {TOK_DEFAULT, "default"},
363 {TOK_STRUCT, "struct"},
364 {TOK_TYPEDEF, "typedef"},
366 {TOK_OPAQUE, "opaque"},
371 {TOK_UNSIGNED, "unsigned"},
372 {TOK_SHORT, "short"},
374 {TOK_FLOAT, "float"},
375 {TOK_DOUBLE, "double"},
376 {TOK_STRING, "string"},
377 {TOK_PROGRAM, "program"},
378 {TOK_VERSION, "version"},
394 for (s = symbols; s->kind != TOK_EOF; s++) {
395 len = strlen(s->str);
396 if (strncmp(str, s->str, len) == 0) {
397 if (!isalnum(str[len]) && str[len] != '_') {
398 tokp->kind = s->kind;
405 tokp->kind = TOK_IDENT;
406 for (len = 0; isalnum(str[len]) || str[len] == '_'; len++);
407 tokp->str = alloc(len + 1);
408 (void) strncpy(tokp->str, str, len);
417 return (line == curline && *line == '#');
424 return (line == curline && *line == '%');
431 f_print(fout, "%s", line + 1);
435 docppline(line, lineno, fname)
445 while (isspace(*line)) {
449 while (isdigit(*line)) {
452 while (isspace(*line)) {
456 error("preprocessor error");
459 p = file = alloc(strlen(line) + 1);
460 while (*line && *line != '"') {
464 error("preprocessor error");