lang: ast: implement parsing of f-strings
This commit is contained in:
@@ -416,6 +416,29 @@ enum ivy_status expr_finalise_arith(
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_fstring(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_type == EXPR_TYPE_STMT) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
if (state->s_prev_component == EXPR_CMP_OPERAND) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_type = EXPR_TYPE_ARITH;
|
||||
state->s_prev_component = EXPR_CMP_OPERAND;
|
||||
state->s_prev_token = tok->t_type;
|
||||
|
||||
parser_push_state(ctx, IVY_AST_FSTRING, 0);
|
||||
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_operand(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
@@ -601,6 +624,47 @@ struct token_parse_result arith_parse_left_paren(
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_right_paren(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_type != EXPR_TYPE_ARITH) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
if (state->s_paren_depth == 0 && state->s_sub_type == EXPR_SUBTYPE_NONE) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_RIGHT_PAREN;
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_left_brace(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0);
|
||||
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_right_brace(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_terminator == IVY_SYM_RIGHT_BRACE) {
|
||||
state->s_prev_token = IVY_SYM_RIGHT_BRACE;
|
||||
struct token_parse_result result = expr_finalise_and_return(ctx, state);
|
||||
result.r_flags = PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
|
||||
return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0);
|
||||
}
|
||||
|
||||
static struct ivy_ast_selector_node *keyword_selector_from_label_list(b_queue *labels)
|
||||
{
|
||||
struct ivy_ast_selector_node *sel
|
||||
@@ -693,24 +757,6 @@ static struct ivy_ast_msg_node *expr_finalise_complex_msg(
|
||||
return msg;
|
||||
}
|
||||
|
||||
struct token_parse_result arith_parse_right_paren(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (state->s_type != EXPR_TYPE_ARITH) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
if (state->s_paren_depth == 0 && state->s_sub_type == EXPR_SUBTYPE_NONE) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_RIGHT_PAREN;
|
||||
return expr_finalise_and_return(ctx, state);
|
||||
}
|
||||
|
||||
static enum ivy_status begin_cascade_operation(struct ivy_parser *ctx)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
|
||||
@@ -26,12 +26,15 @@ struct ast_node_type expr_node_ops = {
|
||||
TOK_PARSER(DOUBLE, arith_parse_operand),
|
||||
TOK_PARSER(ATOM, arith_parse_operand),
|
||||
TOK_PARSER(STRING, arith_parse_operand),
|
||||
TOK_PARSER(STR_START, arith_parse_fstring),
|
||||
TOK_PARSER(SYMBOL, arith_parse_operator),
|
||||
TOK_PARSER(LABEL, arith_parse_label),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(LEFT_PAREN, arith_parse_left_paren),
|
||||
SYM_PARSER(RIGHT_PAREN, arith_parse_right_paren),
|
||||
SYM_PARSER(LEFT_BRACE, arith_parse_left_brace),
|
||||
SYM_PARSER(RIGHT_BRACE, arith_parse_right_brace),
|
||||
SYM_PARSER(SEMICOLON, arith_parse_semicolon),
|
||||
SYM_PARSER(UNDERSCORE, arith_parse_operand),
|
||||
SYM_PARSER(CARET, arith_parse_caret),
|
||||
|
||||
@@ -106,6 +106,8 @@ extern enum ivy_status arith_add_child(
|
||||
|
||||
extern struct token_parse_result arith_parse_ident(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_fstring(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_operand(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_operator(
|
||||
@@ -116,6 +118,10 @@ extern struct token_parse_result arith_parse_left_paren(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_right_paren(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_left_brace(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_right_brace(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_semicolon(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result arith_parse_dot(
|
||||
|
||||
Reference in New Issue
Block a user