2
0

parser.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * 文件名: parser.c
  3. * 目标: __parser.h中结构体的相关函数
  4. */
  5. #include "aFunCore.h"
  6. #include "tool.h"
  7. #include "__parser.h"
  8. static af_Lexical *makeLexical(void);
  9. static void freeLexical(af_Lexical *lex);
  10. static af_Syntactic *makeSyntactic(void);
  11. static void freeSyntactic(af_Syntactic *syntactic);
  12. af_Parser *makeParser(DLC_SYMBOL(readerFunc) read_func, DLC_SYMBOL(destructReaderFunc) destruct_func, size_t data_size){
  13. af_Parser *parser = calloc(1, sizeof(af_Parser));
  14. parser->reader = makeReader(read_func, destruct_func, data_size);
  15. parser->lexical = makeLexical();
  16. parser->syntactic = makeSyntactic();
  17. return parser;
  18. }
  19. void freeParser(af_Parser *parser) {
  20. freeReader(parser->reader);
  21. freeLexical(parser->lexical);
  22. freeSyntactic(parser->syntactic);
  23. free(parser);
  24. }
  25. void initParser(af_Parser *parser) {
  26. initReader(parser->reader);
  27. }
  28. static af_Lexical *makeLexical(void) {
  29. af_Lexical *lex = calloc(1, sizeof(af_Lexical));
  30. lex->status = lex_begin;
  31. return lex;
  32. }
  33. static void freeLexical(af_Lexical *lex) {
  34. free(lex);
  35. }
  36. static af_Syntactic *makeSyntactic(void) {
  37. af_Syntactic *syntactic = calloc(1, sizeof(af_Syntactic));
  38. return syntactic;
  39. }
  40. static void freeSyntactic(af_Syntactic *syntactic) {
  41. free(syntactic->text);
  42. free(syntactic);
  43. }
  44. /* makeParser函数封装 */
  45. struct readerDataString {
  46. char *str;
  47. bool free_str;
  48. size_t index;
  49. size_t len;
  50. };
  51. static size_t readFuncString(struct readerDataString *data, char *dest, size_t len, int *mode) {
  52. if (data->index == data->len) // 读取到末尾
  53. return 0;
  54. if (data->index + len > data->len) { // 超出长度范围
  55. len = data->len - data->index;
  56. *mode = READER_MODE_FINISHED;
  57. }
  58. memcpy(dest, data->str + data->index, len);
  59. data->index += len;
  60. return len;
  61. }
  62. static void destructFunc(struct readerDataString *data) {
  63. if (data->free_str)
  64. free(data->str);
  65. }
  66. af_Parser *makeParserByString(char *str, bool free_str){
  67. DLC_SYMBOL(readerFunc) read_func = MAKE_SYMBOL(readFuncString, readerFunc);
  68. DLC_SYMBOL(destructReaderFunc) destruct = MAKE_SYMBOL(destructFunc, destructReaderFunc);
  69. af_Parser *parser = makeParser(read_func, destruct, sizeof(struct readerDataString));
  70. ((struct readerDataString *)parser->reader->data)->str = str;
  71. ((struct readerDataString *)parser->reader->data)->free_str = free_str;
  72. ((struct readerDataString *)parser->reader->data)->len = strlen(str);
  73. initParser(parser);
  74. FREE_SYMBOL(read_func);
  75. FREE_SYMBOL(destruct);
  76. return parser;
  77. }
  78. struct readerDataFile {
  79. FILE *file;
  80. bool no_first;
  81. };
  82. static size_t readFuncFile(struct readerDataFile *data, char *dest, size_t len, int *mode) {
  83. if (!data->no_first) {
  84. data->no_first = true;
  85. char ch;
  86. if (fread(&ch, sizeof(char), 1, data->file) != 1) {
  87. *mode = READER_MODE_FINISHED;
  88. return 0;
  89. }
  90. if (ch == (char)0xEF) {
  91. /* 处理BOM编码 */
  92. char ch_[2];
  93. if (fread(ch_, sizeof(char), 2, data->file) != 2 || ch_[0] != (char)0xBB || ch_[1] != (char)0xBF) {
  94. writeErrorLog(aFunCoreLogger, "Parser utf-8 with error BOM");
  95. printf_stderr(0, HT_aFunGetText(error_bom, "Read utf-8 file with bad bom."));
  96. *mode = READER_MODE_ERROR;
  97. return 0;
  98. }
  99. writeTrackLog(aFunCoreLogger, "Parser utf-8 with BOM");
  100. } else {
  101. ungetc(ch, data->file);
  102. writeTrackLog(aFunCoreLogger, "Parser utf-8 without BOM");
  103. }
  104. }
  105. size_t len_r = fread(dest, sizeof(char), len, data->file);
  106. if (CLEAR_FERROR(data->file)) { // ferror在feof前执行
  107. *mode = READER_MODE_ERROR;
  108. printf_stderr(0, HT_aFunGetText(stdin_error, "The file io error/eof"));
  109. } else if (feof(data->file))
  110. *mode = READER_MODE_FINISHED;
  111. return len_r;
  112. }
  113. static void destructFile(struct readerDataFile *data) {
  114. if (data->file != NULL)
  115. fclose(data->file);
  116. }
  117. af_Parser *makeParserByFile(FilePath path){
  118. FILE *file = fopen(path, "rb");
  119. if (file == NULL) {
  120. writeErrorLog(aFunCoreLogger, "File open error: %s", path);
  121. return NULL;
  122. } else
  123. writeTrackLog(aFunCoreLogger, "File: %s", path);
  124. DLC_SYMBOL(readerFunc) read_func = MAKE_SYMBOL(readFuncFile, readerFunc);
  125. DLC_SYMBOL(destructReaderFunc) destruct = MAKE_SYMBOL(destructFile, destructReaderFunc);
  126. af_Parser *parser = makeParser(read_func, destruct, sizeof(struct readerDataString));
  127. ((struct readerDataFile *)parser->reader->data)->file = file;
  128. initParser(parser);
  129. FREE_SYMBOL(read_func);
  130. FREE_SYMBOL(destruct);
  131. return parser;
  132. }
  133. struct readerDataStdin {
  134. bool no_first;
  135. void *sig_int;
  136. void *sig_term;
  137. char *data;
  138. size_t index;
  139. size_t len;
  140. };
  141. static sig_atomic_t stdin_interrupt = 0;
  142. static void stdinSignalFunc(int signum) {
  143. stdin_interrupt = 1;
  144. }
  145. static void setStdinSignalFunc(struct readerDataStdin *data) {
  146. data->sig_int = signal(SIGINT, stdinSignalFunc);
  147. data->sig_term = signal(SIGTERM, stdinSignalFunc);
  148. }
  149. static void resetStdinSignalFunc(void) {
  150. stdin_interrupt = 0;
  151. signal(SIGINT, stdinSignalFunc);
  152. signal(SIGTERM, stdinSignalFunc);
  153. }
  154. static bool getStdinSignalFunc(void) {
  155. bool re = stdin_interrupt == 1;
  156. stdin_interrupt = 0;
  157. resetStdinSignalFunc();
  158. return re;
  159. }
  160. static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len, int *mode) {
  161. if (data->index == data->len) { // 读取内容
  162. if (CLEAR_STDIN()) {
  163. writeErrorLog(aFunCoreLogger, "The stdin IO error, %d, %d", ferror(stdin), feof(stdin));
  164. printf_stderr(0, HT_aFunGetText(stdin_error, ""));
  165. *mode = READER_MODE_ERROR;
  166. return 0;
  167. }
  168. if (data->no_first)
  169. fputs("\r.... ", stdout);
  170. else {
  171. fclear_stdin();
  172. fputs("\r>>>> ", stdout);
  173. }
  174. fflush(stdout);
  175. data->no_first = true;
  176. free(data->data);
  177. /* 在Linux平台, 只用当数据写入stdin缓冲行时checkStdin才true */
  178. /* 在Windows平台则是根据读取的最后一个字符是否为\n或者是否有按键按下来确定缓冲区是否有内容 */
  179. while (!checkStdin()) { // 无内容则一直循环等到
  180. if (getStdinSignalFunc()) { // 设置了中断函数, 并且该函数返回0
  181. printf_stdout(0, "\n %s \n", HT_aFunGetText(Interrupt_n, "Interrupt"));
  182. *mode = READER_MODE_ERROR;
  183. return 0;
  184. }
  185. }
  186. int ch = fgetc_stdin();
  187. if (ch == '\n' || ch == EOF) {
  188. /* 读取结束 */
  189. *mode = READER_MODE_FINISHED;
  190. return 0;
  191. }
  192. fungetc_stdin(ch);
  193. /* 读取内容的长度不得少于STDIN_MAX_SZIE, 否则可能导致编码转换错误 */
  194. if (fgets_stdin(&data->data, STDIN_MAX_SIZE) == 0) {
  195. writeErrorLog(aFunCoreLogger, "The stdin buf too large (> %d)", STDIN_MAX_SIZE);
  196. printf_stderr(0, "%s (> %d)", HT_aFunGetText(stdin_too_large, "The stdin buf too large"), STDIN_MAX_SIZE);
  197. *mode = READER_MODE_ERROR;
  198. return 0;
  199. }
  200. data->index = 0;
  201. data->len = STR_LEN(data->data);
  202. }
  203. if (data->index + len > data->len) // 超出长度范围
  204. len = data->len - data->index;
  205. memcpy(dest, data->data + data->index, len);
  206. data->index += len;
  207. return len;
  208. }
  209. static void destructStdin(struct readerDataStdin *data) {
  210. free(data->data);
  211. if (data->sig_int != SIG_ERR)
  212. signal(SIGINT, data->sig_int);
  213. if (data->sig_term != SIG_ERR)
  214. signal(SIGTERM, data->sig_term);
  215. }
  216. static void initStdin(struct readerDataStdin *data) {
  217. stdin_interrupt = 0;
  218. setStdinSignalFunc(data);
  219. }
  220. af_Parser *makeParserByStdin(){
  221. if (CLEAR_FERROR(stdin))
  222. return NULL;
  223. DLC_SYMBOL(readerFunc) read_func = MAKE_SYMBOL(readFuncStdin, readerFunc);
  224. DLC_SYMBOL(destructReaderFunc) destruct = MAKE_SYMBOL(destructStdin, destructReaderFunc);
  225. af_Parser *parser = makeParser(read_func, destruct, sizeof(struct readerDataStdin));
  226. initStdin(parser->reader->data);
  227. initParser(parser);
  228. FREE_SYMBOL(read_func);
  229. FREE_SYMBOL(destruct);
  230. return parser;
  231. }