2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * Here we have the token scanner for indent. It scans off one token and puts
46 * it in the global variable "token". It returns a code, indicating the type
55 #include "indent_globs.h"
56 #include "indent_codes.h"
67 struct templ specials[1000] =
103 { /* this is used to facilitate the decision of
104 * what type (alphanumeric, operator) each
106 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 3, 0, 0, 1, 3, 3, 0,
111 0, 0, 3, 3, 0, 3, 0, 3,
112 1, 1, 1, 1, 1, 1, 1, 1,
113 1, 1, 0, 0, 3, 3, 3, 3,
114 0, 1, 1, 1, 1, 1, 1, 1,
115 1, 1, 1, 1, 1, 1, 1, 1,
116 1, 1, 1, 1, 1, 1, 1, 1,
117 1, 1, 1, 0, 0, 0, 3, 1,
118 0, 1, 1, 1, 1, 1, 1, 1,
119 1, 1, 1, 1, 1, 1, 1, 1,
120 1, 1, 1, 1, 1, 1, 1, 1,
121 1, 1, 1, 0, 3, 0, 3, 0
127 int unary_delim; /* this is set to 1 if the current token
128 * forces a following operator to be unary */
129 static int last_code; /* the last token type returned */
130 static int l_struct; /* set to 1 if the last token was 'struct' */
131 int code; /* internal code to be returned */
132 char qchar; /* the delimiter character for a string */
134 e_token = s_token; /* point to start of place to save token */
136 ps.col_1 = ps.last_nl; /* tell world that this token started in
137 * column 1 iff the last thing scanned was nl */
140 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
141 ps.col_1 = false; /* leading blanks imply token is not in column
143 if (++buf_ptr >= buf_end)
147 /* Scan an alphanumeric token */
148 if (chartype[(int)*buf_ptr] == alphanum || (buf_ptr[0] == '.' && isdigit(buf_ptr[1]))) {
150 * we have a character or number
152 const char *j; /* used for searching thru list of
157 if (isdigit(*buf_ptr) || (buf_ptr[0] == '.' && isdigit(buf_ptr[1]))) {
161 if (*buf_ptr == '0' &&
162 (buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
163 *e_token++ = *buf_ptr++;
164 *e_token++ = *buf_ptr++;
165 while (isxdigit(*buf_ptr)) {
167 *e_token++ = *buf_ptr++;
172 if (*buf_ptr == '.') {
179 *e_token++ = *buf_ptr++;
180 if (!isdigit(*buf_ptr) && *buf_ptr != '.') {
181 if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
187 *e_token++ = *buf_ptr++;
188 if (*buf_ptr == '+' || *buf_ptr == '-')
189 *e_token++ = *buf_ptr++;
194 if (!(seensfx & 1) &&
195 (*buf_ptr == 'U' || *buf_ptr == 'u')) {
197 *e_token++ = *buf_ptr++;
201 if (!(seensfx & 2) &&
202 (*buf_ptr == 'L' || *buf_ptr == 'l')) {
204 if (buf_ptr[1] == buf_ptr[0])
205 *e_token++ = *buf_ptr++;
206 *e_token++ = *buf_ptr++;
214 while (chartype[(int)*buf_ptr] == alphanum || *buf_ptr == BACKSLASH) {
215 /* fill_buffer() terminates buffer with newline */
216 if (*buf_ptr == BACKSLASH) {
217 if (*(buf_ptr + 1) == '\n') {
219 if (buf_ptr >= buf_end)
226 *e_token++ = *buf_ptr++;
227 if (buf_ptr >= buf_end)
231 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
232 if (++buf_ptr >= buf_end)
235 ps.its_a_keyword = false;
236 ps.sizeof_keyword = false;
237 if (l_struct && !ps.p_l_follow) {
238 /* if last token was 'struct' and we're not
239 * in parentheses, then this token
240 * should be treated as a declaration */
246 ps.last_u_d = l_struct; /* Operator after identifier is binary
247 * unless last token was 'struct' */
249 last_code = ident; /* Remember that this is the code we will
253 const char *q = s_token;
254 /* Check if we have an "_t" in the end */
256 (strcmp(q + strlen(q) - 2, "_t") == 0)) {
257 ps.its_a_keyword = true;
259 goto found_auto_typedef;
264 * This loop will check if the token is a keyword.
266 for (p = specials; (j = p->rwd) != 0; p++) {
267 const char *q = s_token; /* point at scanned token */
268 if (*j++ != *q++ || *j++ != *q++)
269 continue; /* This test depends on the fact that
270 * identifiers are always at least 1 character
271 * long (ie. the first two bytes of the
272 * identifier are always meaningful) */
274 break; /* If its a one-character identifier */
277 goto found_keyword; /* I wish that C had a multi-level
280 if (p->rwd) { /* we have a keyword */
282 ps.its_a_keyword = true;
285 case 1: /* it is a switch */
287 case 2: /* a case or default */
290 case 3: /* a "struct" */
292 * Next time around, we will want to know that we have had a
298 case 4: /* one of the declaration keywords */
301 ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.sizeof_mask;
302 break; /* inside parens: cast, param list or sizeof */
307 case 5: /* if, while, for */
310 case 6: /* do, else */
314 ps.sizeof_keyword = true;
315 default: /* all others are treated like any other
318 } /* end of switch */
319 } /* end of if (found_it) */
320 if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0) {
323 if (*tp++ == ')' && (*tp == ';' || *tp == ','))
325 strncpy(ps.procname, token, sizeof ps.procname - 1);
326 ps.in_parameter_declaration = 1;
331 * The following hack attempts to guess whether or not the current
332 * token is in fact a declaration keyword -- one that has been
335 if (((*buf_ptr == '*' && buf_ptr[1] != '=') || isalpha(*buf_ptr) || *buf_ptr == '_')
338 && (ps.last_token == rparen || ps.last_token == semicolon ||
339 ps.last_token == decl ||
340 ps.last_token == lbrace || ps.last_token == rbrace)) {
341 ps.its_a_keyword = true;
346 if (last_code == decl) /* if this is a declared variable, then
347 * following sign is unary */
348 ps.last_u_d = true; /* will make "int a -1" work */
350 return (ident); /* the ident is not in the list */
351 } /* end of procesing for alpanum character */
353 /* Scan a non-alphanumeric token */
355 *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
358 if (++buf_ptr >= buf_end)
363 unary_delim = ps.last_u_d;
364 ps.last_nl = true; /* remember that we just had a newline */
365 code = (had_eof ? 0 : newline);
368 * if data has been exhausted, the newline is a dummy, and we should
369 * return code to stop
373 case '\'': /* start of quoted character */
374 case '"': /* start of string */
380 e_token = chfont(&bodyf, &stringf, e_token);
382 do { /* copy the string */
383 while (1) { /* move one character or [/<char>]<char> */
384 if (*buf_ptr == '\n') {
385 diag2(1, "Unterminated literal");
388 CHECK_SIZE_TOKEN; /* Only have to do this once in this loop,
389 * since CHECK_SIZE guarantees that there
390 * are at least 5 entries left */
391 *e_token = *buf_ptr++;
392 if (buf_ptr >= buf_end)
394 if (*e_token == BACKSLASH) { /* if escape, copy extra char */
395 if (*buf_ptr == '\n') /* check for escaped newline */
398 *++e_token = BACKSLASH;
399 if (*buf_ptr == BACKSLASH)
400 *++e_token = BACKSLASH;
402 *++e_token = *buf_ptr++;
403 ++e_token; /* we must increment this again because we
404 * copied two chars */
405 if (buf_ptr >= buf_end)
409 break; /* we copied one character */
410 } /* end of while (1) */
411 } while (*e_token++ != qchar);
413 e_token = chfont(&stringf, &bodyf, e_token - 1);
433 unary_delim = ps.last_u_d;
456 * if (ps.in_or_st) ps.block_init = 1;
458 /* ? code = ps.block_init ? lparen : lbrace; */
464 /* ? code = ps.block_init ? rparen : rbrace; */
468 case 014: /* a form feed */
469 unary_delim = ps.last_u_d;
470 ps.last_nl = true; /* remember this so we can set 'ps.col_1'
486 case '+': /* check for -, +, --, ++ */
487 code = (ps.last_u_d ? unary_op : binary_op);
490 if (*buf_ptr == token[0]) {
491 /* check for doubled character */
492 *e_token++ = *buf_ptr++;
493 /* buffer overflow will be checked at end of loop */
494 if (last_code == ident || last_code == rparen) {
495 code = (ps.last_u_d ? unary_op : postop);
496 /* check for following ++ or -- */
500 else if (*buf_ptr == '=')
501 /* check for operator += */
502 *e_token++ = *buf_ptr++;
503 else if (*buf_ptr == '>') {
504 /* check for operator -> */
505 *e_token++ = *buf_ptr++;
506 if (!pointer_as_binop) {
509 ps.want_blank = false;
512 break; /* buffer overflow will be checked at end of
519 if (chartype[*buf_ptr] == opchar) { /* we have two char assignment */
520 e_token[-1] = *buf_ptr++;
521 if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
522 *e_token++ = *buf_ptr++;
523 *e_token++ = '='; /* Flip =+ to += */
527 if (*buf_ptr == '=') {/* == */
528 *e_token++ = '='; /* Flip =+ to += */
536 /* can drop thru!!! */
540 case '!': /* ops like <, <<, <=, !=, etc */
541 if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
542 *e_token++ = *buf_ptr;
543 if (++buf_ptr >= buf_end)
547 *e_token++ = *buf_ptr++;
548 code = (ps.last_u_d ? unary_op : binary_op);
553 if (token[0] == '/' && *buf_ptr == '*') {
554 /* it is start of comment */
557 if (++buf_ptr >= buf_end)
561 unary_delim = ps.last_u_d;
564 while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
566 * handle ||, &&, etc, and also things as in int *****i
568 *e_token++ = *buf_ptr;
569 if (++buf_ptr >= buf_end)
572 code = (ps.last_u_d ? unary_op : binary_op);
576 } /* end of switch */
577 if (code != newline) {
581 if (buf_ptr >= buf_end) /* check for input buffer empty */
583 ps.last_u_d = unary_delim;
584 *e_token = '\0'; /* null terminate the token */
589 * Add the given keyword to the keyword table, using val as the keyword type
592 addkey(char *key, int val)
594 struct templ *p = specials;
596 if (p->rwd[0] == key[0] && strcmp(p->rwd, key) == 0)
600 if (p >= specials + sizeof specials / sizeof specials[0])
601 return; /* For now, table overflows are silently