__grammar.c 12 KB

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