[03b2b2c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup bithenge
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Script parsing.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <ctype.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
[4056ad0] | 40 | #include "expression.h"
|
---|
[03b2b2c] | 41 | #include "os.h"
|
---|
| 42 | #include "script.h"
|
---|
| 43 | #include "transform.h"
|
---|
| 44 | #include "tree.h"
|
---|
| 45 |
|
---|
| 46 | /** Tokens with more characters than this may be read incorrectly. */
|
---|
| 47 | #define MAX_TOKEN_SIZE 256
|
---|
| 48 | #define BUFFER_SIZE 4096
|
---|
| 49 |
|
---|
| 50 | /** Single-character symbols are represented by the character itself. Every
|
---|
| 51 | * other token uses one of these values: */
|
---|
| 52 | typedef enum {
|
---|
| 53 | TOKEN_ERROR = -128,
|
---|
| 54 | TOKEN_EOF,
|
---|
| 55 | TOKEN_IDENTIFIER,
|
---|
[4056ad0] | 56 | TOKEN_INTEGER,
|
---|
[04a7435f] | 57 | TOKEN_LEFT_ARROW,
|
---|
[03b2b2c] | 58 |
|
---|
| 59 | /* Keywords */
|
---|
[04a7435f] | 60 | TOKEN_STRUCT,
|
---|
[03b2b2c] | 61 | TOKEN_TRANSFORM,
|
---|
| 62 | } token_type_t;
|
---|
| 63 |
|
---|
| 64 | /** Singly-linked list of named transforms. */
|
---|
| 65 | typedef struct transform_list {
|
---|
| 66 | char *name;
|
---|
| 67 | bithenge_transform_t *transform;
|
---|
| 68 | struct transform_list *next;
|
---|
| 69 | } transform_list_t;
|
---|
| 70 |
|
---|
| 71 | /** State kept by the parser. */
|
---|
| 72 | typedef struct {
|
---|
| 73 | /** Rather than constantly checking return values, the parser uses this
|
---|
| 74 | * to indicate whether an error has occurred. */
|
---|
| 75 | int error;
|
---|
[32eb01b] | 76 |
|
---|
[03b2b2c] | 77 | /** The list of named transforms. */
|
---|
| 78 | transform_list_t *transform_list;
|
---|
[32eb01b] | 79 |
|
---|
[03b2b2c] | 80 | /** The name of the script file. */
|
---|
| 81 | const char *filename;
|
---|
| 82 | /** The script file being read from. */
|
---|
| 83 | FILE *file;
|
---|
| 84 | /** The buffer that holds script code. There is always a '\0' after the
|
---|
| 85 | * current position to prevent reading too far. */
|
---|
| 86 | char buffer[BUFFER_SIZE];
|
---|
| 87 | /** The start position within the buffer of the next unread token. */
|
---|
| 88 | size_t buffer_pos;
|
---|
| 89 | /** The start position within the buffer of the current token. */
|
---|
| 90 | size_t old_buffer_pos;
|
---|
| 91 | /** The line number of the current token. */
|
---|
| 92 | int lineno;
|
---|
| 93 | /** Added to a buffer position to find the column number. */
|
---|
| 94 | int line_offset;
|
---|
[32eb01b] | 95 |
|
---|
[03b2b2c] | 96 | /** The type of the current token. */
|
---|
| 97 | token_type_t token;
|
---|
| 98 | union {
|
---|
| 99 | /** The value of a TOKEN_IDENTIFIER token. Unless changed to
|
---|
| 100 | * NULL, it will be freed when the next token is read. */
|
---|
| 101 | char *token_string;
|
---|
[4056ad0] | 102 | /** The value of a TOKEN_INTEGER token. */
|
---|
| 103 | bithenge_int_t token_int;
|
---|
[03b2b2c] | 104 | };
|
---|
[32eb01b] | 105 |
|
---|
| 106 | /** The names of the current transform's parameters. */
|
---|
| 107 | char **parameter_names;
|
---|
| 108 | /** The number of parameters. */
|
---|
| 109 | int num_params;
|
---|
[03b2b2c] | 110 | } state_t;
|
---|
| 111 |
|
---|
| 112 | /** Free the previous token's data. This must be called before changing
|
---|
| 113 | * state->token. */
|
---|
| 114 | static void done_with_token(state_t *state)
|
---|
| 115 | {
|
---|
| 116 | if (state->token == TOKEN_IDENTIFIER)
|
---|
| 117 | free(state->token_string);
|
---|
| 118 | state->token = TOKEN_ERROR;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /** Note that an error has occurred. */
|
---|
| 122 | static void error_errno(state_t *state, int error)
|
---|
| 123 | {
|
---|
| 124 | // Don't overwrite a previous error.
|
---|
| 125 | if (state->error == EOK) {
|
---|
| 126 | done_with_token(state);
|
---|
| 127 | state->token = TOKEN_ERROR;
|
---|
| 128 | state->error = error;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Note that a syntax error has occurred and print an error message. */
|
---|
| 133 | static void syntax_error(state_t *state, const char *message)
|
---|
| 134 | {
|
---|
[978ccaf1] | 135 | // Printing multiple errors is confusing.
|
---|
[03b2b2c] | 136 | if (state->error == EOK) {
|
---|
| 137 | size_t start_char = state->old_buffer_pos + state->line_offset;
|
---|
| 138 | size_t end_char = state->buffer_pos + state->line_offset;
|
---|
| 139 | size_t size = end_char - start_char;
|
---|
| 140 | fprintf(stderr, "%s:%d:", state->filename, state->lineno);
|
---|
| 141 | if (size <= 1)
|
---|
| 142 | fprintf(stderr, "%zd: ", start_char);
|
---|
| 143 | else
|
---|
| 144 | fprintf(stderr, "%zd-%zd: ", start_char, end_char - 1);
|
---|
| 145 | fprintf(stderr, "%s: \"%.*s\"\n", message, (int)size,
|
---|
| 146 | state->buffer + state->old_buffer_pos);
|
---|
| 147 | error_errno(state, EINVAL);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /** Ensure the buffer contains enough characters to read a token. */
|
---|
| 152 | static void fill_buffer(state_t *state)
|
---|
| 153 | {
|
---|
| 154 | if (state->buffer_pos + MAX_TOKEN_SIZE < BUFFER_SIZE)
|
---|
| 155 | return;
|
---|
| 156 |
|
---|
| 157 | size_t empty_size = state->buffer_pos;
|
---|
| 158 | size_t used_size = BUFFER_SIZE - 1 - state->buffer_pos;
|
---|
| 159 | memmove(state->buffer, state->buffer + state->buffer_pos, used_size);
|
---|
| 160 | state->line_offset += state->buffer_pos;
|
---|
| 161 | state->buffer_pos = 0;
|
---|
| 162 |
|
---|
| 163 | size_t read_size = fread(state->buffer + used_size, 1, empty_size,
|
---|
| 164 | state->file);
|
---|
| 165 | if (ferror(state->file))
|
---|
| 166 | error_errno(state, EIO);
|
---|
| 167 | state->buffer[used_size + read_size] = '\0';
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /** Read the next token. */
|
---|
| 171 | static void next_token(state_t *state)
|
---|
| 172 | {
|
---|
| 173 | fill_buffer(state);
|
---|
| 174 | done_with_token(state);
|
---|
| 175 | state->old_buffer_pos = state->buffer_pos;
|
---|
| 176 | char ch = state->buffer[state->buffer_pos];
|
---|
| 177 | if (ch == '\0') {
|
---|
| 178 | state->token = TOKEN_EOF;
|
---|
[02dcb20] | 179 | } else if (ch == '#') {
|
---|
| 180 | while (state->buffer[state->buffer_pos] != '\n'
|
---|
| 181 | && state->buffer[state->buffer_pos] != '\0') {
|
---|
| 182 | state->buffer_pos++;
|
---|
| 183 | fill_buffer(state);
|
---|
| 184 | }
|
---|
| 185 | next_token(state);
|
---|
| 186 | return;
|
---|
[03b2b2c] | 187 | } else if (isspace(ch)) {
|
---|
| 188 | // Will eventually reach the '\0' at the end
|
---|
| 189 | while (isspace(state->buffer[state->buffer_pos])) {
|
---|
| 190 | if (state->buffer[state->buffer_pos] == '\n') {
|
---|
| 191 | state->lineno++;
|
---|
| 192 | state->line_offset = -state->buffer_pos;
|
---|
| 193 | }
|
---|
| 194 | state->buffer_pos++;
|
---|
| 195 | }
|
---|
| 196 | next_token(state);
|
---|
| 197 | return;
|
---|
| 198 | } else if (isalpha(ch)) {
|
---|
[600f5d1] | 199 | while (isalnum(state->buffer[state->buffer_pos])
|
---|
| 200 | || state->buffer[state->buffer_pos] == '_')
|
---|
[03b2b2c] | 201 | state->buffer_pos++;
|
---|
| 202 | char *value = str_ndup(state->buffer + state->old_buffer_pos,
|
---|
| 203 | state->buffer_pos - state->old_buffer_pos);
|
---|
| 204 | if (!value) {
|
---|
| 205 | error_errno(state, ENOMEM);
|
---|
[04a7435f] | 206 | } else if (!str_cmp(value, "struct")) {
|
---|
| 207 | state->token = TOKEN_STRUCT;
|
---|
| 208 | free(value);
|
---|
[03b2b2c] | 209 | } else if (!str_cmp(value, "transform")) {
|
---|
| 210 | state->token = TOKEN_TRANSFORM;
|
---|
| 211 | free(value);
|
---|
| 212 | } else {
|
---|
| 213 | state->token = TOKEN_IDENTIFIER;
|
---|
| 214 | state->token_string = value;
|
---|
| 215 | }
|
---|
[4056ad0] | 216 | } else if (isdigit(ch)) {
|
---|
| 217 | while (isdigit(state->buffer[state->buffer_pos]))
|
---|
| 218 | state->buffer_pos++;
|
---|
| 219 | state->token = TOKEN_INTEGER;
|
---|
| 220 | int rc = bithenge_parse_int(state->buffer +
|
---|
| 221 | state->old_buffer_pos, &state->token_int);
|
---|
| 222 | if (rc != EOK)
|
---|
| 223 | error_errno(state, rc);
|
---|
[04a7435f] | 224 | } else if (ch == '<') {
|
---|
| 225 | state->token = ch;
|
---|
| 226 | state->buffer_pos++;
|
---|
| 227 | if (state->buffer[state->buffer_pos] == '-') {
|
---|
| 228 | state->buffer_pos++;
|
---|
| 229 | state->token = TOKEN_LEFT_ARROW;
|
---|
| 230 | }
|
---|
[03b2b2c] | 231 | } else {
|
---|
| 232 | state->token = ch;
|
---|
| 233 | state->buffer_pos++;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Allocate memory and handle failure by setting the error in the state. The
|
---|
| 238 | * caller must check the state for errors before using the return value of this
|
---|
| 239 | * function. */
|
---|
| 240 | static void *state_malloc(state_t *state, size_t size)
|
---|
| 241 | {
|
---|
| 242 | if (state->error != EOK)
|
---|
| 243 | return NULL;
|
---|
| 244 | void *result = malloc(size);
|
---|
| 245 | if (result == NULL)
|
---|
| 246 | error_errno(state, ENOMEM);
|
---|
| 247 | return result;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[04a7435f] | 250 | /** Reallocate memory and handle failure by setting the error in the state. If
|
---|
| 251 | * an error occurs, the existing pointer will be returned. */
|
---|
| 252 | static void *state_realloc(state_t *state, void *ptr, size_t size)
|
---|
| 253 | {
|
---|
| 254 | if (state->error != EOK)
|
---|
| 255 | return ptr;
|
---|
| 256 | void *result = realloc(ptr, size);
|
---|
| 257 | if (result == NULL) {
|
---|
| 258 | error_errno(state, ENOMEM);
|
---|
| 259 | return ptr;
|
---|
| 260 | }
|
---|
| 261 | return result;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[03b2b2c] | 264 | /** Expect and consume a certain token. If the next token is of the wrong type,
|
---|
| 265 | * an error is caused. */
|
---|
| 266 | static void expect(state_t *state, token_type_t type)
|
---|
| 267 | {
|
---|
| 268 | if (state->token != type) {
|
---|
| 269 | syntax_error(state, "unexpected");
|
---|
| 270 | return;
|
---|
| 271 | }
|
---|
| 272 | next_token(state);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Expect and consume an identifier token. If the next token is not an
|
---|
| 276 | * identifier, an error is caused and this function returns null. */
|
---|
| 277 | static char *expect_identifier(state_t *state)
|
---|
| 278 | {
|
---|
| 279 | if (state->token != TOKEN_IDENTIFIER) {
|
---|
| 280 | syntax_error(state, "unexpected (identifier expected)");
|
---|
| 281 | return NULL;
|
---|
| 282 | }
|
---|
| 283 | char *val = state->token_string;
|
---|
| 284 | state->token_string = NULL;
|
---|
| 285 | next_token(state);
|
---|
| 286 | return val;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[0d1a8fd] | 289 | /** Find a transform by name. A reference will be added to the transform.
|
---|
[03b2b2c] | 290 | * @return The found transform, or NULL if none was found. */
|
---|
| 291 | static bithenge_transform_t *get_named_transform(state_t *state,
|
---|
| 292 | const char *name)
|
---|
| 293 | {
|
---|
[0d1a8fd] | 294 | for (transform_list_t *e = state->transform_list; e; e = e->next) {
|
---|
| 295 | if (!str_cmp(e->name, name)) {
|
---|
| 296 | bithenge_transform_inc_ref(e->transform);
|
---|
[03b2b2c] | 297 | return e->transform;
|
---|
[0d1a8fd] | 298 | }
|
---|
| 299 | }
|
---|
| 300 | for (int i = 0; bithenge_primitive_transforms[i].name; i++) {
|
---|
| 301 | if (!str_cmp(bithenge_primitive_transforms[i].name, name)) {
|
---|
| 302 | bithenge_transform_t *xform =
|
---|
| 303 | bithenge_primitive_transforms[i].transform;
|
---|
| 304 | bithenge_transform_inc_ref(xform);
|
---|
| 305 | return xform;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
[03b2b2c] | 308 | return NULL;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | /** Add a named transform. This function takes ownership of the name and a
|
---|
[04a7435f] | 312 | * reference to the transform. If an error has occurred, either may be null. */
|
---|
[03b2b2c] | 313 | static void add_named_transform(state_t *state, bithenge_transform_t *xform, char *name)
|
---|
| 314 | {
|
---|
| 315 | transform_list_t *entry = state_malloc(state, sizeof(*entry));
|
---|
| 316 | if (state->error != EOK) {
|
---|
| 317 | free(name);
|
---|
| 318 | bithenge_transform_dec_ref(xform);
|
---|
| 319 | free(entry);
|
---|
| 320 | return;
|
---|
| 321 | }
|
---|
| 322 | entry->name = name;
|
---|
| 323 | entry->transform = xform;
|
---|
| 324 | entry->next = state->transform_list;
|
---|
| 325 | state->transform_list = entry;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[04a7435f] | 328 | static bithenge_transform_t *parse_transform(state_t *state);
|
---|
| 329 |
|
---|
[4056ad0] | 330 | static bithenge_expression_t *parse_expression(state_t *state)
|
---|
| 331 | {
|
---|
| 332 | if (state->token == TOKEN_INTEGER) {
|
---|
| 333 | bithenge_int_t val = state->token_int;
|
---|
| 334 | next_token(state);
|
---|
| 335 | bithenge_node_t *node;
|
---|
| 336 | int rc = bithenge_new_integer_node(&node, val);
|
---|
| 337 | if (rc != EOK) {
|
---|
| 338 | error_errno(state, rc);
|
---|
| 339 | return NULL;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | bithenge_expression_t *expr;
|
---|
| 343 | rc = bithenge_const_expression(&expr, node);
|
---|
| 344 | if (rc != EOK) {
|
---|
| 345 | error_errno(state, rc);
|
---|
| 346 | return NULL;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[32eb01b] | 349 | return expr;
|
---|
| 350 | } else if (state->token == TOKEN_IDENTIFIER) {
|
---|
| 351 | int i;
|
---|
| 352 | for (i = 0; i < state->num_params; i++)
|
---|
| 353 | if (!str_cmp(state->parameter_names[i],
|
---|
| 354 | state->token_string))
|
---|
| 355 | break;
|
---|
| 356 |
|
---|
| 357 | if (i == state->num_params) {
|
---|
| 358 | syntax_error(state, "unknown identifier");
|
---|
| 359 | return NULL;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | bithenge_expression_t *expr;
|
---|
| 363 | int rc = bithenge_param_expression(&expr, i);
|
---|
| 364 | if (rc != EOK) {
|
---|
| 365 | error_errno(state, rc);
|
---|
| 366 | return NULL;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | next_token(state);
|
---|
| 370 |
|
---|
[4056ad0] | 371 | return expr;
|
---|
| 372 | } else {
|
---|
| 373 | syntax_error(state, "expression expected");
|
---|
| 374 | return NULL;
|
---|
| 375 | }
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | // state->token must be TOKEN_IDENTIFIER when this is called
|
---|
| 379 | static bithenge_transform_t *parse_invocation(state_t *state)
|
---|
| 380 | {
|
---|
| 381 | bithenge_transform_t *result = get_named_transform(state,
|
---|
| 382 | state->token_string);
|
---|
| 383 | if (!result)
|
---|
| 384 | syntax_error(state, "transform not found");
|
---|
| 385 | next_token(state);
|
---|
| 386 |
|
---|
| 387 | bithenge_expression_t **params = NULL;
|
---|
| 388 | int num_params = 0;
|
---|
| 389 | if (state->token == '(') {
|
---|
| 390 | next_token(state);
|
---|
| 391 | while (state->error == EOK && state->token != ')') {
|
---|
| 392 | if (num_params)
|
---|
| 393 | expect(state, ',');
|
---|
| 394 | params = state_realloc(state, params,
|
---|
| 395 | (num_params + 1)*sizeof(*params));
|
---|
| 396 | if (state->error != EOK)
|
---|
| 397 | break;
|
---|
| 398 | params[num_params] = parse_expression(state);
|
---|
| 399 | num_params++;
|
---|
| 400 | }
|
---|
| 401 | expect(state, ')');
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | /* TODO: show correct error position */
|
---|
| 405 | if (state->error == EOK
|
---|
| 406 | && bithenge_transform_num_params(result) != num_params)
|
---|
| 407 | syntax_error(state, "incorrect number of parameters before");
|
---|
| 408 |
|
---|
| 409 | if (state->error != EOK) {
|
---|
| 410 | while (num_params--)
|
---|
| 411 | bithenge_expression_dec_ref(params[num_params]);
|
---|
| 412 | free(params);
|
---|
| 413 | bithenge_transform_dec_ref(result);
|
---|
| 414 | return NULL;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | if (num_params) {
|
---|
| 418 | int rc = bithenge_param_wrapper(&result, result, params);
|
---|
| 419 | if (rc != EOK) {
|
---|
| 420 | error_errno(state, rc);
|
---|
| 421 | result = NULL;
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | return result;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[04a7435f] | 428 | static bithenge_transform_t *parse_struct(state_t *state)
|
---|
| 429 | {
|
---|
| 430 | size_t num = 0;
|
---|
| 431 | bithenge_named_transform_t *subxforms;
|
---|
| 432 | /* We keep an extra space for the {NULL, NULL} terminator. */
|
---|
| 433 | subxforms = state_malloc(state, sizeof(*subxforms));
|
---|
| 434 | expect(state, TOKEN_STRUCT);
|
---|
| 435 | expect(state, '{');
|
---|
| 436 | while (state->error == EOK && state->token != '}') {
|
---|
[600f5d1] | 437 | if (state->token == '.') {
|
---|
| 438 | expect(state, '.');
|
---|
| 439 | subxforms[num].name = expect_identifier(state);
|
---|
| 440 | expect(state, TOKEN_LEFT_ARROW);
|
---|
| 441 | } else {
|
---|
| 442 | subxforms[num].name = NULL;
|
---|
| 443 | expect(state, TOKEN_LEFT_ARROW);
|
---|
| 444 | }
|
---|
[04a7435f] | 445 | subxforms[num].transform = parse_transform(state);
|
---|
| 446 | expect(state, ';');
|
---|
| 447 | num++;
|
---|
| 448 | subxforms = state_realloc(state, subxforms,
|
---|
| 449 | (num + 1)*sizeof(*subxforms));
|
---|
| 450 | }
|
---|
| 451 | expect(state, '}');
|
---|
| 452 |
|
---|
| 453 | if (state->error != EOK) {
|
---|
| 454 | while (num--) {
|
---|
| 455 | free((void *)subxforms[num].name);
|
---|
| 456 | bithenge_transform_dec_ref(subxforms[num].transform);
|
---|
| 457 | }
|
---|
| 458 | free(subxforms);
|
---|
| 459 | return NULL;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | subxforms[num].name = NULL;
|
---|
| 463 | subxforms[num].transform = NULL;
|
---|
| 464 | bithenge_transform_t *result;
|
---|
| 465 | int rc = bithenge_new_struct(&result, subxforms);
|
---|
| 466 | if (rc != EOK) {
|
---|
| 467 | error_errno(state, rc);
|
---|
| 468 | return NULL;
|
---|
| 469 | }
|
---|
| 470 | return result;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[600f5d1] | 473 | /** Parse a transform without composition.
|
---|
[03b2b2c] | 474 | * @return The parsed transform, or NULL if an error occurred. */
|
---|
[600f5d1] | 475 | static bithenge_transform_t *parse_transform_no_compose(state_t *state)
|
---|
[03b2b2c] | 476 | {
|
---|
| 477 | if (state->token == TOKEN_IDENTIFIER) {
|
---|
[4056ad0] | 478 | return parse_invocation(state);
|
---|
[04a7435f] | 479 | } else if (state->token == TOKEN_STRUCT) {
|
---|
| 480 | return parse_struct(state);
|
---|
[03b2b2c] | 481 | } else {
|
---|
| 482 | syntax_error(state, "unexpected (transform expected)");
|
---|
| 483 | return NULL;
|
---|
| 484 | }
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[600f5d1] | 487 | /** Parse a transform.
|
---|
| 488 | * @return The parsed transform, or NULL if an error occurred. */
|
---|
| 489 | static bithenge_transform_t *parse_transform(state_t *state)
|
---|
| 490 | {
|
---|
| 491 | bithenge_transform_t *result = parse_transform_no_compose(state);
|
---|
| 492 | bithenge_transform_t **xforms = NULL;
|
---|
| 493 | size_t num = 1;
|
---|
| 494 | while (state->token == TOKEN_LEFT_ARROW) {
|
---|
| 495 | expect(state, TOKEN_LEFT_ARROW);
|
---|
| 496 | xforms = state_realloc(state, xforms,
|
---|
| 497 | (num + 1) * sizeof(*xforms));
|
---|
| 498 | if (state->error != EOK)
|
---|
| 499 | break;
|
---|
| 500 | xforms[num] = parse_transform_no_compose(state);
|
---|
| 501 | num++;
|
---|
| 502 | }
|
---|
| 503 | if (state->error != EOK) {
|
---|
| 504 | while (xforms && num--)
|
---|
| 505 | bithenge_transform_dec_ref(xforms[num]);
|
---|
| 506 | free(xforms);
|
---|
| 507 | bithenge_transform_dec_ref(result);
|
---|
| 508 | return NULL;
|
---|
| 509 | }
|
---|
| 510 | if (xforms) {
|
---|
| 511 | xforms[0] = result;
|
---|
| 512 | int rc = bithenge_new_composed_transform(&result, xforms, num);
|
---|
| 513 | if (rc != EOK) {
|
---|
| 514 | error_errno(state, rc);
|
---|
| 515 | return NULL;
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
| 518 | return result;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[03b2b2c] | 521 | /** Parse a definition. */
|
---|
| 522 | static void parse_definition(state_t *state)
|
---|
| 523 | {
|
---|
| 524 | expect(state, TOKEN_TRANSFORM);
|
---|
| 525 | char *name = expect_identifier(state);
|
---|
[32eb01b] | 526 |
|
---|
| 527 | if (state->token == '(') {
|
---|
| 528 | next_token(state);
|
---|
| 529 | while (state->error == EOK && state->token != ')') {
|
---|
| 530 | if (state->num_params)
|
---|
| 531 | expect(state, ',');
|
---|
| 532 | state->parameter_names = state_realloc(state,
|
---|
| 533 | state->parameter_names,
|
---|
| 534 | (state->num_params + 1)*sizeof(*state->parameter_names));
|
---|
| 535 | if (state->error != EOK)
|
---|
| 536 | break;
|
---|
| 537 | state->parameter_names[state->num_params++] =
|
---|
| 538 | expect_identifier(state);
|
---|
| 539 | }
|
---|
| 540 | expect(state, ')');
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[03b2b2c] | 543 | expect(state, '=');
|
---|
| 544 | bithenge_transform_t *xform = parse_transform(state);
|
---|
| 545 | expect(state, ';');
|
---|
[32eb01b] | 546 |
|
---|
| 547 | if (state->error == EOK && state->num_params) {
|
---|
| 548 | int rc = bithenge_new_param_transform(&xform, xform,
|
---|
| 549 | state->num_params);
|
---|
| 550 | if (rc != EOK) {
|
---|
| 551 | xform = NULL;
|
---|
| 552 | error_errno(state, rc);
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[03b2b2c] | 556 | add_named_transform(state, xform, name);
|
---|
[32eb01b] | 557 |
|
---|
| 558 | for (int i = 0; i < state->num_params; i++)
|
---|
| 559 | free(state->parameter_names[i]);
|
---|
| 560 | free(state->parameter_names);
|
---|
| 561 | state->parameter_names = NULL;
|
---|
| 562 | state->num_params = 0;
|
---|
[03b2b2c] | 563 | }
|
---|
| 564 |
|
---|
| 565 | /** Initialize the state. */
|
---|
| 566 | static void state_init(state_t *state, const char *filename)
|
---|
| 567 | {
|
---|
| 568 | state->error = EOK;
|
---|
| 569 | state->transform_list = NULL;
|
---|
[32eb01b] | 570 | state->parameter_names = NULL;
|
---|
| 571 | state->num_params = 0;
|
---|
[03b2b2c] | 572 | state->token = TOKEN_ERROR;
|
---|
| 573 | state->old_buffer_pos = state->buffer_pos = BUFFER_SIZE - 1;
|
---|
| 574 | state->lineno = 1;
|
---|
| 575 | state->line_offset = (int)-state->buffer_pos + 1;
|
---|
| 576 | state->filename = filename;
|
---|
| 577 | state->file = fopen(filename, "r");
|
---|
| 578 | if (!state->file) {
|
---|
| 579 | error_errno(state, errno);
|
---|
| 580 | } else {
|
---|
| 581 | next_token(state);
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | /** Destroy the state. */
|
---|
| 586 | static void state_destroy(state_t *state)
|
---|
| 587 | {
|
---|
| 588 | done_with_token(state);
|
---|
| 589 | state->token = TOKEN_ERROR;
|
---|
| 590 | fclose(state->file);
|
---|
| 591 | transform_list_t *entry = state->transform_list;
|
---|
| 592 | while (entry) {
|
---|
| 593 | transform_list_t *next = entry->next;
|
---|
| 594 | free(entry->name);
|
---|
| 595 | bithenge_transform_dec_ref(entry->transform);
|
---|
[f2da0bb] | 596 | free(entry);
|
---|
[03b2b2c] | 597 | entry = next;
|
---|
| 598 | }
|
---|
[32eb01b] | 599 | for (int i = 0; i < state->num_params; i++)
|
---|
| 600 | free(state->parameter_names[i]);
|
---|
| 601 | free(state->parameter_names);
|
---|
[03b2b2c] | 602 | }
|
---|
| 603 |
|
---|
| 604 | /** Parse a script file.
|
---|
| 605 | * @param filename The name of the script file.
|
---|
| 606 | * @param[out] out Stores the "main" transform.
|
---|
| 607 | * @return EOK on success, EINVAL on syntax error, or an error code from
|
---|
| 608 | * errno.h. */
|
---|
| 609 | int bithenge_parse_script(const char *filename, bithenge_transform_t **out)
|
---|
| 610 | {
|
---|
| 611 | state_t state;
|
---|
| 612 | state_init(&state, filename);
|
---|
| 613 | while (state.error == EOK && state.token != TOKEN_EOF)
|
---|
| 614 | parse_definition(&state);
|
---|
| 615 | *out = get_named_transform(&state, "main");
|
---|
| 616 | int rc = state.error;
|
---|
| 617 | state_destroy(&state);
|
---|
| 618 | if (rc == EOK && !*out) {
|
---|
| 619 | fprintf(stderr, "no \"main\" transform\n");
|
---|
| 620 | rc = EINVAL;
|
---|
| 621 | }
|
---|
| 622 | return rc;
|
---|
| 623 | }
|
---|
[978ccaf1] | 624 |
|
---|
| 625 | /** @}
|
---|
| 626 | */
|
---|