blob: 339d0b293c78bcbb626e6783143ed80a1eb8e7db [file] [log] [blame]
Gavin Howard5715b042018-02-12 16:11:42 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard5715b042018-02-12 16:11:42 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -070018 *
Gavin Howardd2a05252018-09-27 14:00:40 -060019 * Common code for the lexers.
Gavin Howard5715b042018-02-12 16:11:42 -070020 *
21 */
22
Gavin Howard27fdfb92018-03-21 07:56:59 -060023#include <assert.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070024#include <ctype.h>
25#include <stdbool.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070026#include <string.h>
27
Gavin Howard29493062018-03-20 19:57:37 -060028#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070029#include <lex.h>
Gavin Howardd5551672018-09-22 19:52:42 -060030#include <vm.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070031
Gavin Howard50c8c2d2018-12-27 11:58:34 -070032BcStatus bc_lex_invalidChar(BcLex *l, char c) {
33 l->t = BC_LEX_INVALID;
Gavin Howard03cd1122018-12-31 14:08:15 -070034 return bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c);
Gavin Howard50c8c2d2018-12-27 11:58:34 -070035}
36
Gavin Howarded5c8312018-09-27 12:04:08 -060037void bc_lex_lineComment(BcLex *l) {
Gavin Howardad477312018-12-24 15:51:35 -070038 l->t = BC_LEX_WHITESPACE;
Gavin Howard9a0d2e52018-12-28 13:43:01 -070039 while (l->i < l->len && l->buf[l->i] != '\n') ++l->i;
Gavin Howarded5c8312018-09-27 12:04:08 -060040}
41
Gavin Howard52446f22018-12-13 11:39:22 -070042BcStatus bc_lex_comment(BcLex *l) {
43
44 size_t i, nlines = 0;
45 const char *buf = l->buf;
46 bool end = false;
47 char c;
48
Gavin Howardad477312018-12-24 15:51:35 -070049 l->t = BC_LEX_WHITESPACE;
Gavin Howard52446f22018-12-13 11:39:22 -070050
51 for (i = ++l->i; !end; i += !end) {
52
Gavin Howard1ab22d22019-01-03 13:32:17 -070053 for (; (c = buf[i]) && c != '*'; ++i) nlines += (c == '\n');
Gavin Howard52446f22018-12-13 11:39:22 -070054
Gavin Howard1ab22d22019-01-03 13:32:17 -070055 if (!c || buf[i + 1] == '\0') {
Gavin Howard52446f22018-12-13 11:39:22 -070056 l->i = i;
Gavin Howard03cd1122018-12-31 14:08:15 -070057 return bc_lex_err(l, BC_ERROR_PARSE_COMMENT);
Gavin Howard52446f22018-12-13 11:39:22 -070058 }
59
60 end = buf[i + 1] == '/';
61 }
62
63 l->i = i + 2;
64 l->line += nlines;
65
66 return BC_STATUS_SUCCESS;
67}
68
Gavin Howard364df3b2018-09-28 09:48:19 -060069void bc_lex_whitespace(BcLex *l) {
70 char c;
Gavin Howardad477312018-12-24 15:51:35 -070071 l->t = BC_LEX_WHITESPACE;
Gavin Howard53eba8b2018-10-31 15:14:37 -060072 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
Gavin Howard364df3b2018-09-28 09:48:19 -060073}
74
Gavin Howard3ae1f8e2019-02-15 11:56:25 -070075void bc_lex_commonTokens(BcLex *l, char c) {
76 if (!c) l->t = BC_LEX_EOF;
77 else if (c == '\n') l->t = BC_LEX_NLINE;
78 else bc_lex_whitespace(l);
79}
80
Gavin Howard63738202018-09-26 15:34:20 -060081BcStatus bc_lex_number(BcLex *l, char start) {
Gavin Howard8a596d42018-01-15 15:46:01 -070082
Gavin Howard53eba8b2018-10-31 15:14:37 -060083 const char *buf = l->buf + l->i;
Gavin Howard8dd307e2019-01-08 23:05:19 -070084 size_t i;
Gavin Howard1ab22d22019-01-03 13:32:17 -070085 char last_valid, c;
Gavin Howard94f14102019-01-11 09:39:57 -070086 bool last_pt, pt = (start == '.');
Gavin Howardf2a40492018-03-05 11:27:29 -070087
Gavin Howardad477312018-12-24 15:51:35 -070088 l->t = BC_LEX_NUMBER;
Gavin Howard196f7db2018-12-29 21:41:54 -070089 last_valid = BC_IS_BC ? 'Z' : 'F';
Gavin Howard07732ec2018-02-27 15:40:02 -070090
Gavin Howardad477312018-12-24 15:51:35 -070091 bc_vec_npop(&l->str, l->str.len);
Gavin Howardad477312018-12-24 15:51:35 -070092 bc_vec_push(&l->str, &start);
Gavin Howarda628aa22018-09-12 13:52:45 -060093
Gavin Howard94f14102019-01-11 09:39:57 -070094 for (i = 0; (c = buf[i]) && (BC_LEX_NUM_CHAR(c, last_valid, pt) ||
Gavin Howard8dd307e2019-01-08 23:05:19 -070095 (c == '\\' && buf[i + 1] == '\n')); ++i)
96 {
Gavin Howard25f60882019-01-11 09:20:51 -070097 if (c == '\\') {
98
99 if (buf[i + 1] == '\n') {
100
101 i += 2;
102
103 // Make sure to eat whitespace at the beginning of the line.
104 while(isspace(buf[i]) && buf[i] != '\n') ++i;
105
106 c = buf[i];
107
Gavin Howard94f14102019-01-11 09:39:57 -0700108 if (!BC_LEX_NUM_CHAR(c, last_valid, pt)) break;
Gavin Howard25f60882019-01-11 09:20:51 -0700109 }
110 else break;
Gavin Howard63738202018-09-26 15:34:20 -0600111 }
Gavin Howard8dd307e2019-01-08 23:05:19 -0700112
Gavin Howard94f14102019-01-11 09:39:57 -0700113 last_pt = (c == '.');
Gavin Howard25f60882019-01-11 09:20:51 -0700114 if (pt && last_pt) break;
Gavin Howard94f14102019-01-11 09:39:57 -0700115 pt = pt || last_pt;
Gavin Howard07732ec2018-02-27 15:40:02 -0700116
Gavin Howardad477312018-12-24 15:51:35 -0700117 bc_vec_push(&l->str, &c);
Gavin Howard63738202018-09-26 15:34:20 -0600118 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700119
Gavin Howardad477312018-12-24 15:51:35 -0700120 bc_vec_pushByte(&l->str, '\0');
Gavin Howard53eba8b2018-10-31 15:14:37 -0600121 l->i += i;
Gavin Howard8a596d42018-01-15 15:46:01 -0700122
Gavin Howard63738202018-09-26 15:34:20 -0600123 return BC_STATUS_SUCCESS;
Gavin Howard8a596d42018-01-15 15:46:01 -0700124}
125
Gavin Howard8412ba82018-10-04 13:03:24 -0600126BcStatus bc_lex_name(BcLex *l) {
127
Gavin Howard88c25302018-10-17 13:32:23 -0600128 size_t i = 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600129 const char *buf = l->buf + l->i - 1;
Gavin Howard88c25302018-10-17 13:32:23 -0600130 char c = buf[i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600131
Gavin Howardad477312018-12-24 15:51:35 -0700132 l->t = BC_LEX_NAME;
Gavin Howard8412ba82018-10-04 13:03:24 -0600133
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600134 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600135
Gavin Howardad477312018-12-24 15:51:35 -0700136 bc_vec_string(&l->str, i, buf);
Gavin Howard8412ba82018-10-04 13:03:24 -0600137
138 // Increment the index. We minus 1 because it has already been incremented.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600139 l->i += i - 1;
Gavin Howard8412ba82018-10-04 13:03:24 -0600140
141 return BC_STATUS_SUCCESS;
142}
143
Gavin Howard48354e82019-01-02 18:15:56 -0700144void bc_lex_init(BcLex *l) {
Gavin Howard63738202018-09-26 15:34:20 -0600145 assert(l);
Gavin Howardad477312018-12-24 15:51:35 -0700146 bc_vec_init(&l->str, sizeof(char), NULL);
Gavin Howard69185042018-09-10 15:46:20 -0600147}
148
Gavin Howard63738202018-09-26 15:34:20 -0600149void bc_lex_free(BcLex *l) {
150 assert(l);
Gavin Howardad477312018-12-24 15:51:35 -0700151 bc_vec_free(&l->str);
Gavin Howard69185042018-09-10 15:46:20 -0600152}
153
Gavin Howard63738202018-09-26 15:34:20 -0600154void bc_lex_file(BcLex *l, const char *file) {
155 assert(l && file);
156 l->line = 1;
Gavin Howard7536dcf2018-12-15 19:27:09 -0700157 vm->file = file;
Gavin Howard8a596d42018-01-15 15:46:01 -0700158}
159
Gavin Howard364df3b2018-09-28 09:48:19 -0600160BcStatus bc_lex_next(BcLex *l) {
161
162 BcStatus s;
163
164 assert(l);
165
Gavin Howardad477312018-12-24 15:51:35 -0700166 l->last = l->t;
Gavin Howard56158792019-01-14 12:03:14 -0700167 l->line += (l->i != 0 && l->buf[l->i - 1] == '\n');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700168
Gavin Howard03cd1122018-12-31 14:08:15 -0700169 if (l->last == BC_LEX_EOF) return bc_lex_err(l, BC_ERROR_PARSE_EOF);
Gavin Howard364df3b2018-09-28 09:48:19 -0600170
Gavin Howardad477312018-12-24 15:51:35 -0700171 l->t = BC_LEX_EOF;
Gavin Howardc39fd492018-10-04 10:07:03 -0600172
Gavin Howardf9b86ee2018-12-28 13:18:27 -0700173 if (l->i == l->len) return BC_STATUS_SUCCESS;
Gavin Howard364df3b2018-09-28 09:48:19 -0600174
Gavin Howard364df3b2018-09-28 09:48:19 -0600175 // Loop until failure or we don't have whitespace. This
176 // is so the parser doesn't get inundated with whitespace.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600177 do {
Gavin Howard48354e82019-01-02 18:15:56 -0700178 s = vm->next(l);
Gavin Howardad477312018-12-24 15:51:35 -0700179 } while (!s && l->t == BC_LEX_WHITESPACE);
Gavin Howard364df3b2018-09-28 09:48:19 -0600180
181 return s;
Gavin Howard35753922018-03-21 19:22:08 -0600182}
Gavin Howardc9a9c472018-10-02 17:23:01 -0600183
184BcStatus bc_lex_text(BcLex *l, const char *text) {
185 assert(l && text);
Gavin Howard890d0c02018-10-30 16:34:50 -0600186 l->buf = text;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600187 l->i = 0;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600188 l->len = strlen(text);
Gavin Howardad477312018-12-24 15:51:35 -0700189 l->t = l->last = BC_LEX_INVALID;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600190 return bc_lex_next(l);
191}