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