134 lines
2.5 KiB
C
134 lines
2.5 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_FLAG,
|
|
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_ELSE,
|
|
__KW_INDEX_LIMIT,
|
|
};
|
|
|
|
enum token_symbol {
|
|
SYM_NONE = 0,
|
|
__SYM_INDEX_BASE = 300,
|
|
SYM_PLUS,
|
|
SYM_HYPHEN,
|
|
SYM_FORWARD_SLASH,
|
|
SYM_ASTERISK,
|
|
SYM_AMPERSAND,
|
|
SYM_PERCENT,
|
|
SYM_SQUOTE,
|
|
SYM_DQUOTE,
|
|
SYM_HASH,
|
|
SYM_SEMICOLON,
|
|
SYM_COMMA,
|
|
SYM_DOLLAR,
|
|
SYM_DOLLAR_LEFT_PAREN,
|
|
SYM_DOLLAR_LEFT_BRACE,
|
|
SYM_PIPE,
|
|
SYM_AT,
|
|
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_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;
|
|
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);
|
|
|
|
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);
|
|
|
|
#endif
|