- 将依赖项从libcore重命名为scc_core - 更新头文件包含路径从<libcore.h>到<scc_core.h> - 保持原有功能不变 refactor(lexer): 重命名libcore为scc_core并添加词法流式解析功能 - 将依赖项从libcore重命名为scc_core - 移除不再需要的scc_lexer_token结构体定义 - 重命名struct cc_lexer为struct scc_lexer - 添加scc_lexer_stream_t流式解析器相关定义和实现 - 新增lexer_stream.c文件实现流式token缓冲功能 refactor(lexer_log): 重命名logger变量和头文件定义 - 将头文件保护宏从__SMCC_LEXER_LOG_H__改为__SCC_LEXER_LOG_H__ - 将logger变量从__smcc_lexer_log改为__scc_lexer_log - 更新头文件包含从<libcore.h>到<scc_core.h> refactor(lexer_token): 重新组织token头文件结构 - 将头文件保护宏从__SMCC_CC_TOKEN_H__改为__SCC_LEXER_TOKEN_H__ - 更新头文件包含从<libcore.h>到<scc_core.h> - 将scc_lexer_token结构体定义移至该文件 refactor(lexer): 简化token匹配代码格式 - 移除LCC相关的注释内容 - 优化括号符号的token匹配代码格式,使用clang-format控制 refactor(pprocessor): 更新依赖项名称和头文件包含 - 将libcore重命名为scc_core - 将libutils重命名为scc_utils - 更新头文件包含路径 refactor(runtime): 重命名libcore为scc_core并重构目录结构 - 将libcore目录重命名为scc_core - 将libutils目录重命名为scc_utils - 更新所有相关的头文件包含路径 - 修改cbuild.toml中的包名称 - 更新core_vec.h中的宏定义以支持标准库模式
105 lines
2.9 KiB
C
105 lines
2.9 KiB
C
/**
|
||
* @file lexer.h
|
||
* @brief C语言词法分析器核心数据结构与接口
|
||
*/
|
||
|
||
#ifndef __SCC_LEXER_H__
|
||
#define __SCC_LEXER_H__
|
||
|
||
#include "lexer_token.h"
|
||
#include <scc_core.h>
|
||
|
||
/**
|
||
* @brief 词法分析器核心结构体
|
||
*
|
||
* 封装词法分析所需的状态信息和缓冲区管理
|
||
*/
|
||
typedef struct scc_lexer {
|
||
scc_probe_stream_t *stream;
|
||
scc_pos_t pos;
|
||
} scc_lexer_t;
|
||
|
||
/**
|
||
* @brief 初始化词法分析器
|
||
* @param[out] lexer 要初始化的词法分析器实例
|
||
* @param[in] stream 输入流对象指针
|
||
*/
|
||
void scc_lexer_init(scc_lexer_t *lexer, scc_probe_stream_t *stream);
|
||
|
||
/**
|
||
* @brief 获取原始token
|
||
* @param[in] lexer 词法分析器实例
|
||
* @param[out] token 输出token存储位置
|
||
*
|
||
* 此函数会返回所有类型的token,包括空白符等无效token
|
||
*/
|
||
void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token);
|
||
|
||
/**
|
||
* @brief 获取有效token
|
||
* @param[in] lexer 词法分析器实例
|
||
* @param[out] token 输出token存储位置
|
||
*
|
||
* 此函数会自动跳过空白符等无效token,返回对语法分析有意义的token
|
||
*/
|
||
void scc_lexer_get_valid_token(scc_lexer_t *lexer, scc_lexer_tok_t *token);
|
||
|
||
typedef SCC_VEC(scc_lexer_tok_t) scc_lexer_tok_vec_t;
|
||
|
||
typedef struct scc_lexer_stream scc_lexer_stream_t;
|
||
struct scc_lexer_stream {
|
||
scc_lexer_t *lexer;
|
||
scc_lexer_tok_vec_t toks; // 循环缓冲区
|
||
usize curr_pos; // 当前读取位置(逻辑位置)
|
||
usize probe_pos; // 已填充位置(逻辑位置)
|
||
cbool need_comment;
|
||
|
||
/// @brief 向前读取n个token
|
||
const scc_lexer_tok_t *(*peek)(scc_lexer_stream_t *stream, usize n);
|
||
|
||
/// @brief 指针推进到offset
|
||
void (*advance)(scc_lexer_stream_t *stream, usize offset);
|
||
|
||
/// @brief 销毁并释放资源
|
||
void (*drop)(scc_lexer_stream_t *stream);
|
||
};
|
||
|
||
/**
|
||
* @brief 将词法分析器转换成流式输出(自带缓冲区)
|
||
* @param[in] lexer 已经词法分析器实例
|
||
* @param[out] stream 输出流对象指针
|
||
* @param[in] need_comment 输出时是否需要注释
|
||
*/
|
||
void scc_lexer_to_stream(scc_lexer_t *lexer, scc_lexer_stream_t *stream,
|
||
cbool need_comment);
|
||
|
||
static inline const scc_lexer_tok_t *
|
||
scc_lexer_stream_current(scc_lexer_stream_t *stream) {
|
||
Assert(stream != null);
|
||
return stream->peek(stream, 0);
|
||
}
|
||
|
||
static inline const scc_lexer_tok_t *
|
||
scc_lexer_stream_peek(scc_lexer_stream_t *stream, usize n) {
|
||
Assert(stream != null);
|
||
return stream->peek(stream, n);
|
||
}
|
||
|
||
static inline void scc_lexer_stream_consume(scc_lexer_stream_t *stream) {
|
||
Assert(stream != null);
|
||
return stream->advance(stream, 1);
|
||
}
|
||
|
||
static inline void scc_lexer_stream_advance(scc_lexer_stream_t *stream,
|
||
usize n) {
|
||
Assert(stream != null);
|
||
return stream->advance(stream, n);
|
||
}
|
||
|
||
static inline void scc_lexer_stream_drop(scc_lexer_stream_t *stream) {
|
||
Assert(stream != null);
|
||
return stream->drop(stream);
|
||
}
|
||
|
||
#endif /* __SCC_LEXER_H__ */
|