lexical.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "__virtualmath.h"
  2. /**
  3. * 从文件中读取一个字节,并处理is_back
  4. * 每次从文件中读取字符时,则会保存字符到back.p中,调用backChar回退一个字符的时候则不需要传入字符了
  5. * @param file
  6. * @return 返回一个字符,若为EOF则返回-1
  7. */
  8. wint_t readChar(LexFile *file){
  9. if (file->back.is_back)
  10. file->back.is_back = false;
  11. else {
  12. if (file->status == 2) {
  13. file->back.p = file->str[file->seek];
  14. if (file->back.p != NUL)
  15. file->seek ++;
  16. else
  17. file->back.p = WEOF;
  18. } else
  19. file->back.p = fgetwc(file->file);
  20. }
  21. if (file->back.p == L'\n')
  22. file->line++;
  23. return file->back.p;
  24. }
  25. /**
  26. * 设置字符回退
  27. * @param file
  28. */
  29. void backChar(LexFile *file){
  30. file->back.is_back = true;
  31. if (file->back.p == L'\n')
  32. file->line --;
  33. }
  34. void clearLexFile(LexFile *file) {
  35. wint_t ch;
  36. backChar(file);
  37. while ((ch = readChar(file)) != L'\n' && ch != WEOF)
  38. PASS;
  39. }
  40. static LexFile *makeLexCore(){
  41. LexFile *tmp = memCalloc(1, sizeof(LexFile));
  42. tmp->status = 1;
  43. tmp->file = stdin;
  44. tmp->str = NULL;
  45. tmp->seek = 0;
  46. tmp->back.is_back = false;
  47. tmp->back.p = WEOF;
  48. tmp->line = 1;
  49. tmp->filter_data.enter = 0;
  50. return tmp;
  51. }
  52. LexFile *makeLexFile(char *dir){
  53. LexFile *tmp = makeLexCore();
  54. tmp->status = dir == NULL ? 1 : 0;
  55. if (dir != NULL) {
  56. tmp->file = fopen(dir, "r");
  57. tmp->status = 0;
  58. }
  59. return tmp;
  60. }
  61. LexFile *makeLexStr(wchar_t *str){
  62. LexFile *tmp = makeLexCore();
  63. tmp->status = 2;
  64. tmp->file = NULL;
  65. tmp->str = memWidecpy(str);
  66. return tmp;
  67. }
  68. void freeLexFile(LexFile *file) {
  69. FREE_BASE(file, return_);
  70. if (file->status == 0)
  71. fclose(file->file);
  72. memFree(file->str);
  73. memFree(file);
  74. return_:
  75. return;
  76. }
  77. /**
  78. * 初始化mather,代码被复用
  79. * @param mather
  80. */
  81. void setupMather(LexMather *mather){
  82. mather->len = 0;
  83. mather->ascii = 0;
  84. mather->str = NULL;
  85. mather->second_str = NULL;
  86. mather->string_type = L'"';
  87. mather->status = LEXMATHER_START;
  88. }
  89. LexMather *makeMather(){
  90. LexMather *tmp = memCalloc(1, sizeof(LexMather));
  91. setupMather(tmp);
  92. return tmp;
  93. }
  94. void freeMather(LexMather *mather) {
  95. memFree(mather->str);
  96. memFree(mather->second_str);
  97. mather->len = 0;
  98. memFree(mather);
  99. }
  100. LexMathers *makeMathers(int size){
  101. LexMathers *tmp = memCalloc(1, sizeof(LexMathers));
  102. tmp->size = size;
  103. tmp->mathers = (struct LexMather**)memCalloc(size, sizeof(LexMather*));
  104. for(int i=0;i < size; i++)
  105. tmp->mathers[i] = makeMather();
  106. return tmp;
  107. }
  108. void freeMathers(LexMathers *mathers) {
  109. FREE_BASE(mathers, return_);
  110. for(int i=0;i < mathers->size; i++)
  111. freeMather(mathers->mathers[i]);
  112. memFree(mathers->mathers);
  113. mathers->size = 0;
  114. memFree(mathers);
  115. return_:
  116. return;
  117. }
  118. /**
  119. * 初始化mathers,本质是初始化mathers.mathers内所有的mather
  120. * @param mathers
  121. */
  122. void setupMathers(LexMathers *mathers){
  123. for (int i=0;i < mathers->size;i++){
  124. if(mathers->mathers[i]->str != NULL){
  125. memFree(mathers->mathers[i]->str);
  126. memFree(mathers->mathers[i]->second_str);
  127. }
  128. setupMather(mathers->mathers[i]);
  129. }
  130. }
  131. /**
  132. * 检查mathers中mather的匹配情况。
  133. * 情况1:只出现一个匹配器处于END状态,其他均处于MISTAKE或者END_SECOND状态,则视为匹配成功,返回END状态的匹配器
  134. * 情况2:只出现一个匹配器处于END_SECOND状态,其他均处于MISTAKE状态无END状态,则视为匹配成功,返回END_SECOND状态的匹配器
  135. * 情况3:全部都在MISTAKE,返回-2,匹配失败
  136. * 其他情况:匹配还未完成,返回-1
  137. * @param mathers
  138. * @param max
  139. * @return
  140. */
  141. int checkoutMather(LexMathers *mathers, int max) {
  142. bool return_1 = false;
  143. int mistake_count = 0;
  144. int end_count = 0, end_index = -1;
  145. int end_second_count = 0, end_second_index = -1;
  146. for (int i=0;i < mathers->size;i++){
  147. if(mathers->mathers[i]->status == LEXMATHER_END_1){
  148. end_count ++;
  149. end_index = i;
  150. }
  151. else if(mathers->mathers[i]->status == LEXMATHER_END_2){
  152. end_second_count ++;
  153. end_second_index = i;
  154. }
  155. else if(mathers->mathers[i]->status == LEXMATHER_MISTAKE)
  156. mistake_count ++;
  157. else
  158. return_1 = true;
  159. }
  160. if (return_1)
  161. goto return_;
  162. if (mistake_count == max)
  163. return -2;
  164. else if(end_count == 1)
  165. return end_index;
  166. else if(end_second_count == 1)
  167. return end_second_index;
  168. return_:
  169. return -1;
  170. }