2 * From: @(#)rpc_scan.c 1.11 89/02/22 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_scan.c, Scanner for the RPC protocol compiler
34 * Copyright (C) 1987, Sun Microsystems, Inc.
41 #include "rpc_parse.h"
45 #define startcomment(where) (where[0] == '/' && where[1] == '*')
46 #define endcomment(where) (where[-1] == '*' && where[0] == '/')
48 static int pushed
= 0; /* is a token pushed */
49 static token lasttok
; /* last token, if pushed */
51 static void unget_token (token
* tokp
);
52 static void findstrconst (const char **str
, const char **val
);
53 static void findchrconst (const char **str
, const char **val
);
54 static void findconst (const char **str
, const char **val
);
55 static void findkind (const char **mark
, token
* tokp
);
56 static int cppline (const char *line
);
57 static int directive (const char *line
);
58 static void printdirective (const char *line
);
59 static void docppline (const char *line
, int *lineno
, const char **fname
);
62 * scan expecting 1 given token
65 scan (tok_kind expect
, token
* tokp
)
68 if (tokp
->kind
!= expect
)
73 * scan expecting any of the 2 given tokens
76 scan2 (tok_kind expect1
, tok_kind expect2
, token
* tokp
)
79 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
)
81 expected2 (expect1
, expect2
);
86 * scan expecting any of the 3 given token
89 scan3 (tok_kind expect1
, tok_kind expect2
, tok_kind expect3
, token
* tokp
)
92 if (tokp
->kind
!= expect1
&& tokp
->kind
!= expect2
93 && tokp
->kind
!= expect3
)
95 expected3 (expect1
, expect2
, expect3
);
100 * scan expecting a constant, possibly symbolic
103 scan_num (token
*tokp
)
111 error (_("constant or identifier expected"));
116 * Peek at the next token
126 * Peek at the next token and scan it if it matches what you expect
129 peekscan (tok_kind expect
, token
*tokp
)
132 if (tokp
->kind
== expect
)
141 * Get the next token, printing out any directive that are encountered.
144 get_token (token
*tokp
)
161 if (!fgets (curline
, MAXLINESIZE
, fin
))
163 tokp
->kind
= TOK_EOF
;
173 else if (cppline (curline
))
175 docppline (curline
, &linenum
,
178 else if (directive (curline
))
180 printdirective (curline
);
189 else if (isspace (*where
))
191 while (isspace (*where
))
198 for (where
++; *where
; where
++)
200 if (endcomment (where
))
208 else if (startcomment (where
))
220 * 'where' is not whitespace, comment or directive Must be a token!
225 tokp
->kind
= TOK_COLON
;
229 tokp
->kind
= TOK_SEMICOLON
;
233 tokp
->kind
= TOK_COMMA
;
237 tokp
->kind
= TOK_EQUAL
;
241 tokp
->kind
= TOK_STAR
;
245 tokp
->kind
= TOK_LBRACKET
;
249 tokp
->kind
= TOK_RBRACKET
;
253 tokp
->kind
= TOK_LBRACE
;
257 tokp
->kind
= TOK_RBRACE
;
261 tokp
->kind
= TOK_LPAREN
;
265 tokp
->kind
= TOK_RPAREN
;
269 tokp
->kind
= TOK_LANGLE
;
273 tokp
->kind
= TOK_RANGLE
;
278 tokp
->kind
= TOK_STRCONST
;
279 findstrconst (&where
, &tokp
->str
);
282 tokp
->kind
= TOK_CHARCONST
;
283 findchrconst (&where
, &tokp
->str
);
297 tokp
->kind
= TOK_IDENT
;
298 findconst (&where
, &tokp
->str
);
302 if (!(isalpha (*where
) || *where
== '_'))
307 s_print (buf
, _("illegal character in file: "));
308 p
= buf
+ strlen (buf
);
309 if (isprint (*where
))
311 s_print (p
, "%c", *where
);
315 s_print (p
, "%d", *where
);
319 findkind (&where
, tokp
);
325 unget_token (token
* tokp
)
332 findstrconst (const char **str
, const char **val
)
343 while (*p
&& *p
!= '"');
346 error (_("unterminated string constant"));
350 tmp
= alloc (size
+ 1);
351 strncpy (tmp
, *str
, size
);
358 findchrconst (const char **str
, const char **val
)
369 while (*p
&& *p
!= '\'');
372 error (_("unterminated string constant"));
378 error (_("empty char string"));
380 tmp
= alloc (size
+ 1);
381 strncpy (tmp
, *str
, size
);
388 findconst (const char **str
, const char **val
)
395 if (*p
== '0' && *(p
+ 1) == 'x')
402 while (isxdigit (*p
));
410 while (isdigit (*p
));
413 tmp
= alloc (size
+ 1);
414 strncpy (tmp
, *str
, size
);
420 static const token symbols
[] =
422 {TOK_CONST
, "const"},
423 {TOK_UNION
, "union"},
424 {TOK_SWITCH
, "switch"},
426 {TOK_DEFAULT
, "default"},
427 {TOK_STRUCT
, "struct"},
428 {TOK_TYPEDEF
, "typedef"},
430 {TOK_OPAQUE
, "opaque"},
435 {TOK_UNSIGNED
, "unsigned"},
436 {TOK_SHORT
, "short"},
438 {TOK_HYPER
, "hyper"},
439 {TOK_FLOAT
, "float"},
440 {TOK_DOUBLE
, "double"},
441 {TOK_STRING
, "string"},
442 {TOK_PROGRAM
, "program"},
443 {TOK_VERSION
, "version"},
448 findkind (const char **mark
, token
*tokp
)
456 for (s
= symbols
; s
->kind
!= TOK_EOF
; s
++)
458 len
= strlen (s
->str
);
459 if (strncmp (str
, s
->str
, len
) == 0)
461 if (!isalnum (str
[len
]) && str
[len
] != '_')
463 tokp
->kind
= s
->kind
;
470 tokp
->kind
= TOK_IDENT
;
471 for (len
= 0; isalnum (str
[len
]) || str
[len
] == '_'; len
++);
472 tmp
= alloc (len
+ 1);
473 strncpy (tmp
, str
, len
);
480 cppline (const char *line
)
482 return line
== curline
&& *line
== '#';
486 directive (const char *line
)
488 return line
== curline
&& *line
== '%';
492 printdirective (const char *line
)
494 f_print (fout
, "%s", line
+ 1);
498 docppline (const char *line
, int *lineno
, const char **fname
)
505 while (isspace (*line
))
510 while (isdigit (*line
))
514 while (isspace (*line
))
520 error (_("preprocessor error"));
523 p
= file
= alloc (strlen (line
) + 1);
524 while (*line
&& *line
!= '"')
530 error (_("preprocessor error"));