Files
bshell/bshell.runtime/include/bshell/parse/token.h
T
wash edfb3e24a3 bshell: re-organise build into three separate components
bshell: the front-end binary
bshell.runtime: contains the parser, compiler, and classes needed to run bshell scripts
bshell.core: contains the builtin commandlets and aliases
2026-05-25 10:33:29 +01:00

191 lines
4.3 KiB
C

#ifndef BSHELL_PARSE_TOKEN_H_
#define BSHELL_PARSE_TOKEN_H_
#include <fx/queue.h>
#include <fx/value.h>
#include <stdbool.h>
struct bshell_char_cell {
unsigned long c_row, c_col;
};
enum bshell_lex_token_type {
BSHELL_TOK_NONE = 0,
__BSHELL_TOK_INDEX_BASE = 100,
BSHELL_TOK_KEYWORD,
BSHELL_TOK_SYMBOL,
BSHELL_TOK_INT,
BSHELL_TOK_DOUBLE,
BSHELL_TOK_WORD,
BSHELL_TOK_WORD_START,
BSHELL_TOK_WORD_END,
BSHELL_TOK_FLAG,
BSHELL_TOK_OPERATOR,
BSHELL_TOK_VAR,
BSHELL_TOK_VAR_SPLAT,
BSHELL_TOK_STRING,
BSHELL_TOK_STR_START,
BSHELL_TOK_STR_END,
BSHELL_TOK_LINEFEED,
__BSHELL_TOK_INDEX_LIMIT,
};
enum bshell_lex_keyword {
BSHELL_KW_NONE = 0,
__BSHELL_KW_INDEX_BASE = 200,
BSHELL_KW_FUNC,
BSHELL_KW_IF,
BSHELL_KW_ELSEIF,
BSHELL_KW_ELSE,
__BSHELL_KW_INDEX_LIMIT,
};
enum bshell_lex_operator {
BSHELL_TKOP_NONE = 0,
__BSHELL_TKOP_INDEX_BASE = 300,
BSHELL_TKOP_F,
BSHELL_TKOP_BAND,
BSHELL_TKOP_BOR,
BSHELL_TKOP_BXOR,
BSHELL_TKOP_BNOT,
BSHELL_TKOP_SHL,
BSHELL_TKOP_SHR,
BSHELL_TKOP_EQ,
BSHELL_TKOP_NE,
BSHELL_TKOP_GT,
BSHELL_TKOP_LT,
BSHELL_TKOP_GE,
BSHELL_TKOP_LE,
BSHELL_TKOP_MATCH,
BSHELL_TKOP_NOTMATCH,
BSHELL_TKOP_REPLACE,
BSHELL_TKOP_LIKE,
BSHELL_TKOP_NOTLIKE,
BSHELL_TKOP_IN,
BSHELL_TKOP_NOTIN,
BSHELL_TKOP_CONTAINS,
BSHELL_TKOP_NOTCONTAINS,
BSHELL_TKOP_AND,
BSHELL_TKOP_OR,
BSHELL_TKOP_XOR,
BSHELL_TKOP_NOT,
BSHELL_TKOP_SPLIT,
BSHELL_TKOP_JOIN,
BSHELL_TKOP_IS,
BSHELL_TKOP_ISNOT,
BSHELL_TKOP_AS,
__BSHELL_TKOP_INDEX_LIMIT,
};
enum bshell_lex_symbol {
BSHELL_SYM_NONE = 0,
__BSHELL_SYM_INDEX_BASE = 400,
BSHELL_SYM_BANG,
BSHELL_SYM_PLUS,
BSHELL_SYM_HYPHEN,
BSHELL_SYM_FORWARD_SLASH,
BSHELL_SYM_ASTERISK,
BSHELL_SYM_AMPERSAND,
BSHELL_SYM_PERCENT,
BSHELL_SYM_SQUOTE,
BSHELL_SYM_DQUOTE,
BSHELL_SYM_HASH,
BSHELL_SYM_COLON_COLON,
BSHELL_SYM_SEMICOLON,
BSHELL_SYM_COMMA,
BSHELL_SYM_DOLLAR,
BSHELL_SYM_DOLLAR_LEFT_PAREN,
BSHELL_SYM_DOLLAR_LEFT_BRACE,
BSHELL_SYM_DOT,
BSHELL_SYM_DOT_DOT,
BSHELL_SYM_PIPE,
BSHELL_SYM_AT,
BSHELL_SYM_AT_LEFT_PAREN,
BSHELL_SYM_AT_LEFT_BRACE,
BSHELL_SYM_LEFT_BRACE,
BSHELL_SYM_RIGHT_BRACE,
BSHELL_SYM_LEFT_BRACKET,
BSHELL_SYM_RIGHT_BRACKET,
BSHELL_SYM_LEFT_PAREN,
BSHELL_SYM_RIGHT_PAREN,
BSHELL_SYM_EQUAL,
BSHELL_SYM_PLUS_EQUAL,
BSHELL_SYM_HYPHEN_EQUAL,
BSHELL_SYM_ASTERISK_EQUAL,
BSHELL_SYM_FORWARD_SLASH_EQUAL,
BSHELL_SYM_PERCENT_EQUAL,
BSHELL_SYM_QUESTION_DOT,
BSHELL_SYM_QUESTION_LEFT_BRACKET,
__BSHELL_SYM_INDEX_LIMIT,
};
struct bshell_lex_token {
enum bshell_lex_token_type tok_type;
struct bshell_char_cell tok_start, tok_end;
fx_queue_entry tok_entry;
union {
enum bshell_lex_keyword tok_keyword;
enum bshell_lex_symbol tok_symbol;
enum bshell_lex_operator tok_operator;
fx_value tok_number;
char *tok_str;
};
};
extern struct bshell_lex_token *bshell_lex_token_create(
enum bshell_lex_token_type type);
extern struct bshell_lex_token *bshell_lex_token_create_with_string(
enum bshell_lex_token_type type,
const char *s);
extern void bshell_lex_token_destroy(struct bshell_lex_token *tok);
extern struct bshell_lex_token *bshell_lex_token_change_type(
struct bshell_lex_token *tok,
enum bshell_lex_token_type new_type);
extern void bshell_lex_token_change_string(
struct bshell_lex_token *tok,
const char *s);
static inline bool bshell_lex_token_is_symbol(
struct bshell_lex_token *tok,
enum bshell_lex_symbol sym)
{
return (tok->tok_type == BSHELL_TOK_SYMBOL && tok->tok_symbol == sym);
}
static inline bool bshell_lex_token_is_keyword(
struct bshell_lex_token *tok,
enum bshell_lex_keyword kw)
{
return (tok->tok_type == BSHELL_TOK_KEYWORD && tok->tok_keyword == kw);
}
static inline bool bshell_lex_token_type_has_string_value(
enum bshell_lex_token_type type)
{
switch (type) {
case BSHELL_TOK_WORD:
case BSHELL_TOK_STRING:
case BSHELL_TOK_FLAG:
case BSHELL_TOK_VAR:
case BSHELL_TOK_VAR_SPLAT:
return true;
default:
return false;
}
}
static inline bool bshell_lex_token_has_string_value(
const struct bshell_lex_token *tok)
{
return bshell_lex_token_type_has_string_value(tok->tok_type);
}
extern const char *bshell_token_type_to_string(enum bshell_lex_token_type type);
extern const char *bshell_lex_keyword_to_string(
enum bshell_lex_keyword keyword);
extern const char *bshell_lex_symbol_to_string(enum bshell_lex_symbol sym);
extern const char *bshell_lex_operator_to_string(enum bshell_lex_operator op);
#endif