From 7071630af8ca304958d1f07d79a14fa7749741e2 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 10 May 2026 19:10:14 +0100 Subject: [PATCH] parse: lex: add flags for lexer states --- bshell/parse/lex.h | 1 + bshell/parse/lex/arithmetic.c | 11 +++++++---- bshell/parse/lex/command.c | 9 ++++++--- bshell/parse/lex/lex-internal.h | 10 +++++++++- bshell/parse/lex/lex.c | 6 ++++-- bshell/parse/lex/statement.c | 19 +++++++++++-------- bshell/parse/lex/string.c | 2 +- 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/bshell/parse/lex.h b/bshell/parse/lex.h index 115240a..4b20ae8 100644 --- a/bshell/parse/lex.h +++ b/bshell/parse/lex.h @@ -42,6 +42,7 @@ struct lex_state { unsigned int s_paren_depth; fx_queue_entry s_entry; fx_string *s_tempstr; + unsigned int s_flags; }; struct lex_ctx { diff --git a/bshell/parse/lex/arithmetic.c b/bshell/parse/lex/arithmetic.c index 7bcf56d..63645f9 100644 --- a/bshell/parse/lex/arithmetic.c +++ b/bshell/parse/lex/arithmetic.c @@ -22,7 +22,7 @@ static enum bshell_status arithmetic_symbol(struct lex_ctx *ctx) case SYM_HASH: return read_line_comment(ctx); case SYM_DQUOTE: - if (!lex_state_push(ctx, LEX_STATE_STRING)) { + if (!lex_state_push(ctx, LEX_STATE_STRING, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -67,10 +67,13 @@ static enum bshell_status arithmetic_symbol(struct lex_ctx *ctx) switch (sym->id) { case SYM_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_EXPRESSION); + lex_state_push( + ctx, + LEX_STATE_STATEMENT, + STATEMENT_F_DISABLE_KEYWORDS); return BSHELL_SUCCESS; case SYM_DOLLAR_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_STATEMENT); + lex_state_push(ctx, LEX_STATE_STATEMENT, 0); return BSHELL_SUCCESS; case SYM_RIGHT_PAREN: lex_state_pop(ctx); @@ -88,7 +91,7 @@ static enum bshell_status arithmetic_symbol(struct lex_ctx *ctx) static enum bshell_status arithmetic_word(struct lex_ctx *ctx) { struct lex_token *word = NULL; - enum bshell_status status = read_word(ctx, &word); + enum bshell_status status = read_word(ctx, 0, &word); if (status != BSHELL_SUCCESS) { return status; } diff --git a/bshell/parse/lex/command.c b/bshell/parse/lex/command.c index 62394a8..a32cf76 100644 --- a/bshell/parse/lex/command.c +++ b/bshell/parse/lex/command.c @@ -22,7 +22,7 @@ static enum bshell_status command_symbol(struct lex_ctx *ctx) case SYM_HASH: return read_line_comment(ctx); case SYM_DQUOTE: - if (!lex_state_push(ctx, LEX_STATE_STRING)) { + if (!lex_state_push(ctx, LEX_STATE_STRING, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -67,10 +67,13 @@ static enum bshell_status command_symbol(struct lex_ctx *ctx) switch (sym->id) { case SYM_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_EXPRESSION); + lex_state_push( + ctx, + LEX_STATE_STATEMENT, + STATEMENT_F_DISABLE_KEYWORDS); return BSHELL_SUCCESS; case SYM_DOLLAR_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_STATEMENT); + lex_state_push(ctx, LEX_STATE_STATEMENT, 0); return BSHELL_SUCCESS; case SYM_RIGHT_PAREN: lex_state_pop(ctx); diff --git a/bshell/parse/lex/lex-internal.h b/bshell/parse/lex/lex-internal.h index 76afa0b..348ba90 100644 --- a/bshell/parse/lex/lex-internal.h +++ b/bshell/parse/lex/lex-internal.h @@ -7,6 +7,10 @@ struct lex_ctx; +enum state_flags { + STATEMENT_F_DISABLE_KEYWORDS = 0x01u, +}; + typedef enum bshell_status (*lex_state_pump_token)(struct lex_ctx *); typedef enum bshell_status (*lex_state_begin)(struct lex_ctx *); typedef enum bshell_status (*lex_state_end)(struct lex_ctx *); @@ -24,9 +28,13 @@ extern enum bshell_status pump_token_command(struct lex_ctx *ctx); extern enum bshell_status pump_token_arithmetic(struct lex_ctx *ctx); extern enum bshell_status pump_token_string(struct lex_ctx *ctx); +extern void set_token_start(struct lex_ctx *ctx); +extern void set_token_end(struct lex_ctx *ctx); + extern struct lex_state *lex_state_push( struct lex_ctx *ctx, - enum lex_state_type_id state_type); + enum lex_state_type_id state_type, + enum state_flags flags); extern void lex_state_pop(struct lex_ctx *ctx); extern struct lex_state *lex_state_get(struct lex_ctx *ctx); extern void lex_state_change(struct lex_ctx *ctx, enum lex_state_type_id type); diff --git a/bshell/parse/lex/lex.c b/bshell/parse/lex/lex.c index 945f094..74b6532 100644 --- a/bshell/parse/lex/lex.c +++ b/bshell/parse/lex/lex.c @@ -85,7 +85,8 @@ static const struct lex_state_type *state_types[] = { struct lex_state *lex_state_push( struct lex_ctx *ctx, - enum lex_state_type_id state_type) + enum lex_state_type_id state_type, + enum state_flags flags) { struct lex_state *state = malloc(sizeof *state); if (!state) { @@ -95,6 +96,7 @@ struct lex_state *lex_state_push( memset(state, 0x0, sizeof *state); state->s_type = state_types[state_type]; + state->s_flags = flags; fx_queue_push_back(&ctx->lex_state, &state->s_entry); if (state->s_type->s_begin) { @@ -272,7 +274,7 @@ enum bshell_status lex_ctx_init( ctx->lex_status = BSHELL_SUCCESS; ctx->lex_buf = fx_stringstream_create(); ctx->lex_sym_tree = build_symbol_tree(); - lex_state_push(ctx, LEX_STATE_STATEMENT); + lex_state_push(ctx, LEX_STATE_STATEMENT, 0); ctx->lex_src = src; ctx->lex_ch = FX_WCHAR_INVALID; diff --git a/bshell/parse/lex/statement.c b/bshell/parse/lex/statement.c index 7e8fad7..c655306 100644 --- a/bshell/parse/lex/statement.c +++ b/bshell/parse/lex/statement.c @@ -22,13 +22,13 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) case SYM_HASH: return read_line_comment(ctx); case SYM_DQUOTE: - if (!lex_state_push(ctx, LEX_STATE_STRING)) { + if (!lex_state_push(ctx, LEX_STATE_STRING, 0)) { return BSHELL_ERR_NO_MEMORY; } return BSHELL_SUCCESS; case SYM_DOLLAR: - if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC)) { + if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -40,7 +40,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) enqueue_token(ctx, tok); return status; case SYM_AT: - if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC)) { + if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -52,7 +52,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) enqueue_token(ctx, tok); return status; case SYM_DOLLAR_LEFT_BRACE: - if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC)) { + if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -64,7 +64,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) enqueue_token(ctx, tok); return status; case SYM_AT_LEFT_BRACE: - if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC)) { + if (!lex_state_push(ctx, LEX_STATE_ARITHMETIC, 0)) { return BSHELL_ERR_NO_MEMORY; } @@ -83,11 +83,14 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) switch (sym->id) { case SYM_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_EXPRESSION); + lex_state_push( + ctx, + LEX_STATE_STATEMENT, + STATEMENT_F_DISABLE_KEYWORDS); return BSHELL_SUCCESS; case SYM_LEFT_BRACE: case SYM_DOLLAR_LEFT_PAREN: - lex_state_push(ctx, LEX_STATE_STATEMENT); + lex_state_push(ctx, LEX_STATE_STATEMENT, 0); return BSHELL_SUCCESS; case SYM_RIGHT_PAREN: case SYM_RIGHT_BRACE: @@ -109,7 +112,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx) static enum bshell_status statement_word(struct lex_ctx *ctx) { struct lex_token *word = NULL; - enum bshell_status status = read_word(ctx, &word); + enum bshell_status status = read_word(ctx, 0, &word); if (status != BSHELL_SUCCESS) { return status; } diff --git a/bshell/parse/lex/string.c b/bshell/parse/lex/string.c index 1b986bc..fd4692a 100644 --- a/bshell/parse/lex/string.c +++ b/bshell/parse/lex/string.c @@ -18,7 +18,7 @@ static enum bshell_status string_symbol(struct lex_ctx *ctx) return status; } - lex_state_push(ctx, LEX_STATE_STATEMENT); + lex_state_push(ctx, LEX_STATE_STATEMENT, 0); return BSHELL_SUCCESS; case SYM_DQUOTE: lex_state_pop(ctx);