__grammar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "__grammar.h"
  2. /**
  3. * 二元匹配器
  4. * twoOperation:
  5. * | callBack
  6. * | twoOperation getSymbol callBack
  7. * @param callBack 符号左、右值匹配函数
  8. * @param getSymbol 符号处理函数
  9. * @param call_type 左、右值类型
  10. * @param self_type 输出token的类型
  11. * @param call_name 左、右值名称(log)
  12. * @param self_name 输出值名称(log)
  13. * @param is_right 表达式是否从右运算到左
  14. */
  15. inline void twoOperation(ParserMessage *pm, Inter *inter, PasersFunction callBack, GetSymbolFunction getSymbol,
  16. int call_type, int self_type, char *call_name, char *self_name, bool is_right) {
  17. bool is_right_ = false;
  18. while(true){
  19. Token *left_token = NULL;
  20. Token *right_token = NULL;
  21. Statement *st = NULL;
  22. long int line = 0;
  23. if (readBackToken(pm) != self_type){
  24. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  25. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, call_type, &st, NULL))
  26. goto return_;
  27. addStatementToken(self_type, st, pm);
  28. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  29. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  30. continue;
  31. }
  32. left_token = popAheadToken(pm);
  33. line = left_token->line;
  34. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
  35. if (getSymbol(CALLPASERSSIGNATURE, readBackToken(pm), &st))
  36. delToken(pm);
  37. else{
  38. backToken_(pm, left_token);
  39. goto return_;
  40. }
  41. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  42. "%s: get symbol success\n%s: call %s[right]\n", self_name, self_name, call_name);
  43. callBack(CALLPASERSSIGNATURE); // 获得右值
  44. if (!call_success(pm)){
  45. freeToken(left_token, true, true);
  46. freeStatement(st);
  47. goto return_;
  48. }
  49. if (readBackToken(pm) != call_type){ // 若非正确数值
  50. syntaxError(pm, syntax_error, line, 3, "ERROR from ", self_name, "(get right)");
  51. freeToken(left_token, true, true);
  52. freeStatement(st);
  53. goto return_;
  54. }
  55. right_token = popAheadToken(pm);
  56. addToken_(pm, setOperationFromToken(&st, left_token, right_token, self_type, is_right_));
  57. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  58. "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  59. is_right_ = is_right; // 第一次is_right不生效
  60. }
  61. return_:
  62. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: return\n", self_name);
  63. }
  64. /**
  65. * 尾巴一元匹配器
  66. * tailOperation:
  67. * | callBack
  68. * | tailOperation tailFunction
  69. * @param callBack 符号左、右值匹配函数
  70. * @param tailFunction 尾巴处理函数
  71. * @param call_type 左、右值类型
  72. * @param self_type 输出token的类型
  73. * @param call_name 左、右值名称(log)
  74. * @param self_name 输出值名称(log)
  75. */
  76. inline void tailOperation(PASERSSIGNATURE, PasersFunction callBack, TailFunction tailFunction, int call_type,
  77. int self_type, char *call_name, char *self_name){
  78. while(true){
  79. Token *left_token = NULL;
  80. struct Statement *st = NULL;
  81. if (readBackToken(pm) != self_type){
  82. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  83. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, call_type, &st, NULL))
  84. goto return_;
  85. addStatementToken(self_type, st, pm);
  86. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  87. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  88. continue;
  89. }
  90. left_token = popAheadToken(pm);
  91. int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
  92. if (tail_status == -1){
  93. backToken_(pm, left_token);
  94. goto return_;
  95. }
  96. else if(tail_status == 0) {
  97. freeToken(left_token, true, true);
  98. goto return_;
  99. }
  100. addStatementToken(self_type, st, pm);
  101. freeToken(left_token, true, false);
  102. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
  103. }
  104. return_:
  105. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: return\n", self_name);
  106. }
  107. /**
  108. * syntax错误处理器
  109. * @param pm
  110. * @param message 错误信息
  111. * @param status 错误类型
  112. */
  113. void syntaxError(ParserMessage *pm, int status, long int line, int num, ...) {
  114. char *message = NULL;
  115. if (pm->status != success)
  116. return;
  117. if (status <= 0){
  118. message = memStrcpy("Not Message", 0, false, false);
  119. goto not_message;
  120. }
  121. va_list message_args;
  122. va_start(message_args, num);
  123. for (int i=0; i < num; i++)
  124. message = memStrcat(message, va_arg(message_args, char *), true);
  125. va_end(message_args);
  126. char info[100];
  127. snprintf(info, 100, "\non line %ld\nin file ", line);
  128. message = memStrcat(message, info, true);
  129. message = memStrcat(message, pm->file, true);
  130. not_message:
  131. pm->status = status;
  132. pm->status_message = message;
  133. }
  134. int readBackToken(ParserMessage *pm){
  135. writeLog(pm->grammar_debug, GRAMMAR_DEBUG, "token operation number : %d\n", pm->count);
  136. writeLog(pm->paser_debug, DEBUG, "\ntoken operation number : %d\n", pm->count);
  137. pm->count ++;
  138. Token *tmp = popNewToken(pm->tm, pm->paser_debug);
  139. if (tmp->token_type == -2){
  140. freeToken(tmp, true, false);
  141. syntaxError(pm, lexical_error, tmp->line, 1, "lexical make some error");
  142. }
  143. addBackToken(pm->tm->ts, tmp, pm->paser_debug);
  144. return tmp->token_type;
  145. }
  146. Token *popAheadToken(ParserMessage *pm){
  147. doubleLog(pm, GRAMMAR_DEBUG, DEBUG, "token operation number : %d\n", pm->count ++);
  148. return popNewToken(pm->tm, pm->paser_debug);
  149. }
  150. bool checkToken_(ParserMessage *pm, int type){
  151. if (readBackToken(pm) != type)
  152. return false;
  153. delToken(pm);
  154. return true;
  155. }
  156. bool commandCallControl_(ParserMessage *pm, Inter *inter, MakeControlFunction callBack, int type, Statement **st,
  157. char *log_message, bool must_operation, char *error_message) {
  158. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, log_message, NULL);
  159. Token *tmp_token = NULL;
  160. *st = NULL;
  161. parserControl(CALLPASERSSIGNATURE, callBack, type, must_operation, error_message);
  162. if (!call_success(pm) || readBackToken(pm) != type)
  163. return false;
  164. tmp_token = popAheadToken(pm);
  165. *st = tmp_token->data.st;
  166. freeToken(tmp_token, true, false);
  167. return true;
  168. }
  169. inline bool commandCallBack_(PASERSSIGNATURE, PasersFunction callBack, int type, Statement **st, char *message){
  170. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, message, NULL);
  171. return callChildStatement(CALLPASERSSIGNATURE, callBack, type, st, NULL);
  172. }
  173. bool callParserCode(PASERSSIGNATURE, Statement **st,char *message){
  174. Statement *new_st = NULL;
  175. *st = NULL;
  176. if(!callChildStatement(CALLPASERSSIGNATURE, parserCode, CODE, &new_st, message))
  177. return false;
  178. if (*st != NULL)
  179. freeStatement(*st);
  180. *st = new_st;
  181. return true;
  182. }
  183. bool callParserAs(PASERSSIGNATURE, Statement **st,char *message){
  184. *st = NULL;
  185. if (readBackToken(pm) == MATHER_AS) {
  186. delToken(pm);
  187. return callChildStatement(CALLPASERSSIGNATURE, parserOperation, OPERATION, st, message);
  188. }
  189. return true;
  190. }
  191. bool callChildToken(ParserMessage *pm, Inter *inter, PasersFunction callBack, int type, Token **tmp, char *message,
  192. int error_type) {
  193. *tmp = NULL;
  194. callBack(CALLPASERSSIGNATURE);
  195. if (!call_success(pm))
  196. return false;
  197. if (readBackToken(pm) != type) {
  198. if (message != NULL)
  199. syntaxError(pm, error_type, (*tmp)->line, 1, message);
  200. return false;
  201. }
  202. *tmp = popAheadToken(pm);
  203. return true;
  204. }
  205. bool callChildStatement(PASERSSIGNATURE, PasersFunction callBack, int type, Statement **st, char *message){
  206. Token *tmp = NULL;
  207. *st = NULL;
  208. bool status = callChildToken(CALLPASERSSIGNATURE, callBack, type, &tmp, message, syntax_error);
  209. if (!status)
  210. return false;
  211. *st = tmp->data.st;
  212. freeToken(tmp, true, false);
  213. return true;
  214. }
  215. /**
  216. * is_dict的默认模式为 s_2 ,一般情况默认模式为 s_1
  217. * 若获得MUL则进入模式 s_3, 若获得POW则进入模式 s_4
  218. * get operation [1]
  219. * 若模式为 s_1
  220. * - 检查是否为sep符号
  221. * - 若不是sep符号则检查是否为ass符号
  222. * - 若是ass符号则进入 s_2 模式
  223. * - 若不是ass符号则标注该参数为最后匹配参数
  224. * - 若是sep符号则保持 s_1 模式
  225. * 若模式为 s_2
  226. * - 检查是否为ass符号
  227. * - 若不是ass符号则报错
  228. * - 若是ass符号则保持 s_2 模式
  229. * 若模式为 s_3 / s_4
  230. * - 检查是否为sep符号
  231. * - 若不是sep符号则标注该参数为最后匹配参数
  232. * - 若是sep则保持 s_3 / s_4 模式
  233. * ... 合成 Parameter 并且链接 ...
  234. * 重复操作
  235. *
  236. * @param is_formal 是否为形式参数, 若为true,则限定*args为only_value的结尾, **kwargs为name_value结尾
  237. * @param is_list 若为true则关闭对name_value和**kwargs的支持
  238. * @param is_dict 若为true则关闭对only_value和*args的支持
  239. * @param sep 设定分割符号
  240. * @param ass 设定赋值符号
  241. * @return
  242. */
  243. bool parserParameter(ParserMessage *pm, Inter *inter, Parameter **pt, bool is_formal, bool is_list, bool is_dict, int sep,
  244. int ass) {
  245. Parameter *new_pt = NULL;
  246. Token *tmp;
  247. bool last_pt = false;
  248. enum {
  249. s_1, // only_value模式
  250. s_2, // name_value模式
  251. s_3, // only_args模式
  252. s_4, // name_args模式
  253. } status;
  254. if (is_dict)
  255. status = s_2; // is_formal关闭对only_value的支持
  256. else
  257. status = s_1;
  258. while (!last_pt){
  259. tmp = NULL;
  260. if (!is_dict && status != s_2 && checkToken_(pm, MATHER_MUL)) // is_formal关闭对*args的支持
  261. status = s_3;
  262. else if (!is_list && checkToken_(pm, MATHER_POW)) // is_formal关闭对*args的支持
  263. status = s_4;
  264. parserPolynomial(CALLPASERSSIGNATURE);
  265. if (!call_success(pm))
  266. goto error_;
  267. if (readBackToken(pm) != POLYNOMIAL) {
  268. if (status == s_3) {
  269. long int line = pm->tm->ts->token_list->line;
  270. syntaxError(pm, syntax_error, line, 1, "Don't get a parameter after *");
  271. goto error_;
  272. }
  273. break;
  274. }
  275. tmp = popAheadToken(pm);
  276. int pt_type = value_par;
  277. if (status == s_1){
  278. if (!checkToken_(pm, sep)){
  279. if (is_list || !checkToken_(pm, ass)) // // is_list关闭对name_value的支持
  280. last_pt = true;
  281. else {
  282. pt_type = name_par;
  283. status = s_2;
  284. }
  285. }
  286. }
  287. else if (status == s_2){
  288. pt_type = name_par;
  289. if (!checkToken_(pm, ass))
  290. goto error_;
  291. }
  292. else if (status == s_3){
  293. pt_type = args_par;
  294. if (!checkToken_(pm, sep))
  295. last_pt = true;
  296. }
  297. else {
  298. pt_type = kwargs_par;
  299. if (!checkToken_(pm, sep))
  300. last_pt = true;
  301. }
  302. if (pt_type == value_par)
  303. new_pt = connectValueParameter(tmp->data.st, new_pt);
  304. else if (pt_type == name_par){
  305. Statement *tmp_value;
  306. if (!callChildStatement(CALLPASERSSIGNATURE, parserPolynomial, POLYNOMIAL, &tmp_value, "Don't get a parameter value"))
  307. goto error_;
  308. new_pt = connectNameParameter(tmp_value, tmp->data.st, new_pt);
  309. if (!checkToken_(pm, sep))
  310. last_pt = true;
  311. }
  312. else if (pt_type == args_par){
  313. new_pt = connectArgsParameter(tmp->data.st, new_pt);
  314. if (is_formal)
  315. status = s_2; // 是否规定*args只出现一次
  316. else
  317. status = s_1;
  318. }
  319. else {
  320. new_pt = connectKwargsParameter(tmp->data.st, new_pt);
  321. if (is_formal)
  322. last_pt = true; // 是否规定**kwargs只出现一次
  323. else
  324. status = s_2;
  325. }
  326. freeToken(tmp, true, false);
  327. }
  328. *pt = new_pt;
  329. return true;
  330. error_:
  331. freeToken(tmp, true, true);
  332. freeParameter(new_pt, true);
  333. *pt = NULL;
  334. return false;
  335. }