2016-08-01 16:40:42 +00:00
|
|
|
/*-
|
2017-11-18 14:26:50 +00:00
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1985 Sun Microsystems, Inc.
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-10-28 18:35:32 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif /* not lint */
|
2001-10-28 18:35:32 +00:00
|
|
|
#endif
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we have the token scanner for indent. It scans off one token and puts
|
|
|
|
* it in the global variable "token". It returns a code, indicating the type
|
|
|
|
* of token scanned.
|
|
|
|
*/
|
|
|
|
|
2003-06-15 09:28:17 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-07-16 05:46:50 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include "indent_globs.h"
|
|
|
|
#include "indent_codes.h"
|
2002-03-30 17:10:20 +00:00
|
|
|
#include "indent.h"
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
struct templ {
|
2002-03-30 17:10:20 +00:00
|
|
|
const char *rwd;
|
1994-05-27 12:33:43 +00:00
|
|
|
int rwcode;
|
|
|
|
};
|
|
|
|
|
2016-08-04 15:27:09 +00:00
|
|
|
/*
|
|
|
|
* This table has to be sorted alphabetically, because it'll be used in binary
|
|
|
|
* search. For the same reason, string must be the first thing in struct templ.
|
|
|
|
*/
|
|
|
|
struct templ specials[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2018-06-03 16:52:30 +00:00
|
|
|
{"_Bool", 4},
|
|
|
|
{"_Complex", 4},
|
|
|
|
{"_Imaginary", 4},
|
2016-12-02 01:25:51 +00:00
|
|
|
{"auto", 10},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"bool", 4},
|
2016-08-03 16:33:34 +00:00
|
|
|
{"break", 9},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"case", 8},
|
2001-10-28 18:35:32 +00:00
|
|
|
{"char", 4},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"complex", 4},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"const", 4},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"continue", 12},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"default", 8},
|
|
|
|
{"do", 6},
|
2001-10-28 18:35:32 +00:00
|
|
|
{"double", 4},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"else", 6},
|
|
|
|
{"enum", 3},
|
2016-12-02 01:25:51 +00:00
|
|
|
{"extern", 10},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"float", 4},
|
|
|
|
{"for", 5},
|
|
|
|
{"global", 4},
|
|
|
|
{"goto", 9},
|
|
|
|
{"if", 5},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"imaginary", 4},
|
|
|
|
{"inline", 12},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"int", 4},
|
2001-10-28 18:35:32 +00:00
|
|
|
{"long", 4},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"offsetof", 1},
|
2016-12-02 01:25:51 +00:00
|
|
|
{"register", 10},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"restrict", 12},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"return", 9},
|
2001-10-28 18:35:32 +00:00
|
|
|
{"short", 4},
|
2018-06-03 16:52:30 +00:00
|
|
|
{"signed", 4},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"sizeof", 2},
|
2016-12-02 01:25:51 +00:00
|
|
|
{"static", 10},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"struct", 3},
|
|
|
|
{"switch", 7},
|
2018-06-01 09:41:15 +00:00
|
|
|
{"typedef", 11},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"union", 3},
|
2001-10-28 18:35:32 +00:00
|
|
|
{"unsigned", 4},
|
|
|
|
{"void", 4},
|
2004-02-09 13:13:03 +00:00
|
|
|
{"volatile", 4},
|
2016-08-04 15:27:09 +00:00
|
|
|
{"while", 5}
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
2016-08-04 15:27:09 +00:00
|
|
|
const char **typenames;
|
|
|
|
int typename_count;
|
|
|
|
int typename_top = -1;
|
|
|
|
|
2018-07-16 05:46:50 +00:00
|
|
|
/*
|
|
|
|
* The transition table below was rewritten by hand from lx's output, given
|
|
|
|
* the following definitions. lx is Katherine Flavel's lexer generator.
|
|
|
|
*
|
|
|
|
* O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
|
|
|
|
* H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
|
|
|
|
* BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
|
|
|
|
* FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
|
|
|
|
*
|
|
|
|
* D+ E FS? -> $float;
|
|
|
|
* D* "." D+ E? FS? -> $float;
|
|
|
|
* D+ "." E? FS? -> $float; HP H+ IS? -> $int;
|
|
|
|
* HP H+ P FS? -> $float; NZ D* IS? -> $int;
|
|
|
|
* HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
|
|
|
|
* HP H+ "." P FS -> $float; BP B+ IS? -> $int;
|
|
|
|
*/
|
|
|
|
static char const *table[] = {
|
|
|
|
/* examples:
|
|
|
|
00
|
|
|
|
s 0xx
|
|
|
|
t 00xaa
|
|
|
|
a 11 101100xxa..
|
|
|
|
r 11ee0001101lbuuxx.a.pp
|
|
|
|
t.01.e+008bLuxll0Ll.aa.p+0
|
|
|
|
states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
|
|
|
['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
|
|
|
|
['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
|
|
|
|
['7'] = "DEIDEHHHIJ U VUVVZZZ",
|
|
|
|
['9'] = "DEJDEHHHJJ U VUVVZZZ",
|
|
|
|
['a'] = " U VUVV ",
|
|
|
|
['b'] = " K U VUVV ",
|
|
|
|
['e'] = " FFF FF U VUVV ",
|
|
|
|
['f'] = " f f U VUVV f",
|
|
|
|
['u'] = " MM M i iiM M ",
|
|
|
|
['x'] = " N ",
|
|
|
|
['p'] = " FFX ",
|
|
|
|
['L'] = " LLf fL PR Li L f",
|
|
|
|
['l'] = " OOf fO S P O i O f",
|
|
|
|
['+'] = " G Y ",
|
|
|
|
['.'] = "B EE EE T W ",
|
|
|
|
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
|
|
|
[0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
|
1994-05-27 12:33:43 +00:00
|
|
|
};
|
|
|
|
|
2016-08-04 15:27:09 +00:00
|
|
|
static int
|
|
|
|
strcmp_type(const void *e1, const void *e2)
|
|
|
|
{
|
|
|
|
return (strcmp(e1, *(const char * const *)e2));
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2018-06-03 14:13:11 +00:00
|
|
|
lexi(struct parser_state *state)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int unary_delim; /* this is set to 1 if the current token
|
|
|
|
* forces a following operator to be unary */
|
|
|
|
int code; /* internal code to be returned */
|
|
|
|
char qchar; /* the delimiter character for a string */
|
|
|
|
|
|
|
|
e_token = s_token; /* point to start of place to save token */
|
|
|
|
unary_delim = false;
|
2018-06-03 14:13:11 +00:00
|
|
|
state->col_1 = state->last_nl; /* tell world that this token started
|
|
|
|
* in column 1 iff the last thing
|
|
|
|
* scanned was a newline */
|
|
|
|
state->last_nl = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
|
2018-06-03 14:13:11 +00:00
|
|
|
state->col_1 = false; /* leading blanks imply token is not in column
|
1994-05-27 12:33:43 +00:00
|
|
|
* 1 */
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan an alphanumeric token */
|
2018-07-16 05:46:50 +00:00
|
|
|
if (isalnum((unsigned char)*buf_ptr) ||
|
2018-08-15 18:19:45 +00:00
|
|
|
*buf_ptr == '_' || *buf_ptr == '$' ||
|
2018-06-03 17:03:55 +00:00
|
|
|
(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* we have a character or number
|
|
|
|
*/
|
2002-06-24 17:40:27 +00:00
|
|
|
struct templ *p;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2018-06-03 17:03:55 +00:00
|
|
|
if (isdigit((unsigned char)*buf_ptr) ||
|
|
|
|
(buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
|
2018-07-16 05:46:50 +00:00
|
|
|
char s;
|
|
|
|
unsigned char i;
|
|
|
|
|
|
|
|
for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
|
|
|
|
i = (unsigned char)*buf_ptr;
|
|
|
|
if (i >= nitems(table) || table[i] == NULL ||
|
|
|
|
table[i][s - 'A'] == ' ') {
|
|
|
|
s = table[0][s - 'A'];
|
|
|
|
break;
|
1998-05-19 20:41:20 +00:00
|
|
|
}
|
2018-07-16 05:46:50 +00:00
|
|
|
s = table[i][s - 'A'];
|
|
|
|
CHECK_SIZE_TOKEN(1);
|
|
|
|
*e_token++ = *buf_ptr++;
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
1998-05-19 20:41:20 +00:00
|
|
|
}
|
2018-07-16 05:46:50 +00:00
|
|
|
/* s now indicates the type: f(loating), i(integer), u(nknown) */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-16 05:46:50 +00:00
|
|
|
while (isalnum((unsigned char)*buf_ptr) ||
|
|
|
|
*buf_ptr == BACKSLASH ||
|
2018-08-15 18:19:45 +00:00
|
|
|
*buf_ptr == '_' || *buf_ptr == '$') {
|
2001-10-19 19:10:36 +00:00
|
|
|
/* fill_buffer() terminates buffer with newline */
|
|
|
|
if (*buf_ptr == BACKSLASH) {
|
|
|
|
if (*(buf_ptr + 1) == '\n') {
|
|
|
|
buf_ptr += 2;
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2018-06-03 18:19:41 +00:00
|
|
|
CHECK_SIZE_TOKEN(1);
|
2001-10-19 19:10:36 +00:00
|
|
|
/* copy it over */
|
1994-05-27 12:33:43 +00:00
|
|
|
*e_token++ = *buf_ptr++;
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
2018-06-03 18:19:41 +00:00
|
|
|
*e_token = '\0';
|
2016-11-27 20:38:14 +00:00
|
|
|
|
|
|
|
if (s_token[0] == 'L' && s_token[1] == '\0' &&
|
|
|
|
(*buf_ptr == '"' || *buf_ptr == '\''))
|
|
|
|
return (strpfx);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
2018-06-03 14:13:11 +00:00
|
|
|
state->keyword = 0;
|
2018-06-03 16:21:15 +00:00
|
|
|
if (state->last_token == structure && !state->p_l_follow) {
|
2004-02-09 15:27:02 +00:00
|
|
|
/* if last token was 'struct' and we're not
|
|
|
|
* in parentheses, then this token
|
1994-05-27 12:33:43 +00:00
|
|
|
* should be treated as a declaration */
|
2018-06-03 14:13:11 +00:00
|
|
|
state->last_u_d = true;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (decl);
|
|
|
|
}
|
2018-06-03 16:21:15 +00:00
|
|
|
/*
|
|
|
|
* Operator after identifier is binary unless last token was 'struct'
|
|
|
|
*/
|
|
|
|
state->last_u_d = (state->last_token == structure);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2016-08-04 15:27:09 +00:00
|
|
|
p = bsearch(s_token,
|
|
|
|
specials,
|
|
|
|
sizeof(specials) / sizeof(specials[0]),
|
|
|
|
sizeof(specials[0]),
|
|
|
|
strcmp_type);
|
|
|
|
if (p == NULL) { /* not a special keyword... */
|
|
|
|
char *u;
|
|
|
|
|
|
|
|
/* ... so maybe a type_t or a typedef */
|
2018-06-10 16:44:18 +00:00
|
|
|
if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
|
2016-08-04 15:27:09 +00:00
|
|
|
strcmp(u, "_t") == 0) || (typename_top >= 0 &&
|
|
|
|
bsearch(s_token, typenames, typename_top + 1,
|
|
|
|
sizeof(typenames[0]), strcmp_type))) {
|
2018-06-03 14:13:11 +00:00
|
|
|
state->keyword = 4; /* a type name */
|
|
|
|
state->last_u_d = true;
|
2016-08-04 15:27:09 +00:00
|
|
|
goto found_typename;
|
2010-03-31 17:05:30 +00:00
|
|
|
}
|
2016-08-04 15:27:09 +00:00
|
|
|
} else { /* we have a keyword */
|
2018-06-03 14:13:11 +00:00
|
|
|
state->keyword = p->rwcode;
|
|
|
|
state->last_u_d = true;
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (p->rwcode) {
|
2016-08-03 16:33:34 +00:00
|
|
|
case 7: /* it is a switch */
|
1994-05-27 12:33:43 +00:00
|
|
|
return (swstmt);
|
2016-08-03 16:33:34 +00:00
|
|
|
case 8: /* a case or default */
|
1994-05-27 12:33:43 +00:00
|
|
|
return (casestmt);
|
|
|
|
|
|
|
|
case 3: /* a "struct" */
|
2004-02-09 15:27:02 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case 4: /* one of the declaration keywords */
|
2016-08-04 15:27:09 +00:00
|
|
|
found_typename:
|
2018-06-03 14:13:11 +00:00
|
|
|
if (state->p_l_follow) {
|
2016-08-03 16:33:34 +00:00
|
|
|
/* inside parens: cast, param list, offsetof or sizeof */
|
2018-06-03 14:13:11 +00:00
|
|
|
state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2018-06-03 19:05:20 +00:00
|
|
|
if (state->last_token == period || state->last_token == unary_op) {
|
|
|
|
state->keyword = 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-03 16:21:15 +00:00
|
|
|
if (p != NULL && p->rwcode == 3)
|
|
|
|
return (structure);
|
|
|
|
if (state->p_l_follow)
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (decl);
|
|
|
|
|
|
|
|
case 5: /* if, while, for */
|
|
|
|
return (sp_paren);
|
|
|
|
|
|
|
|
case 6: /* do, else */
|
|
|
|
return (sp_nparen);
|
|
|
|
|
2016-12-02 01:25:51 +00:00
|
|
|
case 10: /* storage class specifier */
|
|
|
|
return (storage);
|
|
|
|
|
2018-06-01 09:41:15 +00:00
|
|
|
case 11: /* typedef */
|
|
|
|
return (type_def);
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
default: /* all others are treated like any other
|
|
|
|
* identifier */
|
|
|
|
return (ident);
|
|
|
|
} /* end of switch */
|
|
|
|
} /* end of if (found_it) */
|
2018-06-03 14:13:11 +00:00
|
|
|
if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
|
|
|
|
state->in_parameter_declaration == 0 && state->block_init == 0) {
|
2002-06-24 17:40:27 +00:00
|
|
|
char *tp = buf_ptr;
|
1994-05-27 12:33:43 +00:00
|
|
|
while (tp < buf_end)
|
|
|
|
if (*tp++ == ')' && (*tp == ';' || *tp == ','))
|
|
|
|
goto not_proc;
|
2018-06-03 14:13:11 +00:00
|
|
|
strncpy(state->procname, token, sizeof state->procname - 1);
|
|
|
|
if (state->in_decl)
|
|
|
|
state->in_parameter_declaration = 1;
|
2018-06-03 16:21:15 +00:00
|
|
|
return (funcname);
|
1994-05-27 12:33:43 +00:00
|
|
|
not_proc:;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The following hack attempts to guess whether or not the current
|
|
|
|
* token is in fact a declaration keyword -- one that has been
|
|
|
|
* typedefd
|
|
|
|
*/
|
2018-06-03 16:42:58 +00:00
|
|
|
else if (!state->p_l_follow && !state->block_init &&
|
|
|
|
!state->in_stmt &&
|
|
|
|
((*buf_ptr == '*' && buf_ptr[1] != '=') ||
|
|
|
|
isalpha((unsigned char)*buf_ptr)) &&
|
|
|
|
(state->last_token == semicolon || state->last_token == lbrace ||
|
|
|
|
state->last_token == rbrace)) {
|
2018-06-03 14:13:11 +00:00
|
|
|
state->keyword = 4; /* a type name */
|
|
|
|
state->last_u_d = true;
|
1994-05-27 12:33:43 +00:00
|
|
|
return decl;
|
|
|
|
}
|
2018-06-03 16:21:15 +00:00
|
|
|
if (state->last_token == decl) /* if this is a declared variable,
|
|
|
|
* then following sign is unary */
|
2018-06-03 14:13:11 +00:00
|
|
|
state->last_u_d = true; /* will make "int a -1" work */
|
1994-05-27 12:33:43 +00:00
|
|
|
return (ident); /* the ident is not in the list */
|
|
|
|
} /* end of procesing for alpanum character */
|
|
|
|
|
|
|
|
/* Scan a non-alphanumeric token */
|
|
|
|
|
2018-06-03 18:19:41 +00:00
|
|
|
CHECK_SIZE_TOKEN(3); /* things like "<<=" */
|
1994-05-27 12:33:43 +00:00
|
|
|
*e_token++ = *buf_ptr; /* if it is only a one-character token, it is
|
|
|
|
* moved here */
|
|
|
|
*e_token = '\0';
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
|
|
|
|
switch (*token) {
|
|
|
|
case '\n':
|
2018-06-03 14:13:11 +00:00
|
|
|
unary_delim = state->last_u_d;
|
|
|
|
state->last_nl = true; /* remember that we just had a newline */
|
1994-05-27 12:33:43 +00:00
|
|
|
code = (had_eof ? 0 : newline);
|
|
|
|
|
|
|
|
/*
|
2002-10-16 13:58:39 +00:00
|
|
|
* if data has been exhausted, the newline is a dummy, and we should
|
1994-05-27 12:33:43 +00:00
|
|
|
* return code to stop
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\'': /* start of quoted character */
|
|
|
|
case '"': /* start of string */
|
|
|
|
qchar = *token;
|
|
|
|
do { /* copy the string */
|
|
|
|
while (1) { /* move one character or [/<char>]<char> */
|
|
|
|
if (*buf_ptr == '\n') {
|
2005-11-20 13:48:15 +00:00
|
|
|
diag2(1, "Unterminated literal");
|
1994-05-27 12:33:43 +00:00
|
|
|
goto stop_lit;
|
|
|
|
}
|
2018-06-03 18:19:41 +00:00
|
|
|
CHECK_SIZE_TOKEN(2);
|
1994-05-27 12:33:43 +00:00
|
|
|
*e_token = *buf_ptr++;
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
if (*e_token == BACKSLASH) { /* if escape, copy extra char */
|
|
|
|
if (*buf_ptr == '\n') /* check for escaped newline */
|
|
|
|
++line_no;
|
|
|
|
*++e_token = *buf_ptr++;
|
|
|
|
++e_token; /* we must increment this again because we
|
|
|
|
* copied two chars */
|
|
|
|
if (buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break; /* we copied one character */
|
|
|
|
} /* end of while (1) */
|
|
|
|
} while (*e_token++ != qchar);
|
|
|
|
stop_lit:
|
|
|
|
code = ident;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('('):
|
|
|
|
case ('['):
|
|
|
|
unary_delim = true;
|
|
|
|
code = lparen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (')'):
|
|
|
|
case (']'):
|
|
|
|
code = rparen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
2018-06-03 14:13:11 +00:00
|
|
|
unary_delim = state->last_u_d;
|
1994-05-27 12:33:43 +00:00
|
|
|
code = preesc;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
unary_delim = true;
|
|
|
|
code = question;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (':'):
|
|
|
|
code = colon;
|
|
|
|
unary_delim = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (';'):
|
|
|
|
unary_delim = true;
|
|
|
|
code = semicolon;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('{'):
|
|
|
|
unary_delim = true;
|
|
|
|
|
|
|
|
/*
|
2018-06-03 14:13:11 +00:00
|
|
|
* if (state->in_or_st) state->block_init = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2018-06-03 14:13:11 +00:00
|
|
|
/* ? code = state->block_init ? lparen : lbrace; */
|
1994-05-27 12:33:43 +00:00
|
|
|
code = lbrace;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ('}'):
|
|
|
|
unary_delim = true;
|
2018-06-03 14:13:11 +00:00
|
|
|
/* ? code = state->block_init ? rparen : rbrace; */
|
1994-05-27 12:33:43 +00:00
|
|
|
code = rbrace;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 014: /* a form feed */
|
2018-06-03 14:13:11 +00:00
|
|
|
unary_delim = state->last_u_d;
|
|
|
|
state->last_nl = true; /* remember this so we can set 'state->col_1'
|
1994-05-27 12:33:43 +00:00
|
|
|
* right */
|
|
|
|
code = form_feed;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (','):
|
|
|
|
unary_delim = true;
|
|
|
|
code = comma;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
unary_delim = false;
|
|
|
|
code = period;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
case '+': /* check for -, +, --, ++ */
|
2018-06-03 14:13:11 +00:00
|
|
|
code = (state->last_u_d ? unary_op : binary_op);
|
1994-05-27 12:33:43 +00:00
|
|
|
unary_delim = true;
|
|
|
|
|
|
|
|
if (*buf_ptr == token[0]) {
|
|
|
|
/* check for doubled character */
|
|
|
|
*e_token++ = *buf_ptr++;
|
|
|
|
/* buffer overflow will be checked at end of loop */
|
2018-06-03 16:21:15 +00:00
|
|
|
if (state->last_token == ident || state->last_token == rparen) {
|
2018-06-03 14:13:11 +00:00
|
|
|
code = (state->last_u_d ? unary_op : postop);
|
1994-05-27 12:33:43 +00:00
|
|
|
/* check for following ++ or -- */
|
|
|
|
unary_delim = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*buf_ptr == '=')
|
|
|
|
/* check for operator += */
|
|
|
|
*e_token++ = *buf_ptr++;
|
|
|
|
else if (*buf_ptr == '>') {
|
|
|
|
/* check for operator -> */
|
|
|
|
*e_token++ = *buf_ptr++;
|
2018-06-03 13:40:58 +00:00
|
|
|
unary_delim = false;
|
|
|
|
code = unary_op;
|
2018-06-03 14:13:11 +00:00
|
|
|
state->want_blank = false;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
break; /* buffer overflow will be checked at end of
|
|
|
|
* switch */
|
|
|
|
|
|
|
|
case '=':
|
2018-06-03 14:13:11 +00:00
|
|
|
if (state->in_or_st)
|
|
|
|
state->block_init = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (*buf_ptr == '=') {/* == */
|
|
|
|
*e_token++ = '='; /* Flip =+ to += */
|
|
|
|
buf_ptr++;
|
|
|
|
*e_token = 0;
|
|
|
|
}
|
|
|
|
code = binary_op;
|
|
|
|
unary_delim = true;
|
|
|
|
break;
|
|
|
|
/* can drop thru!!! */
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
case '<':
|
|
|
|
case '!': /* ops like <, <<, <=, !=, etc */
|
|
|
|
if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
|
|
|
|
*e_token++ = *buf_ptr;
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
|
|
|
if (*buf_ptr == '=')
|
|
|
|
*e_token++ = *buf_ptr++;
|
2018-06-03 14:13:11 +00:00
|
|
|
code = (state->last_u_d ? unary_op : binary_op);
|
1994-05-27 12:33:43 +00:00
|
|
|
unary_delim = true;
|
|
|
|
break;
|
|
|
|
|
2018-06-03 16:42:58 +00:00
|
|
|
case '*':
|
|
|
|
unary_delim = true;
|
|
|
|
if (!state->last_u_d) {
|
|
|
|
if (*buf_ptr == '=')
|
|
|
|
*e_token++ = *buf_ptr++;
|
|
|
|
code = binary_op;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
|
2018-06-03 18:19:41 +00:00
|
|
|
if (*buf_ptr == '*') {
|
|
|
|
CHECK_SIZE_TOKEN(1);
|
2018-06-03 16:42:58 +00:00
|
|
|
*e_token++ = *buf_ptr;
|
2018-06-03 18:19:41 +00:00
|
|
|
}
|
2018-06-03 16:42:58 +00:00
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
|
|
|
if (ps.in_decl) {
|
|
|
|
char *tp = buf_ptr;
|
|
|
|
|
|
|
|
while (isalpha((unsigned char)*tp) ||
|
|
|
|
isspace((unsigned char)*tp)) {
|
|
|
|
if (++tp >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
|
|
|
if (*tp == '(')
|
|
|
|
ps.procname[0] = ' ';
|
|
|
|
}
|
|
|
|
code = unary_op;
|
|
|
|
break;
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
default:
|
|
|
|
if (token[0] == '/' && *buf_ptr == '*') {
|
|
|
|
/* it is start of comment */
|
|
|
|
*e_token++ = '*';
|
|
|
|
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
|
|
|
|
code = comment;
|
2018-06-03 14:13:11 +00:00
|
|
|
unary_delim = state->last_u_d;
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
|
|
|
|
/*
|
|
|
|
* handle ||, &&, etc, and also things as in int *****i
|
|
|
|
*/
|
2018-06-03 18:19:41 +00:00
|
|
|
CHECK_SIZE_TOKEN(1);
|
1994-05-27 12:33:43 +00:00
|
|
|
*e_token++ = *buf_ptr;
|
|
|
|
if (++buf_ptr >= buf_end)
|
|
|
|
fill_buffer();
|
|
|
|
}
|
2018-06-03 14:13:11 +00:00
|
|
|
code = (state->last_u_d ? unary_op : binary_op);
|
1994-05-27 12:33:43 +00:00
|
|
|
unary_delim = true;
|
|
|
|
|
|
|
|
|
|
|
|
} /* end of switch */
|
|
|
|
if (buf_ptr >= buf_end) /* check for input buffer empty */
|
|
|
|
fill_buffer();
|
2018-06-03 14:13:11 +00:00
|
|
|
state->last_u_d = unary_delim;
|
2018-06-03 18:19:41 +00:00
|
|
|
CHECK_SIZE_TOKEN(1);
|
1994-05-27 12:33:43 +00:00
|
|
|
*e_token = '\0'; /* null terminate the token */
|
|
|
|
return (code);
|
|
|
|
}
|
|
|
|
|
2018-07-16 05:46:50 +00:00
|
|
|
/* Initialize constant transition table */
|
|
|
|
void
|
|
|
|
init_constant_tt(void)
|
|
|
|
{
|
|
|
|
table['-'] = table['+'];
|
|
|
|
table['8'] = table['9'];
|
|
|
|
table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
|
|
|
|
table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
|
|
|
|
table['B'] = table['b'];
|
|
|
|
table['E'] = table['e'];
|
|
|
|
table['U'] = table['u'];
|
|
|
|
table['X'] = table['x'];
|
|
|
|
table['P'] = table['p'];
|
|
|
|
table['F'] = table['f'];
|
|
|
|
}
|
|
|
|
|
2001-10-28 18:35:32 +00:00
|
|
|
void
|
2016-08-04 15:27:09 +00:00
|
|
|
alloc_typenames(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
typenames = (const char **)malloc(sizeof(typenames[0]) *
|
|
|
|
(typename_count = 16));
|
|
|
|
if (typenames == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
add_typename(const char *key)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2016-08-04 15:27:09 +00:00
|
|
|
int comparison;
|
2016-08-23 01:40:45 +00:00
|
|
|
const char *copy;
|
2016-08-04 15:27:09 +00:00
|
|
|
|
|
|
|
if (typename_top + 1 >= typename_count) {
|
|
|
|
typenames = realloc((void *)typenames,
|
|
|
|
sizeof(typenames[0]) * (typename_count *= 2));
|
|
|
|
if (typenames == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
if (typename_top == -1)
|
2016-08-23 01:40:45 +00:00
|
|
|
typenames[++typename_top] = copy = strdup(key);
|
2016-08-04 15:27:09 +00:00
|
|
|
else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
|
|
|
|
/* take advantage of sorted input */
|
2016-08-23 01:40:45 +00:00
|
|
|
if (comparison == 0) /* remove duplicates */
|
|
|
|
return;
|
|
|
|
typenames[++typename_top] = copy = strdup(key);
|
2016-08-04 15:27:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int p;
|
|
|
|
|
2016-08-23 02:07:08 +00:00
|
|
|
for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
|
2016-08-04 15:27:09 +00:00
|
|
|
/* find place for the new key */;
|
|
|
|
if (comparison == 0) /* remove duplicates */
|
1994-05-27 12:33:43 +00:00
|
|
|
return;
|
2016-08-04 15:27:09 +00:00
|
|
|
memmove(&typenames[p + 1], &typenames[p],
|
|
|
|
sizeof(typenames[0]) * (++typename_top - p));
|
2016-08-23 01:40:45 +00:00
|
|
|
typenames[p] = copy = strdup(key);
|
2016-08-03 22:08:07 +00:00
|
|
|
}
|
2016-08-23 01:40:45 +00:00
|
|
|
|
|
|
|
if (copy == NULL)
|
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|