__grammar.c 13 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(PASERSSIGNATURE, 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) || readBackToken(pm) != call_type){ // 若非正确数值
  45. syntaxError(pm, syntax_error, line, 3, "ERROR from ", self_name, "(get right)");
  46. freeToken(left_token, true, true);
  47. freeStatement(st);
  48. goto return_;
  49. }
  50. right_token = popAheadToken(pm);
  51. addToken_(pm, setOperationFromToken(&st, left_token, right_token, self_type, is_right_));
  52. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  53. "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  54. is_right_ = is_right; // 第一次is_right不生效
  55. }
  56. return_:
  57. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: return\n", self_name);
  58. }
  59. /**
  60. * 尾巴一元匹配器
  61. * tailOperation:
  62. * | callBack
  63. * | tailOperation tailFunction
  64. * @param callBack 符号左、右值匹配函数
  65. * @param tailFunction 尾巴处理函数
  66. * @param call_type 左、右值类型
  67. * @param self_type 输出token的类型
  68. * @param call_name 左、右值名称(log)
  69. * @param self_name 输出值名称(log)
  70. */
  71. inline void tailOperation(PASERSSIGNATURE, PasersFunction callBack, TailFunction tailFunction, int call_type,
  72. int self_type, char *call_name, char *self_name){
  73. while(true){
  74. Token *left_token = NULL;
  75. struct Statement *st = NULL;
  76. if (readBackToken(pm) != self_type){
  77. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  78. if (!callChildStatement(CALLPASERSSIGNATURE, callBack, call_type, &st, NULL))
  79. goto return_;
  80. addStatementToken(self_type, st, pm);
  81. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  82. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  83. continue;
  84. }
  85. left_token = popAheadToken(pm);
  86. int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
  87. if (tail_status == -1){
  88. backToken_(pm, left_token);
  89. goto return_;
  90. }
  91. else if(tail_status == 0) {
  92. freeToken(left_token, true, true);
  93. goto return_;
  94. }
  95. addStatementToken(self_type, st, pm);
  96. freeToken(left_token, true, false);
  97. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
  98. }
  99. return_:
  100. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: return\n", self_name);
  101. }
  102. /**
  103. * syntax错误处理器
  104. * @param pm
  105. * @param message 错误信息
  106. * @param status 错误类型
  107. */
  108. void syntaxError(ParserMessage *pm, int status, long int line, int num, ...) {
  109. char *message = NULL;
  110. if (pm->status != success)
  111. return;
  112. if (status <= 0){
  113. message = memStrcpy("Not Message");
  114. goto not_message;
  115. }
  116. va_list message_args;
  117. va_start(message_args, num);
  118. for (int i=0; i < num; i++)
  119. message = memStrcat(message, va_arg(message_args, char *), true, false);
  120. va_end(message_args);
  121. char info[100];
  122. snprintf(info, 100, "\non line %ld\nin file ", line);
  123. message = memStrcat(message, info, true, false);
  124. message = memStrcat(message, pm->file, true, false);
  125. not_message:
  126. pm->status = status;
  127. pm->status_message = message;
  128. }
  129. int readBackToken(ParserMessage *pm){
  130. writeLog(pm->grammar_debug, GRAMMAR_DEBUG, "token operation number : %d\n", pm->count);
  131. writeLog(pm->paser_debug, DEBUG, "\ntoken operation number : %d\n", pm->count);
  132. pm->count ++;
  133. Token *tmp = popNewToken(pm->tm, pm->paser_debug);
  134. if (tmp->token_type == -2){
  135. freeToken(tmp, true, false);
  136. syntaxError(pm, lexical_error, tmp->line, 1, "lexical make some error");
  137. }
  138. addBackToken(pm->tm->ts, tmp, pm->paser_debug);
  139. return tmp->token_type;
  140. }
  141. Token *popAheadToken(ParserMessage *pm){
  142. doubleLog(pm, GRAMMAR_DEBUG, DEBUG, "token operation number : %d\n", pm->count ++);
  143. return popNewToken(pm->tm, pm->paser_debug);
  144. }
  145. bool checkToken(ParserMessage *pm, int type){
  146. if (readBackToken(pm) != type)
  147. return false;
  148. delToken(pm);
  149. return true;
  150. }
  151. bool commandCallControl_(PASERSSIGNATURE, MakeControlFunction callBack, int type, Statement **st,
  152. char *log_message, bool must_operation, char *error_message) {
  153. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, log_message, NULL);
  154. Token *tmp_token = NULL;
  155. *st = NULL;
  156. parserControl(CALLPASERSSIGNATURE, callBack, type, must_operation, error_message);
  157. if (!call_success(pm) || readBackToken(pm) != type)
  158. return false;
  159. tmp_token = popAheadToken(pm);
  160. *st = tmp_token->data.st;
  161. freeToken(tmp_token, true, false);
  162. return true;
  163. }
  164. inline bool commandCallBack_(PASERSSIGNATURE, PasersFunction callBack, int type, Statement **st, char *message){
  165. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, message, NULL);
  166. return callChildStatement(CALLPASERSSIGNATURE, callBack, type, st, NULL);
  167. }
  168. bool callParserCode(PASERSSIGNATURE, Statement **st, char *message, long int line) {
  169. Token *tmp;
  170. *st = NULL;
  171. parserCode(CALLPASERSSIGNATURE);
  172. if (!call_success(pm) || readBackToken(pm) != CODE) {
  173. if (message != NULL)
  174. syntaxError(pm, syntax_error, line, 1, message);
  175. return false;
  176. }
  177. tmp = popAheadToken(pm);
  178. *st = tmp->data.st;
  179. freeToken(tmp, true, false);
  180. return true;
  181. }
  182. bool callParserAs(PASERSSIGNATURE, Statement **st,char *message){
  183. *st = NULL;
  184. if (readBackToken(pm) == MATHER_AS) {
  185. delToken(pm);
  186. return callChildStatement(CALLPASERSSIGNATURE, parserOperation, OPERATION, st, message);
  187. }
  188. return true;
  189. }
  190. bool callChildToken(PASERSSIGNATURE, PasersFunction callBack, int type, Token **tmp, char *message,
  191. int error_type) {
  192. *tmp = NULL;
  193. callBack(CALLPASERSSIGNATURE);
  194. if (!call_success(pm) || readBackToken(pm) != type) {
  195. if (message != NULL) {
  196. *tmp = popAheadToken(pm);
  197. syntaxError(pm, error_type, (*tmp)->line, 1, message);
  198. backToken_(pm ,(*tmp));
  199. }
  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的支持 (is_list和is_dict同时为true表示纯 a,b,c 匹配)
  239. * @param sep 设定分割符号
  240. * @param ass 设定赋值符号
  241. * @return
  242. */
  243. bool parserParameter(PASERSSIGNATURE, 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 && !is_list)
  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. }