blob: 6a44bcf17dff1e0b5e1ffbb0d569f6dec8fa7755 [file] [log] [blame]
/*
* *****************************************************************************
*
* Copyright 2018 Gavin D. Howard
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* *****************************************************************************
*
* Definitions for bc's lexer.
*
*/
#ifndef BC_LEX_H
#define BC_LEX_H
#include <stdbool.h>
#include <stdlib.h>
#include <bc.h>
#define BC_LEX_GEN_ENUM(ENUM) ENUM,
#define BC_LEX_GEN_STR(STRING) #STRING,
// BC_LEX_OP_NEGATE is not used in lexing;
// it is only for parsing.
#define BC_LEX_TOKEN_FOREACH(TOKEN) \
TOKEN(BC_LEX_OP_INC) \
TOKEN(BC_LEX_OP_DEC) \
\
TOKEN(BC_LEX_OP_NEGATE) \
\
TOKEN(BC_LEX_OP_POWER) \
\
TOKEN(BC_LEX_OP_MULTIPLY) \
TOKEN(BC_LEX_OP_DIVIDE) \
TOKEN(BC_LEX_OP_MODULUS) \
\
TOKEN(BC_LEX_OP_PLUS) \
TOKEN(BC_LEX_OP_MINUS) \
\
TOKEN(BC_LEX_OP_REL_EQUAL) \
TOKEN(BC_LEX_OP_REL_LESS_EQ) \
TOKEN(BC_LEX_OP_REL_GREATER_EQ) \
TOKEN(BC_LEX_OP_REL_NOT_EQ) \
TOKEN(BC_LE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X_KEY_READ) \
TOKEN(BC_LEX_KEY_RETURN) \
TOKEN(BC_LEX_KEY_SCALE) \
TOKEN(BC_LEX_KEY_SQRT) \
TOKEN(BC_LEX_KEY_WHILE) \
\
TOKEN(BC_LEX_EOF) \
\
TOKEN(BC_LEX_INVALID) \
typedef enum BcLexTokenType {
BC_LEX_TOKEN_FOREACH(BC_LEX_GEN_ENUM)
} BcLexTokenType;
typedef struct BcLexToken {
BcLexTokenType type;
char *string;
} BcLexToken;
typedef struct BcLex {
const char *buffer;
size_t idx;
size_t line;
bool newline;
const char *file;
size_t len;
} BcLex;
typedef struct BcLexKeyword {
const char name[9];
const char len;
const bool posix;
} BcLexKeyword;
#define KW_TABLE_ENTRY(a, b, c) { .name = a, .len = b, .posix = c }
// ** Exclude start. **
BcStatus bc_lex_init(BcLex *lex, const char *file);
BcStatus bc_lex_text(BcLex *lex, const char *text);
BcStatus bc_lex_next(BcLex *lex, BcLexToken *token);
BcStatus bc_lex_printToken(BcLexToken *token);
// ** Exclude end. **
extern const char *bc_lex_token_type_strs[];
extern const BcLexKeyword bc_lex_keywords[20];
#endif // BC_LEX_H