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 * *****************************************************************************
46 * Lexes an identifier, which may be a keyword.
49 static void bc_lex_identifier(BcLex *l) {
51 // We already passed the first character, so we need to be sure to include
53 const char *buf = l->buf + l->i - 1;
56 // This loop is simply checking for keywords.
57 for (i = 0; i < bc_lex_kws_len; ++i) {
59 const BcLexKeyword *kw = bc_lex_kws + i;
60 size_t n = BC_LEX_KW_LEN(kw);
62 if (!strncmp(buf, kw->name, n) && !isalnum(buf[n]) && buf[n] != '_') {
64 // If the keyword has been redefined, and redefinition is allowed
65 // (it is not allowed for builtin libraries), break out of the loop
66 // and use it as a name. This depends on the argument parser to
67 // ensure that only non-POSIX keywords get redefined.
68 if (!vm.no_redefine && vm.redefined_kws[i]) break;
70 l->t = BC_LEX_KW_AUTO + (BcLexType) i;
72 // Warn or error, as appropriate for the mode, if the keyword is not
73 // in the POSIX standard.
74 if (!BC_LEX_KW_POSIX(kw)) bc_lex_verr(l, BC_ERR_POSIX_KW, kw->name);
76 // We minus 1 because the index has already been incremented.
79 // Already have the token; bail.
84 // If not a keyword, parse the name.
87 // POSIX doesn't allow identifiers that are more than one character, so we
88 // might have to warn or error here too.
89 if (BC_ERR(l->str.len - 1 > 1))
90 bc_lex_verr(l, BC_ERR_POSIX_NAME_LEN, l->str.v);
94 * Parses a bc string. This is separate from dc strings because dc strings need
98 static void bc_lex_string(BcLex *l) {
100 // We need to keep track of newlines to increment them properly.
101 size_t len, nlines, i;
114 assert(!vm.is_stdin || buf == vm.buffer.v);
116 // Fortunately for us, bc doesn't escape quotes. Instead, the equivalent
117 // is '\q', which makes this loop simpler.
118 for (i = l->i; (c = buf[i]) && c != '"'; ++i) nlines += (c == '\n');
120 if (BC_ERR(c == '\0') && !vm.eof && l->is_stdin)
121 got_more = bc_lex_readLine(l);
123 } while (got_more && c != '"');
125 // If the string did not end properly, barf.
128 bc_lex_err(l, BC_ERR_PARSE_STRING);
131 // Set the temp string to the parsed string.
133 bc_vec_string(&l->str, len, l->buf + l->i);
140 * This function takes a lexed operator and checks to see if it's the assignment
141 * version, setting the token appropriately.
142 * @param l The lexer.
143 * @param with The token to assign if it is an assignment operator.
144 * @param without The token to assign if it is not an assignment operator.
146 static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) {
147 if (l->buf[l->i] == '=') {
154 void bc_lex_token(BcLex *l) {
156 // We increment here. This means that all lexing needs to take that into
157 // account, such as when parsing an identifier. If we don't, the first
158 // character of every identifier would be missing.
159 char c = l->buf[l->i++], c2;
161 // This is the workhorse of the lexer.
172 bc_lex_commonTokens(l, c);
178 // Even though it's not an assignment, we can use this.
179 bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT);
181 // POSIX doesn't allow boolean not.
182 if (l->t == BC_LEX_OP_BOOL_NOT)
183 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "!");
196 // POSIX does not allow line comments.
197 bc_lex_err(l, BC_ERR_POSIX_COMMENT);
198 bc_lex_lineComment(l);
204 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS);
212 // Either we have boolean and or an error. And boolean and is not
214 if (BC_NO_ERR(c2 == '&')) {
216 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "&&");
219 l->t = BC_LEX_OP_BOOL_AND;
221 else bc_lex_invalidChar(l, c);
225 #if BC_ENABLE_EXTRA_MATH
228 l->t = BC_LEX_OP_TRUNC;
234 bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLACES, BC_LEX_OP_PLACES);
237 #endif // BC_ENABLE_EXTRA_MATH
241 l->t = (BcLexType) (c - '(' + BC_LEX_LPAREN);
247 bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY);
255 // Have to check for increment first.
258 l->t = BC_LEX_OP_INC;
260 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS);
274 // Have to check for decrement first.
277 l->t = BC_LEX_OP_DEC;
279 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS);
287 // If it's alone, it's an alias for last.
288 if (BC_LEX_NUM_CHAR(c2, true, false)) bc_lex_number(l, c);
290 l->t = BC_LEX_KW_LAST;
291 bc_lex_err(l, BC_ERR_POSIX_DOT);
300 if (c2 =='*') bc_lex_comment(l);
301 else bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE);
321 // Apparently, GNU bc (and maybe others) allows any uppercase letter as
322 // a number. When single digits, they act like the ones above. When
323 // multi-digit, any letter above the input base is automatically set to
324 // the biggest allowable digit in the input base.
352 l->t = BC_LEX_SCOLON;
358 #if BC_ENABLE_EXTRA_MATH
364 bc_lex_assign(l, BC_LEX_OP_ASSIGN_LSHIFT, BC_LEX_OP_LSHIFT);
367 #endif // BC_ENABLE_EXTRA_MATH
368 bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT);
374 bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN);
380 #if BC_ENABLE_EXTRA_MATH
386 bc_lex_assign(l, BC_LEX_OP_ASSIGN_RSHIFT, BC_LEX_OP_RSHIFT);
389 #endif // BC_ENABLE_EXTRA_MATH
390 bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT);
397 l->t = (BcLexType) (c - '[' + BC_LEX_LBRACKET);
403 // In bc, a backslash+newline is whitespace.
404 if (BC_NO_ERR(l->buf[l->i] == '\n')) {
406 l->t = BC_LEX_WHITESPACE;
408 else bc_lex_invalidChar(l, c);
414 bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER);
445 bc_lex_identifier(l);
452 l->t = (BcLexType) (c - '{' + BC_LEX_LBRACE);
460 // Once again, boolean or is not allowed by POSIX.
461 if (BC_NO_ERR(c2 == '|')) {
463 bc_lex_verr(l, BC_ERR_POSIX_BOOL, "||");
466 l->t = BC_LEX_OP_BOOL_OR;
468 else bc_lex_invalidChar(l, c);
475 bc_lex_invalidChar(l, c);