parse: lex: add flags for lexer states

This commit is contained in:
2026-05-10 19:10:14 +01:00
parent f5d847736a
commit 7071630af8
7 changed files with 39 additions and 19 deletions
+1
View File
@@ -42,6 +42,7 @@ struct lex_state {
unsigned int s_paren_depth; unsigned int s_paren_depth;
fx_queue_entry s_entry; fx_queue_entry s_entry;
fx_string *s_tempstr; fx_string *s_tempstr;
unsigned int s_flags;
}; };
struct lex_ctx { struct lex_ctx {
+7 -4
View File
@@ -22,7 +22,7 @@ static enum bshell_status arithmetic_symbol(struct lex_ctx *ctx)
case SYM_HASH: case SYM_HASH:
return read_line_comment(ctx); return read_line_comment(ctx);
case SYM_DQUOTE: 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_ERR_NO_MEMORY;
} }
@@ -67,10 +67,13 @@ static enum bshell_status arithmetic_symbol(struct lex_ctx *ctx)
switch (sym->id) { switch (sym->id) {
case SYM_LEFT_PAREN: 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; return BSHELL_SUCCESS;
case SYM_DOLLAR_LEFT_PAREN: case SYM_DOLLAR_LEFT_PAREN:
lex_state_push(ctx, LEX_STATE_STATEMENT); lex_state_push(ctx, LEX_STATE_STATEMENT, 0);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
case SYM_RIGHT_PAREN: case SYM_RIGHT_PAREN:
lex_state_pop(ctx); 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) static enum bshell_status arithmetic_word(struct lex_ctx *ctx)
{ {
struct lex_token *word = NULL; 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) { if (status != BSHELL_SUCCESS) {
return status; return status;
} }
+6 -3
View File
@@ -22,7 +22,7 @@ static enum bshell_status command_symbol(struct lex_ctx *ctx)
case SYM_HASH: case SYM_HASH:
return read_line_comment(ctx); return read_line_comment(ctx);
case SYM_DQUOTE: 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_ERR_NO_MEMORY;
} }
@@ -67,10 +67,13 @@ static enum bshell_status command_symbol(struct lex_ctx *ctx)
switch (sym->id) { switch (sym->id) {
case SYM_LEFT_PAREN: 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; return BSHELL_SUCCESS;
case SYM_DOLLAR_LEFT_PAREN: case SYM_DOLLAR_LEFT_PAREN:
lex_state_push(ctx, LEX_STATE_STATEMENT); lex_state_push(ctx, LEX_STATE_STATEMENT, 0);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
case SYM_RIGHT_PAREN: case SYM_RIGHT_PAREN:
lex_state_pop(ctx); lex_state_pop(ctx);
+9 -1
View File
@@ -7,6 +7,10 @@
struct lex_ctx; 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_pump_token)(struct lex_ctx *);
typedef enum bshell_status (*lex_state_begin)(struct lex_ctx *); typedef enum bshell_status (*lex_state_begin)(struct lex_ctx *);
typedef enum bshell_status (*lex_state_end)(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_arithmetic(struct lex_ctx *ctx);
extern enum bshell_status pump_token_string(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( extern struct lex_state *lex_state_push(
struct lex_ctx *ctx, 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 void lex_state_pop(struct lex_ctx *ctx);
extern struct lex_state *lex_state_get(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); extern void lex_state_change(struct lex_ctx *ctx, enum lex_state_type_id type);
+4 -2
View File
@@ -85,7 +85,8 @@ static const struct lex_state_type *state_types[] = {
struct lex_state *lex_state_push( struct lex_state *lex_state_push(
struct lex_ctx *ctx, 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); struct lex_state *state = malloc(sizeof *state);
if (!state) { if (!state) {
@@ -95,6 +96,7 @@ struct lex_state *lex_state_push(
memset(state, 0x0, sizeof *state); memset(state, 0x0, sizeof *state);
state->s_type = state_types[state_type]; state->s_type = state_types[state_type];
state->s_flags = flags;
fx_queue_push_back(&ctx->lex_state, &state->s_entry); fx_queue_push_back(&ctx->lex_state, &state->s_entry);
if (state->s_type->s_begin) { if (state->s_type->s_begin) {
@@ -272,7 +274,7 @@ enum bshell_status lex_ctx_init(
ctx->lex_status = BSHELL_SUCCESS; ctx->lex_status = BSHELL_SUCCESS;
ctx->lex_buf = fx_stringstream_create(); ctx->lex_buf = fx_stringstream_create();
ctx->lex_sym_tree = build_symbol_tree(); 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_src = src;
ctx->lex_ch = FX_WCHAR_INVALID; ctx->lex_ch = FX_WCHAR_INVALID;
+11 -8
View File
@@ -22,13 +22,13 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx)
case SYM_HASH: case SYM_HASH:
return read_line_comment(ctx); return read_line_comment(ctx);
case SYM_DQUOTE: 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_ERR_NO_MEMORY;
} }
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
case SYM_DOLLAR: 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; return BSHELL_ERR_NO_MEMORY;
} }
@@ -40,7 +40,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx)
enqueue_token(ctx, tok); enqueue_token(ctx, tok);
return status; return status;
case SYM_AT: 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; return BSHELL_ERR_NO_MEMORY;
} }
@@ -52,7 +52,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx)
enqueue_token(ctx, tok); enqueue_token(ctx, tok);
return status; return status;
case SYM_DOLLAR_LEFT_BRACE: 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; return BSHELL_ERR_NO_MEMORY;
} }
@@ -64,7 +64,7 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx)
enqueue_token(ctx, tok); enqueue_token(ctx, tok);
return status; return status;
case SYM_AT_LEFT_BRACE: 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; return BSHELL_ERR_NO_MEMORY;
} }
@@ -83,11 +83,14 @@ static enum bshell_status statement_symbol(struct lex_ctx *ctx)
switch (sym->id) { switch (sym->id) {
case SYM_LEFT_PAREN: 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; return BSHELL_SUCCESS;
case SYM_LEFT_BRACE: case SYM_LEFT_BRACE:
case SYM_DOLLAR_LEFT_PAREN: case SYM_DOLLAR_LEFT_PAREN:
lex_state_push(ctx, LEX_STATE_STATEMENT); lex_state_push(ctx, LEX_STATE_STATEMENT, 0);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
case SYM_RIGHT_PAREN: case SYM_RIGHT_PAREN:
case SYM_RIGHT_BRACE: 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) static enum bshell_status statement_word(struct lex_ctx *ctx)
{ {
struct lex_token *word = NULL; 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) { if (status != BSHELL_SUCCESS) {
return status; return status;
} }
+1 -1
View File
@@ -18,7 +18,7 @@ static enum bshell_status string_symbol(struct lex_ctx *ctx)
return status; return status;
} }
lex_state_push(ctx, LEX_STATE_STATEMENT); lex_state_push(ctx, LEX_STATE_STATEMENT, 0);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
case SYM_DQUOTE: case SYM_DQUOTE:
lex_state_pop(ctx); lex_state_pop(ctx);