Files
bshell/bshell/parse/token.h
T

185 lines
3.3 KiB
C

#ifndef IVY_LANG_LEX_H_
#define IVY_LANG_LEX_H_
#include <fx/queue.h>
#include <stdbool.h>
struct char_cell {
unsigned long c_row, c_col;
};
enum token_type {
TOK_NONE = 0,
__TOK_INDEX_BASE = 100,
TOK_KEYWORD,
TOK_SYMBOL,
TOK_INT,
TOK_DOUBLE,
TOK_WORD,
TOK_WORD_START,
TOK_WORD_END,
TOK_FLAG,
TOK_OPERATOR,
TOK_VAR,
TOK_VAR_SPLAT,
TOK_STRING,
TOK_STR_START,
TOK_STR_END,
TOK_LINEFEED,
__TOK_INDEX_LIMIT,
};
enum token_keyword {
KW_NONE = 0,
__KW_INDEX_BASE = 200,
KW_FUNC,
KW_IF,
KW_ELSEIF,
KW_ELSE,
__KW_INDEX_LIMIT,
};
enum token_operator {
TKOP_NONE = 0,
__TKOP_INDEX_BASE = 300,
TKOP_F,
TKOP_BAND,
TKOP_BOR,
TKOP_BXOR,
TKOP_BNOT,
TKOP_SHL,
TKOP_SHR,
TKOP_EQ,
TKOP_NE,
TKOP_GT,
TKOP_LT,
TKOP_GE,
TKOP_LE,
TKOP_MATCH,
TKOP_NOTMATCH,
TKOP_REPLACE,
TKOP_LIKE,
TKOP_NOTLIKE,
TKOP_IN,
TKOP_NOTIN,
TKOP_CONTAINS,
TKOP_NOTCONTAINS,
TKOP_AND,
TKOP_OR,
TKOP_XOR,
TKOP_NOT,
TKOP_SPLIT,
TKOP_JOIN,
TKOP_IS,
TKOP_ISNOT,
TKOP_AS,
__TKOP_INDEX_LIMIT,
};
enum token_symbol {
SYM_NONE = 0,
__SYM_INDEX_BASE = 400,
SYM_BANG,
SYM_PLUS,
SYM_HYPHEN,
SYM_FORWARD_SLASH,
SYM_ASTERISK,
SYM_AMPERSAND,
SYM_PERCENT,
SYM_SQUOTE,
SYM_DQUOTE,
SYM_HASH,
SYM_COLON_COLON,
SYM_SEMICOLON,
SYM_COMMA,
SYM_DOLLAR,
SYM_DOLLAR_LEFT_PAREN,
SYM_DOLLAR_LEFT_BRACE,
SYM_DOT,
SYM_DOT_DOT,
SYM_PIPE,
SYM_AT,
SYM_AT_LEFT_PAREN,
SYM_AT_LEFT_BRACE,
SYM_LEFT_BRACE,
SYM_RIGHT_BRACE,
SYM_LEFT_BRACKET,
SYM_RIGHT_BRACKET,
SYM_LEFT_PAREN,
SYM_RIGHT_PAREN,
SYM_EQUAL,
SYM_PLUS_EQUAL,
SYM_HYPHEN_EQUAL,
SYM_ASTERISK_EQUAL,
SYM_FORWARD_SLASH_EQUAL,
SYM_PERCENT_EQUAL,
SYM_QUESTION_DOT,
SYM_QUESTION_LEFT_BRACKET,
__SYM_INDEX_LIMIT,
};
struct lex_token {
enum token_type tok_type;
struct char_cell tok_start, tok_end;
fx_queue_entry tok_entry;
union {
enum token_keyword tok_keyword;
enum token_symbol tok_symbol;
enum token_operator tok_operator;
long long tok_int;
double tok_double;
char *tok_str;
};
};
extern struct lex_token *lex_token_create(enum token_type type);
extern struct lex_token *lex_token_create_with_string(
enum token_type type,
const char *s);
extern void lex_token_destroy(struct lex_token *tok);
extern struct lex_token *lex_token_change_type(
struct lex_token *tok,
enum token_type new_type);
extern void lex_token_change_string(struct lex_token *tok, const char *s);
static inline bool lex_token_is_symbol(
struct lex_token *tok,
enum token_symbol sym)
{
return (tok->tok_type == TOK_SYMBOL && tok->tok_symbol == sym);
}
static inline bool lex_token_is_keyword(
struct lex_token *tok,
enum token_keyword kw)
{
return (tok->tok_type == TOK_KEYWORD && tok->tok_keyword == kw);
}
static inline bool lex_token_type_has_string_value(enum token_type type)
{
switch (type) {
case TOK_WORD:
case TOK_STRING:
case TOK_FLAG:
case TOK_VAR:
case TOK_VAR_SPLAT:
return true;
default:
return false;
}
}
static inline bool lex_token_has_string_value(const struct lex_token *tok)
{
return lex_token_type_has_string_value(tok->tok_type);
}
extern const char *token_type_to_string(enum token_type type);
extern const char *token_keyword_to_string(enum token_keyword keyword);
extern const char *token_symbol_to_string(enum token_symbol sym);
extern const char *token_operator_to_string(enum token_operator op);
#endif