1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "../util/cf-parser.h"
#include "decl.h"
static inline void err_specifier_exists(struct cf_parser *cfp,
const char *storage)
{
cf_adderror(cfp, "'$1' specifier already exists", LEX_ERROR, storage,
NULL, NULL);
}
static inline void err_reserved_name(struct cf_parser *cfp, const char *name)
{
cf_adderror(cfp, "'$1' is a reserved name", LEX_ERROR, name, NULL,
NULL);
}
static inline void err_existing_name(struct cf_parser *cfp, const char *name)
{
cf_adderror(cfp, "'$1' already exists", LEX_ERROR, name, NULL, NULL);
}
static bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name,
uint32_t *type)
{
if (strref_cmp(name, "in") == 0) {
if (*type & CALL_PARAM_IN)
err_specifier_exists(cfp, "in");
*type |= CALL_PARAM_IN;
} else if (strref_cmp(name, "out") == 0) {
if (*type & CALL_PARAM_OUT)
err_specifier_exists(cfp, "out");
*type |= CALL_PARAM_OUT;
} else {
return false;
}
return true;
}
#define TYPE_OR_STORAGE "type or storage specifier"
static bool get_type(struct strref *ref, enum call_param_type *type,
bool is_return)
{
if (strref_cmp(ref, "int") == 0)
*type = CALL_PARAM_TYPE_INT;
else if (strref_cmp(ref, "float") == 0)
*type = CALL_PARAM_TYPE_FLOAT;
else if (strref_cmp(ref, "bool") == 0)
*type = CALL_PARAM_TYPE_BOOL;
else if (strref_cmp(ref, "ptr") == 0)
*type = CALL_PARAM_TYPE_PTR;
else if (strref_cmp(ref, "string") == 0)
*type = CALL_PARAM_TYPE_STRING;
else if (is_return && strref_cmp(ref, "void") == 0)
*type = CALL_PARAM_TYPE_VOID;
else
return false;
return true;
}
static bool is_reserved_name(const char *str)
{
return (strcmp(str, "int") == 0) || (strcmp(str, "float") == 0) ||
(strcmp(str, "bool") == 0) || (strcmp(str, "ptr") == 0) ||
(strcmp(str, "string") == 0) || (strcmp(str, "void") == 0) ||
(strcmp(str, "return") == 0);
}
static bool name_exists(struct decl_info *decl, const char *name)
{
for (size_t i = 0; i < decl->params.num; i++) {
const char *param_name = decl->params.array[i].name;
if (strcmp(name, param_name) == 0)
return true;
}
return false;
}
static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
{
struct strref ref;
int code;
struct decl_param param = {0};
/* get storage specifiers */
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
while (is_in_out_specifier(cfp, &ref, ¶m.flags)) {
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
}
/* parameters not marked with specifiers are input parameters */
if (param.flags == 0)
param.flags = CALL_PARAM_IN;
if (!get_type(&ref, ¶m.type, false)) {
cf_adderror_expecting(cfp, "type");
cf_go_to_token(cfp, ",", ")");
return PARSE_CONTINUE;
}
/* name */
code = cf_next_name(cfp, ¶m.name, "parameter name", ",");
if (code != PARSE_SUCCESS)
return code;
if (name_exists(decl, param.name))
err_existing_name(cfp, param.name);
if (is_reserved_name(param.name))
err_reserved_name(cfp, param.name);
da_push_back(decl->params, ¶m);
return PARSE_SUCCESS;
}
static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
{
struct cf_token peek;
int code;
if (!cf_peek_valid_token(cfp, &peek))
return;
while (peek.type == CFTOKEN_NAME) {
code = parse_param(cfp, decl);
if (code == PARSE_EOF)
return;
if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
return;
if (cf_token_is(cfp, ")"))
break;
else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
return;
if (!cf_peek_valid_token(cfp, &peek))
return;
}
if (!cf_token_is(cfp, ")"))
cf_next_token_should_be(cfp, ")", NULL, NULL);
}
static void print_errors(struct cf_parser *cfp, const char *decl_string)
{
char *errors = error_data_buildstring(&cfp->error_list);
if (errors) {
blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
decl_string, errors);
bfree(errors);
}
}
bool parse_decl_string(struct decl_info *decl, const char *decl_string)
{
struct cf_parser cfp;
struct strref ret_type;
struct decl_param ret_param = {0};
int code;
bool success;
decl->decl_string = decl_string;
ret_param.flags = CALL_PARAM_OUT;
cf_parser_init(&cfp);
if (!cf_parser_parse(&cfp, decl_string, "declaration"))
goto fail;
code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
if (code == PARSE_EOF)
goto fail;
if (!get_type(&ret_type, &ret_param.type, true))
cf_adderror_expecting(&cfp, "return type");
code = cf_next_name(&cfp, &decl->name, "function name", "(");
if (code == PARSE_EOF)
goto fail;
if (is_reserved_name(decl->name))
err_reserved_name(&cfp, decl->name);
code = cf_next_token_should_be(&cfp, "(", "(", NULL);
if (code == PARSE_EOF)
goto fail;
parse_params(&cfp, decl);
fail:
success = !error_data_has_errors(&cfp.error_list);
if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
ret_param.name = bstrdup("return");
da_push_back(decl->params, &ret_param);
}
if (!success)
decl_info_free(decl);
print_errors(&cfp, decl_string);
cf_parser_free(&cfp);
return success;
}
|