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 * *****************************************************************************
46 static void bc_lex_identifier(BcLex *l) {
49 const char *buf = l->buf + l->i - 1;
51 for (i = 0; i < bc_lex_kws_len; ++i) {
53 const BcLexKeyword *kw = bc_lex_kws + i;
54 size_t n = BC_LEX_KW_LEN(kw);
56 if (!strncmp(buf, kw->name, n) && !isalnum(buf[n]) && buf[n] != '_') {
58 l->t = BC_LEX_KW_AUTO + (BcLexType) i;
60 if (!BC_LEX_KW_POSIX(kw))
61 bc_lex_verr(l, BC_ERROR_POSIX_KW, kw->name);
63 // We minus 1 because the index has already been incremented.
71 if (BC_ERR(l->str.len - 1 > 1))
72 bc_lex_verr(l, BC_ERROR_POSIX_NAME_LEN, l->str.v);
75 static void bc_lex_string(BcLex *l) {
77 size_t len, nlines = 0, i = l->i;
78 const char *buf = l->buf;
83 for (; (c = buf[i]) && c != '"'; ++i) nlines += c == '\n';
85 if (BC_ERR(c == '\0')) {
87 bc_lex_err(l, BC_ERROR_PARSE_STRING);
91 bc_vec_string(&l->str, len, l->buf + l->i);
97 static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) {
98 if (l->buf[l->i] == '=') {
105 void bc_lex_token(BcLex *l) {
107 char c = l->buf[l->i++], c2;
109 // This is the workhorse of the lexer.
120 bc_lex_commonTokens(l, c);
126 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
128 if (l->t == BC_LEX_OP_BOOL_NOT)
129 bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "!");
142 bc_lex_err(l, BC_ERROR_POSIX_COMMENT);
143 bc_lex_lineComment(l);
149 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
156 if (BC_NO_ERR(c2 == '&')) {
158 bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "&&");
161 l->t = BC_LEX_OP_BOOL_AND;
163 else bc_lex_invalidChar(l, c);
167 #if BC_ENABLE_EXTRA_MATH
170 l->t = BC_LEX_OP_TRUNC;
176 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLACES, BC_LEX_OP_PLACES);
179 #endif // BC_ENABLE_EXTRA_MATH
183 l->t = (BcLexType) (c - '(' + BC_LEX_LPAREN);
189 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
198 l->t = BC_LEX_OP_INC;
200 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
215 l->t = BC_LEX_OP_DEC;
217 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
224 if (BC_LEX_NUM_CHAR(c2, true, false)) bc_lex_number(l, c);
226 l->t = BC_LEX_KW_LAST;
227 bc_lex_err(l, BC_ERROR_POSIX_DOT);
235 if (c2 =='*') bc_lex_comment(l);
236 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
256 // Apparently, GNU bc (and maybe others) allows any uppercase letter as
257 // a number. When single digits, they act like the ones above. When
258 // multi-digit, any letter above the input base is automatically set to
259 // the biggest allowable digit in the input base.
287 l->t = BC_LEX_SCOLON;
293 #if BC_ENABLE_EXTRA_MATH
298 bc_lex_assign(l, BC_LEX_OP_ASSIGN_LSHIFT, BC_LEX_OP_LSHIFT);
301 #endif // BC_ENABLE_EXTRA_MATH
302 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
308 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
314 #if BC_ENABLE_EXTRA_MATH
319 bc_lex_assign(l, BC_LEX_OP_ASSIGN_RSHIFT, BC_LEX_OP_RSHIFT);
322 #endif // BC_ENABLE_EXTRA_MATH
323 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
330 l->t = (BcLexType) (c - '[' + BC_LEX_LBRACKET);
336 if (BC_NO_ERR(l->buf[l->i] == '\n')) {
338 l->t = BC_LEX_WHITESPACE;
340 else bc_lex_invalidChar(l, c);
346 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
377 bc_lex_identifier(l);
384 l->t = (BcLexType) (c - '{' + BC_LEX_LBRACE);
392 if (BC_NO_ERR(c2 == '|')) {
394 bc_lex_verr(l, BC_ERROR_POSIX_BOOL, "||");
397 l->t = BC_LEX_OP_BOOL_OR;
399 else bc_lex_invalidChar(l, c);
406 bc_lex_invalidChar(l, c);