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 * Definitions for bc's lexer.
46 #define bc_lex_err(l, e) (bc_vm_error((e), (l)->line))
47 #define bc_lex_verr(l, e, ...) (bc_vm_error((e), (l)->line, __VA_ARGS__))
52 #define BC_LEX_NEG_CHAR (BC_IS_BC ? '-' : '_')
53 #define BC_LEX_LAST_NUM_CHAR (BC_IS_BC ? 'Z' : 'F')
55 #define BC_LEX_NEG_CHAR ('-')
56 #define BC_LEX_LAST_NUM_CHAR ('Z')
61 #define BC_LEX_NEG_CHAR ('_')
62 #define BC_LEX_LAST_NUM_CHAR ('F')
66 #define BC_LEX_NUM_CHAR(c, pt, int_only) \
67 (isdigit(c) || ((c) >= 'A' && (c) <= BC_LEX_LAST_NUM_CHAR) || \
68 ((c) == '.' && !(pt) && !(int_only)))
70 // BC_LEX_NEG is not used in lexing; it is only for parsing.
71 typedef enum BcLexType {
83 #if BC_ENABLE_EXTRA_MATH
85 #endif // BC_ENABLE_EXTRA_MATH
94 #if BC_ENABLE_EXTRA_MATH
99 #endif // BC_ENABLE_EXTRA_MATH
112 BC_LEX_OP_ASSIGN_POWER,
113 BC_LEX_OP_ASSIGN_MULTIPLY,
114 BC_LEX_OP_ASSIGN_DIVIDE,
115 BC_LEX_OP_ASSIGN_MODULUS,
116 BC_LEX_OP_ASSIGN_PLUS,
117 BC_LEX_OP_ASSIGN_MINUS,
118 #if BC_ENABLE_EXTRA_MATH
119 BC_LEX_OP_ASSIGN_PLACES,
120 BC_LEX_OP_ASSIGN_LSHIFT,
121 BC_LEX_OP_ASSIGN_RSHIFT,
122 #endif // BC_ENABLE_EXTRA_MATH
160 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
162 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
167 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
169 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
172 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
174 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
178 #if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
180 #endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
203 #if BC_ENABLE_EXTRA_MATH
205 #endif // BC_ENABLE_EXTRA_MATH
217 typedef void (*BcLexNext)(struct BcLex*);
219 typedef struct BcLex {
232 void bc_lex_init(BcLex *l);
233 void bc_lex_free(BcLex *l);
234 void bc_lex_file(BcLex *l, const char *file);
235 void bc_lex_text(BcLex *l, const char *text);
236 void bc_lex_next(BcLex *l);
238 void bc_lex_lineComment(BcLex *l);
239 void bc_lex_comment(BcLex *l);
240 void bc_lex_whitespace(BcLex *l);
241 void bc_lex_number(BcLex *l, char start);
242 void bc_lex_name(BcLex *l);
243 void bc_lex_commonTokens(BcLex *l, char c);
245 void bc_lex_invalidChar(BcLex *l, char c);