-/* $Id: reader.c,v 1.73 2017/07/09 19:15:35 tom Exp $ */
+/* $Id: reader.c,v 1.84 2020/09/10 20:26:13 tom Exp $ */
#include "defs.h"
/* The line size must be a positive integer. One hundred was chosen */
/* because few lines in Yacc input grammars exceed 100 characters. */
/* Note that if a line exceeds LINESIZE characters, the line buffer */
-/* will be expanded to accomodate it. */
+/* will be expanded to accommodate it. */
#define LINESIZE 100
param *lex_param;
param *parse_param;
+static const char *code_keys[] =
+{
+ "", "requires", "provides", "top", "imports",
+};
+
+struct code_lines code_lines[CODE_MAX];
+
#if defined(YYBTYACC)
int destructor = 0; /* =1 if at least one %destructor */
else
UNLESS(!isdigit(UCH(ch)));
line_1st = n;
- ld = ldNUM;
+ ld = ldNUM; /* this is needed, but cppcheck says no... */
/* FALLTHRU */
case ldNUM:
if (isdigit(UCH(ch)))
get_line(void)
{
FILE *f = input_file;
- int c;
- int i;
do
{
+ int c;
+ int i;
+
if (saw_eof || (c = getc(f)) == EOF)
{
if (line)
/* *INDENT-OFF* */
static struct keyword
{
- char name[14];
+ char name[16];
int token;
}
keywords[] = {
{ "binary", NONASSOC },
+ { "code", XCODE },
{ "debug", XXXDEBUG },
#if defined(YYBTYACC)
{ "destructor", DESTRUCTOR },
{ "error-verbose",ERROR_VERBOSE },
{ "expect", EXPECT },
{ "expect-rr", EXPECT_RR },
- { "ident", IDENT },
+ { "ident", IDENT },
#if defined(YYBTYACC)
{ "initial-action", INITIAL_ACTION },
#endif
{ "nonassoc", NONASSOC },
{ "parse-param", PARSE_PARAM },
{ "pure-parser", PURE_PARSER },
- { "right", RIGHT },
+ { "right", RIGHT },
{ "start", START },
{ "term", TOKEN },
{ "token", TOKEN },
- { "token-table", TOKEN_TABLE },
+ { "token-table", TOKEN_TABLE },
{ "type", TYPE },
{ "union", UNION },
{ "yacc", POSIX_YACC },
{
int c;
char *t_cptr = cptr;
- struct keyword *key;
c = *++cptr;
if (isalpha(UCH(c)))
{
+ struct keyword *key;
+
cinc = 0;
for (;;)
{
copy_string(int quote)
{
struct mstring *temp = msnew();
- int c;
struct ainfo a;
a.a_lineno = lineno;
a.a_line = dup_line();
for (;;)
{
- c = *cptr++;
+ int c = *cptr++;
+
mputc(temp, c);
if (c == quote)
{
return msdone(temp);
}
+static int
+check_key(int pos)
+{
+ const char *key = code_keys[pos];
+ while (*cptr && *key)
+ if (*key++ != *cptr++)
+ return 0;
+ if (*key || (!isspace(UCH(*cptr)) && *cptr != L_CURL))
+ return 0;
+ cptr--;
+ return 1;
+}
+
+static void
+copy_code(void)
+{
+ int c;
+ int curl;
+ int cline;
+ int on_line = 0;
+ int pos = CODE_HEADER;
+ struct mstring *code_mstr;
+
+ /* read %code <keyword> { */
+ for (;;)
+ {
+ c = *++cptr;
+ if (c == EOF)
+ unexpected_EOF();
+ if (isspace(UCH(c)))
+ continue;
+
+ if (c == L_CURL)
+ break;
+
+ if (pos == CODE_HEADER)
+ {
+ switch (UCH(c))
+ {
+ case 'r':
+ pos = CODE_REQUIRES;
+ break;
+ case 'p':
+ pos = CODE_PROVIDES;
+ break;
+ case 't':
+ pos = CODE_TOP;
+ break;
+ case 'i':
+ pos = CODE_IMPORTS;
+ break;
+ default:
+ break;
+ }
+
+ if (pos == -1 || !check_key(pos))
+ {
+ syntax_error(lineno, line, cptr);
+ /*NOTREACHED */
+ }
+ }
+ }
+
+ cptr++; /* skip initial curl */
+ while (*cptr && isspace(UCH(*cptr))) /* skip space */
+ cptr++;
+ curl = 1; /* nesting count */
+
+ /* gather text */
+ code_lines[pos].name = code_keys[pos];
+ if ((cline = (int)code_lines[pos].num) != 0)
+ {
+ code_mstr = msrenew(code_lines[pos].lines);
+ }
+ else
+ {
+ code_mstr = msnew();
+ }
+ cline++;
+ msprintf(code_mstr, line_format, lineno, input_file_name);
+ for (;;)
+ {
+ c = *cptr++;
+ switch (c)
+ {
+ case '\0':
+ get_line();
+ if (line == NULL)
+ {
+ unexpected_EOF();
+ /*NOTREACHED */
+ }
+ continue;
+ case '\n':
+ cline++;
+ on_line = 0;
+ break;
+ case L_CURL:
+ curl++;
+ break;
+ case R_CURL:
+ if (--curl == 0)
+ {
+ if (on_line > 1)
+ {
+ mputc(code_mstr, '\n');
+ cline++;
+ }
+ code_lines[pos].lines = msdone(code_mstr);
+ code_lines[pos].num = (size_t) cline;
+ return;
+ }
+ break;
+ default:
+ break;
+ }
+ mputc(code_mstr, c);
+ on_line++;
+ }
+}
+
static void
copy_text(void)
{
{
buf_size = (size_t) linesize;
buf = TMALLOC(char, buf_size);
+ NO_SPACE(buf);
}
else if (c == '\n')
{
+ char *tmp;
+
get_line();
if (line == NULL)
unexpected_EOF();
--cptr;
buf_size += (size_t) linesize;
- buf = TREALLOC(char, buf, buf_size);
+ tmp = TREALLOC(char, buf, buf_size);
+ NO_SPACE(tmp);
+ buf = tmp;
}
- NO_SPACE(buf);
if (curly)
{
if ((state == 2) && (c == L_CURL))
if (parms[i] == ']')
{
int level = 1;
- while (i >= 0 && level > 0 && parms[i] != '[')
+ while (i >= 0)
{
- if (parms[i] == ']')
+ char ch = parms[i--];
+ if (ch == ']')
+ {
++level;
- else if (parms[i] == '[')
- --level;
- i--;
+ }
+ else if (ch == '[')
+ {
+ if (--level <= 1)
+ {
+ ++i;
+ break;
+ }
+ }
}
if (i <= 0)
unexpected_EOF();
static int
is_reserved(char *name)
{
- char *s;
-
if (strcmp(name, ".") == 0 ||
strcmp(name, "$accept") == 0 ||
strcmp(name, "$end") == 0)
if (name[0] == '$' && name[1] == '$' && isdigit(UCH(name[2])))
{
- s = name + 3;
+ char *s = name + 3;
+
while (isdigit(UCH(*s)))
++s;
if (*s == NUL)
if (n > MAXYYINT)
{
syntax_error(lineno, line, base);
- n = 0;
- break;
+ /*NOTREACHED */
}
}
declare_argtypes(bucket *bp)
{
char *tags[MAXARGS];
- int args = 0, c;
+ int args = 0;
if (bp->args >= 0)
retyped_warning(bp->name);
cptr++; /* skip open paren */
for (;;)
{
- c = nextc();
+ int c = nextc();
if (c == EOF)
unexpected_EOF();
if (c != '<')
static void
read_declarations(void)
{
- int c, k;
-
cache_size = CACHE_SIZE;
cache = TMALLOC(char, cache_size);
NO_SPACE(cache);
for (;;)
{
- c = nextc();
+ int k;
+ int c = nextc();
+
if (c == EOF)
unexpected_EOF();
if (c != '%')
copy_ident();
break;
+ case XCODE:
+ copy_code();
+ break;
+
case TEXT:
copy_text();
break;
{
char *p = *theptr;
struct mstring *c = msnew();
- int i, j, n;
+ int i, n;
Value_t *offsets = NULL, maxoffset;
bucket **rhs;
}
if (maxoffset > 0)
{
+ int j;
+
offsets = TMALLOC(Value_t, maxoffset + 1);
NO_SPACE(offsets);
{
char *p = *theptr;
int rv = 0;
- int i, j, n = 0;
+ int i, n = 0;
Value_t *offsets = NULL, maxoffset = 0;
bucket **rhs;
char *tag = 0;
}
if (maxoffset > 0)
{
+ int j;
+
offsets = TMALLOC(Value_t, maxoffset + 1);
NO_SPACE(offsets);
{
char *arg;
if (!(p = parse_id(p, &arg)))
+ {
+ FREE(offsets);
return 0;
+ }
for (i = plhs[nrules]->args - 1; i >= 0; i--)
if (arg == plhs[nrules]->argnames[i])
break;
rv = 0;
if (maxoffset > 0)
FREE(offsets);
- if (*p || rv <= 0)
+ if (p == 0 || *p || rv <= 0)
return 0;
*theptr = p + 1;
return rv;
{
int c;
bucket *bp;
- char *s_cptr;
int s_lineno;
#if defined(YYBTYACC)
char *args = NULL;
for (;;)
{
+ char *s_cptr;
+
c = nextc();
if (c != '%')
break;
s_cptr = cptr;
switch (keyword())
{
+ case XCODE:
+ copy_code();
+ break;
+
case MARK:
no_grammar();
static void
end_rule(void)
{
- int i;
-
if (!last_was_action && plhs[nrules]->tag)
{
if (pitem[nitems - 1])
{
+ int i;
+
for (i = nitems - 1; (i > 0) && pitem[i]; --i)
continue;
if (pitem[i + 1] == 0 || pitem[i + 1]->tag != plhs[nrules]->tag)
copy_destructor(void)
{
char *code_text;
- int c;
struct ainfo a;
bucket *bp;
for (;;)
{
- c = nextc();
+ int c = nextc();
if (c == EOF)
unexpected_EOF();
if (c == '<')
static void
read_grammar(void)
{
- int c;
-
initialize_grammar();
advance_to_start();
for (;;)
{
- c = nextc();
+ int c = nextc();
+
if (c == EOF)
break;
if (isalpha(UCH(c))
pack_names(void)
{
bucket *bp;
- char *p, *s, *t;
+ char *p;
+ char *t;
name_pool_size = 13; /* 13 == sizeof("$end") + sizeof("$accept") */
for (bp = first_symbol; bp; bp = bp->next)
t = name_pool + 13;
for (bp = first_symbol; bp; bp = bp->next)
{
+ char *s = bp->name;
+
p = t;
- s = bp->name;
while ((*t++ = *s++) != 0)
continue;
FREE(bp->name);
static void
protect_string(char *src, char **des)
{
- unsigned len;
- char *s;
- char *d;
-
*des = src;
if (src)
{
- len = 1;
+ char *s;
+ char *d;
+
+ unsigned len = 1;
+
s = src;
while (*s)
{
{
int i;
Value_t j;
- Assoc_t assoc;
- Value_t prec2;
ritem = TMALLOC(Value_t, nitems);
NO_SPACE(ritem);
j = 4;
for (i = 3; i < nrules; ++i)
{
+ Assoc_t assoc;
+ Value_t prec2;
+
#if defined(YYBTYACC)
if (plhs[i]->args > 0)
{
{
int i;
bucket *bp;
- char *tag;
for (i = 2; i < nsyms; ++i)
{
- tag = symbol_type_tag[i];
+ char *tag = symbol_type_tag[i];
+
if (symbol_destructor[i] == NULL)
{
if (tag == NULL)