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:
@@ -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__ */
|
||||
Reference in New Issue
Block a user