diff --git a/external/bsd/byacc/dist/defs.h b/external/bsd/byacc/dist/defs.h index fccf52737488..02fed45cc9c1 100644 --- a/external/bsd/byacc/dist/defs.h +++ b/external/bsd/byacc/dist/defs.h @@ -1,4 +1,4 @@ -/* $NetBSD: defs.h,v 1.16 2019/10/06 23:29:42 christos Exp $ */ +/* $NetBSD: defs.h,v 1.17 2019/10/15 15:58:46 christos Exp $ */ /* Id: defs.h,v 1.61 2019/06/16 15:07:51 tom Exp */ @@ -126,11 +126,12 @@ #define TOKEN_TABLE 16 #define ERROR_VERBOSE 17 #define XXXDEBUG 18 +#define XCODE 19 #if defined(YYBTYACC) -#define LOCATIONS 19 -#define DESTRUCTOR 20 -#define INITIAL_ACTION 21 +#define LOCATIONS 20 +#define DESTRUCTOR 21 +#define INITIAL_ACTION 22 #endif /* symbol classes */ @@ -580,6 +581,17 @@ extern void output(void); /* reader.c */ extern void reader(void); +#define CODE_HEADER 0 +#define CODE_REQUIRES 1 +#define CODE_PROVIDES 2 +#define CODE_TOP 3 +#define CODE_IMPORTS 4 +#define CODE_MAX 5 +struct code_lines { + char *lines; + size_t num; +} code_lines[CODE_MAX]; + /* skeleton.c (generated by skel2c) */ extern void write_section(FILE * fp, const char *const section[]); diff --git a/external/bsd/byacc/dist/output.c b/external/bsd/byacc/dist/output.c index f4f06e40d1ba..4de1f7eb721d 100644 --- a/external/bsd/byacc/dist/output.c +++ b/external/bsd/byacc/dist/output.c @@ -1,11 +1,11 @@ -/* $NetBSD: output.c,v 1.20 2018/12/23 20:27:23 jakllsch Exp $ */ +/* $NetBSD: output.c,v 1.21 2019/10/15 15:58:46 christos Exp $ */ /* Id: output.c,v 1.87 2018/05/10 09:08:46 tom Exp */ #include "defs.h" #include -__RCSID("$NetBSD: output.c,v 1.20 2018/12/23 20:27:23 jakllsch Exp $"); +__RCSID("$NetBSD: output.c,v 1.21 2019/10/15 15:58:46 christos Exp $"); #define StaticOrR (rflag ? "" : "static ") #define CountLine(fp) (!rflag || ((fp) == code_file)) @@ -36,6 +36,16 @@ static Value_t *check; static int lowzero; static long high; +static void +output_code_lines(FILE *fp, int cl) +{ + if (code_lines[cl].lines == NULL) + return; + if (fp == code_file) + outline += code_lines[cl].num; + fputs(code_lines[cl].lines, fp); +} + static void putc_code(FILE * fp, int c) { @@ -1244,6 +1254,12 @@ output_defines(FILE * fp) if (fp != defines_file || iflag) fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]); + if (fp == defines_file) + { + output_code_lines(fp, CODE_REQUIRES); + output_code_lines(fp, CODE_PROVIDES); + } + if (token_table && rflag && fp != externs_file) { if (fp == code_file) @@ -1269,7 +1285,10 @@ output_defines(FILE * fp) } #if defined(YYBTYACC) if (locations) + { output_ltype(fp); + fprintf(fp, "extern YYLTYPE %slloc;\n", symbol_prefix); + } #endif } } @@ -2007,6 +2026,7 @@ output(void) free_shifts(); free_reductions(); + output_code_lines(code_file, CODE_TOP); #if defined(YYBTYACC) output_backtracking_parser(output_file); if (rflag) @@ -2023,6 +2043,9 @@ output(void) else fp = code_file; + output_code_lines(code_file, CODE_REQUIRES); + output_code_lines(code_file, CODE_PROVIDES); + output_prefix(fp); output_pure_parser(fp); #if defined(YY_NO_LEAKS) diff --git a/external/bsd/byacc/dist/reader.c b/external/bsd/byacc/dist/reader.c index bcc221eac5a2..a1f198140543 100644 --- a/external/bsd/byacc/dist/reader.c +++ b/external/bsd/byacc/dist/reader.c @@ -1,11 +1,11 @@ -/* $NetBSD: reader.c,v 1.16 2018/12/23 20:27:23 jakllsch Exp $ */ +/* $NetBSD: reader.c,v 1.17 2019/10/15 15:58:46 christos Exp $ */ /* Id: reader.c,v 1.74 2017/12/04 17:50:02 tom Exp */ #include "defs.h" #include -__RCSID("$NetBSD: reader.c,v 1.16 2018/12/23 20:27:23 jakllsch Exp $"); +__RCSID("$NetBSD: reader.c,v 1.17 2019/10/15 15:58:46 christos Exp $"); /* The line size must be a positive integer. One hundred was chosen */ /* because few lines in Yacc input grammars exceed 100 characters. */ @@ -67,6 +67,12 @@ char line_format[] = "#line %d \"%s\"\n"; 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 */ @@ -447,6 +453,7 @@ static struct keyword } keywords[] = { { "binary", NONASSOC }, + { "code", XCODE }, { "debug", XXXDEBUG }, #if defined(YYBTYACC) { "destructor", DESTRUCTOR }, @@ -666,6 +673,115 @@ copy_comment(void) 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 = 0; + int pos = CODE_HEADER; + struct mstring *code_mstr; + + /* read %code { */ + 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); + return; + } + } + } + + cptr++; /* skip initial curl */ + while (*cptr && isspace(UCH(*cptr))) /* skip space */ + cptr++; + curl = 1; /* nesting count */ + + /* gather text */ + code_mstr = msnew(); + cline++; + msprintf(code_mstr, "/* %%code %s block start */\n", code_keys[pos]); + for (;;) + { + c = *cptr++; + switch (c) + { + case '\0': + get_line(); + if (line == NULL) + { + unexpected_EOF(); + return; + } + continue; + case '\n': + cline++; + break; + case L_CURL: + curl++; + break; + case R_CURL: + if (--curl == 0) + { + cline++; + msprintf(code_mstr, "/* %%code %s block end */\n", + code_keys[pos]); + code_lines[pos].lines = msdone(code_mstr); + code_lines[pos].num = cline; + return; + } + break; + default: + break; + } + mputc(code_mstr, c); + } +} + static void copy_text(void) { @@ -1691,6 +1807,10 @@ read_declarations(void) copy_ident(); break; + case XCODE: + copy_code(); + break; + case TEXT: copy_text(); break; @@ -2255,6 +2375,10 @@ advance_to_start(void) s_cptr = cptr; switch (keyword()) { + case XCODE: + copy_code(); + break; + case MARK: no_grammar();