2 * From: @(#)rpc_scan.c 1.11 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_scan.c, Scanner for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include "rpc_parse.h"
46 #define startcomment(where) (where[0] == '/' && where[1] == '*')
47 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
49 static int pushed = 0; /* is a token pushed */
50 static token lasttok; /* last token, if pushed */
52 static void unget_token (token * tokp);
53 static void findstrconst (const char **str, const char **val);
54 static void findchrconst (const char **str, const char **val);
55 static void findconst (const char **str, const char **val);
56 static void findkind (const char **mark, token * tokp);
57 static int cppline (const char *line);
58 static int directive (const char *line);
59 static void printdirective (const char *line);
60 static void docppline (const char *line, int *lineno, const char **fname);
63 * scan expecting 1 given token
66 scan (tok_kind expect, token * tokp)
69 if (tokp->kind != expect)
74 * scan expecting any of the 2 given tokens
77 scan2 (tok_kind expect1, tok_kind expect2, token * tokp)
80 if (tokp->kind != expect1 && tokp->kind != expect2)
82 expected2 (expect1, expect2);
87 * scan expecting any of the 3 given token
90 scan3 (tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
93 if (tokp->kind != expect1 && tokp->kind != expect2
94 && tokp->kind != expect3)
96 expected3 (expect1, expect2, expect3);
101 * scan expecting a constant, possibly symbolic
104 scan_num (token *tokp)
112 error (_("constant or identifier expected"));
117 * Peek at the next token
127 * Peek at the next token and scan it if it matches what you expect
130 peekscan (tok_kind expect, token *tokp)
133 if (tokp->kind == expect)
142 * Get the next token, printing out any directive that are encountered.
145 get_token (token *tokp)
162 if (!fgets (curline, MAXLINESIZE, fin))
164 tokp->kind = TOK_EOF;
174 else if (cppline (curline))
176 docppline (curline, &linenum,
179 else if (directive (curline))
181 printdirective (curline);
190 else if (isspace (*where))
192 while (isspace (*where))
199 for (where++; *where; where++)
201 if (endcomment (where))
209 else if (startcomment (where))
221 * 'where' is not whitespace, comment or directive Must be a token!
226 tokp->kind = TOK_COLON;
230 tokp->kind = TOK_SEMICOLON;
234 tokp->kind = TOK_COMMA;
238 tokp->kind = TOK_EQUAL;
242 tokp->kind = TOK_STAR;
246 tokp->kind = TOK_LBRACKET;
250 tokp->kind = TOK_RBRACKET;
254 tokp->kind = TOK_LBRACE;
258 tokp->kind = TOK_RBRACE;
262 tokp->kind = TOK_LPAREN;
266 tokp->kind = TOK_RPAREN;
270 tokp->kind = TOK_LANGLE;
274 tokp->kind = TOK_RANGLE;
279 tokp->kind = TOK_STRCONST;
280 findstrconst (&where, &tokp->str);
283 tokp->kind = TOK_CHARCONST;
284 findchrconst (&where, &tokp->str);
298 tokp->kind = TOK_IDENT;
299 findconst (&where, &tokp->str);
303 if (!(isalpha (*where) || *where == '_'))
308 s_print (buf, _("illegal character in file: "));
309 p = buf + strlen (buf);
310 if (isprint (*where))
312 s_print (p, "%c", *where);
316 s_print (p, "%d", *where);
320 findkind (&where, tokp);
326 unget_token (token * tokp)
333 findstrconst (const char **str, const char **val)
344 while (*p && *p != '"');
347 error (_("unterminated string constant"));
351 tmp = alloc (size + 1);
352 strncpy (tmp, *str, size);
359 findchrconst (const char **str, const char **val)
370 while (*p && *p != '\'');
373 error (_("unterminated string constant"));
379 error (_("empty char string"));
381 tmp = alloc (size + 1);
382 strncpy (tmp, *str, size);
389 findconst (const char **str, const char **val)
396 if (*p == '0' && *(p + 1) == 'x')
403 while (isxdigit (*p));
411 while (isdigit (*p));
414 tmp = alloc (size + 1);
415 strncpy (tmp, *str, size);
421 static const token symbols[] =
423 {TOK_CONST, "const"},
424 {TOK_UNION, "union"},
425 {TOK_SWITCH, "switch"},
427 {TOK_DEFAULT, "default"},
428 {TOK_STRUCT, "struct"},
429 {TOK_TYPEDEF, "typedef"},
431 {TOK_OPAQUE, "opaque"},
436 {TOK_UNSIGNED, "unsigned"},
437 {TOK_SHORT, "short"},
439 {TOK_HYPER, "hyper"},
440 {TOK_FLOAT, "float"},
441 {TOK_DOUBLE, "double"},
442 {TOK_STRING, "string"},
443 {TOK_PROGRAM, "program"},
444 {TOK_VERSION, "version"},
449 findkind (const char **mark, token *tokp)
457 for (s = symbols; s->kind != TOK_EOF; s++)
459 len = strlen (s->str);
460 if (strncmp (str, s->str, len) == 0)
462 if (!isalnum (str[len]) && str[len] != '_')
464 tokp->kind = s->kind;
471 tokp->kind = TOK_IDENT;
472 for (len = 0; isalnum (str[len]) || str[len] == '_'; len++);
473 tmp = alloc (len + 1);
474 strncpy (tmp, str, len);
481 cppline (const char *line)
483 return line == curline && *line == '#';
487 directive (const char *line)
489 return line == curline && *line == '%';
493 printdirective (const char *line)
495 f_print (fout, "%s", line + 1);
499 docppline (const char *line, int *lineno, const char **fname)
506 while (isspace (*line))
511 while (isdigit (*line))
515 while (isspace (*line))
521 error (_("preprocessor error"));
524 p = file = alloc (strlen (line) + 1);
525 while (*line && *line != '"')
531 error (_("preprocessor error"));