1
0
Эх сурвалжийг харах

feat: reader添加错误系统

SongZihuan 3 жил өмнө
parent
commit
4c76e5d976

+ 5 - 1
include/core/reader.h

@@ -2,7 +2,11 @@
 #define AFUN_READER_H
 #include "tool.h"
 
-typedef size_t readerFunc(void *data, char *dest, size_t len, bool *is_end);
+#define READER_MODE_NORMAL (0)
+#define READER_MODE_FINISHED (1)
+#define READER_MODE_ERROR (2)
+
+typedef size_t readerFunc(void *data, char *dest, size_t len, int *mde);
 DEFINE_DLC_SYMBOL(readerFunc);
 
 typedef void destructReaderFunc(void *data);

+ 2 - 2
lang/hgt.py

@@ -24,7 +24,7 @@ if __name__ != '__main__':
 
 argv = sys.argv
 if len(argv) < 7:
-    warnings.warn(f"Too few argument [{len(argv)}]")
+    warnings.warn(f"Too few argument [{len(argv)}]", UserWarning)
     raise ArgError()
 
 output_dir = argv[1]
@@ -89,7 +89,7 @@ for file in file_list:
                 if i[0] in flat_list:
                     if flat_list[i[0]][0] != default_ and default_ != '""' and flat_list[i[0]][0] != '""':
                         # 若果是空字串则可以覆盖
-                        warnings.warn(f"Double define text: {i[0]}")
+                        warnings.warn(f"Double define text: {i[0]}", UserWarning)
                         continue
                     elif default_ == '""':
                         default_ = flat_list[i[0]][0]

+ 23 - 23
lang/tr/zh_cn.py

@@ -22,26 +22,26 @@ help_info = r'''[参数]
 [Github page]
    <https://github.com/aFun-org/aFunlang>'''
    
-command_line_n = '命令行'
-command_line_tips = '在顶层活动空间运行输入的代码'
-usage_n = '帮助'
-cl_arg_error_e = '命令行参数错误'
-cl_not_file_e = '没有代码需要被运行'
-exit_code_n = '退出代码'
-arg_conflict_n = '参数冲突'
-build_not_src_e = '没有源码被编译'
-build_many_src_e = '太多源码被编译 (不要使用 --out 参数)'
-build_src_not_exists_e = '需要编译的文件不存在'
-build_src_already = '已经编译'
-build_use_f = '使用 --fource 强制编译'
-build_file = '文件将被编译'
-run_save_e = '字节码编译失败'
-run_source_not_aub_e = '运行的文件不是.aun文件'
-run_bt_not_aub_e = '运行的文件不是.aub文件'
-run_load_bt_e = '字节码加载失败'
-run_file_aun_aub_e = '运行的文件不是.aun或.aub文件'
-run_file_not_exists_e = '需要运行的文件不存在'
-build_in_aun_e = '被编译的文件不是.aun文件'
-build_out_aub_e = '编译输出的文件不是.aun文件'
-build_error_e = '编译失败'
-Interrupt_n = '中断'
+# command_line_n = '命令行'
+# command_line_tips = '在顶层活动空间运行输入的代码'
+# usage_n = '帮助'
+# cl_arg_error_e = '命令行参数错误'
+# cl_not_file_e = '没有代码需要被运行'
+# exit_code_n = '退出代码'
+# arg_conflict_n = '参数冲突'
+# build_not_src_e = '没有源码被编译'
+# build_many_src_e = '太多源码被编译 (不要使用 --out 参数)'
+# build_src_not_exists_e = '需要编译的文件不存在'
+# build_src_already = '已经编译'
+# build_use_f = '使用 --fource 强制编译'
+# build_file = '文件将被编译'
+# run_save_e = '字节码编译失败'
+# run_source_not_aub_e = '运行的文件不是.aun文件'
+# run_bt_not_aub_e = '运行的文件不是.aub文件'
+# run_load_bt_e = '字节码加载失败'
+# run_file_aun_aub_e = '运行的文件不是.aun或.aub文件'
+# run_file_not_exists_e = '需要运行的文件不存在'
+# build_in_aun_e = '被编译的文件不是.aun文件'
+# build_out_aub_e = '编译输出的文件不是.aun文件'
+# build_error_e = '编译失败'
+# Interrupt_n = '中断'

+ 2 - 1
src/core/__reader.h

@@ -10,7 +10,7 @@
 
 typedef struct af_Reader af_Reader;
 
-typedef size_t readerFunc(void *data, char *dest, size_t len, bool *is_end);
+typedef size_t readerFunc(void *data, char *dest, size_t len, int *mode);
 NEW_DLC_SYMBOL(readerFunc, readerFunc);
 
 typedef void destructReaderFunc(void *data);
