- 新增 pproc_expand.h 头文件,定义宏展开相关的数据结构和函数接口 - 重命名宏相关类型和函数,将 scc_pp_* 前缀统一改为 scc_pproc_* - 修改宏参数解析逻辑,支持更灵活的参数处理方式 - 实现完整的宏展开功能,包括对象宏和函数宏的展开 - 添加字符串化操作符 (#) 的支持 - 改进预处理器主循环逻辑,优化宏展开流程 - 更新单元测试用例,增加对宏参数解析和字符串化的测试
60 lines
2.0 KiB
C
60 lines
2.0 KiB
C
/**
|
||
* @file pprocessor.h
|
||
* @brief C语言预处理器核心数据结构与接口
|
||
*/
|
||
|
||
#ifndef __SCC_PPROC_H__
|
||
#define __SCC_PPROC_H__
|
||
|
||
#include "pproc_macro.h"
|
||
#include <scc_core.h>
|
||
#include <scc_core_ring.h>
|
||
#include <scc_lexer.h>
|
||
|
||
// 预处理器状态结构
|
||
|
||
// 条件编译状态栈
|
||
typedef struct {
|
||
int active; // 当前层级是否有效(即应该输出 token)
|
||
int skip; // 当前层级是否跳过(即不输出 token)
|
||
// 可根据需要增加状态,如 #if 的结果、#elif 已执行等
|
||
} scc_pproc_if_state_t;
|
||
typedef SCC_VEC(scc_pproc_if_state_t) scc_pproc_if_stack_t;
|
||
|
||
// 文件包含栈
|
||
typedef struct {
|
||
scc_lexer_t *lexer; // 当前文件的 lexer
|
||
scc_lexer_tok_ring_t *tok_ring; // 当前文件的 token 环(由 lexer 提供)
|
||
// 可能还需要保存当前位置等
|
||
} scc_pproc_file_state_t;
|
||
typedef SCC_VEC(scc_pproc_file_state_t) scc_pproc_file_stack_t;
|
||
typedef SCC_VEC(scc_lexer_tok_ring_t *) scc_pproc_ring_vec_t;
|
||
|
||
typedef struct scc_pproc {
|
||
scc_lexer_tok_ring_t *cur_ring;
|
||
scc_lexer_tok_ring_t expanded_ring;
|
||
scc_strpool_t strpool;
|
||
int at_line_start;
|
||
|
||
scc_pproc_macro_table_t macro_table;
|
||
scc_pproc_if_stack_t if_stack;
|
||
scc_pproc_file_stack_t file_stack;
|
||
|
||
scc_lexer_tok_ring_t ring;
|
||
int ring_ref_count;
|
||
} scc_pproc_t;
|
||
|
||
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input);
|
||
scc_lexer_tok_ring_t *scc_pproc_to_ring(scc_pproc_t *pp, int ring_size);
|
||
void scc_pproc_drop(scc_pproc_t *pp);
|
||
|
||
void scc_pproc_handle_directive(scc_pproc_t *pp);
|
||
void scc_pproc_expand_by_src(scc_pproc_t *pp, const scc_pproc_macro_t *macro);
|
||
void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
|
||
scc_lexer_tok_vec_t *args, int need_full);
|
||
void scc_pproc_parse_function_macro(scc_pproc_t *pp,
|
||
const scc_lexer_tok_t *ident);
|
||
void scc_pproc_parse_object_macro(scc_pproc_t *pp,
|
||
const scc_lexer_tok_t *ident);
|
||
#endif /* __SCC_PPROC_H__ */
|