X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/0876ef2649dfc1d1c1fe86a0d56e9506806b37b9..3a3760ad26782403f6686183be3faa8b22cf849b:/src/parse.y diff --git a/src/parse.y b/src/parse.y index f90013a..263129c 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1,7 +1,7 @@ %code top { /* * Parser for C declarations. - * Copyright © 2011 Nick Bowler + * Copyright © 2011-2012, 2021, 2023 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ } %name-prefix "cdecl__yy" -%parse-param {yyscan_t scanner} +%parse-param {void *scanner} %parse-param {struct cdecl **out} %lex-param {yyscan_t scanner} %define api.pure @@ -33,6 +33,8 @@ #include "scan.h" #include "cdecl.h" +#include "cdecl-internal.h" +#include "errmsg.h" #define FAIL(msg) do { \ yyerror(&yylloc, NULL, NULL, msg); \ @@ -41,8 +43,10 @@ #define ALLOC(ptr, size) do { \ (ptr) = malloc(size); \ - if (!(ptr)) \ - FAIL("failed to allocate memory"); \ + if (!(ptr)) { \ + cdecl__errmsg(CDECL__ENOMEM); \ + YYERROR; \ + } \ } while (0) #define ALLOC_STRUCT(ptr, type, ...) do { \ @@ -56,12 +60,14 @@ } %code provides { -void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *); +void cdecl__free(struct cdecl *); int cdecl__yyparse(void *scanner, struct cdecl **out); +const char *cdecl__token_name(unsigned token); } %union { uintmax_t uintval; + unsigned spectype; _Bool boolval; char *strval; struct cdecl_declspec *declspec; @@ -131,10 +137,19 @@ static void free_decl(struct cdecl *x) } } -void cdecl_free(struct cdecl *decl) +void cdecl__free(struct cdecl *decl) { free_decl(decl); } + +static void +yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err) +{ + if (strstr(err, "T_LEX_ERROR")) + return; + + cdecl__err(CDECL_ENOPARSE, "%s", err); +} %} %destructor { free($$); } @@ -203,7 +218,7 @@ void cdecl_free(struct cdecl *decl) %type vla_ident %type varargs -%type declspec_simple typespec_simple qualifier_simple +%type declspec_simple typespec_simple qualifier_simple %type declspec_notype declspec_noid typespec_noid typespec %type qualifier qualifiers %type declspecs declspecs_noid @@ -333,7 +348,7 @@ vla_ident: T_IDENT | T_ASTERISK { array: T_LBRACKET T_UINT T_RBRACKET { if ($2 == 0) - FAIL("array length must be positive"); + FAIL(_("array length must be positive")); ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, @@ -560,7 +575,7 @@ english_array: T_VLA T_ARRAY english_vla T_OF { .u.array.vla = $3); } | T_ARRAY T_UINT T_OF { if ($2 == 0) - FAIL("array length must be positive"); + FAIL(_("array length must be positive")); ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_ARRAY, @@ -577,12 +592,16 @@ english_vla: T_IDENT | { } %% -void -yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, - const char *err) -{ - if (strstr(err, "T_LEX_ERROR")) - return; - fprintf(stderr, "%s\n", err); +/* + * Expose the token string table to the rest of the library, in order to + * produce strings that match parser keywords. + * + * In order for this to work properly, the Bison output must be postprocessed + * by fix-yytname.awk to remove pointless quotation marks from the keyword + * strings. + */ +const char *cdecl__token_name(unsigned token) +{ + return yytname[YYTRANSLATE(token)]; }