2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Common code for the lexers.
45 void bc_lex_invalidChar(BcLex *l, char c) {
46 l->t = BC_LEX_INVALID;
47 bc_lex_verr(l, BC_ERR_PARSE_CHAR, c);
50 void bc_lex_lineComment(BcLex *l) {
51 l->t = BC_LEX_WHITESPACE;
52 while (l->i < l->len && l->buf[l->i] != '\n') l->i += 1;
55 void bc_lex_comment(BcLex *l) {
58 const char *buf = l->buf;
63 l->t = BC_LEX_WHITESPACE;
65 for (i = l->i; !end; i += !end) {
67 for (; (c = buf[i]) && c != '*'; ++i) nlines += (c == '\n');
69 if (BC_ERR(!c || buf[i + 1] == '\0')) {
71 bc_lex_err(l, BC_ERR_PARSE_COMMENT);
74 end = buf[i + 1] == '/';
81 void bc_lex_whitespace(BcLex *l) {
83 l->t = BC_LEX_WHITESPACE;
84 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
87 void bc_lex_commonTokens(BcLex *l, char c) {
88 if (!c) l->t = BC_LEX_EOF;
89 else if (c == '\n') l->t = BC_LEX_NLINE;
90 else bc_lex_whitespace(l);
93 static size_t bc_lex_num(BcLex *l, char start, bool int_only) {
95 const char *buf = l->buf + l->i;
98 bool last_pt, pt = (start == '.');
100 for (i = 0; (c = buf[i]) && (BC_LEX_NUM_CHAR(c, pt, int_only) ||
101 (c == '\\' && buf[i + 1] == '\n')); ++i)
105 if (buf[i + 1] == '\n') {
109 // Make sure to eat whitespace at the beginning of the line.
110 while(isspace(buf[i]) && buf[i] != '\n') i += 1;
114 if (!BC_LEX_NUM_CHAR(c, pt, int_only)) break;
119 last_pt = (c == '.');
120 if (pt && last_pt) break;
123 bc_vec_push(&l->str, &c);
129 void bc_lex_number(BcLex *l, char start) {
131 l->t = BC_LEX_NUMBER;
133 bc_vec_npop(&l->str, l->str.len);
134 bc_vec_push(&l->str, &start);
136 l->i += bc_lex_num(l, start, false);
138 #if BC_ENABLE_EXTRA_MATH
140 char c = l->buf[l->i];
145 if (BC_IS_POSIX) bc_lex_err(l, BC_ERR_POSIX_EXP_NUM);
148 bc_vec_push(&l->str, &c);
152 if (c == BC_LEX_NEG_CHAR) {
153 bc_vec_push(&l->str, &c);
158 if (BC_ERR(!BC_LEX_NUM_CHAR(c, false, true)))
159 bc_lex_verr(l, BC_ERR_PARSE_CHAR, c);
161 l->i += bc_lex_num(l, 0, true);
164 #endif // BC_ENABLE_EXTRA_MATH
166 bc_vec_pushByte(&l->str, '\0');
169 void bc_lex_name(BcLex *l) {
172 const char *buf = l->buf + l->i - 1;
177 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
179 bc_vec_string(&l->str, i, buf);
181 // Increment the index. We minus 1 because it has already been incremented.
185 void bc_lex_init(BcLex *l) {
186 BC_SIG_ASSERT_LOCKED;
188 bc_vec_init(&l->str, sizeof(char), NULL);
191 void bc_lex_free(BcLex *l) {
192 BC_SIG_ASSERT_LOCKED;
194 bc_vec_free(&l->str);
197 void bc_lex_file(BcLex *l, const char *file) {
198 assert(l != NULL && file != NULL);
203 void bc_lex_next(BcLex *l) {
208 l->line += (l->i != 0 && l->buf[l->i - 1] == '\n');
210 if (BC_ERR(l->last == BC_LEX_EOF)) bc_lex_err(l, BC_ERR_PARSE_EOF);
214 if (l->i == l->len) return;
216 // Loop until failure or we don't have whitespace. This
217 // is so the parser doesn't get inundated with whitespace.
220 } while (l->t == BC_LEX_WHITESPACE);
223 void bc_lex_text(BcLex *l, const char *text) {
224 assert(l != NULL && text != NULL);
227 l->len = strlen(text);
228 l->t = l->last = BC_LEX_INVALID;