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 * *****************************************************************************
45 static void bc_lex_identifier(BcLex *l) {
48 const char *buf = l->buf + l->i - 1;
50 for (i = 0; i < bc_lex_kws_len; ++i) {
52 const BcLexKeyword *kw = bc_lex_kws + i;
53 size_t n = BC_LEX_KW_LEN(kw);
55 if (!strncmp(buf, kw->name, n) && !isalnum(buf[n]) && buf[n] != '_') {
57 l->t = BC_LEX_KW_AUTO + (BcLexType) i;
59 if (!BC_LEX_KW_POSIX(kw))
60 bc_lex_verr(l, BC_ERR_POSIX_KW, kw->name);
62 // We minus 1 because the index has already been incremented.
70 if (BC_ERR(l->str.len - 1 > 1))
71 bc_lex_verr(l, BC_ERR_POSIX_NAME_LEN, l->str.v);
74 static void bc_lex_string(BcLex *l) {
76 size_t len, nlines = 0, i = l->i;
77 const char *buf = l->buf;
82 for (; (c = buf[i]) && c != '"'; ++i) nlines += c == '\n';
84 if (BC_ERR(c == '\0')) {
86 bc_lex_err(l, BC_ERR_PARSE_STRING);
90 bc_vec_string(&l->str, len, l->buf + l->i);
96 static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) {
97 if (l->buf[l->i] == '=') {
104 void bc_lex_token(BcLex *l) {
106 char c = l->buf[l->i++], c2;
108 // This is the workhorse of the lexer.
119 bc_lex_commonTokens(l, c);
125 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
127 if (l->t == BC_LEX_OP_BOOL_NOT)
128 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "!");
141 bc_lex_err(l, BC_ERR_POSIX_COMMENT);
142 bc_lex_lineComment(l);
148 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
155 if (BC_NO_ERR(c2 == '&')) {
157 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "&&");
160 l->t = BC_LEX_OP_BOOL_AND;
162 else bc_lex_invalidChar(l, c);
166 #if BC_ENABLE_EXTRA_MATH
169 l->t = BC_LEX_OP_TRUNC;
175 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLACES, BC_LEX_OP_PLACES);
178 #endif // BC_ENABLE_EXTRA_MATH
182 l->t = (BcLexType) (c - '(' + BC_LEX_LPAREN);
188 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
197 l->t = BC_LEX_OP_INC;
199 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
214 l->t = BC_LEX_OP_DEC;
216 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
223 if (BC_LEX_NUM_CHAR(c2, true, false)) bc_lex_number(l, c);
225 l->t = BC_LEX_KW_LAST;
226 bc_lex_err(l, BC_ERR_POSIX_DOT);
234 if (c2 =='*') bc_lex_comment(l);
235 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
255 // Apparently, GNU bc (and maybe others) allows any uppercase letter as
256 // a number. When single digits, they act like the ones above. When
257 // multi-digit, any letter above the input base is automatically set to
258 // the biggest allowable digit in the input base.
286 l->t = BC_LEX_SCOLON;
292 #if BC_ENABLE_EXTRA_MATH
297 bc_lex_assign(l, BC_LEX_OP_ASSIGN_LSHIFT, BC_LEX_OP_LSHIFT);
300 #endif // BC_ENABLE_EXTRA_MATH
301 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
307 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
313 #if BC_ENABLE_EXTRA_MATH
318 bc_lex_assign(l, BC_LEX_OP_ASSIGN_RSHIFT, BC_LEX_OP_RSHIFT);
321 #endif // BC_ENABLE_EXTRA_MATH
322 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
329 l->t = (BcLexType) (c - '[' + BC_LEX_LBRACKET);
335 if (BC_NO_ERR(l->buf[l->i] == '\n')) {
337 l->t = BC_LEX_WHITESPACE;
339 else bc_lex_invalidChar(l, c);
345 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
376 bc_lex_identifier(l);
383 l->t = (BcLexType) (c - '{' + BC_LEX_LBRACE);
391 if (BC_NO_ERR(c2 == '|')) {
393 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "||");
396 l->t = BC_LEX_OP_BOOL_OR;
398 else bc_lex_invalidChar(l, c);
405 bc_lex_invalidChar(l, c);