diff options
Diffstat (limited to 'src/as_tokenizer.c')
| -rw-r--r-- | src/as_tokenizer.c | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c index f2c3ccb..7a71707 100644 --- a/src/as_tokenizer.c +++ b/src/as_tokenizer.c @@ -83,12 +83,12 @@ struct result next_token_impl(struct allocator * alct, struct input_stream * s) if (c == '\n') { input_stream_next_char(s); *t = (struct token){.type = TK_NEWLINE, .line = s->line, .col = s->col}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } if (c == ':') { input_stream_next_char(s); *t = (struct token){.type = TK_COLON, .line = s->line, .col = s->col}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } if (c == ' ' || c == '\t') { input_stream_next_char(s); @@ -105,7 +105,7 @@ struct result next_token_impl(struct allocator * alct, struct input_stream * s) } } *t = (struct token){.type = TK_ARG, .ival = ival, .line = s->line, .col = s->col}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } if (is_start_of_identifier(c)) { size_t line = s->line; @@ -114,8 +114,7 @@ struct result next_token_impl(struct allocator * alct, struct input_stream * s) size_t i = 0; while (1) { if (i >= 255) { - errmsg = safe_sprintf(alct, "error: identifier too long\n"); - return (struct result){.value = NULL, .errmsg = errmsg}; + return err(safe_sprintf(alct, "error: identifier too long\n")); } input_stream_next_char(s); sval[i++] = c; @@ -127,36 +126,33 @@ struct result next_token_impl(struct allocator * alct, struct input_stream * s) sval[i] = '\0'; if (isOp(sval)) { *t = (struct token){.type = TK_OP, .sval = sval, .line = line, .col = col}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } *t = (struct token){.type = TK_TAG, .sval = sval, .line = line, .col = col}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } - errmsg = safe_sprintf(alct, "error: invalid character %c at line %d, col %d\n", c, s->line, s->col); - return (struct result){.value = NULL, .errmsg = errmsg}; + return err(safe_sprintf(alct, "error: invalid character %c at line %d, col %d\n", c, s->line, s->col)); } // end of file *t = (struct token){.type = TK_ENDOFFILE}; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } struct result next_token(struct allocator * alct, struct token_stream * ts) { if (ts->buf != NULL) { struct token * t = ts->buf; ts->buf = NULL; - return (struct result){.value = t, .errmsg = NULL}; + return ok(t); } return next_token_impl(alct, ts->s); } struct result peek_token(struct allocator * alct, struct token_stream * ts) { if (ts->buf != NULL) { - return (struct result){.value = ts->buf, .errmsg = NULL}; + return ok(ts->buf); } - struct result result = next_token_impl(alct, ts->s); - if (result.errmsg != NULL) return result; - ts->buf = result.value; - return result; + ts->buf = unwrap(next_token_impl(alct, ts->s)); + return ok(ts->buf); } void print_token(struct token * t) { |
