feat 重构stream流API并适配lex_parse和lexer

This commit is contained in:
zzy
2025-12-08 23:04:11 +08:00
parent 1ab07a5815
commit 36bff64a91
17 changed files with 402 additions and 244 deletions

View File

@@ -57,7 +57,7 @@ static inline void cstring_free(cstring_t *str) {
if (str == null) {
return;
}
if (str->data != null && str->cap != 0) {
if (str->cap != 0 && str->data != null) {
smcc_free(str->data);
str->data = null;
}

View File

@@ -1,67 +1,120 @@
#ifndef __SMCC_CORE_STREAM_H__
#define __SMCC_CORE_STREAM_H__
#ifndef __SMCC_CORE_PROBE_STREAM_H__
#define __SMCC_CORE_PROBE_STREAM_H__
#include "core_impl.h"
#include "core_macro.h"
#include "core_mem.h"
#include "core_str.h"
struct core_stream;
typedef struct core_stream core_stream_t;
struct core_probe_stream;
typedef struct core_probe_stream core_probe_stream_t;
#define core_stream_eof (-1)
struct core_stream {
/**
* @brief 带探针的流接口
*
* 这个流提供了双指针机制:当前读取位置(头指针)和探针位置(尾指针)。
* 尾指针只能向前移动,用于查看而不消费。
* 头指针可以前进或单次后退,但不能一直后退到尾指针后面。
*/
struct core_probe_stream {
cstring_t name;
/// @brief 消费头指针处的字符(移动头指针)
int (*consume)(core_probe_stream_t *stream);
/// @brief 查看当前探针位置的字符,不移动任何指针
int (*peek)(core_probe_stream_t *stream);
/// @brief 移动探针位置并返回字符
int (*next)(core_probe_stream_t *stream);
/// @brief 移动头指针到探针位置
void (*sync)(core_probe_stream_t *stream);
/// @brief 重置探针位置到头指针位置
void (*reset)(core_probe_stream_t *stream);
/// @brief 回退一个字符(单次后退,头指针后退一步)
cbool (*back)(core_probe_stream_t *stream);
/// @brief 读取指定数量的字符到缓冲区
usize (*read_buf)(core_stream_t *stream, char *buffer, usize count);
usize (*read_buf)(core_probe_stream_t *stream, char *buffer, usize count);
/// @brief 获取下一个字符
int (*peek_char)(core_stream_t *stream);
/// @brief 检查是否到达流末尾
cbool (*is_at_end)(core_probe_stream_t *stream);
/// @brief 重置字符流位置
void (*reset_char)(core_stream_t *stream);
/// @brief 读取并消费下一个字符(移动流位置)
int (*next_char)(core_stream_t *stream);
/// @brief 释放资源
void (*free_stream)(core_stream_t *steam);
/// @brief 销毁流并释放资源
void (*destroy)(core_probe_stream_t *stream);
};
static inline usize core_stream_read_buf(core_stream_t *self, char *buffer,
usize count) {
static inline int core_probe_stream_consume(core_probe_stream_t *self) {
return self->consume(self);
}
static inline int core_probe_stream_peek(core_probe_stream_t *self) {
return self->peek(self);
}
static inline int core_probe_stream_next(core_probe_stream_t *self) {
return self->next(self);
}
static inline void core_probe_stream_sync(core_probe_stream_t *self) {
self->sync(self);
}
static inline cbool core_probe_stream_back(core_probe_stream_t *self) {
return self->back(self);
}
static inline void core_probe_stream_reset(core_probe_stream_t *self) {
self->reset(self);
}
static inline usize core_probe_stream_read_buf(core_probe_stream_t *self,
char *buffer, usize count) {
return self->read_buf(self, buffer, count);
}
static inline int core_stream_peek_char(core_stream_t *self) {
return self->peek_char(self);
static inline cbool core_probe_stream_is_at_end(core_probe_stream_t *self) {
return self->is_at_end(self);
}
static inline void core_stream_reset_char(core_stream_t *self) {
self->reset_char(self);
static inline cbool core_probe_stream_has_more(core_probe_stream_t *self) {
return !self->is_at_end(self);
}
static inline int core_stream_next_char(core_stream_t *self) {
return self->next_char(self);
static inline void core_probe_stream_destroy(core_probe_stream_t *self) {
self->destroy(self);
}
static inline void core_stream_free_stream(core_stream_t *self) {
self->free_stream(self);
}
#ifndef __SMCC_CORE_NO_MEM_STREAM__
typedef struct core_mem_stream {
core_stream_t stream;
#ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__
/**
* @brief 内存探针流结构
*/
typedef struct core_mem_probe_stream {
core_probe_stream_t stream;
const char *data;
usize data_length;
usize curr_pos;
usize peek_pos;
cbool owned;
} core_mem_stream_t;
core_stream_t *core_mem_stream_init(core_mem_stream_t *stream, const char *data,
usize length, cbool need_copy);
usize curr_pos; // 当前读取位置
usize probe_pos; // 探针位置用于peek
cbool owned; // 是否拥有数据(需要释放)
} core_mem_probe_stream_t;
/**
* @brief 初始化内存探针流
*
* @param stream 流结构指针
* @param data 数据指针
* @param length 数据长度
* @param need_copy 是否需要复制数据
* @return core_probe_stream_t* 成功返回流指针失败返回NULL
*/
core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy);
#endif
#endif /* __SMCC_CORE_STREAM_H__ */
#endif /* __SMCC_CORE_PROBE_STREAM_H__ */

View File

@@ -70,6 +70,7 @@
(vec).cap = cap; \
(vec).data = data; \
} \
Assert((vec).data != null); \
(vec).data[(vec).size++] = value; \
} while (0)

View File

@@ -1,10 +1,82 @@
#include <core_log.h>
#include <core_stream.h>
// 内存流的具体实现结构
static usize read_buf(core_stream_t *_stream, char *buffer, usize count) {
Assert(buffer != null && buffer != null);
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
#ifndef __SMCC_CORE_NO_MEM_PROBE_STREAM__
static int mem_probe_stream_consume(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
if (stream->curr_pos >= stream->data_length) {
return core_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(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return core_stream_eof;
}
// 只查看而不移动探针位置
return (int)(unsigned char)stream->data[stream->probe_pos];
}
static int mem_probe_stream_next(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
if (stream->probe_pos >= stream->data_length) {
return core_stream_eof;
}
// 返回探针位置的字符,并将探针位置向前移动
int ch = (int)(unsigned char)stream->data[stream->probe_pos];
stream->probe_pos++;
return ch;
}
static void mem_probe_stream_sync(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
// 移动头指针到探针位置(消费已查看的字符)
if (stream->probe_pos > stream->curr_pos) {
stream->curr_pos = stream->probe_pos;
}
}
static cbool mem_probe_stream_back(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
// 只能回退一个字符,且不能回退到探针位置之前
if (stream->curr_pos == 0 || stream->curr_pos <= stream->probe_pos) {
return false;
}
stream->curr_pos--;
return true;
}
static usize mem_probe_stream_read_buf(core_probe_stream_t *_stream, char *buffer,
usize count) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_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;
@@ -12,63 +84,48 @@ static usize read_buf(core_stream_t *_stream, char *buffer, usize count) {
if (to_read > 0) {
smcc_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?]");
LOG_WARN("Reading past end of stream [maybe count is too large or "
"negative?]");
}
return to_read;
}
static int peek_char(core_stream_t *_stream) {
static void mem_probe_stream_reset(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
// 如果已经到达末尾返回EOF
if (stream->peek_pos >= stream->data_length) {
return core_stream_eof; // EOF
}
return (int)(unsigned char)stream->data[stream->peek_pos++];
// 重置探针位置到头指针位置
stream->probe_pos = stream->curr_pos;
}
static int next_char(core_stream_t *_stream) {
Assert(_stream != NULL);
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
// 如果已经到达末尾返回EOF
if (stream->curr_pos >= stream->data_length) {
return core_stream_eof; // EOF
}
unsigned char ch = stream->data[stream->curr_pos++];
if (stream->peek_pos < stream->curr_pos) {
stream->peek_pos = stream->curr_pos;
}
return (int)ch;
}
static void reset_char(core_stream_t *_stream) {
Assert(_stream != NULL);
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
stream->peek_pos = stream->curr_pos;
}
static void free_stream(core_stream_t *_stream) {
static cbool mem_probe_stream_is_at_end(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_stream_t *stream = (core_mem_stream_t *)_stream;
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
return stream->curr_pos >= stream->data_length;
}
static void mem_probe_stream_destroy(core_probe_stream_t *_stream) {
Assert(_stream != null);
core_mem_probe_stream_t *stream = (core_mem_probe_stream_t *)_stream;
// FIXME maybe double free?
cstring_free(&stream->stream.name);
if (stream->owned) {
smcc_free((void *)stream->data);
stream->data = null;
}
}
core_stream_t *core_mem_stream_init(core_mem_stream_t *stream, const char *data,
usize length, cbool need_copy) {
core_probe_stream_t *core_mem_probe_stream_init(core_mem_probe_stream_t *stream,
const char *data, usize length,
cbool need_copy) {
if (stream == null || data == null) {
LOG_ERROR("param error");
return null;
@@ -94,15 +151,22 @@ core_stream_t *core_mem_stream_init(core_mem_stream_t *stream, const char *data,
}
stream->data_length = length;
stream->curr_pos = 0;
stream->peek_pos = 0;
stream->probe_pos = 0;
stream->stream.name = cstring_from_cstr("mem_stream");
stream->stream.name = cstring_from_cstr("mem_probe_stream");
stream->stream.read_buf = read_buf;
stream->stream.peek_char = peek_char;
stream->stream.next_char = next_char;
stream->stream.reset_char = reset_char;
stream->stream.free_stream = free_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.destroy = mem_probe_stream_destroy;
return (void *)stream;
return (core_probe_stream_t *)stream;
}
#endif /* __SMCC_CORE_NO_MEM_PROBE_STREAM__ */

View File

@@ -16,6 +16,16 @@
#define log_exit exit
#endif
#ifdef __GNUC__ // GCC, Clang, ICC
#define __smcc_log_unreachable() (__builtin_unreachable())
#elif defined _MSC_VER // MSVC
#define __smcc_log_unreachable() (__assume(false))
#elif defined __SMCC_BUILT_IN__ // The SMCC (my compiler)
#define __smcc_log_unreachable() (__smcc_builtin_unreachable())
#else
#define __smcc_log_unreachable()
#endif
#ifndef log_snprintf
#define log_snprintf(...)
#warning "log_snprintf not defined"
@@ -172,6 +182,8 @@ void logger_destroy(logger_t *logger);
do { \
if (!(cond)) { \
LOG_FATAL(__VA_ARGS__); \
log_exit(1); \
__smcc_log_unreachable(); \
} \
} while (0)