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:
zzy
2026-02-16 16:56:40 +08:00
parent 088050c903
commit 0e7dec202a
30 changed files with 1840 additions and 1979 deletions

View File

@@ -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__

View File

@@ -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);

View File

@@ -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__ */

View 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__ */

View File

@@ -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__ */

View File

@@ -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;

View File

@@ -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__ */

View 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}};