refactor(lex_parser): 移除旧的词法解析器实现并更新依赖
移除了 libs/lex_parser 目录下的所有头文件和源文件,包括: - lex_parser.h 和 lex_parser.c 核心解析功能 - 所有测试文件(test_char.c, test_identifier.c, test_number.c, test_skip_block_comment.c, test_skip_line.c, test_string.c) 更新了 lexer 模块的依赖配置,将 lex_parser 替换为 sstream, 同时更新了 lexer.h 中的相关包含头文件和数据结构定义, 简化了 scc_lexer_t 结构体的字段。
This commit is contained in:
@@ -157,40 +157,128 @@ class VectorPrinter(gdb.ValuePrinter):
|
||||
|
||||
|
||||
class HashTablePrinter(gdb.ValuePrinter):
|
||||
def __init__(self, val: gdb.Value):
|
||||
self.val: gdb.Value = val
|
||||
"""打印 scc_hashtable_t 结构"""
|
||||
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
@staticmethod
|
||||
def check_type(val: gdb.Value) -> bool:
|
||||
if val.type.name in ["scc_hashtable_t", "scc_hashtable"]:
|
||||
def check_type(val):
|
||||
# 通过类型名或关键字段检查
|
||||
type_name = val.type.name
|
||||
if type_name and type_name in ("scc_hashtable_t", "scc_hashtable"):
|
||||
return True
|
||||
try:
|
||||
fields = {f.name for f in val.type.fields()}
|
||||
required = {"entries", "count", "tombstone_count", "hash_func", "key_cmp"}
|
||||
if required.issubset(fields):
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def to_string(self):
|
||||
count = self.val["count"]
|
||||
tombstone = self.val["tombstone_count"]
|
||||
cap = self.val["entries"]["size"] # 总槽位数
|
||||
return f"hashtable(count={count}, tombstone={tombstone}, capacity={cap})"
|
||||
|
||||
def append_printer():
|
||||
"注册方式一:传统append方法(您之前有效的方式)self"
|
||||
gdb.pretty_printers.append(
|
||||
lambda val: VectorPrinter(val) if VectorPrinter.check_type(val) else None
|
||||
)
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
def num_children(self):
|
||||
return int(self.val["count"])
|
||||
|
||||
def children(self):
|
||||
entries = self.val["entries"]
|
||||
size = int(entries["size"])
|
||||
data = entries["data"]
|
||||
if size == 0 or data == 0:
|
||||
return
|
||||
# ENTRY_ACTIVE = 1(根据枚举定义)
|
||||
for i in range(size):
|
||||
entry = data[i]
|
||||
state = int(entry["state"])
|
||||
if state == 1: # 只输出有效条目
|
||||
yield (f"[{i}]", entry)
|
||||
|
||||
|
||||
def register_new_printer():
|
||||
"注册方式二:新版注册方法(备用方案)"
|
||||
class StrPoolPrinter(gdb.ValuePrinter):
|
||||
"""打印 scc_strpool_t,将键值作为字符串展示"""
|
||||
|
||||
def str_lookup_function(val):
|
||||
if VectorPrinter.check_type(val) is False:
|
||||
return None
|
||||
ret = VectorPrinter(val)
|
||||
# print(
|
||||
# f"ret {ret}, type {val.type.name}, {[(i.name, i.type) for i in val.type.fields()]}"
|
||||
# )
|
||||
return ret
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
self.ht = val["ht"] # 内部哈希表
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(), str_lookup_function)
|
||||
# if gdb.current_progspace() is not None:
|
||||
# pts = gdb.current_progspace().pretty_printers
|
||||
# print(pts, len(pts))
|
||||
# pts.append(str_lookup_function)
|
||||
@staticmethod
|
||||
def check_type(val):
|
||||
type_name = val.type.name
|
||||
if type_name and type_name == "scc_strpool_t":
|
||||
return True
|
||||
try:
|
||||
fields = {f.name for f in val.type.fields()}
|
||||
if "ht" in fields:
|
||||
# 可进一步检查 ht 的类型,但非必须
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def to_string(self):
|
||||
count = self.ht["count"]
|
||||
cap = self.ht["entries"]["size"]
|
||||
return f"strpool(count={count}, capacity={cap})"
|
||||
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
def num_children(self):
|
||||
return int(self.ht["count"])
|
||||
|
||||
def children(self):
|
||||
entries = self.ht["entries"]
|
||||
size = int(entries["size"])
|
||||
data = entries["data"]
|
||||
if size == 0 or data == 0:
|
||||
return
|
||||
const_char_ptr = gdb.lookup_type("const char").pointer()
|
||||
char_ptr = gdb.lookup_type("char").pointer()
|
||||
|
||||
for i in range(size):
|
||||
entry = data[i]
|
||||
state = int(entry["state"])
|
||||
if state == 1: # ACTIVE
|
||||
key_val = entry["key"]
|
||||
value_val = entry["value"]
|
||||
|
||||
# 尝试将 void* 转为字符串
|
||||
try:
|
||||
key_str = key_val.cast(const_char_ptr).string()
|
||||
except:
|
||||
key_str = str(key_val) # 失败则回退到地址
|
||||
|
||||
try:
|
||||
value_str = value_val.cast(char_ptr).string()
|
||||
except:
|
||||
value_str = str(value_val)
|
||||
|
||||
# 使用带引号的字符串作为名称,值直接是字符串
|
||||
yield (repr(key_str), value_str)
|
||||
|
||||
|
||||
def register_pretty_printers():
|
||||
"""统一的查找函数,注册所有打印机"""
|
||||
|
||||
def lookup_function(val):
|
||||
if VectorPrinter.check_type(val):
|
||||
return VectorPrinter(val)
|
||||
if HashTablePrinter.check_type(val):
|
||||
return HashTablePrinter(val)
|
||||
if StrPoolPrinter.check_type(val):
|
||||
return StrPoolPrinter(val)
|
||||
return None
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(), lookup_function)
|
||||
|
||||
|
||||
class VectorInfoCommand(gdb.Command):
|
||||
@@ -216,7 +304,5 @@ class VectorInfoCommand(gdb.Command):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 双重注册保证兼容性
|
||||
# append_printer() # 保留您原来有效的方式
|
||||
register_new_printer() # 添加新版注册
|
||||
register_pretty_printers()
|
||||
VectorInfoCommand()
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
#include <scc_core_impl.h>
|
||||
#include <scc_core_macro.h>
|
||||
#include <scc_core_mem.h>
|
||||
#include <scc_core_pos.h>
|
||||
#include <scc_core_str.h>
|
||||
#include <scc_core_stream.h>
|
||||
#include <scc_core_vec.h>
|
||||
|
||||
#endif // __SCC_CORE_H__
|
||||
|
||||
@@ -18,6 +18,7 @@ typedef enum {
|
||||
|
||||
scc_file_t scc_fopen(const char *path, scc_fmode_t mode);
|
||||
void scc_fclose(scc_file_t file);
|
||||
usize scc_fsize(scc_file_t file);
|
||||
usize scc_fread(scc_file_t file, void *buffer, usize size);
|
||||
usize scc_fwrite(scc_file_t file, const void *buffer, usize size);
|
||||
cbool scc_fexists(const char *path);
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef __SCC_CORE_POS_H__
|
||||
#define __SCC_CORE_POS_H__
|
||||
|
||||
#include "scc_core_str.h"
|
||||
#include "scc_core_type.h"
|
||||
typedef struct scc_pos {
|
||||
scc_cstring_t name;
|
||||
usize line;
|
||||
usize col;
|
||||
usize offset;
|
||||
} scc_pos_t;
|
||||
|
||||
static inline scc_pos_t scc_pos_create() {
|
||||
return (scc_pos_t){scc_cstring_create(), 1, 1, 0};
|
||||
}
|
||||
|
||||
static inline void scc_pos_next(scc_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->col++;
|
||||
}
|
||||
|
||||
static inline void scc_pos_next_line(scc_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->line++;
|
||||
pos->col = 1;
|
||||
}
|
||||
|
||||
#endif /* __SCC_CORE_POS_H__ */
|
||||
178
runtime/scc_core/include/scc_core_ring.h
Normal file
178
runtime/scc_core/include/scc_core_ring.h
Normal file
@@ -0,0 +1,178 @@
|
||||
#ifndef __SCC_CORE_RING_H__
|
||||
#define __SCC_CORE_RING_H__
|
||||
|
||||
#include <scc_core.h>
|
||||
|
||||
/**
|
||||
* @def SCC_RING(type)
|
||||
* @brief 声明环形缓冲区结构体
|
||||
* @param type 存储的元素类型
|
||||
*
|
||||
* 生成包含以下字段的结构体:
|
||||
* - data: 元素数组
|
||||
* - cap: 容量
|
||||
* - head: 已消费的逻辑索引
|
||||
* - probe: 预览索引
|
||||
* - tail: 已填充的逻辑末尾索引
|
||||
* - fill: 填充回调函数 (当需要新元素时调用)
|
||||
*/
|
||||
#define SCC_RING(type) \
|
||||
struct { \
|
||||
type *data; \
|
||||
usize cap; \
|
||||
usize head; \
|
||||
usize probe; \
|
||||
usize tail; \
|
||||
cbool (*fill)(type * out, void *userdata); \
|
||||
void *userdata; \
|
||||
}
|
||||
|
||||
// ==================== 内部辅助宏 (不直接使用) ====================
|
||||
|
||||
#define scc_ring_phys(ring, idx) ((idx) % (ring).cap)
|
||||
|
||||
/**
|
||||
* @brief 确保 probe 位置有数据可用 (尝试填充)
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param ok 变量名 (如 int ok_flag) ,宏会将其设置为 true 或 false
|
||||
*/
|
||||
#define scc_ring_ensure(ring, ok) \
|
||||
do { \
|
||||
ok = 1; \
|
||||
if ((ring).probe < (ring).tail) \
|
||||
break; \
|
||||
/* probe == tail,需要填充新元素 */ \
|
||||
if (!(ring).fill) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
if ((ring).tail - (ring).head >= (ring).cap) { \
|
||||
ok = 0; /* 缓冲区满,无法填充 */ \
|
||||
break; \
|
||||
} \
|
||||
usize phys_tail = scc_ring_phys(ring, (ring).tail); \
|
||||
if (!(ring).fill(&(ring).data[phys_tail], (ring).userdata)) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
(ring).tail++; \
|
||||
} while (0)
|
||||
|
||||
// ==================== 用户操作宏 ====================
|
||||
|
||||
/**
|
||||
* @brief 初始化环形缓冲区
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param cap 容量
|
||||
* @param fill_func 填充回调函数 (可传 NULL)
|
||||
*
|
||||
* 内存分配失败由 scc_malloc 内部处理 (如 LOG_FATAL)
|
||||
*/
|
||||
#define scc_ring_init(ring, _cap, fill_func, _userdata) \
|
||||
do { \
|
||||
(ring).data = scc_malloc((_cap) * sizeof(*(ring).data)); \
|
||||
(ring).cap = (_cap); \
|
||||
(ring).head = 0; \
|
||||
(ring).probe = 0; \
|
||||
(ring).tail = 0; \
|
||||
(ring).fill = (fill_func); \
|
||||
(ring).userdata = (_userdata); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 释放环形缓冲区内存
|
||||
* @param ring 环形缓冲区变量
|
||||
*/
|
||||
#define scc_ring_free(ring) \
|
||||
do { \
|
||||
scc_free((ring).data); \
|
||||
(ring).data = NULL; \
|
||||
(ring).cap = (ring).head = (ring).probe = (ring).tail = 0; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 预览 probe 位置的元素 (不移动 probe)
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param val 变量名,用于接收元素值 (例如 int ch)
|
||||
* @param ok 变量名,用于接收成功状态 (cbool 类型)
|
||||
*/
|
||||
#define scc_ring_peek(ring, val, ok) \
|
||||
do { \
|
||||
scc_ring_ensure(ring, ok); \
|
||||
if (!(ok)) \
|
||||
break; \
|
||||
if ((ring).probe >= (ring).tail) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
usize _phys = scc_ring_phys(ring, (ring).probe); \
|
||||
val = (ring).data[_phys]; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 获取 probe 位置的元素,并将 probe 前进一步
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param val 变量名,用于接收元素值 (例如 int ch)
|
||||
* @param ok 变量名,用于接收成功状态 (cbool 类型)
|
||||
*/
|
||||
#define scc_ring_next(ring, val, ok) \
|
||||
do { \
|
||||
scc_ring_ensure(ring, ok); \
|
||||
if (!(ok)) \
|
||||
break; \
|
||||
if ((ring).probe >= (ring).tail) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
usize _phys = scc_ring_phys(ring, (ring).probe); \
|
||||
val = (ring).data[_phys]; \
|
||||
(ring).probe++; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 将 probe 后退一步 (不能低于 head)
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param ok 变量名,用于接收成功状态 (cbool 类型)
|
||||
*/
|
||||
#define scc_ring_back(ring, ok) \
|
||||
do { \
|
||||
if ((ring).probe > (ring).head) { \
|
||||
(ring).probe--; \
|
||||
ok = 1; \
|
||||
} else { \
|
||||
ok = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 将 probe 重置为 head
|
||||
* @param ring 环形缓冲区变量
|
||||
*/
|
||||
#define scc_ring_reset(ring) ((ring).probe = (ring).head)
|
||||
|
||||
/**
|
||||
* @brief 将 head 移动到 probe 位置,标记 probe 之前的元素为已消费
|
||||
* @param ring 环形缓冲区变量
|
||||
*/
|
||||
#define scc_ring_consume(ring) ((ring).head = (ring).probe)
|
||||
|
||||
/**
|
||||
* @brief 返回 probe 到 tail 之间的元素个数 (可预览数量)
|
||||
* @param ring 环形缓冲区变量
|
||||
* @return 可预览元素个数
|
||||
*/
|
||||
#define scc_ring_available(ring) ((ring).tail - (ring).probe)
|
||||
|
||||
/**
|
||||
* @brief 获取 probe 位置的元素,并将 probe 前进一步同时标记为已消费
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param val 变量名,用于接收元素值 (例如 int ch)
|
||||
* @param ok 变量名,用于接收成功状态 (cbool 类型)
|
||||
*/
|
||||
#define scc_ring_next_consume(ring, val, ok) \
|
||||
do { \
|
||||
scc_ring_next(ring, val, ok); \
|
||||
scc_ring_consume(ring); \
|
||||
} while (0)
|
||||
|
||||
#endif /* __SCC_CORE_RING_H__ */
|
||||
@@ -1,130 +0,0 @@
|
||||
#ifndef __SMCC_CORE_PROBE_STREAM_H__
|
||||
#define __SMCC_CORE_PROBE_STREAM_H__
|
||||
|
||||
#include "scc_core_impl.h"
|
||||
#include "scc_core_macro.h"
|
||||
#include "scc_core_mem.h"
|
||||
#include "scc_core_str.h"
|
||||
|
||||
struct scc_probe_stream;
|
||||
typedef struct scc_probe_stream scc_probe_stream_t;
|
||||
|
||||
#define scc_stream_eof (-1)
|
||||
|
||||
/**
|
||||
* @brief 带探针的流接口
|
||||
*
|
||||
* 这个流提供了双指针机制:当前读取位置(头指针)和探针位置(尾指针)。
|
||||
* 尾指针只能向前移动,用于查看而不消费。
|
||||
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
|
||||
*/
|
||||
struct scc_probe_stream {
|
||||
scc_cstring_t name;
|
||||
|
||||
/// @brief 消费头指针处的字符(移动头指针)
|
||||
int (*consume)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 查看当前探针位置的字符,不移动任何指针
|
||||
int (*peek)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 移动探针位置并返回字符
|
||||
int (*next)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 回退一个字符(单次后退,探针位置后退一步)
|
||||
cbool (*back)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 移动头指针到探针位置
|
||||
void (*sync)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 重置探针位置到头指针位置
|
||||
void (*reset)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 读取指定数量的字符到缓冲区
|
||||
usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count);
|
||||
|
||||
/// @brief 检查是否到达流末尾
|
||||
cbool (*is_at_end)(scc_probe_stream_t *stream);
|
||||
|
||||
/// @brief 销毁流并释放资源
|
||||
void (*drop)(scc_probe_stream_t *stream);
|
||||
};
|
||||
|
||||
static inline int scc_probe_stream_consume(scc_probe_stream_t *self) {
|
||||
return self->consume(self);
|
||||
}
|
||||
|
||||
static inline int scc_probe_stream_peek(scc_probe_stream_t *self) {
|
||||
return self->peek(self);
|
||||
}
|
||||
|
||||
static inline int scc_probe_stream_next(scc_probe_stream_t *self) {
|
||||
return self->next(self);
|
||||
}
|
||||
|
||||
static inline void scc_probe_stream_sync(scc_probe_stream_t *self) {
|
||||
self->sync(self);
|
||||
}
|
||||
|
||||
static inline cbool scc_probe_stream_back(scc_probe_stream_t *self) {
|
||||
return self->back(self);
|
||||
}
|
||||
|
||||
static inline void scc_probe_stream_reset(scc_probe_stream_t *self) {
|
||||
self->reset(self);
|
||||
}
|
||||
|
||||
static inline usize scc_probe_stream_read_buf(scc_probe_stream_t *self,
|
||||
char *buffer, usize count) {
|
||||
return self->read_buf(self, buffer, count);
|
||||
}
|
||||
|
||||
static inline cbool scc_probe_stream_is_at_end(scc_probe_stream_t *self) {
|
||||
return self->is_at_end(self);
|
||||
}
|
||||
|
||||
static inline cbool scc_probe_stream_has_more(scc_probe_stream_t *self) {
|
||||
return !self->is_at_end(self);
|
||||
}
|
||||
|
||||
static inline void scc_probe_stream_drop(scc_probe_stream_t *self) {
|
||||
self->drop(self);
|
||||
}
|
||||
|
||||
#ifndef __SCC_NO_MEM_PROBE_STREAM__
|
||||
/**
|
||||
* @brief 内存探针流结构
|
||||
*/
|
||||
typedef struct scc_mem_probe_stream {
|
||||
scc_probe_stream_t stream;
|
||||
const char *data;
|
||||
usize data_length;
|
||||
usize curr_pos; // 当前读取位置
|
||||
usize probe_pos; // 探针位置(用于peek)
|
||||
cbool owned; // 是否拥有数据(如果拥有将会自动释放)
|
||||
} scc_mem_probe_stream_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化内存探针流(由你负责scc_mem_probe_stream_t的释放)
|
||||
*
|
||||
* @param stream 流结构指针
|
||||
* @param data 数据指针
|
||||
* @param length 数据长度
|
||||
* @param owned 是否拥有数据(如果拥有将会自动释放)
|
||||
* @return core_probe_stream_t* 成功返回流指针,失败返回NULL
|
||||
*/
|
||||
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
|
||||
const char *data, usize length,
|
||||
cbool owned);
|
||||
/**
|
||||
* @brief 构造内存探针流(其中drop会自动释放内存)
|
||||
*
|
||||
* @param data
|
||||
* @param length
|
||||
* @param owned 是否拥有数据(如果拥有将会自动释放)
|
||||
* @return scc_probe_stream_t*
|
||||
*/
|
||||
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
|
||||
cbool owned);
|
||||
#endif
|
||||
|
||||
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */
|
||||
@@ -53,6 +53,20 @@ void scc_fclose(scc_file_t file) {
|
||||
}
|
||||
}
|
||||
|
||||
usize scc_fsize(scc_file_t file) {
|
||||
FILE *fp = (FILE *)file;
|
||||
if (fseek(fp, 0, SEEK_END) != 0) {
|
||||
perror("fseek failed");
|
||||
return 0;
|
||||
}
|
||||
usize fsize = ftell(fp);
|
||||
if (fseek(fp, 0, SEEK_SET)) {
|
||||
perror("fseek failed");
|
||||
return 0;
|
||||
}
|
||||
return fsize;
|
||||
}
|
||||
|
||||
usize scc_fread(scc_file_t file, void *buffer, usize size) {
|
||||
if (!file || !buffer)
|
||||
return 0;
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
#include <scc_core_log.h>
|
||||
#include <scc_core_stream.h>
|
||||
|
||||
#ifndef __SCC_CORE_NO_MEM_PROBE_STREAM__
|
||||
|
||||
static int mem_probe_stream_consume(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
if (stream->curr_pos >= stream->data_length) {
|
||||
return scc_stream_eof;
|
||||
}
|
||||
|
||||
unsigned char ch = stream->data[stream->curr_pos++];
|
||||
// 如果探针位置落后于当前读取位置,则更新探针位置
|
||||
if (stream->probe_pos < stream->curr_pos) {
|
||||
stream->probe_pos = stream->curr_pos;
|
||||
}
|
||||
return (int)ch;
|
||||
}
|
||||
|
||||
static int mem_probe_stream_peek(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
if (stream->probe_pos >= stream->data_length) {
|
||||
return scc_stream_eof;
|
||||
}
|
||||
|
||||
// 只查看而不移动探针位置
|
||||
return (int)(unsigned char)stream->data[stream->probe_pos];
|
||||
}
|
||||
|
||||
static int mem_probe_stream_next(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
if (stream->probe_pos >= stream->data_length) {
|
||||
return scc_stream_eof;
|
||||
}
|
||||
|
||||
// 返回探针位置的字符,并将探针位置向前移动
|
||||
int ch = (int)(unsigned char)stream->data[stream->probe_pos];
|
||||
stream->probe_pos++;
|
||||
return ch;
|
||||
}
|
||||
|
||||
static void mem_probe_stream_sync(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
// 移动头指针到探针位置(消费已查看的字符)
|
||||
if (stream->probe_pos > stream->curr_pos) {
|
||||
stream->curr_pos = stream->probe_pos;
|
||||
}
|
||||
}
|
||||
|
||||
static cbool mem_probe_stream_back(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
// 只能回退一个字符
|
||||
if (stream->probe_pos == 0)
|
||||
return false;
|
||||
if (stream->curr_pos + 1 > stream->probe_pos)
|
||||
return false;
|
||||
|
||||
stream->probe_pos--;
|
||||
return true;
|
||||
}
|
||||
|
||||
static usize mem_probe_stream_read_buf(scc_probe_stream_t *_stream,
|
||||
char *buffer, usize count) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
if (buffer == null) {
|
||||
LOG_WARN("Buffer is null");
|
||||
return 0;
|
||||
}
|
||||
|
||||
usize remaining = stream->data_length - stream->curr_pos;
|
||||
usize to_read = (remaining < count) ? remaining : count;
|
||||
|
||||
if (to_read > 0) {
|
||||
scc_memcpy(buffer, stream->data + stream->curr_pos, to_read);
|
||||
stream->curr_pos += to_read;
|
||||
// 更新探针位置
|
||||
if (stream->probe_pos < stream->curr_pos) {
|
||||
stream->probe_pos = stream->curr_pos;
|
||||
}
|
||||
} else {
|
||||
LOG_WARN("Reading past end of stream [maybe count is too large or "
|
||||
"negative?]");
|
||||
}
|
||||
|
||||
return to_read;
|
||||
}
|
||||
|
||||
static void mem_probe_stream_reset(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
// 重置探针位置到头指针位置
|
||||
stream->probe_pos = stream->curr_pos;
|
||||
}
|
||||
|
||||
static cbool mem_probe_stream_is_at_end(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
return stream->curr_pos >= stream->data_length;
|
||||
}
|
||||
|
||||
static void mem_probe_stream_drop(scc_probe_stream_t *_stream) {
|
||||
Assert(_stream != null);
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
|
||||
scc_cstring_free(&stream->stream.name);
|
||||
|
||||
if (stream->owned) {
|
||||
scc_free((void *)stream->data);
|
||||
stream->data = null;
|
||||
}
|
||||
}
|
||||
|
||||
scc_probe_stream_t *scc_mem_probe_stream_init(scc_mem_probe_stream_t *stream,
|
||||
const char *data, usize length,
|
||||
cbool owned) {
|
||||
if (stream == null || data == null) {
|
||||
LOG_ERROR("param error");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (length == 0) {
|
||||
LOG_WARN("input memory is empty");
|
||||
owned = false;
|
||||
}
|
||||
|
||||
stream->owned = owned;
|
||||
stream->data = data;
|
||||
stream->data_length = length;
|
||||
stream->curr_pos = 0;
|
||||
stream->probe_pos = 0;
|
||||
|
||||
stream->stream.name = scc_cstring_from_cstr("mem_probe_stream");
|
||||
|
||||
// 设置函数指针
|
||||
stream->stream.consume = mem_probe_stream_consume;
|
||||
stream->stream.peek = mem_probe_stream_peek;
|
||||
stream->stream.next = mem_probe_stream_next;
|
||||
stream->stream.sync = mem_probe_stream_sync;
|
||||
stream->stream.back = mem_probe_stream_back;
|
||||
stream->stream.read_buf = mem_probe_stream_read_buf;
|
||||
stream->stream.reset = mem_probe_stream_reset;
|
||||
stream->stream.is_at_end = mem_probe_stream_is_at_end;
|
||||
stream->stream.drop = mem_probe_stream_drop;
|
||||
|
||||
return (scc_probe_stream_t *)stream;
|
||||
}
|
||||
|
||||
static void scc_owned_mem_stream_drop(scc_probe_stream_t *_stream) {
|
||||
scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream;
|
||||
mem_probe_stream_drop(_stream);
|
||||
scc_free(stream);
|
||||
}
|
||||
|
||||
scc_probe_stream_t *scc_mem_probe_stream_alloc(const char *data, usize length,
|
||||
cbool owned) {
|
||||
scc_mem_probe_stream_t *stream =
|
||||
(scc_mem_probe_stream_t *)scc_malloc(sizeof(scc_mem_probe_stream_t));
|
||||
if (stream == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
scc_probe_stream_t *ret =
|
||||
scc_mem_probe_stream_init(stream, data, length, owned);
|
||||
stream->stream.drop = scc_owned_mem_stream_drop;
|
||||
Assert(ret != null);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* __SCC_CORE_NO_MEM_PROBE_STREAM__ */
|
||||
326
runtime/scc_core/tests/test_core_ring.c
Normal file
326
runtime/scc_core/tests/test_core_ring.c
Normal file
@@ -0,0 +1,326 @@
|
||||
#include <scc_core.h>
|
||||
#include <scc_core_ring.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <utest/acutest.h>
|
||||
|
||||
// 为测试定义简单的 token 类型(包含动态字符串)
|
||||
typedef struct {
|
||||
int id;
|
||||
char *data;
|
||||
} test_token_t;
|
||||
|
||||
// 定义环形缓冲区类型别名(方便使用)
|
||||
typedef SCC_RING(char) char_ring_t;
|
||||
typedef SCC_RING(test_token_t) token_ring_t;
|
||||
|
||||
/* ------------------- 字符流测试辅助 ------------------ */
|
||||
static const char *test_chars = "abcdefghijklmnopqrstuvwxyz";
|
||||
static size_t char_index = 0;
|
||||
|
||||
cbool char_fill(char *out, void *userdata) {
|
||||
(void)userdata;
|
||||
if (char_index < strlen(test_chars)) {
|
||||
*out = test_chars[char_index++];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void reset_char_fill(void) { char_index = 0; }
|
||||
|
||||
/* ------------------- token 流测试辅助 ------------------ */
|
||||
static int token_id = 0;
|
||||
|
||||
cbool token_fill(test_token_t *out, void *userdata) {
|
||||
(void)userdata;
|
||||
if (token_id < 10) { // 只产生 10 个 token
|
||||
out->id = token_id;
|
||||
out->data = (char *)scc_malloc(20);
|
||||
if (!out->data)
|
||||
return false;
|
||||
snprintf_(out->data, 20, "token%d", token_id);
|
||||
token_id++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void reset_token_fill(void) { token_id = 0; }
|
||||
|
||||
void free_token(test_token_t *tok) {
|
||||
if (tok->data) {
|
||||
scc_free(tok->data);
|
||||
tok->data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* ==================== 字符环形缓冲区测试 ==================== */
|
||||
void test_char_ring_basic(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 4, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'a');
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'b');
|
||||
|
||||
// peek
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'c');
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'c');
|
||||
|
||||
// back
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'c');
|
||||
|
||||
// consume & reset
|
||||
scc_ring_consume(ring);
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'c');
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'd');
|
||||
|
||||
scc_ring_reset(ring);
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'c');
|
||||
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == false); // 不能低于 head
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_char_ring_full(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 3, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // a
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // b
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // c
|
||||
// 缓冲区满,peek 应失败
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == false);
|
||||
|
||||
scc_ring_consume(ring); // 释放已读空间
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // d
|
||||
TEST_CHECK(c == 'd');
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // e
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // f
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == false); // 再次满
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_char_ring_eof(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 32, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == test_chars[i]);
|
||||
}
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == false);
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == false);
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_char_ring_back_boundary(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 4, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // a
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // b
|
||||
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == false); // 已到 head
|
||||
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'a');
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_char_ring_consume_reset(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 5, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // a
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // b
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // c
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true); // 此时 probe 指向 a
|
||||
|
||||
scc_ring_consume(ring); // head 移至 a
|
||||
scc_ring_reset(ring); // probe 也移至 a
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == 'a'); // 应该返回 a
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // b
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true); // c
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_char_ring_wrap(void) {
|
||||
reset_char_fill();
|
||||
char_ring_t ring;
|
||||
scc_ring_init(ring, 3, char_fill, 0);
|
||||
char c;
|
||||
cbool ok;
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
scc_ring_next(ring, c, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(c == test_chars[i]);
|
||||
scc_ring_consume(ring); // 立即消费,保持缓冲区几乎为空
|
||||
}
|
||||
scc_ring_peek(ring, c, ok);
|
||||
TEST_CHECK(ok == false); // 无数据
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
/* ==================== token 环形缓冲区测试 ==================== */
|
||||
void test_token_ring_basic(void) {
|
||||
reset_token_fill();
|
||||
token_ring_t ring;
|
||||
scc_ring_init(ring, 3, token_fill, 0);
|
||||
test_token_t tok;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 0);
|
||||
free_token(&tok);
|
||||
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 1);
|
||||
free_token(&tok);
|
||||
|
||||
scc_ring_peek(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 2); // peek 不应消费
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 2);
|
||||
|
||||
scc_ring_back(ring, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
scc_ring_peek(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 2);
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 2);
|
||||
free_token(&tok);
|
||||
|
||||
scc_ring_consume(ring);
|
||||
// 消费剩余 token
|
||||
while (1) {
|
||||
scc_ring_next(ring, tok, ok);
|
||||
if (!ok)
|
||||
break;
|
||||
free_token(&tok);
|
||||
}
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
void test_token_ring_full(void) {
|
||||
reset_token_fill();
|
||||
token_ring_t ring;
|
||||
scc_ring_init(ring, 2, token_fill, 0);
|
||||
test_token_t tok;
|
||||
cbool ok;
|
||||
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 0);
|
||||
free_token(&tok);
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 1);
|
||||
free_token(&tok);
|
||||
scc_ring_peek(ring, tok, ok);
|
||||
TEST_CHECK(ok == false); // 缓冲区满
|
||||
|
||||
scc_ring_consume(ring);
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 2);
|
||||
free_token(&tok);
|
||||
|
||||
scc_ring_next(ring, tok, ok);
|
||||
TEST_CHECK(ok == true);
|
||||
TEST_CHECK(tok.id == 3);
|
||||
free_token(&tok);
|
||||
scc_ring_peek(ring, tok, ok);
|
||||
TEST_CHECK(ok == false); // 再次满
|
||||
|
||||
scc_ring_free(ring);
|
||||
}
|
||||
|
||||
/* ==================== 测试列表 ==================== */
|
||||
TEST_LIST = {{"test_char_ring_basic", test_char_ring_basic},
|
||||
{"test_char_ring_full", test_char_ring_full},
|
||||
{"test_char_ring_eof", test_char_ring_eof},
|
||||
{"test_char_ring_back_boundary", test_char_ring_back_boundary},
|
||||
{"test_char_ring_consume_reset", test_char_ring_consume_reset},
|
||||
{"test_char_ring_wrap", test_char_ring_wrap},
|
||||
{"test_token_ring_basic", test_token_ring_basic},
|
||||
{"test_token_ring_full", test_token_ring_full},
|
||||
{NULL, NULL}};
|
||||
Reference in New Issue
Block a user