2026-05-11 23:02:02 +01:00
|
|
|
#include "lex-internal.h"
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status hashtable_hyphen(struct bshell_lex_ctx *ctx)
|
2026-05-11 23:02:02 +01:00
|
|
|
{
|
|
|
|
|
fx_wchar c = peek_char(ctx);
|
|
|
|
|
if (!fx_wchar_is_alnum(c)) {
|
2026-05-25 10:33:29 +01:00
|
|
|
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
|
|
|
|
handle_lex_state_transition(ctx, BSHELL_SYM_HYPHEN);
|
2026-05-11 23:02:02 +01:00
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = NULL;
|
2026-05-11 23:02:02 +01:00
|
|
|
enum bshell_status status = read_word(
|
|
|
|
|
ctx,
|
|
|
|
|
READ_NO_SET_TOKEN_START | READ_APPEND_HYPHEN,
|
|
|
|
|
&tok);
|
|
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
unsigned int token_type = BSHELL_TOK_WORD;
|
2026-05-24 20:13:55 +01:00
|
|
|
if (convert_word_to_number(tok)) {
|
|
|
|
|
token_type = tok->tok_type;
|
2026-05-11 23:02:02 +01:00
|
|
|
/* because of APPEND_HYPHEN (which is needed to ensure operator
|
|
|
|
|
* tokens are detected properly), the resulting number will be
|
|
|
|
|
* negative.
|
|
|
|
|
* this token will be preceded by a HYPHEN token, so the number
|
|
|
|
|
* must be positive */
|
2026-05-24 20:13:55 +01:00
|
|
|
fx_value neg = FX_INT(-1);
|
|
|
|
|
fx_value_multiply(&tok->tok_number, &neg, &tok->tok_number);
|
2026-05-25 10:33:29 +01:00
|
|
|
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
2026-05-11 23:02:02 +01:00
|
|
|
} else if (convert_word_to_operator(ctx, tok)) {
|
|
|
|
|
token_type = tok->tok_operator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle_lex_state_transition(ctx, token_type);
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status hashtable_symbol(struct bshell_lex_ctx *ctx)
|
2026-05-11 23:02:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
const struct bshell_lex_token_definition *sym = NULL;
|
2026-05-11 23:02:02 +01:00
|
|
|
enum bshell_status status = read_symbol(ctx, &sym);
|
|
|
|
|
|
|
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle_lex_state_transition(ctx, sym->id);
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = NULL;
|
2026-05-11 23:02:02 +01:00
|
|
|
switch (sym->id) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_SQUOTE:
|
2026-05-11 23:02:02 +01:00
|
|
|
status = read_literal_string(ctx, &tok);
|
|
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return BSHELL_SUCCESS;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_HYPHEN:
|
2026-05-11 23:02:02 +01:00
|
|
|
return hashtable_hyphen(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_HASH:
|
2026-05-11 23:02:02 +01:00
|
|
|
return read_line_comment(ctx);
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_DOLLAR:
|
|
|
|
|
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
2026-05-11 23:02:02 +01:00
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return status;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_AT:
|
|
|
|
|
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
2026-05-11 23:02:02 +01:00
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return status;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
|
|
|
|
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
2026-05-11 23:02:02 +01:00
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return status;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
push_symbol(ctx, sym->id);
|
|
|
|
|
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status hashtable_word(struct bshell_lex_ctx *ctx)
|
2026-05-11 23:02:02 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *word = NULL;
|
2026-05-11 23:02:02 +01:00
|
|
|
enum bshell_status status = read_word(ctx, 0, &word);
|
|
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 20:13:55 +01:00
|
|
|
convert_word_to_number(word);
|
2026-05-11 23:02:02 +01:00
|
|
|
|
|
|
|
|
handle_lex_state_transition(ctx, word->tok_type);
|
|
|
|
|
enqueue_token(ctx, word);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status hashtable_pump_token(struct bshell_lex_ctx *ctx)
|
2026-05-11 23:02:02 +01:00
|
|
|
{
|
|
|
|
|
fx_wchar c = peek_char(ctx);
|
|
|
|
|
bool newline = false;
|
|
|
|
|
|
|
|
|
|
set_token_start(ctx);
|
|
|
|
|
while (fx_wchar_is_space(c)) {
|
|
|
|
|
if (c == '\n') {
|
|
|
|
|
newline = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_token_end(ctx);
|
|
|
|
|
advance_char_noread(ctx);
|
|
|
|
|
c = peek_char_noread(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 23:57:35 +01:00
|
|
|
#if 1
|
2026-05-11 23:02:02 +01:00
|
|
|
if (newline) {
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_LINEFEED);
|
2026-05-11 23:02:02 +01:00
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (char_can_begin_symbol(ctx, c)) {
|
|
|
|
|
return hashtable_symbol(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hashtable_word(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static const struct bshell_lex_state_link links[] = {
|
2026-05-12 22:51:45 +01:00
|
|
|
LINK_PUSH_WITH_TERM(
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_EQUAL,
|
|
|
|
|
BSHELL_LEX_STATE_STATEMENT,
|
2026-05-12 22:51:45 +01:00
|
|
|
0,
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_RIGHT_BRACE,
|
|
|
|
|
BSHELL_SYM_SEMICOLON,
|
|
|
|
|
BSHELL_TOK_LINEFEED),
|
2026-05-12 22:51:45 +01:00
|
|
|
LINK_PUSH_WITH_TERM(
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_TOK_LINEFEED,
|
|
|
|
|
BSHELL_LEX_STATE_STATEMENT,
|
2026-05-12 22:51:45 +01:00
|
|
|
0,
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_SEMICOLON,
|
|
|
|
|
BSHELL_TOK_LINEFEED),
|
|
|
|
|
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
|
2026-05-11 23:02:02 +01:00
|
|
|
LINK_PUSH(
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_LEFT_PAREN,
|
|
|
|
|
BSHELL_LEX_STATE_STATEMENT,
|
2026-05-11 23:02:02 +01:00
|
|
|
STATEMENT_F_DISABLE_KEYWORDS),
|
2026-05-25 10:33:29 +01:00
|
|
|
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
|
|
|
|
LINK_POP2(BSHELL_SYM_RIGHT_BRACE, LINK_ALLOW_RECURSION),
|
2026-05-11 23:02:02 +01:00
|
|
|
LINK_END,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-11 23:57:35 +01:00
|
|
|
static const unsigned int symbols[] = {
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_EQUAL,
|
|
|
|
|
BSHELL_SYM_DQUOTE,
|
|
|
|
|
BSHELL_SYM_SQUOTE,
|
|
|
|
|
BSHELL_SYM_SEMICOLON,
|
|
|
|
|
BSHELL_SYM_RIGHT_BRACE,
|
|
|
|
|
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
|
|
|
|
BSHELL_SYM_LEFT_PAREN,
|
|
|
|
|
BSHELL_SYM_HASH,
|
|
|
|
|
BSHELL_SYM_NONE,
|
2026-05-11 23:57:35 +01:00
|
|
|
};
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
const struct bshell_lex_state_type lex_hashtable_state = {
|
|
|
|
|
.s_id = BSHELL_LEX_STATE_HASHTABLE,
|
2026-05-11 23:02:02 +01:00
|
|
|
.s_pump_token = hashtable_pump_token,
|
|
|
|
|
.s_links = links,
|
2026-05-11 23:57:35 +01:00
|
|
|
.s_symbols = symbols,
|
2026-05-11 23:02:02 +01:00
|
|
|
};
|