feat(parser): 启用parser和ast模块并重构解析器结构
- 在cbuild.toml中启用parser和ast依赖项 - 将AST内置类型枚举重命名为SCC_AST_BUILTIN_TYPE_*前缀格式 - 修复ast_def.h中的类型字段命名,将builtin改为type - 添加逗号操作符支持到表达式操作符枚举中 - 更新字面量表达式的lexeme字段为const char*指针和owned标志 - 重构解析器头文件结构,分离为parser.h、parser_utils.h、scc_sema.h等 - 实现新的解析器工具函数,包括预览、消费、回溯等功能 - 更新声明解析逻辑,使用新的解析器接口进行token处理 - 添加符号表语义分析功能框架 - 修复词法分析器中token移动时的空指针检查 - 统一使用scc_tree_dump_printf替代直接的scc_printf调用
This commit is contained in:
@@ -15,21 +15,10 @@
|
||||
|
||||
// #define ANSI_FMT
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_COLORED(ctx, color, before_str, fmt, after_str, \
|
||||
...) \
|
||||
scc_printf(before_str "%s" fmt "%s" after_str, \
|
||||
ctx->use_color ? color : "", ##__VA_ARGS__, \
|
||||
ctx->use_color ? ANSI_NONE : "");
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_AROUND(ctx, color, around_str, fmt, ...) \
|
||||
SCC_TREE_DUMP_PRINT_COLORED(ctx, color, around_str, fmt, around_str, \
|
||||
##__VA_ARGS__)
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_PURE(ctx, color, fmt, ...) \
|
||||
SCC_TREE_DUMP_PRINT_COLORED(ctx, color, "", fmt, "", ##__VA_ARGS__)
|
||||
|
||||
typedef SCC_VEC(cbool) scc_ast_dump_stack_t;
|
||||
|
||||
typedef void (*scc_tree_dump_output_t)(void *userdata, const char *fmt, ...);
|
||||
|
||||
typedef struct {
|
||||
scc_ast_dump_stack_t stack; ///< 每层是否为最后子节点
|
||||
cbool use_color; ///< 是否使用颜色输出
|
||||
@@ -43,10 +32,15 @@ typedef struct {
|
||||
const char *value_color; ///< 值颜色
|
||||
const char *branch_color; ///< 分支符号颜色
|
||||
const char *reset_color; ///< 重置颜色
|
||||
|
||||
scc_tree_dump_output_t output_func;
|
||||
void *output_userdata;
|
||||
} scc_tree_dump_ctx_t;
|
||||
|
||||
static inline void scc_tree_dump_ctx_init(scc_tree_dump_ctx_t *ctx,
|
||||
cbool use_color) {
|
||||
cbool use_color,
|
||||
scc_tree_dump_output_t output_func,
|
||||
void *output_userdata) {
|
||||
ctx->use_color = use_color;
|
||||
scc_vec_init(ctx->stack);
|
||||
|
||||
@@ -59,8 +53,14 @@ static inline void scc_tree_dump_ctx_init(scc_tree_dump_ctx_t *ctx,
|
||||
ctx->value_color = use_color ? SCC_TREE_DUMP_VALUE_COLOR : "";
|
||||
ctx->branch_color = use_color ? SCC_TREE_DUMP_BRANCH_COLOR : "";
|
||||
ctx->reset_color = use_color ? SCC_TREE_DUMP_RESET_COLOR : "";
|
||||
|
||||
ctx->output_func = output_func;
|
||||
ctx->output_userdata = output_userdata;
|
||||
}
|
||||
|
||||
#define scc_tree_dump_printf(ctx, fmt, ...) \
|
||||
(ctx)->output_func((ctx)->output_userdata, fmt, ##__VA_ARGS__)
|
||||
|
||||
static inline void scc_tree_dump_ctx_drop(scc_tree_dump_ctx_t *ctx) {
|
||||
scc_vec_free(ctx->stack);
|
||||
}
|
||||
@@ -78,13 +78,28 @@ static void scc_tree_print_indent(scc_tree_dump_ctx_t *ctx) {
|
||||
}
|
||||
Assert(data != null);
|
||||
if (ctx->use_color) {
|
||||
scc_printf("%s%s%s", ctx->branch_color, data, ctx->reset_color);
|
||||
ctx->output_func(ctx->output_userdata, "%s%s%s", ctx->branch_color,
|
||||
data, ctx->reset_color);
|
||||
} else {
|
||||
scc_printf("%s", data);
|
||||
ctx->output_func(ctx->output_userdata, "%s", data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_COLORED(ctx, color, before_str, fmt, after_str, \
|
||||
...) \
|
||||
(ctx)->output_func((ctx)->output_userdata, \
|
||||
before_str "%s" fmt "%s" after_str, \
|
||||
(ctx)->use_color ? color : "", ##__VA_ARGS__, \
|
||||
(ctx)->use_color ? ANSI_NONE : "");
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_AROUND(ctx, color, around_str, fmt, ...) \
|
||||
SCC_TREE_DUMP_PRINT_COLORED(ctx, color, around_str, fmt, around_str, \
|
||||
##__VA_ARGS__)
|
||||
|
||||
#define SCC_TREE_DUMP_PRINT_PURE(ctx, color, fmt, ...) \
|
||||
SCC_TREE_DUMP_PRINT_COLORED(ctx, color, "", fmt, "", ##__VA_ARGS__)
|
||||
|
||||
// 推入新的层级到栈中
|
||||
static inline void scc_tree_dump_push_level(scc_tree_dump_ctx_t *ctx,
|
||||
cbool is_last_child) {
|
||||
|
||||
Reference in New Issue
Block a user