__grammar.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "__grammar.h"
  2. inline void twoOperation(ParserMessage *pm, Inter *inter, PasersFunction callBack, GetSymbolFunction getSymbol,
  3. int type, int self_type, char *call_name, char *self_name, bool is_right) {
  4. bool is_right_ = false;
  5. while(true){
  6. Token *left_token = NULL;
  7. Token *right_token = NULL;
  8. struct Statement *st = NULL;
  9. readBackToken(pm);
  10. if (readBackToken(pm) != self_type){
  11. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  12. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, type, &st, NULL))
  13. goto return_;
  14. addStatementToken(self_type, st, pm);
  15. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  16. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  17. continue;
  18. }
  19. left_token = popAheadToken(pm);
  20. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
  21. if (getSymbol(CALLPASERSSIGNATURE, readBackToken(pm), &st)){
  22. delToken(pm);
  23. }
  24. else{
  25. backToken_(pm, left_token);
  26. goto return_;
  27. }
  28. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  29. "%s: get symbol success\n%s: call %s[right]\n", self_name, self_name, call_name);
  30. callBack(CALLPASERSSIGNATURE); // 获得右值
  31. if (!call_success(pm)){
  32. freeToken(left_token, true, false);
  33. freeStatement(st);
  34. goto return_;
  35. }
  36. if (readBackToken(pm) != type){ // 若非正确数值
  37. syntaxError(pm, syntax_error, 3, "ERROR from ", self_name, "(get right)");
  38. freeToken(left_token, true, true);
  39. freeStatement(st);
  40. goto return_;
  41. }
  42. right_token = popAheadToken(pm);
  43. addToken_(pm, setOperationFromToken(&st, left_token, right_token, self_type, is_right_));
  44. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  45. "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  46. is_right_ = is_right; // 第一次is_right不生效
  47. }
  48. return_:
  49. return;
  50. }
  51. inline void tailOperation(PASERSSIGNATURE, PasersFunction callBack, TailFunction tailFunction, int type, int self_type,
  52. char *call_name, char *self_name){
  53. while(true){
  54. Token *left_token = NULL;
  55. struct Statement *st = NULL;
  56. if (readBackToken(pm) != self_type){
  57. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  58. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, type, &st, NULL))
  59. goto return_;
  60. addStatementToken(self_type, st, pm);
  61. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  62. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  63. continue;
  64. }
  65. left_token = popAheadToken(pm);
  66. int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
  67. if (tail_status == -1){
  68. backToken_(pm, left_token);
  69. goto return_;
  70. }
  71. else if(!tail_status){
  72. goto error_;
  73. }
  74. addStatementToken(self_type, st, pm);
  75. freeToken(left_token, true, false);
  76. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
  77. continue;
  78. error_:
  79. freeToken(left_token, true, true);
  80. goto return_;
  81. }
  82. return_:
  83. return;
  84. }
  85. /**
  86. * syntax错误处理器
  87. * @param pm
  88. * @param message 错误信息
  89. * @param status 错误类型
  90. */
  91. void syntaxError(ParserMessage *pm, int status, int num, ...) {
  92. if (pm->status != success)
  93. return;
  94. char *message = NULL;
  95. va_list message_args;
  96. if (status <= 0){
  97. message = "Not message";
  98. goto not_message;
  99. }
  100. va_start(message_args, num);
  101. for (int i=0; i < num; i++) {
  102. char *new_message;
  103. new_message = memStrcat(message, va_arg(message_args, char *));
  104. memFree(message);
  105. message = new_message;
  106. }
  107. va_end(message_args);
  108. not_message:
  109. pm->status = status;
  110. pm->status_message = message;
  111. }
  112. int readBackToken(ParserMessage *pm){
  113. writeLog(pm->grammar_debug, GRAMMAR_DEBUG, "token operation number : %d\n", pm->count);
  114. writeLog(pm->paser_debug, DEBUG, "\ntoken operation number : %d\n", pm->count);
  115. pm->count ++;
  116. Token *tmp = popNewToken(pm->tm, pm->paser_debug);
  117. if (tmp->token_type == -2){
  118. freeToken(tmp, true, false);
  119. syntaxError(pm, lexical_error, 1, "lexical make some error");
  120. }
  121. addBackToken(pm->tm->ts, tmp, pm->paser_debug);
  122. return tmp->token_type;
  123. }
  124. Token *popAheadToken(ParserMessage *pm){
  125. doubleLog(pm, GRAMMAR_DEBUG, DEBUG, "token operation number : %d\n", pm->count ++);
  126. return popNewToken(pm->tm, pm->paser_debug);
  127. }
  128. bool checkToken_(ParserMessage *pm, int type){
  129. if (readBackToken(pm) != type)
  130. return false;
  131. delToken(pm);
  132. return true;
  133. }
  134. bool commandCallControl_(PASERSSIGNATURE, MakeControlFunction callBack, int type, Statement **st, char *message){
  135. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, message, NULL);
  136. Token *tmp_token = NULL;
  137. parserControl(CALLPASERSSIGNATURE, callBack, type);
  138. if (!call_success(pm) || readBackToken(pm) != type)
  139. return false;
  140. tmp_token = popAheadToken(pm);
  141. *st = tmp_token->data.st;
  142. freeToken(tmp_token, true, false);
  143. return true;
  144. }
  145. inline bool commandCallBack_(PASERSSIGNATURE, PasersFunction callBack, int type, Statement **st, char *message){
  146. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, message, NULL);
  147. return callChildStatement(CALLPASERSSIGNATURE, callBack, type, st, NULL);
  148. }
  149. bool callParserCode(PASERSSIGNATURE, Statement **st,char *message){
  150. Statement *new_st = NULL;
  151. if(!callChildStatement(CALLPASERSSIGNATURE, parserCode, CODE, &new_st, message)){
  152. return false;
  153. }
  154. if (*st != NULL)
  155. freeStatement(*st);
  156. *st = new_st;
  157. return true;
  158. }
  159. bool callParserAs(PASERSSIGNATURE, Statement **st,char *message){
  160. if (readBackToken(pm) == MATHER_AS) {
  161. delToken(pm);
  162. return callChildStatement(CALLPASERSSIGNATURE, parserOperation, OPERATION, st, message);
  163. }
  164. else
  165. *st = NULL;
  166. return true;
  167. }
  168. bool callChildToken(ParserMessage *pm, Inter *inter, PasersFunction callBack, int type, Token **tmp, char *message,
  169. int error_type) {
  170. callBack(CALLPASERSSIGNATURE);
  171. if (!call_success(pm)) {
  172. *tmp = NULL;
  173. return false;
  174. }
  175. if (readBackToken(pm) != type) {
  176. *tmp = NULL;
  177. if (message != NULL)
  178. syntaxError(pm, error_type, 1, message);
  179. return false;
  180. }
  181. *tmp = popAheadToken(pm);
  182. return true;
  183. }
  184. bool callChildStatement(PASERSSIGNATURE, PasersFunction callBack, int type, Statement **st, char *message){
  185. Token *tmp = NULL;
  186. bool status = callChildToken(CALLPASERSSIGNATURE, callBack, type, &tmp, message, syntax_error);
  187. if (!status){
  188. *st = NULL;
  189. return false;
  190. }
  191. *st = tmp->data.st;
  192. freeToken(tmp, true, false);
  193. return true;
  194. }