feat(lex_parser): 初始化词法解析器模块
新增词法解析器库 `smcc_lex_parser`,包含基础的词法规则解析功能: - 支持字符、字符串、数字、标识符的解析 - 支持跳过注释、空白符、行尾等辅助函数 - 提供对应的单元测试用例,覆盖各类合法与非法输入情况 该模块依赖 `libcore`,并被 `smcc_lex` 模块引用以支持更上层的词法分析逻辑。
This commit is contained in:
28
runtime/libcore/include/core_pos.h
Normal file
28
runtime/libcore/include/core_pos.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef __SMCC_CORE_POS_H__
|
||||
#define __SMCC_CORE_POS_H__
|
||||
|
||||
#include "core_str.h"
|
||||
#include "core_type.h"
|
||||
typedef struct {
|
||||
cstring_t name;
|
||||
usize line;
|
||||
usize col;
|
||||
usize offset;
|
||||
} core_pos_t;
|
||||
|
||||
static inline core_pos_t core_pos_init() {
|
||||
return (core_pos_t){cstring_new(), 1, 1, 0};
|
||||
}
|
||||
|
||||
static inline void core_pos_next(core_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->col++;
|
||||
}
|
||||
|
||||
static inline void core_pos_next_line(core_pos_t *pos) {
|
||||
pos->offset++;
|
||||
pos->line++;
|
||||
pos->col = 1;
|
||||
}
|
||||
|
||||
#endif /* __SMCC_CORE_POS_H__ */
|
||||
@@ -54,12 +54,15 @@ static inline cstring_t cstring_from_cstr(const char *s) {
|
||||
* @param str 要被释放的字符串指针
|
||||
*/
|
||||
static inline void cstring_free(cstring_t *str) {
|
||||
if (str && str->data && str->cap != 0) {
|
||||
if (str == null) {
|
||||
return;
|
||||
}
|
||||
if (str->data != null && str->cap != 0) {
|
||||
smcc_free(str->data);
|
||||
str->data = null;
|
||||
str->size = 0;
|
||||
str->cap = 0;
|
||||
}
|
||||
str->size = 0;
|
||||
str->cap = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#define SMCC_STR(str) _SMCC_STR(str)
|
||||
|
||||
#define SMCC_ARRLEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
#include <core_pos.h>
|
||||
#include <core_str.h>
|
||||
#include <core_stream.h>
|
||||
#include <core_vec.h>
|
||||
|
||||
@@ -69,11 +69,16 @@ static void free_stream(core_stream_t *_stream) {
|
||||
|
||||
core_stream_t *core_mem_stream_init(core_mem_stream_t *stream, const char *data,
|
||||
usize length, cbool need_copy) {
|
||||
if (stream == null || data == NULL || length == 0) {
|
||||
if (stream == null || data == null) {
|
||||
LOG_ERROR("param error");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (length == 0) {
|
||||
LOG_WARN("input memory is empty");
|
||||
need_copy = false;
|
||||
}
|
||||
|
||||
stream->owned = need_copy;
|
||||
if (need_copy) {
|
||||
char *buf = (char *)smcc_malloc(length);
|
||||
|
||||
@@ -79,7 +79,11 @@ void init_logger(logger_t *logger, const char *name) {
|
||||
log_set_level(logger, LOG_LEVEL_ALL);
|
||||
}
|
||||
|
||||
logger_t *log_get(const char *name) { return &__default_logger_root; }
|
||||
logger_t *log_get(const char *name) {
|
||||
// TODO for -Wunused-parameter
|
||||
(void)name;
|
||||
return &__default_logger_root;
|
||||
}
|
||||
|
||||
void log_set_level(logger_t *logger, int level) {
|
||||
if (logger)
|
||||
@@ -95,4 +99,8 @@ void log_set_handler(logger_t *logger, log_handler handler) {
|
||||
__default_logger_root.handler = handler;
|
||||
}
|
||||
|
||||
void logger_destroy(logger_t *logger) { return; }
|
||||
void logger_destroy(logger_t *logger) {
|
||||
// TODO for -Wunused-parameter
|
||||
(void)logger;
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user