feat(lex_parser, pprocessor): rename identifier header check and add macro system

- Rename `scc_lex_parse_is_identifier_header` to `scc_lex_parse_is_identifier_prefix` for clarity and add a TODO comment
- Update lexer to use the renamed function for consistency
- Fix package and dependency names in `cbuild.toml` (`smcc_pprocesser` → `scc_pprocesser`, `smcc_lex_parser` → `lex_parser`)
- Introduce new macro system with header file `pp_macro.h` defining macro types, structures, and management functions
- Refactor preprocessor initialization and cleanup in `pprocessor.c` to use new macro table and stream handling
- Replace legacy `hashmap` with `scc_pp_macro_table_t` for macro storage
- Improve error handling and resource management in preprocessor lifecycle
This commit is contained in:
zzy
2025-12-13 16:09:46 +08:00
parent 874a58281f
commit 07a76d82f4
16 changed files with 970 additions and 490 deletions

View File

@@ -0,0 +1,90 @@
#ifndef __SCC_PP_MACRO_H__
#define __SCC_PP_MACRO_H__
#include <libcore.h>
#include <libutils.h>
// 宏定义类型
typedef enum {
SCC_PP_MACRO_OBJECT, // 对象宏
SCC_PP_MACRO_FUNCTION, // 函数宏
} scc_pp_macro_type_t;
typedef SCC_VEC(scc_cstring_t) scc_pp_macro_list_t;
// 宏定义结构
typedef struct scc_macro {
scc_cstring_t name; // 宏名称
scc_pp_macro_type_t type; // 宏类型
scc_pp_macro_list_t replaces; // 替换列表
scc_pp_macro_list_t params; // 参数列表(仅函数宏)
} scc_pp_macro_t;
typedef struct scc_macro_table {
scc_hashtable_t table; // 宏定义表
} scc_macro_table_t;
/**
* @brief 创建宏对象
* @param name 宏名称
* @param type 宏类型
* @return 创建的宏对象指针失败返回NULL
*/
scc_pp_macro_t *scc_pp_macro_create(const scc_cstring_t *name,
scc_pp_macro_type_t type);
/**
* @brief 销毁宏对象
* @param macro 要销毁的宏对象
*/
void scc_pp_macro_drop(scc_pp_macro_t *macro);
/**
* @brief 压缩空白字符
* @param tokens token列表
* @return 压缩后的字符串
*/
scc_cstring_t scc_pp_compress_whitespace(const scc_pp_macro_list_t *tokens);
/**
* @brief 添加对象宏
* @param pp 预处理器实例
* @param name 宏名称
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_object_macro(scc_macro_table_t *pp, const scc_cstring_t *name,
const scc_pp_macro_list_t *replacement);
/**
* @brief 添加函数宏
* @param pp 预处理器实例
* @param name 宏名称
* @param params 参数列表
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_function_macro(scc_macro_table_t *pp,
const scc_cstring_t *name,
const scc_pp_macro_list_t *params,
const scc_pp_macro_list_t *replacement);
/**
* @brief 查找宏定义
* @param pp 预处理器实例
* @param name 宏名称
* @return 找到的宏对象指针未找到返回NULL
*/
scc_pp_macro_t *scc_pp_find_macro(scc_macro_table_t *pp, scc_cstring_t *name);
/**
* @brief 从预处理器中删除宏
* @param pp 预处理器实例
* @param name 宏名称
* @return 成功删除返回true未找到返回false
*/
cbool scc_pp_remove_macro(scc_macro_table_t *pp, const scc_cstring_t *name);
void scc_pp_marco_table_init(scc_macro_table_t *macros);
void scc_pp_macro_table_drop(scc_macro_table_t *macros);
#endif /* __SCC_PP_MACRO_H__ */

View File

@@ -0,0 +1,11 @@
#ifndef __SCC_PP_PARSE_H__
#define __SCC_PP_PARSE_H__
#include <libcore.h>
#include <pp_macro.h>
void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos,
scc_macro_table_t *macros);
cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_macro_table_t *macros,
scc_probe_stream_t **out_stream, int depth);
#endif /* __SCC_PP_PARSE_H__ */

View File

