__grammar.c 7.0 KB

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