refactor(lexer): 重构词法分析器头文件结构并优化缓冲区管理
移除了旧的lexer_stream.c实现,引入新的环形缓冲区机制来替代原有的 动态数组缓冲区。更新了词法分析器的核心数据结构,修改了token获取 相关函数的实现以支持新的缓冲区管理方式。 BREAKING CHANGE: 移除了scc_lexer_stream_t相关的API,替换为基于 环形缓冲区的新接口scc_lexer_to_ring和相关函数。 feat(lexer_token): 添加词法分析结果内存泄漏警告注释 docs: 移除预处理器模块的测试文件和相关配置
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
/**
|
||||
* @file lexer.h
|
||||
* @brief C语言词法分析器核心数据结构与接口
|
||||
*/
|
||||
|
||||
#ifndef __SCC_LEXER_H__
|
||||
#define __SCC_LEXER_H__
|
||||
|
||||
#include "lexer_token.h"
|
||||
#include <scc_core.h>
|
||||
#include <scc_sstream.h>
|
||||
|
||||
/**
|
||||
* @brief 词法分析器核心结构体
|
||||
*
|
||||
* 封装词法分析所需的状态信息和缓冲区管理
|
||||
*/
|
||||
typedef struct scc_lexer {
|
||||
scc_sstream_ring_t stream_ref;
|
||||
int jump_macro;
|
||||
} scc_lexer_t;
|
||||
|
||||
void scc_lexer_init(scc_lexer_t *lexer, scc_sstream_ring_t *stream_ref);
|
||||
|
||||
/**
|
||||
* @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__ */
|
||||
@@ -144,6 +144,10 @@ typedef enum scc_tok_subtype {
|
||||
scc_tok_subtype_t scc_get_tok_subtype(scc_tok_type_t type);
|
||||
const char *scc_get_tok_name(scc_tok_type_t type);
|
||||
|
||||
/**
|
||||
* @brief 词法分析结果
|
||||
* @warning 需要手动释放lexeme否则会出现内存泄漏
|
||||
*/
|
||||
typedef struct scc_lexer_token {
|
||||
scc_tok_type_t type;
|
||||
scc_cstring_t lexeme;
|
||||
|
||||
54
libs/lexer/include/scc_lexer.h
Normal file
54
libs/lexer/include/scc_lexer.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @file lexer.h
|
||||
* @brief C语言词法分析器核心数据结构与接口
|
||||
*/
|
||||
|
||||
#ifndef __SCC_LEXER_H__
|
||||
#define __SCC_LEXER_H__
|
||||
|
||||
#include "lexer_token.h"
|
||||
#include <scc_core.h>
|
||||
#include <scc_core_ring.h>
|
||||
#include <scc_sstream.h>
|
||||
|
||||
typedef SCC_RING(scc_lexer_tok_t) scc_lexer_tok_ring_t;
|
||||
typedef SCC_VEC(scc_lexer_tok_t) scc_lexer_tok_vec_t;
|
||||
/**
|
||||
* @brief 词法分析器核心结构体
|
||||
*
|
||||
* 封装词法分析所需的状态信息和缓冲区管理
|
||||
*/
|
||||
typedef struct scc_lexer {
|
||||
scc_sstream_ring_t *stream_ref;
|
||||
scc_lexer_tok_ring_t ring;
|
||||
int ring_ref_count;
|
||||
int jump_macro;
|
||||
} scc_lexer_t;
|
||||
|
||||
void scc_lexer_init(scc_lexer_t *lexer, scc_sstream_ring_t *stream_ref);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
scc_lexer_tok_ring_t *scc_lexer_to_ring(scc_lexer_t *lexer, int ring_size,
|
||||
cbool need_comment);
|
||||
|
||||
void scc_lexer_drop_ring(scc_lexer_tok_ring_t *ring_ref);
|
||||
void scc_lexer_drop(scc_lexer_t *lexer);
|
||||
|
||||
#endif /* __SCC_LEXER_H__ */
|
||||
Reference in New Issue
Block a user