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

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