feat(lexer): 添加预处理器关键字支持并优化词法分析器
添加了完整的C预处理器关键字表,包括define、include、ifdef等关键字, 用于支持预处理器功能。 - 新增SCC_PPKEYWORD_TABLE宏定义所有预处理器关键字 - 在token类型枚举中包含预处理关键字 - 重构词法分析器以正确识别预处理关键字 - 添加scc_lexer_tok_drop函数用于清理token资源 refactor(lexer): 重构词法分析器内部结构 - 修复keywords数组字段名从tok到tok_type - 优化scc_lexer_get_valid_token使用while循环替代do-while - 修改fill_token和fill_valid_token返回类型为cbool - 调整lexer_to_ring参数语义更清晰 fix(sstream): 修正环形缓冲区填充函数返回类型 - 将fill_func返回类型从int改为cbool以保持一致性 - 更新SCC_RING宏文档说明fill回调函数返回值含义 docs(argparse): 重命名examples目录修复路径错误 - 将libs/argparse/example重命名为libs/argparse/examples保持一致性 test(lexer): 更新测试用例适配新的流接口 - 修改测试代码中的scc_sstream_ref_ring为scc_sstream_to_ring - 确保测试用例与新的API保持兼容 style(lexer): 更新示例程序日志级别和实现方式 - 将调试日志改为信息日志 - 使用环形缓冲区实现示例程序的token获取
This commit is contained in:
@@ -10,6 +10,28 @@ typedef enum scc_cstd {
|
||||
SCC_CEXT_SCC,
|
||||
} scc_cstd_t;
|
||||
|
||||
/* clang-format off */
|
||||
/// https://cppreference.cn/w/c/preprocessor
|
||||
#define SCC_PPKEYWORD_TABLE \
|
||||
X(define , SCC_CSTD_C99, SCC_PP_TOK_DEFINE ) \
|
||||
X(elif , SCC_CSTD_C99, SCC_PP_TOK_ELIF ) \
|
||||
X(elifdef , SCC_CSTD_C99, SCC_PP_TOK_ELIFDEF ) \
|
||||
X(elifndef , SCC_CSTD_C99, SCC_PP_TOK_ELIFNDEF ) \
|
||||
X(else , SCC_CSTD_C99, SCC_PP_TOK_ELSE ) \
|
||||
X(embed , SCC_CSTD_C99, SCC_PP_TOK_EMBED ) \
|
||||
X(endif , SCC_CSTD_C99, SCC_PP_TOK_ENDIF ) \
|
||||
X(error , SCC_CSTD_C99, SCC_PP_TOK_ERROR ) \
|
||||
X(if , SCC_CSTD_C99, SCC_PP_TOK_IF ) \
|
||||
X(ifdef , SCC_CEXT_SCC, SCC_PP_TOK_IFDEF ) \
|
||||
X(ifndef , SCC_CSTD_C99, SCC_PP_TOK_IFNDEF ) \
|
||||
X(include , SCC_CSTD_C99, SCC_PP_TOK_INCLUDE ) \
|
||||
X(line , SCC_CEXT_SCC, SCC_PP_TOK_LINE ) \
|
||||
X(pragma , SCC_CSTD_C99, SCC_PP_TOK_PRAGMA ) \
|
||||
X(undef , SCC_CEXT_SCC, SCC_PP_TOK_UNDEF ) \
|
||||
X(warning , SCC_CSTD_C99, SCC_PP_TOK_WARNING ) \
|
||||
// END
|
||||
/* clang-format on */
|
||||
|
||||
/* clang-format off */
|
||||
// WARNING: Using Binary Search To Fast Find Keyword
|
||||
// 你必须确保其中是按照字典序排列
|
||||
@@ -118,14 +140,17 @@ typedef enum scc_cstd {
|
||||
|
||||
// 定义TokenType枚举
|
||||
typedef enum scc_tok_type {
|
||||
// 处理普通token
|
||||
|
||||
#define X(str, subtype, tok) tok,
|
||||
SCC_CTOK_TABLE
|
||||
#undef X
|
||||
|
||||
// 处理关键字(保持原有格式)
|
||||
#define X(name, type, tok) tok,
|
||||
SCC_PPKEYWORD_TABLE
|
||||
#undef X
|
||||
|
||||
#define X(name, subtype, tok, std) tok,
|
||||
SCC_CKEYWORD_TABLE
|
||||
SCC_CKEYWORD_TABLE
|
||||
#undef X
|
||||
} scc_tok_type_t;
|
||||
|
||||
@@ -154,6 +179,10 @@ typedef struct scc_lexer_token {
|
||||
scc_pos_t loc;
|
||||
} scc_lexer_tok_t;
|
||||
|
||||
static inline void scc_lexer_tok_drop(scc_lexer_tok_t *tok) {
|
||||
scc_cstring_free(&tok->lexeme);
|
||||
}
|
||||
|
||||
static inline cbool scc_lexer_tok_match(const scc_lexer_tok_t *tok,
|
||||
scc_tok_type_t type) {
|
||||
return tok->type == type;
|
||||
|
||||
Reference in New Issue
Block a user