%code top {
/*
* Parser for C declarations.
- * Copyright © 2011-2012, 2021 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
#include "scan.h"
#include "cdecl.h"
#include "cdecl-internal.h"
+#include "errmsg.h"
#define FAIL(msg) do { \
yyerror(&yylloc, NULL, NULL, msg); \
#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 { \
%code provides {
void cdecl__free(struct cdecl *);
-void cdecl__yyerror(YYLTYPE *, void *, struct cdecl **, const char *);
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;
{
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($$); } <strval>
%type <strval> vla_ident
%type <boolval> varargs
-%type <uintval> declspec_simple typespec_simple qualifier_simple
+%type <spectype> declspec_simple typespec_simple qualifier_simple
%type <declspec> declspec_notype declspec_noid typespec_noid typespec
%type <declspec> qualifier qualifiers
%type <declspec> declspecs declspecs_noid
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,
.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,
}
%%
-void
-yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
-{
- if (strstr(err, "T_LEX_ERROR"))
- return;
- cdecl__set_error(&(const struct cdecl_error){
- .code = CDECL_ENOPARSE,
- .str = 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)];
}