@@ -26,6 +26,7 @@ struct af_Reader {
     size_t buf_size;  // buf的长度-1
     char *read;
     bool read_end;
+    bool read_error;
     FileLine line;
 
     bool init;  // 是否初始化

+ 6 - 1
src/core/lexical.c

@@ -331,13 +331,18 @@ af_TokenType getTokenFromLexical(char **text, af_Parser *parser) {
     if (parser->lexical->is_end) {
         *text = NULL;
         return TK_EOF;
-    } else if (parser->lexical->is_error) {
+    } else if (parser->lexical->is_error || parser->reader->read_error) {
         *text = NULL;
         return TK_ERROR;
     }
 
     while (1) {
         char ch = getChar(parser->reader);
+        if (parser->reader->read_error) {
+            *text = NULL;
+            return TK_ERROR;
+        }
+
         if (isascii(ch) && iscntrl(ch) && !isspace(ch) && ch != NUL)  // ascii 控制字符
             printLexicalWarning(INCULDE_CONTROL(base), parser);
 

+ 20 - 21
src/core/parser.c

@@ -59,13 +59,13 @@ struct readerDataString {
     size_t len;
 };
 
-static size_t readFuncString(struct readerDataString *data, char *dest, size_t len, bool *read_end) {
+static size_t readFuncString(struct readerDataString *data, char *dest, size_t len, int *mode) {
     if (data->index == data->len)  // 读取到末尾
         return 0;
 
     if (data->index + len > data->len) {  // 超出长度范围
         len = data->len - data->index;
-        *read_end = true;
+        *mode = READER_MODE_FINISHED;
     }
     memcpy(dest, data->str + data->index, len);
     data->index += len;
@@ -95,12 +95,12 @@ struct readerDataFile {
     bool no_first;
 };
 
-static size_t readFuncFile(struct readerDataFile *data, char *dest, size_t len, bool *read_end) {
+static size_t readFuncFile(struct readerDataFile *data, char *dest, size_t len, int *mode) {
     if (!data->no_first) {
         data->no_first = true;
         char ch;
         if (fread(&ch, sizeof(char), 1, data->file) != 1) {
-            *read_end = true;
+            *mode = READER_MODE_FINISHED;
             return 0;
         }
 
@@ -108,7 +108,9 @@ static size_t readFuncFile(struct readerDataFile *data, char *dest, size_t len,
             /* 处理BOM编码 */
             char ch_[2];
             if (fread(ch_, sizeof(char), 2, data->file) != 2 || ch_[0] != (char)0xBB || ch_[1] != (char)0xBF) {
-                *read_end = true;
+                writeErrorLog(aFunCoreLogger, "Parser utf-8 with error BOM");
+                printf_stderr(0, HT_aFunGetText(error_bom, "Read utf-8 file with bad bom."));
+                *mode = READER_MODE_ERROR;
                 return 0;
             }
             writeTrackLog(aFunCoreLogger, "Parser utf-8 with BOM");
@@ -119,8 +121,11 @@ static size_t readFuncFile(struct readerDataFile *data, char *dest, size_t len,
     }
 
     size_t len_r =  fread(dest, sizeof(char), len, data->file);
-    if (CLEAR_FERROR(data->file) || feof(data->file))  // ferror在feof前执行
-        *read_end = true;
+    if (CLEAR_FERROR(data->file)) {  // ferror在feof前执行
+        *mode = READER_MODE_ERROR;
+        printf_stderr(0, HT_aFunGetText(stdin_error, "The file io error/eof"));
+    } else if (feof(data->file))
+        *mode = READER_MODE_FINISHED;
     return len_r;
 }
 
@@ -182,11 +187,12 @@ static bool getStdinSignalFunc(void) {
     return re;
 }
 
-static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len, bool *read_end) {
+static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len, int *mode) {
     if (data->index == data->len) {  // 读取内容
         if (CLEAR_STDIN()) {
-            writeErrorLog(aFunCoreLogger, "The strin IO error, %d, %d", ferror(stdin), feof(stdin));
-            *read_end = true;
+            writeErrorLog(aFunCoreLogger, "The stdin IO error, %d, %d", ferror(stdin), feof(stdin));
+            printf_stderr(0, HT_aFunGetText(stdin_error, ""));
+            *mode = READER_MODE_ERROR;
             return 0;
         }
 
@@ -205,23 +211,15 @@ static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len
         while (!checkStdin()) {  // 无内容则一直循环等到
             if (getStdinSignalFunc()) {  // 设置了中断函数, 并且该函数返回0
                 printf_stdout(0, "\n %s \n", HT_aFunGetText(Interrupt_n, "Interrupt"));
-                *read_end = true;
+                *mode = READER_MODE_ERROR;
                 return 0;
             }
         }
 
         int ch = fgetc_stdin();
-        if (ferror(stdin) || feof(stdin)) {  // 被中断
-            clearerr(stdin);
-            resetStdinSignalFunc();
-            printf_stdout(0, "\n %s \n", HT_aFunGetText(Interrupt_n, "Interrupt"));
-            *read_end = true;
-            return 0;
-        }
-
         if (ch == '\n' || ch == EOF) {
             /* 读取结束 */
-            *read_end = true;
+            *mode = READER_MODE_FINISHED;
             return 0;
         }
 
@@ -230,7 +228,8 @@ static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len
         /* 读取内容的长度不得少于STDIN_MAX_SZIE, 否则可能导致编码转换错误 */
         if (fgets_stdin(&data->data, STDIN_MAX_SIZE) == 0) {
             writeErrorLog(aFunCoreLogger, "The stdin buf too large (> %d)", STDIN_MAX_SIZE);
-            *read_end = true;
+            printf_stderr(0, "%s (> %d)", HT_aFunGetText(stdin_too_large, "The stdin buf too large"), STDIN_MAX_SIZE);
+            *mode = READER_MODE_ERROR;
             return 0;
         }
 

+ 18 - 2
src/core/reader.c

@@ -41,6 +41,7 @@ void *getReaderData(af_Reader *reader) {
 
 char *readWord(size_t del_index, af_Reader *reader) {
     char *re;
+    int mode = READER_MODE_NORMAL;
     reader->read = reader->buf;  // 重置指针
 
     if (del_index == 0)
@@ -53,12 +54,19 @@ char *readWord(size_t del_index, af_Reader *reader) {
     if (!reader->read_end) { // 没到尾部, 则写入数据
         char *write = reader->buf + STR_LEN(reader->buf);  // 数据写入的位置
         size_t len_ = reader->buf_size - STR_LEN(reader->buf);
-        size_t len = GET_SYMBOL(reader->read_func)(reader->data, write, len_, &reader->read_end);
+        size_t len = GET_SYMBOL(reader->read_func)(reader->data, write, len_, &mode);
         if (len > len_)
             len = len_;
         *(write + len) = NUL;
     }
 
+    if (mode == READER_MODE_FINISHED)
+        reader->read_end = true;
+    else if (mode == READER_MODE_ERROR) {
+        reader->read_end = true;
+        reader->read_error = true;
+    }
+
     /* 计算行号 */
     for (char *tmp = re; *tmp != NUL; tmp ++) {
         if (*tmp == '\n')
@@ -85,11 +93,19 @@ char getChar(af_Reader *reader) {
     char *new_buf = NEW_STR(reader->buf_size + NEW_BUF_SIZE);
     memcpy(new_buf, reader->buf, reader->buf_size);
 
-    size_t len = GET_SYMBOL(reader->read_func)(reader->data, new_buf + reader->buf_size, NEW_BUF_SIZE, &reader->read_end);
+    int mode = READER_MODE_NORMAL;
+    size_t len = GET_SYMBOL(reader->read_func)(reader->data, new_buf + reader->buf_size, NEW_BUF_SIZE, &mode);
     if (len > NEW_BUF_SIZE)
         len = NEW_BUF_SIZE;
     *(new_buf + reader->buf_size + len) = NUL;
 
+    if (mode == READER_MODE_FINISHED)
+        reader->read_end = true;
+    else if (mode == READER_MODE_ERROR) {
+        reader->read_end = true;
+        reader->read_error = true;
+    }
+
     free(reader->buf);
     reader->buf = new_buf;
     reader->buf_size = reader->buf_size + NEW_BUF_SIZE;

+ 1 - 1
src/core/syntactic.c

@@ -223,7 +223,7 @@ af_Code *parserCode(FilePath file, af_Parser *parser) {
     if (file == NULL)
         return NULL;
 
-    if (parser->is_error) {
+    if (parser->is_error || parser->reader->read_error || parser->lexical->is_error) {
         freeAllCode(code);
         return NULL;
     }

+ 1 - 1
test/src/reader.c

@@ -4,7 +4,7 @@
 /* 测试程序, 直接调用内部函数 */
 typedef struct af_Reader af_Reader;
 
-typedef size_t readerFunc(void *data, char *dest, size_t len, bool *is_end);
+typedef size_t readerFunc(void *data, char *dest, size_t len, int *mode);
 DEFINE_DLC_SYMBOL(readerFunc);
 
 typedef void destructReaderFunc(void *data);