2026-05-10 14:18:46 +01:00
|
|
|
#include "lex-internal.h"
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status string_symbol(struct bshell_lex_ctx *ctx)
|
2026-05-10 14:18:46 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
const struct bshell_lex_token_definition *sym = NULL;
|
2026-05-10 14:18:46 +01:00
|
|
|
enum bshell_status status = read_symbol(ctx, &sym);
|
|
|
|
|
|
|
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 23:57:35 +01:00
|
|
|
handle_lex_state_transition(ctx, sym->id);
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = NULL;
|
2026-05-10 14:18:46 +01:00
|
|
|
|
|
|
|
|
switch (sym->id) {
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_DQUOTE:
|
2026-05-10 14:18:46 +01:00
|
|
|
return BSHELL_SUCCESS;
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
|
2026-05-12 22:48:08 +01:00
|
|
|
return push_symbol(ctx, sym->id);
|
2026-05-25 10:33:29 +01:00
|
|
|
case BSHELL_SYM_DOLLAR:
|
|
|
|
|
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
2026-05-10 14:18:46 +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-10 14:18:46 +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-10 14:18:46 +01:00
|
|
|
if (status != BSHELL_SUCCESS) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return status;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status string_content(struct bshell_lex_ctx *ctx)
|
2026-05-10 14:18:46 +01:00
|
|
|
{
|
|
|
|
|
fx_wchar c = FX_WCHAR_INVALID;
|
|
|
|
|
fx_string *temp = lex_state_get_tempstr(ctx);
|
2026-05-10 19:13:29 +01:00
|
|
|
set_token_start(ctx);
|
2026-05-10 14:18:46 +01:00
|
|
|
fx_string_clear(temp);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
c = peek_char(ctx);
|
|
|
|
|
if (c == FX_WCHAR_INVALID) {
|
|
|
|
|
/* EOF without end of string */
|
|
|
|
|
ctx->lex_status = BSHELL_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (char_can_begin_symbol(ctx, c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fx_string_append_wc(temp, c);
|
2026-05-10 19:13:29 +01:00
|
|
|
set_token_end(ctx);
|
2026-05-10 14:18:46 +01:00
|
|
|
advance_char(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fx_string_get_size(temp, FX_STRLEN_NORMAL) == 0) {
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = bshell_lex_token_create_with_string(
|
|
|
|
|
BSHELL_TOK_STRING,
|
2026-05-10 14:18:46 +01:00
|
|
|
fx_string_get_cstr(temp));
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status string_begin(struct bshell_lex_ctx *ctx)
|
2026-05-10 14:18:46 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_STR_START);
|
2026-05-10 14:18:46 +01:00
|
|
|
if (!tok) {
|
|
|
|
|
return BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status string_end(struct bshell_lex_ctx *ctx)
|
2026-05-10 14:18:46 +01:00
|
|
|
{
|
2026-05-25 10:33:29 +01:00
|
|
|
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_STR_END);
|
2026-05-10 14:18:46 +01:00
|
|
|
if (!tok) {
|
|
|
|
|
return BSHELL_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enqueue_token(ctx, tok);
|
|
|
|
|
return BSHELL_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static enum bshell_status string_pump_token(struct bshell_lex_ctx *ctx)
|
2026-05-10 14:18:46 +01:00
|
|
|
{
|
|
|
|
|
fx_wchar c = peek_char(ctx);
|
|
|
|
|
|
|
|
|
|
if (char_can_begin_symbol(ctx, c)) {
|
|
|
|
|
return string_symbol(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string_content(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 10:33:29 +01:00
|
|
|
static const struct bshell_lex_state_link links[] = {
|
|
|
|
|
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
|
|
|
|
LINK_POP(BSHELL_SYM_DQUOTE),
|
2026-05-11 23:57:35 +01:00
|
|
|
LINK_END,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const unsigned int symbols[] = {
|
2026-05-25 10:33:29 +01:00
|
|
|
BSHELL_SYM_DOLLAR,
|
|
|
|
|
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
|
|
|
|
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
|
|
|
|
BSHELL_SYM_DQUOTE,
|
|
|
|
|
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_string_state = {
|
|
|
|
|
.s_id = BSHELL_LEX_STATE_STRING,
|
2026-05-10 14:18:46 +01:00
|
|
|
.s_begin = string_begin,
|
|
|
|
|
.s_end = string_end,
|
|
|
|
|
.s_pump_token = string_pump_token,
|
2026-05-11 23:57:35 +01:00
|
|
|
.s_links = links,
|
2026-05-12 22:48:08 +01:00
|
|
|
.s_symbols = symbols,
|
2026-05-10 14:18:46 +01:00
|
|
|
};
|