2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 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) {
59 bool end = false, got_more;
63 l->t = BC_LEX_WHITESPACE;
65 // This loop is complex because it might need to request more data from
66 // stdin if the comment is not ended. This loop is taken until the comment
67 // is finished or we have EOF.
73 // If we are in stdin mode, the buffer must be the one used for stdin.
74 assert(!vm.is_stdin || buf == vm.buffer.v);
76 // Find the end of the comment.
77 for (i = l->i; !end; i += !end) {
79 // While we don't have an asterisk, eat, but increment nlines.
80 for (; (c = buf[i]) && c != '*'; ++i) nlines += (c == '\n');
82 // If this is true, we need to request more data.
83 if (BC_ERR(!c || buf[i + 1] == '\0')) {
86 if (!vm.eof && l->is_stdin) got_more = bc_lex_readLine(l);
91 // If this turns true, we found the end. Yay!
92 end = (buf[i + 1] == '/');
95 } while (got_more && !end);
97 // If we didn't find the end, barf.
100 bc_lex_err(l, BC_ERR_PARSE_COMMENT);
107 void bc_lex_whitespace(BcLex *l) {
111 l->t = BC_LEX_WHITESPACE;
113 // Eat. We don't eat newlines because they can be special.
114 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
117 void bc_lex_commonTokens(BcLex *l, char c) {
118 if (!c) l->t = BC_LEX_EOF;
119 else if (c == '\n') l->t = BC_LEX_NLINE;
120 else bc_lex_whitespace(l);
125 * @param l The lexer.
126 * @param start The start character.
127 * @param int_only Whether this function should only look for an integer. This
128 * is used to implement the exponent of scientific notation.
130 static size_t bc_lex_num(BcLex *l, char start, bool int_only) {
132 const char *buf = l->buf + l->i;
135 bool last_pt, pt = (start == '.');
137 // This loop looks complex. It is not. It is asking if the character is not
138 // a nul byte and it if it a valid num character based on what we have found
139 // thus far, or whether it is a backslash followed by a newline. I can do
140 // i+1 on the buffer because the buffer must have a nul byte.
141 for (i = 0; (c = buf[i]) && (BC_LEX_NUM_CHAR(c, pt, int_only) ||
142 (c == '\\' && buf[i + 1] == '\n')); ++i)
144 // I don't need to test that the next character is a newline because
145 // the loop condition above ensures that.
150 // Make sure to eat whitespace at the beginning of the line.
151 while(isspace(buf[i]) && buf[i] != '\n') i += 1;
155 // If the next character is not a number character, bail.
156 if (!BC_LEX_NUM_CHAR(c, pt, int_only)) break;
159 // Did we find the radix point?
160 last_pt = (c == '.');
162 // If we did, and we already have one, then break because it's not part
164 if (pt && last_pt) break;
166 // Set whether we have found a radix point.
169 bc_vec_push(&l->str, &c);
175 void bc_lex_number(BcLex *l, char start) {
177 l->t = BC_LEX_NUMBER;
179 // Make sure the string is clear.
180 bc_vec_popAll(&l->str);
181 bc_vec_push(&l->str, &start);
184 l->i += bc_lex_num(l, start, false);
186 #if BC_ENABLE_EXTRA_MATH
188 char c = l->buf[l->i];
190 // Do we have a number in scientific notation?
195 if (BC_IS_POSIX) bc_lex_err(l, BC_ERR_POSIX_EXP_NUM);
199 bc_vec_push(&l->str, &c);
203 // Check for negative specifically because bc_lex_num() does not.
204 if (c == BC_LEX_NEG_CHAR) {
205 bc_vec_push(&l->str, &c);
210 // We must have a number character, so barf if not.
211 if (BC_ERR(!BC_LEX_NUM_CHAR(c, false, true)))
212 bc_lex_verr(l, BC_ERR_PARSE_CHAR, c);
214 // Parse the exponent.
215 l->i += bc_lex_num(l, 0, true);
218 #endif // BC_ENABLE_EXTRA_MATH
220 bc_vec_pushByte(&l->str, '\0');
223 void bc_lex_name(BcLex *l) {
226 const char *buf = l->buf + l->i - 1;
231 // Should be obvious. It's looking for valid characters.
232 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
234 // Set the string to the identifier.
235 bc_vec_string(&l->str, i, buf);
237 // Increment the index. We minus 1 because it has already been incremented.
241 void bc_lex_init(BcLex *l) {
242 BC_SIG_ASSERT_LOCKED;
244 bc_vec_init(&l->str, sizeof(char), BC_DTOR_NONE);
247 void bc_lex_free(BcLex *l) {
248 BC_SIG_ASSERT_LOCKED;
250 bc_vec_free(&l->str);
253 void bc_lex_file(BcLex *l, const char *file) {
254 assert(l != NULL && file != NULL);
259 void bc_lex_next(BcLex *l) {
265 // If this wasn't here, the line number would be off.
266 l->line += (l->i != 0 && l->buf[l->i - 1] == '\n');
268 // If the last token was EOF, someone called this one too many times.
269 if (BC_ERR(l->last == BC_LEX_EOF)) bc_lex_err(l, BC_ERR_PARSE_EOF);
273 // We are done if this is true.
274 if (l->i == l->len) return;
276 // Loop until failure or we don't have whitespace. This
277 // is so the parser doesn't get inundated with whitespace.
280 } while (l->t == BC_LEX_WHITESPACE);
284 * Updates the buffer and len so that they are not invalidated when the stdin
286 * @param l The lexer.
287 * @param text The text.
288 * @param len The length of the text.
290 static void bc_lex_fixText(BcLex *l, const char *text, size_t len) {
295 bool bc_lex_readLine(BcLex *l) {
297 bool good = bc_vm_readLine(false);
299 bc_lex_fixText(l, vm.buffer.v, vm.buffer.len - 1);
304 void bc_lex_text(BcLex *l, const char *text, bool is_stdin) {
305 assert(l != NULL && text != NULL);
306 bc_lex_fixText(l, text, strlen(text));
308 l->t = l->last = BC_LEX_INVALID;
309 l->is_stdin = is_stdin;