#if 0
#ifndef lint
static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/usr.bin/indent/lexi.c,v 1.21 2010/04/15 21:41:07 avg Exp $");
#include <err.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "indent_globs.h"
#include "indent_codes.h"
#include "indent.h"
#define alphanum 1
#define opchar 3
struct templ {
const char *rwd;
int rwcode;
};
struct templ specials[1000] =
{
{"switch", 1},
{"case", 2},
{"break", 0},
{"struct", 3},
{"union", 3},
{"enum", 3},
{"default", 2},
{"int", 4},
{"char", 4},
{"float", 4},
{"double", 4},
{"long", 4},
{"short", 4},
{"typdef", 4},
{"unsigned", 4},
{"register", 4},
{"static", 4},
{"global", 4},
{"extern", 4},
{"void", 4},
{"const", 4},
{"volatile", 4},
{"goto", 0},
{"return", 0},
{"if", 5},
{"while", 5},
{"for", 5},
{"else", 6},
{"do", 6},
{"sizeof", 7},
{0, 0}
};
char chartype[128] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 3, 0, 0, 1, 3, 3, 0,
0, 0, 3, 3, 0, 3, 0, 3,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 3, 3, 3, 3,
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 3, 1,
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 3, 0, 3, 0
};
int
lexi(void)
{
int unary_delim;
static int last_code;
static int l_struct;
int code;
char qchar;
e_token = s_token;
unary_delim = false;
ps.col_1 = ps.last_nl;
ps.last_nl = false;
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
ps.col_1 = false;
if (++buf_ptr >= buf_end)
fill_buffer();
}
if (chartype[(int)*buf_ptr] == alphanum || (buf_ptr[0] == '.' && isdigit(buf_ptr[1]))) {
const char *j;
struct templ *p;
if (isdigit(*buf_ptr) || (buf_ptr[0] == '.' && isdigit(buf_ptr[1]))) {
int seendot = 0,
seenexp = 0,
seensfx = 0;
if (*buf_ptr == '0' &&
(buf_ptr[1] == 'x' || buf_ptr[1] == 'X')) {
*e_token++ = *buf_ptr++;
*e_token++ = *buf_ptr++;
while (isxdigit(*buf_ptr)) {
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
}
}
else
while (1) {
if (*buf_ptr == '.') {
if (seendot)
break;
else
seendot++;
}
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
if (!isdigit(*buf_ptr) && *buf_ptr != '.') {
if ((*buf_ptr != 'E' && *buf_ptr != 'e') || seenexp)
break;
else {
seenexp++;
seendot++;
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
if (*buf_ptr == '+' || *buf_ptr == '-')
*e_token++ = *buf_ptr++;
}
}
}
while (1) {
if (!(seensfx & 1) &&
(*buf_ptr == 'U' || *buf_ptr == 'u')) {
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
seensfx |= 1;
continue;
}
if (!(seensfx & 2) &&
(*buf_ptr == 'L' || *buf_ptr == 'l')) {
CHECK_SIZE_TOKEN;
if (buf_ptr[1] == buf_ptr[0])
*e_token++ = *buf_ptr++;
*e_token++ = *buf_ptr++;
seensfx |= 2;
continue;
}
break;
}
}
else
while (chartype[(int)*buf_ptr] == alphanum || *buf_ptr == BACKSLASH) {
if (*buf_ptr == BACKSLASH) {
if (*(buf_ptr + 1) == '\n') {
buf_ptr += 2;
if (buf_ptr >= buf_end)
fill_buffer();
} else
break;
}
CHECK_SIZE_TOKEN;
*e_token++ = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
}
*e_token++ = '\0';
while (*buf_ptr == ' ' || *buf_ptr == '\t') {
if (++buf_ptr >= buf_end)
fill_buffer();
}
ps.its_a_keyword = false;
ps.sizeof_keyword = false;
if (l_struct && !ps.p_l_follow) {
l_struct = false;
last_code = ident;
ps.last_u_d = true;
return (decl);
}
ps.last_u_d = l_struct;
l_struct = false;
last_code = ident;
if (auto_typedefs) {
const char *q = s_token;
size_t q_len = strlen(q);
if (q_len > 2 &&
(strcmp(q + q_len - 2, "_t") == 0)) {
ps.its_a_keyword = true;
ps.last_u_d = true;
goto found_auto_typedef;
}
}
for (p = specials; (j = p->rwd) != 0; p++) {
const char *q = s_token;
if (*j++ != *q++ || *j++ != *q++)
continue;
if (q[-1] == 0)
break;
while (*q++ == *j)
if (*j++ == 0)
goto found_keyword;
}
if (p->rwd) {
found_keyword:
ps.its_a_keyword = true;
ps.last_u_d = true;
switch (p->rwcode) {
case 1:
return (swstmt);
case 2:
return (casestmt);
case 3:
l_struct = true;
case 4:
found_auto_typedef:
if (ps.p_l_follow) {
ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.sizeof_mask;
break;
}
last_code = decl;
return (decl);
case 5:
return (sp_paren);
case 6:
return (sp_nparen);
case 7:
ps.sizeof_keyword = true;
default:
return (ident);
}
}
if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0) {
char *tp = buf_ptr;
while (tp < buf_end)
if (*tp++ == ')' && (*tp == ';' || *tp == ','))
goto not_proc;
strncpy(ps.procname, token, sizeof ps.procname - 1);
ps.in_parameter_declaration = 1;
rparen_count = 1;
not_proc:;
}
if (((*buf_ptr == '*' && buf_ptr[1] != '=') || isalpha(*buf_ptr) || *buf_ptr == '_')
&& !ps.p_l_follow
&& !ps.block_init
&& (ps.last_token == rparen || ps.last_token == semicolon ||
ps.last_token == decl ||
ps.last_token == lbrace || ps.last_token == rbrace)) {
ps.its_a_keyword = true;
ps.last_u_d = true;
last_code = decl;
return decl;
}
if (last_code == decl)
ps.last_u_d = true;
last_code = ident;
return (ident);
}
*e_token++ = *buf_ptr;
*e_token = '\0';
if (++buf_ptr >= buf_end)
fill_buffer();
switch (*token) {
case '\n':
unary_delim = ps.last_u_d;
ps.last_nl = true;
code = (had_eof ? 0 : newline);
break;
case '\'':
case '"':
qchar = *token;
if (troff) {
e_token[-1] = '`';
if (qchar == '"')
*e_token++ = '`';
e_token = chfont(&bodyf, &stringf, e_token);
}
do {
while (1) {
if (*buf_ptr == '\n') {
diag2(1, "Unterminated literal");
goto stop_lit;
}
CHECK_SIZE_TOKEN;
*e_token = *buf_ptr++;
if (buf_ptr >= buf_end)
fill_buffer();
if (*e_token == BACKSLASH) {
if (*buf_ptr == '\n')
++line_no;
if (troff) {
*++e_token = BACKSLASH;
if (*buf_ptr == BACKSLASH)
*++e_token = BACKSLASH;
}
*++e_token = *buf_ptr++;
++e_token;
if (buf_ptr >= buf_end)
fill_buffer();
}
else
break;
}
} while (*e_token++ != qchar);
if (troff) {
e_token = chfont(&stringf, &bodyf, e_token - 1);
if (qchar == '"')
*e_token++ = '\'';
}
stop_lit:
code = ident;
break;
case ('('):
case ('['):
unary_delim = true;
code = lparen;
break;
case (')'):
case (']'):
code = rparen;
break;
case '#':
unary_delim = ps.last_u_d;
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;
code = lbrace;
break;
case ('}'):
unary_delim = true;
code = rbrace;
break;
case 014:
unary_delim = ps.last_u_d;
ps.last_nl = true;
code = form_feed;
break;
case (','):
unary_delim = true;
code = comma;
break;
case '.':
unary_delim = false;
code = period;
break;
case '-':
case '+':
code = (ps.last_u_d ? unary_op : binary_op);
unary_delim = true;
if (*buf_ptr == token[0]) {
*e_token++ = *buf_ptr++;
if (last_code == ident || last_code == rparen) {
code = (ps.last_u_d ? unary_op : postop);
unary_delim = false;
}
}
else if (*buf_ptr == '=')
*e_token++ = *buf_ptr++;
else if (*buf_ptr == '>') {
*e_token++ = *buf_ptr++;
if (!pointer_as_binop) {
unary_delim = false;
code = unary_op;
ps.want_blank = false;
}
}
break;
case '=':
if (ps.in_or_st)
ps.block_init = 1;
#ifdef undef
if (chartype[*buf_ptr] == opchar) {
e_token[-1] = *buf_ptr++;
if ((e_token[-1] == '<' || e_token[-1] == '>') && e_token[-1] == *buf_ptr)
*e_token++ = *buf_ptr++;
*e_token++ = '=';
*e_token = 0;
}
#else
if (*buf_ptr == '=') {
*e_token++ = '=';
buf_ptr++;
*e_token = 0;
}
#endif
code = binary_op;
unary_delim = true;
break;
case '>':
case '<':
case '!':
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++;
code = (ps.last_u_d ? unary_op : binary_op);
unary_delim = true;
break;
default:
if (token[0] == '/' && *buf_ptr == '*') {
*e_token++ = '*';
if (++buf_ptr >= buf_end)
fill_buffer();
code = comment;
unary_delim = ps.last_u_d;
break;
}
while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
*e_token++ = *buf_ptr;
if (++buf_ptr >= buf_end)
fill_buffer();
}
code = (ps.last_u_d ? unary_op : binary_op);
unary_delim = true;
}
if (code != newline) {
l_struct = false;
last_code = code;
}
if (buf_ptr >= buf_end)
fill_buffer();
ps.last_u_d = unary_delim;
*e_token = '\0';
return (code);
}
void
addkey(char *key, int val)
{
struct templ *p = specials;
while (p->rwd)
if (p->rwd[0] == key[0] && strcmp(p->rwd, key) == 0)
return;
else
p++;
if (p >= specials + sizeof specials / sizeof specials[0])
return;
p->rwd = key;
p->rwcode = val;
p[1].rwd = 0;
p[1].rwcode = 0;
}