edfb3e24a3
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
214 lines
4.9 KiB
C
214 lines
4.9 KiB
C
#include <bshell/parse/token.h>
|
|
#include <fx/string.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct bshell_lex_token *bshell_lex_token_create(
|
|
enum bshell_lex_token_type type)
|
|
{
|
|
struct bshell_lex_token *out = malloc(sizeof *out);
|
|
if (!out) {
|
|
return NULL;
|
|
}
|
|
|
|
memset(out, 0x0, sizeof *out);
|
|
|
|
out->tok_type = type;
|
|
|
|
return out;
|
|
}
|
|
|
|
struct bshell_lex_token *bshell_lex_token_create_with_string(
|
|
enum bshell_lex_token_type type,
|
|
const char *s)
|
|
{
|
|
struct bshell_lex_token *tok = bshell_lex_token_create(type);
|
|
if (!tok) {
|
|
return NULL;
|
|
}
|
|
|
|
tok->tok_str = fx_strdup(s);
|
|
if (!tok->tok_str) {
|
|
free(tok);
|
|
return NULL;
|
|
}
|
|
|
|
return tok;
|
|
}
|
|
|
|
void bshell_lex_token_destroy(struct bshell_lex_token *tok)
|
|
{
|
|
switch (tok->tok_type) {
|
|
case BSHELL_TOK_WORD:
|
|
case BSHELL_TOK_FLAG:
|
|
case BSHELL_TOK_STRING:
|
|
if (tok->tok_str) {
|
|
free(tok->tok_str);
|
|
}
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
free(tok);
|
|
}
|
|
|
|
struct bshell_lex_token *bshell_lex_token_change_type(
|
|
struct bshell_lex_token *tok,
|
|
enum bshell_lex_token_type new_type)
|
|
{
|
|
switch (tok->tok_type) {
|
|
case BSHELL_TOK_WORD:
|
|
case BSHELL_TOK_FLAG:
|
|
case BSHELL_TOK_STRING:
|
|
if (tok->tok_str) {
|
|
free(tok->tok_str);
|
|
tok->tok_str = NULL;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
tok->tok_type = new_type;
|
|
return tok;
|
|
}
|
|
|
|
void bshell_lex_token_change_string(struct bshell_lex_token *tok, const char *s)
|
|
{
|
|
if (!bshell_lex_token_has_string_value(tok)) {
|
|
return;
|
|
}
|
|
|
|
if (tok->tok_str) {
|
|
free(tok->tok_str);
|
|
}
|
|
|
|
tok->tok_str = fx_strdup(s);
|
|
}
|
|
|
|
#define ENUM_STR(x) \
|
|
case x: \
|
|
return #x
|
|
|
|
const char *bshell_token_type_to_string(enum bshell_lex_token_type type)
|
|
{
|
|
switch (type) {
|
|
ENUM_STR(BSHELL_TOK_NONE);
|
|
ENUM_STR(BSHELL_TOK_KEYWORD);
|
|
ENUM_STR(BSHELL_TOK_SYMBOL);
|
|
ENUM_STR(BSHELL_TOK_INT);
|
|
ENUM_STR(BSHELL_TOK_DOUBLE);
|
|
ENUM_STR(BSHELL_TOK_WORD);
|
|
ENUM_STR(BSHELL_TOK_WORD_START);
|
|
ENUM_STR(BSHELL_TOK_WORD_END);
|
|
ENUM_STR(BSHELL_TOK_OPERATOR);
|
|
ENUM_STR(BSHELL_TOK_VAR);
|
|
ENUM_STR(BSHELL_TOK_VAR_SPLAT);
|
|
ENUM_STR(BSHELL_TOK_FLAG);
|
|
ENUM_STR(BSHELL_TOK_STRING);
|
|
ENUM_STR(BSHELL_TOK_STR_START);
|
|
ENUM_STR(BSHELL_TOK_STR_END);
|
|
ENUM_STR(BSHELL_TOK_LINEFEED);
|
|
default:
|
|
return "<unknown>";
|
|
}
|
|
}
|
|
|
|
const char *bshell_lex_keyword_to_string(enum bshell_lex_keyword keyword)
|
|
{
|
|
switch (keyword) {
|
|
ENUM_STR(BSHELL_KW_NONE);
|
|
ENUM_STR(BSHELL_KW_FUNC);
|
|
ENUM_STR(BSHELL_KW_IF);
|
|
ENUM_STR(BSHELL_KW_ELSEIF);
|
|
ENUM_STR(BSHELL_KW_ELSE);
|
|
default:
|
|
return "<unknown>";
|
|
}
|
|
}
|
|
|
|
const char *bshell_lex_symbol_to_string(enum bshell_lex_symbol sym)
|
|
{
|
|
switch (sym) {
|
|
ENUM_STR(BSHELL_SYM_NONE);
|
|
ENUM_STR(BSHELL_SYM_PLUS);
|
|
ENUM_STR(BSHELL_SYM_HYPHEN);
|
|
ENUM_STR(BSHELL_SYM_FORWARD_SLASH);
|
|
ENUM_STR(BSHELL_SYM_ASTERISK);
|
|
ENUM_STR(BSHELL_SYM_AMPERSAND);
|
|
ENUM_STR(BSHELL_SYM_PERCENT);
|
|
ENUM_STR(BSHELL_SYM_SQUOTE);
|
|
ENUM_STR(BSHELL_SYM_DQUOTE);
|
|
ENUM_STR(BSHELL_SYM_HASH);
|
|
ENUM_STR(BSHELL_SYM_COLON_COLON);
|
|
ENUM_STR(BSHELL_SYM_SEMICOLON);
|
|
ENUM_STR(BSHELL_SYM_COMMA);
|
|
ENUM_STR(BSHELL_SYM_DOLLAR);
|
|
ENUM_STR(BSHELL_SYM_DOLLAR_LEFT_PAREN);
|
|
ENUM_STR(BSHELL_SYM_DOLLAR_LEFT_BRACE);
|
|
ENUM_STR(BSHELL_SYM_DOT);
|
|
ENUM_STR(BSHELL_SYM_DOT_DOT);
|
|
ENUM_STR(BSHELL_SYM_PIPE);
|
|
ENUM_STR(BSHELL_SYM_AT);
|
|
ENUM_STR(BSHELL_SYM_AT_LEFT_PAREN);
|
|
ENUM_STR(BSHELL_SYM_AT_LEFT_BRACE);
|
|
ENUM_STR(BSHELL_SYM_LEFT_BRACE);
|
|
ENUM_STR(BSHELL_SYM_RIGHT_BRACE);
|
|
ENUM_STR(BSHELL_SYM_LEFT_BRACKET);
|
|
ENUM_STR(BSHELL_SYM_RIGHT_BRACKET);
|
|
ENUM_STR(BSHELL_SYM_LEFT_PAREN);
|
|
ENUM_STR(BSHELL_SYM_RIGHT_PAREN);
|
|
ENUM_STR(BSHELL_SYM_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_PLUS_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_HYPHEN_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_ASTERISK_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_FORWARD_SLASH_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_PERCENT_EQUAL);
|
|
ENUM_STR(BSHELL_SYM_QUESTION_DOT);
|
|
ENUM_STR(BSHELL_SYM_QUESTION_LEFT_BRACKET);
|
|
default:
|
|
return "<unknown>";
|
|
}
|
|
}
|
|
|
|
const char *bshell_lex_operator_to_string(enum bshell_lex_operator op)
|
|
{
|
|
switch (op) {
|
|
ENUM_STR(BSHELL_TKOP_BAND);
|
|
ENUM_STR(BSHELL_TKOP_BOR);
|
|
ENUM_STR(BSHELL_TKOP_BXOR);
|
|
ENUM_STR(BSHELL_TKOP_BNOT);
|
|
ENUM_STR(BSHELL_TKOP_SHL);
|
|
ENUM_STR(BSHELL_TKOP_SHR);
|
|
ENUM_STR(BSHELL_TKOP_EQ);
|
|
ENUM_STR(BSHELL_TKOP_NE);
|
|
ENUM_STR(BSHELL_TKOP_GT);
|
|
ENUM_STR(BSHELL_TKOP_LT);
|
|
ENUM_STR(BSHELL_TKOP_GE);
|
|
ENUM_STR(BSHELL_TKOP_LE);
|
|
ENUM_STR(BSHELL_TKOP_MATCH);
|
|
ENUM_STR(BSHELL_TKOP_NOTMATCH);
|
|
ENUM_STR(BSHELL_TKOP_REPLACE);
|
|
ENUM_STR(BSHELL_TKOP_LIKE);
|
|
ENUM_STR(BSHELL_TKOP_NOTLIKE);
|
|
ENUM_STR(BSHELL_TKOP_IN);
|
|
ENUM_STR(BSHELL_TKOP_F);
|
|
ENUM_STR(BSHELL_TKOP_NOTIN);
|
|
ENUM_STR(BSHELL_TKOP_CONTAINS);
|
|
ENUM_STR(BSHELL_TKOP_NOTCONTAINS);
|
|
ENUM_STR(BSHELL_TKOP_AND);
|
|
ENUM_STR(BSHELL_TKOP_OR);
|
|
ENUM_STR(BSHELL_TKOP_XOR);
|
|
ENUM_STR(BSHELL_TKOP_NOT);
|
|
ENUM_STR(BSHELL_TKOP_SPLIT);
|
|
ENUM_STR(BSHELL_TKOP_JOIN);
|
|
ENUM_STR(BSHELL_TKOP_IS);
|
|
ENUM_STR(BSHELL_TKOP_ISNOT);
|
|
ENUM_STR(BSHELL_TKOP_AS);
|
|
default:
|
|
return "<unknown>";
|
|
}
|
|
}
|