parser.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, bool *read_end) {
  52. if (data->index == data->len) // 读取到末尾
  53. return 0;
  54. if (data->index + len > data->len) { // 超出长度范围
  55. len = data->len - data->index;
  56. *read_end = true;
  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, bool *read_end) {
  83. if (!data->no_first) {
  84. data->no_first = true;
  85. char ch;
  86. if (fread(&ch, sizeof(char), 1, data->file) != 1) {
  87. *read_end = true;
  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. *read_end = true;
  95. return 0;
  96. }
  97. writeTrackLog(aFunCoreLogger, "Parser utf-8 with BOM");
  98. } else {
  99. ungetc(ch, data->file);
  100. writeTrackLog(aFunCoreLogger, "Parser utf-8 without BOM");
  101. }
  102. }
  103. size_t len_r = fread(dest, sizeof(char), len, data->file);
  104. if (CLEAR_FERROR(data->file) || feof(data->file)) // ferror在feof前执行
  105. *read_end = true;
  106. return len_r;
  107. }
  108. static void destructFile(struct readerDataFile *data) {
  109. if (data->file != NULL)
  110. fclose(data->file);
  111. }
  112. af_Parser *makeParserByFile(FilePath path){
  113. FILE *file = fopen(path, "rb");
  114. if (file == NULL) {
  115. writeErrorLog(aFunCoreLogger, "File open error: %s", path);
  116. return NULL;
  117. } else
  118. writeTrackLog(aFunCoreLogger, "File: %s", path);
  119. DLC_SYMBOL(readerFunc) read_func = MAKE_SYMBOL(readFuncFile, readerFunc);
  120. DLC_SYMBOL(destructReaderFunc) destruct = MAKE_SYMBOL(destructFile, destructReaderFunc);
  121. af_Parser *parser = makeParser(read_func, destruct, sizeof(struct readerDataString));
  122. ((struct readerDataFile *)parser->reader->data)->file = file;
  123. initParser(parser);
  124. FREE_SYMBOL(read_func);
  125. FREE_SYMBOL(destruct);
  126. return parser;
  127. }
  128. struct readerDataStdin {
  129. bool no_first;
  130. void *sig_int;
  131. void *sig_term;
  132. char *data;
  133. size_t index;
  134. size_t len;
  135. };
  136. static sig_atomic_t stdin_interrupt = 0;
  137. static void stdinSignalFunc(int signum) {
  138. stdin_interrupt = 1;
  139. }
  140. static void setStdinSignalFunc(struct readerDataStdin *data) {
  141. data->sig_int = signal(SIGINT, stdinSignalFunc);
  142. data->sig_term = signal(SIGTERM, stdinSignalFunc);
  143. }
  144. static void resetStdinSignalFunc(void) {
  145. stdin_interrupt = 0;
  146. signal(SIGINT, stdinSignalFunc);
  147. signal(SIGTERM, stdinSignalFunc);
  148. }
  149. static bool getStdinSignalFunc(void) {
  150. bool re = stdin_interrupt == 1;
  151. stdin_interrupt = 0;
  152. resetStdinSignalFunc();
  153. return re;
  154. }
  155. static size_t readFuncStdin(struct readerDataStdin *data, char *dest, size_t len, bool *read_end) {
  156. if (data->index == data->len) { // 读取内容
  157. if (CLEAR_STDIN()) {
  158. writeErrorLog(aFunCoreLogger, "The strin IO error, %d, %d", ferror(stdin), feof(stdin));
  159. *read_end = true;
  160. return 0;
  161. }
  162. if (data->no_first)
  163. fputs("\r.... ", stdout);
  164. else
  165. fputs("\r>>>> ", stdout);
  166. fflush(stdout);
  167. data->no_first = true;
  168. free(data->data);
  169. while (!checkStdin()) { // 无内容则一直循环等到
  170. if (getStdinSignalFunc()) { // 设置了中断函数, 并且该函数返回0
  171. printf_stdout(0, "\n %s \n", HT_aFunGetText(Interrupt_n, "Interrupt"));
  172. *read_end = true;
  173. return 0;
  174. }
  175. }
  176. int ch = fgetchar_stdin();
  177. if (ferror(stdin) || feof(stdin)) { // 被中断
  178. clearerr(stdin);
  179. resetStdinSignalFunc();
  180. printf_stdout(0, "\n %s \n", HT_aFunGetText(Interrupt_n, "Interrupt"));
  181. *read_end = true;
  182. return 0;
  183. }
  184. if (ch == '\n' || ch == EOF) {
  185. /* 读取结束 */
  186. *read_end = true;
  187. return 0;
  188. }
  189. fungetc_stdin(ch);
  190. if (fgets_stdin(&data->data, STDIN_MAX_SIZE) == 0) {
  191. writeErrorLog(aFunCoreLogger, "The stdin buf too large (> %d)", STDIN_MAX_SIZE);
  192. *read_end = true;
  193. return 0;
  194. }
  195. data->index = 0;
  196. data->len = STR_LEN(data->data);
  197. }
  198. if (data->index + len > data->len) // 超出长度范围
  199. len = data->len - data->index;
  200. memcpy(dest, data->data + data->index, len);
  201. data->index += len;
  202. return len;
  203. }
  204. static void destructStdin(struct readerDataStdin *data) {
  205. free(data->data);
  206. if (data->sig_int != SIG_ERR)
  207. signal(SIGINT, data->sig_int);
  208. if (data->sig_term != SIG_ERR)
  209. signal(SIGTERM, data->sig_term);
  210. }
  211. static void initStdin(struct readerDataStdin *data) {
  212. stdin_interrupt = 0;
  213. setStdinSignalFunc(data);
  214. }
  215. af_Parser *makeParserByStdin(){
  216. if (CLEAR_FERROR(stdin))
  217. return NULL;
  218. DLC_SYMBOL(readerFunc) read_func = MAKE_SYMBOL(readFuncStdin, readerFunc);
  219. DLC_SYMBOL(destructReaderFunc) destruct = MAKE_SYMBOL(destructStdin, destructReaderFunc);
  220. af_Parser *parser = makeParser(read_func, destruct, sizeof(struct readerDataStdin));
  221. initStdin(parser->reader->data);
  222. initParser(parser);
  223. FREE_SYMBOL(read_func);
  224. FREE_SYMBOL(destruct);
  225. return parser;
  226. }