__grammar.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "__grammar.h"
  2. inline void twoOperation(ParserMessage *pm, Inter *inter, void (*callBack)(ParserMessage *, Inter *),
  3. int (*getSymbol)(ParserMessage *, Inter *, int, Statement **), int type, int self_type,
  4. char *call_name, char *self_name, bool is_right) {
  5. bool is_right_ = false;
  6. while(true){
  7. Token *left_token = NULL, *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, "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  45. is_right_ = is_right; // 第一次is_right不生效
  46. }
  47. return_:
  48. return;
  49. }
  50. inline void tailOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*tailFunction)(PASERSSIGNATURE, Token *, Statement **), int type, int self_type, char *call_name, char *self_name){
  51. while(true){
  52. Token *left_token = NULL;
  53. struct Statement *st = NULL;
  54. if (readBackToken(pm) != self_type){
  55. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  56. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, type, &st, NULL))
  57. goto return_;
  58. addStatementToken(self_type, st, pm);
  59. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  60. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  61. continue;
  62. }
  63. left_token= popAheadToken(pm);
  64. int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
  65. if (tail_status == -1){
  66. backToken_(pm, left_token);
  67. goto return_;
  68. }
  69. else if(!tail_status){
  70. goto error_;
  71. }
  72. addStatementToken(self_type, st, pm);
  73. freeToken(left_token, true, false);
  74. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
  75. continue;
  76. error_:
  77. freeToken(left_token, true, true);
  78. goto return_;
  79. }
  80. return_:
  81. return;
  82. }
  83. /**
  84. * syntax错误处理器
  85. * @param pm
  86. * @param message 错误信息
  87. * @param status 错误类型
  88. */
  89. void syntaxError(ParserMessage *pm, int status, int num, ...) {
  90. if (pm->status != success)
  91. return;
  92. char *message = NULL;
  93. va_list message_args;
  94. if (status <= 0){
  95. message = "Not message";
  96. goto not_message;
  97. }
  98. va_start(message_args, num);
  99. for (int i=0; i < num; i++) {
  100. char *new_message;
  101. new_message = memStrcat(message, va_arg(message_args, char *));
  102. memFree(message);
  103. message = new_message;
  104. }
  105. va_end(message_args);
  106. not_message:
  107. pm->status = status;
  108. pm->status_message = message;
  109. }
  110. int readBackToken(ParserMessage *pm){
  111. writeLog(pm->grammar_debug, GRAMMAR_DEBUG, "token operation number : %d\n", pm->count);
  112. writeLog(pm->paser_debug, DEBUG, "\ntoken operation number : %d\n", pm->count);
  113. pm->count ++;
  114. Token *tmp = popNewToken(pm->tm, pm->paser_debug);
  115. if (tmp->token_type == -2){
  116. freeToken(tmp, true, false);
  117. syntaxError(pm, lexical_error, 1, "lexical make some error");
  118. }
  119. addBackToken(pm->tm->ts, tmp, pm->paser_debug);
  120. return tmp->token_type;
  121. }
  122. Token *popAheadToken(ParserMessage *pm){
  123. doubleLog(pm, GRAMMAR_DEBUG, DEBUG, "token operation number : %d\n", pm->count ++);
  124. return popNewToken(pm->tm, pm->paser_debug);
  125. }
  126. bool checkToken_(ParserMessage *pm, int type){
  127. if (readBackToken(pm) != type){
  128. return false;
  129. }
  130. delToken(pm);
  131. return true;
  132. }
  133. bool commandCallControl_(PASERSSIGNATURE, Statement *(*callBack)(Statement *), 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, void (*callBack)(PASERSSIGNATURE), 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, void (*call)(ParserMessage *, Inter *), int type, Token **tmp,
  168. char *message, int error_type) {
  169. call(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, void (*call)(PASERSSIGNATURE), int type, Statement **st, char *message){
  184. Token *tmp = NULL;
  185. bool status = callChildToken(CALLPASERSSIGNATURE, call, 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. }