@@ -1,30 +1,30 @@
#ifndef __SMCC_PP_TOKEN_H__
#define __SMCC_PP_TOKEN_H__
#ifndef __SCC_PP_TOKEN_H__
#define __SCC_PP_TOKEN_H__
/* clang-format off */
/// https://cppreference.cn/w/c/preprocessor
#define PP_INST_TOKEN \
X(define , PP_STD, PP_TOK_DEFINE ) \
X(undef , PP_STD, PP_TOK_UNDEF ) \
X(include , PP_STD, PP_TOK_INCLUDE ) \
X(if , PP_STD, PP_TOK_IF ) \
X(ifdef , PP_STD, PP_TOK_IFDEF ) \
X(ifndef , PP_STD, PP_TOK_IFNDEF ) \
X(else , PP_STD, PP_TOK_ELSE ) \
X(elif , PP_STD, PP_TOK_ELIF ) \
X(elifdef , PP_STD, PP_TOK_ELIFDEF ) \
X(elifndef , PP_C23, PP_TOK_ELIFNDEF ) \
X(endif , PP_STD, PP_TOK_ENDIF ) \
X(line , PP_STD, PP_TOK_LINE ) \
X(embed , PP_C23, PP_TOK_EMBED ) \
X(error , PP_STD, PP_TOK_ERROR ) \
X(warning , PP_C23, PP_TOK_WARNING ) \
X(pragma , PP_STD, PP_TOK_PRAMA ) \
#define SCC_PP_INST_TOKEN \
X(define , SCC_PP_STD, SCC_PP_TOK_DEFINE ) \
X(undef , SCC_PP_STD, SCC_PP_TOK_UNDEF ) \
X(include , SCC_PP_STD, SCC_PP_TOK_INCLUDE ) \
X(if , SCC_PP_STD, SCC_PP_TOK_IF ) \
X(ifdef , SCC_PP_STD, SCC_PP_TOK_IFDEF ) \
X(ifndef , SCC_PP_STD, SCC_PP_TOK_IFNDEF ) \
X(else , SCC_PP_STD, SCC_PP_TOK_ELSE ) \
X(elif , SCC_PP_STD, SCC_PP_TOK_ELIF ) \
X(elifdef , SCC_PP_STD, SCC_PP_TOK_ELIFDEF ) \
X(elifndef , SCC_PP_C23, SCC_PP_TOK_ELIFNDEF ) \
X(endif , SCC_PP_STD, SCC_PP_TOK_ENDIF ) \
X(line , SCC_PP_STD, SCC_PP_TOK_LINE ) \
X(embed , SCC_PP_C23, SCC_PP_TOK_EMBED ) \
X(error , SCC_PP_STD, SCC_PP_TOK_ERROR ) \
X(warning , SCC_PP_C23, SCC_PP_TOK_WARNING ) \
X(pragma , SCC_PP_STD, SCC_PP_TOK_PRAMA ) \
// END
/* clang-format on */
#define X(name, type, tok) tok,
typedef enum pp_token { PP_INST_TOKEN } pp_token_t;
typedef enum scc_pp_token { SCC_PP_INST_TOKEN } scc_pp_token_t;
#undef X
#endif /* __SMCC_PP_TOKEN_H__ */
#endif /* __SCC_PP_TOKEN_H__ */

View File

@@ -1,30 +1,14 @@
// pprocessor.h - 更新后的头文件
/**
* @file pprocessor.h
* @brief C语言预处理器核心数据结构与接口
*/
#ifndef __SMCC_PP_H__
#define __SMCC_PP_H__
#ifndef __SCC_PP_H__
#define __SCC_PP_H__
#include <libcore.h>
#include <libutils.h>
// 宏定义类型
typedef enum {
MACRO_OBJECT, // 对象宏
MACRO_FUNCTION, // 函数宏
} macro_type_t;
typedef VEC(cstring_t) macro_list_t;
// 宏定义结构
typedef struct smcc_macro {
cstring_t name; // 宏名称
macro_type_t type; // 宏类型
macro_list_t replaces; // 替换列表
macro_list_t params; // 参数列表(仅函数宏)
} smcc_macro_t;
#include <pp_macro.h>
// 条件编译状态
typedef enum {
@@ -41,12 +25,12 @@ typedef struct if_stack_item {
} if_stack_item_t;
// 预处理器状态结构
typedef struct smcc_preprocessor {
core_stream_t *stream; // 输出流
strpool_t strpool; // 字符串池
hashmap_t macros; // 宏定义表
VEC(if_stack_item_t) if_stack; // 条件编译栈
} smcc_pp_t;
typedef struct scc_pproc {
scc_probe_stream_t *stream; // 输出流
scc_strpool_t strpool; // 字符串池
scc_macro_table_t macro_table;
SCC_VEC(if_stack_item_t) if_stack; // 条件编译栈
} scc_pproc_t;
/**
* @brief 初始化预处理器
@@ -54,19 +38,25 @@ typedef struct smcc_preprocessor {
* @param[in] input 输入流对象指针
* @return output 输出流对象指针
*/
core_stream_t *pp_init(smcc_pp_t *pp, core_stream_t *input);
/**
* @brief 执行预处理
* @param[in] pp 预处理器实例
* @return 处理结果
*/
int pp_process(smcc_pp_t *pp);
// TODO 内存释放问题
scc_probe_stream_t *scc_pproc_init(scc_pproc_t *pp, scc_probe_stream_t *input);
/**
* @brief 销毁预处理器
* @param[in] pp 预处理器实例
*/
void pp_drop(smcc_pp_t *pp);
void scc_pproc_drop(scc_pproc_t *pp);
#endif /* __SMCC_PP_H__ */
/// inner private struct
typedef SCC_VEC(u8) scc_pp_buffer_t;
typedef struct pp_stream {
scc_probe_stream_t stream;
scc_probe_stream_t *input;
scc_pproc_t *self;
scc_pos_t pos;
scc_probe_stream_t *tmp_stream;
} scc_pp_stream_t;
#endif /* __SMC_PP_H__ */