__grammar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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(P_FUNC, PasersFunction callBack, GetSymbolFunction getSymbol, ChecktLeftToken checkleft,
  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. fline line;
  23. if (readBackToken(pm) != self_type) {
  24. if (!callChildStatement(CP_FUNC, callBack, call_type, &st, NULL))
  25. goto return_;
  26. addStatementToken(self_type, st, pm);
  27. continue;
  28. }
  29. left_token = popNewToken(pm->tm);
  30. line = left_token->line;
  31. if (getSymbol(CP_FUNC, readBackToken(pm), &st))
  32. delToken(pm);
  33. else{
  34. backToken_(pm, left_token);
  35. goto return_;
  36. }
  37. if (checkleft != NULL && !checkleft(CP_FUNC, left_token->data.st)) {
  38. freeToken(left_token, true);
  39. goto return_;
  40. }
  41. callBack(CP_FUNC); // 获得右值
  42. if (!call_success(pm) || readBackToken(pm) != call_type){ // 若非正确数值
  43. syntaxError(pm, syntax_error, line, 5, "ERROR from ", self_name, "(get right ", call_name, ")");
  44. freeToken(left_token, true);
  45. freeStatement(st);
  46. goto return_;
  47. }
  48. right_token = popNewToken(pm->tm);
  49. addToken_(pm, setOperationFromToken(&st, left_token, right_token, self_type, is_right_));
  50. is_right_ = is_right; // 第一次is_right不生效
  51. }
  52. return_: return;
  53. }
  54. /**
  55. * syntax错误处理器
  56. * @param pm
  57. * @param message 错误信息
  58. * @param status 错误类型
  59. */
  60. void syntaxError(ParserMessage *pm, int status, fline line, int num, ...) {
  61. char *message = NULL;
  62. if (pm->status != success)
  63. return;
  64. if (status <= 0){
  65. message = memStrcpy("Not Message");
  66. goto not_message;
  67. }
  68. va_list message_args;
  69. va_start(message_args, num);
  70. for (int i=0; i < num; i++)
  71. message = memStrcat(message, va_arg(message_args, char *), true, false);
  72. va_end(message_args);
  73. char info[100];
  74. snprintf(info, 100, "\non line %llu\nin file ", line);
  75. message = memStrcat(message, info, true, false);
  76. message = memStrcat(message, pm->file, true, false);
  77. not_message:
  78. pm->status = status;
  79. pm->status_message = message;
  80. }
  81. int readBackToken(ParserMessage *pm){
  82. Token *tmp = popNewToken(pm->tm);
  83. int type = tmp->token_type;
  84. if (type == -2) {
  85. if (tmp->data.str == NULL)
  86. syntaxError(pm, lexical_error, tmp->line, 1, "lexical make some error");
  87. else {
  88. char *message = memWcsToStr(tmp->data.str, false);
  89. syntaxError(pm, lexical_error, tmp->line, 1, message);
  90. memFree(message);
  91. }
  92. } else if (type == -3)
  93. syntaxError(pm, int_error, tmp->line, 1, "KeyInterrupt");
  94. addBackToken(pm->tm->ts, tmp);
  95. return type;
  96. }
  97. bool checkToken(ParserMessage *pm, int type, fline *line) {
  98. if (readBackToken(pm) != type)
  99. return false;
  100. line != NULL ? (*line = delToken(pm)) : delToken(pm);
  101. return true;
  102. }
  103. bool commandCallControl_(P_FUNC, MakeControlFunction callBack, int type, Statement **st, bool must_operation, char *error_message) {
  104. Token *tmp_token = NULL;
  105. *st = NULL;
  106. parserControl(CP_FUNC, callBack, type, must_operation, error_message);
  107. if (!call_success(pm) || readBackToken(pm) != type)
  108. return false;
  109. tmp_token = popNewToken(pm->tm);
  110. *st = tmp_token->data.st;
  111. freeToken(tmp_token, false);
  112. return true;
  113. }
  114. bool callParserCode(P_FUNC, Statement **st, char *message, fline line) {
  115. Token *tmp;
  116. *st = NULL;
  117. parserCode(CP_FUNC);
  118. if (!call_success(pm) || readBackToken(pm) != T_CODE) {
  119. if (message != NULL)
  120. syntaxError(pm, syntax_error, line, 1, message);
  121. return false;
  122. }
  123. tmp = popNewToken(pm->tm);
  124. *st = tmp->data.st;
  125. freeToken(tmp, false);
  126. return true;
  127. }
  128. bool callParserAs(P_FUNC, Statement **st, char *message){
  129. *st = NULL;
  130. if (readBackToken(pm) == MATHER_AS) {
  131. delToken(pm);
  132. return callChildStatement(CP_FUNC, parserOperation, T_OPERATION, st, message);
  133. }
  134. return true;
  135. }
  136. bool callChildToken(P_FUNC, PasersFunction callBack, int type, Token **tmp, char *message, int error_type) {
  137. *tmp = NULL;
  138. callBack(CP_FUNC);
  139. if (!call_success(pm) || readBackToken(pm) != type) {
  140. if (message != NULL) {
  141. *tmp = popNewToken(pm->tm);
  142. syntaxError(pm, error_type, (*tmp)->line, 1, message);
  143. backToken_(pm, (*tmp));
  144. }
  145. return false;
  146. }
  147. *tmp = popNewToken(pm->tm);
  148. return true;
  149. }
  150. bool callChildStatement(P_FUNC, PasersFunction callBack, int type, Statement **st, char *message){
  151. Token *tmp = NULL;
  152. *st = NULL;
  153. bool status = callChildToken(CP_FUNC, callBack, type, &tmp, message, syntax_error);
  154. if (!status)
  155. return false;
  156. *st = tmp->data.st;
  157. freeToken(tmp, false);
  158. return true;
  159. }
  160. /**
  161. * is_dict的默认模式为 s_2 ,一般情况默认模式为 s_1
  162. * 若获得MUL则进入模式 s_3, 若获得POW则进入模式 s_4
  163. * get operation [1]
  164. * 若模式为 s_1
  165. * - 检查是否为sep符号
  166. * - 若不是sep符号则检查是否为ass符号
  167. * - 若是ass符号则进入 s_2 模式
  168. * - 若不是ass符号则标注该参数为最后匹配参数
  169. * - 若是sep符号则保持 s_1 模式
  170. * 若模式为 s_2
  171. * - 检查是否为ass符号
  172. * - 若不是ass符号则报错
  173. * - 若是ass符号则保持 s_2 模式
  174. * 若模式为 s_3 / s_4
  175. * - 检查是否为sep符号
  176. * - 若不是sep符号则标注该参数为最后匹配参数
  177. * - 若是sep则保持 s_3 / s_4 模式
  178. * ... 合成 Parameter 并且链接 ...
  179. * 重复操作
  180. *
  181. * @param is_formal 是否为形式参数, 若为true,则限定*args为only_value的结尾, **kwargs为name_value结尾
  182. * @param is_list 若为true则关闭对name_value和**kwargs的支持
  183. * @param is_dict 若为true则关闭对only_value和*args的支持 (is_list和is_dict同时为true表示纯 a,b,c 匹配)
  184. * @param sep 设定分割符号
  185. * @param ass 设定赋值符号
  186. * @return
  187. */
  188. bool parserParameter(P_FUNC, Parameter **pt, bool enter, bool is_formal, bool is_list, int n_sep, bool is_dict, int sep,
  189. int ass, bool space) {
  190. Parameter *new_pt = NULL;
  191. bool last_pt = false;
  192. int is_sep = 0; // 0: 不需要处理 1: 是is_sep 2: 处理过is_sep (当匹配到;设置is_sep为1)
  193. enum {
  194. s_1, // only_value模式
  195. s_2, // name_value模式
  196. s_3, // only_args模式
  197. s_4, // name_args模式
  198. } status;
  199. if (enter)
  200. lexEnter(pm, true);
  201. if (is_dict && !is_list)
  202. status = s_2; // is_formal关闭对only_value的支持
  203. else
  204. status = s_1;
  205. for (int count = 0; !last_pt; count++){ // 计算匹配到parameter的个数
  206. Statement *st;
  207. fline line;
  208. int pt_type = value_par;
  209. if (is_sep == 1 || !is_formal && count > 2) // 限制实参的;分隔符前最多只有三个参数
  210. is_sep = 2;
  211. if (!is_dict && status != s_2 && checkToken(pm, MATHER_MUL, NULL)) // is_formal关闭对*args的支持
  212. status = s_3;
  213. else if (!is_list && checkToken(pm, MATHER_POW, NULL)) // is_formal关闭对*args的支持
  214. status = s_4;
  215. if (space && checkToken(pm, sep, &line))
  216. st = makeBaseValueStatement(null_value, line, pm->file); // 空白符号
  217. else {
  218. Token *tmp;
  219. parserOr(CP_FUNC);
  220. if (!call_success(pm))
  221. goto error_;
  222. if (readBackToken(pm) != T_OR) {
  223. if (status == s_3) {
  224. line = pm->tm->ts->token_list->line;
  225. syntaxError(pm, syntax_error, line, 1, "Don't get a parameter after *");
  226. goto error_;
  227. }
  228. break;
  229. }
  230. tmp = popNewToken(pm->tm);
  231. st = tmp->data.st;
  232. freeToken(tmp, false);
  233. }
  234. switch (status) {
  235. case s_1:
  236. if (!checkToken(pm, sep, NULL)) {
  237. if (is_sep == 0 && n_sep != -1 && checkToken(pm, n_sep, NULL))
  238. is_sep = 1;
  239. else if (is_list || !checkToken(pm, ass, NULL)) // // is_list关闭对name_value的支持
  240. last_pt = true;
  241. else {
  242. pt_type = name_par;
  243. status = s_2;
  244. }
  245. }
  246. break;
  247. case s_2:
  248. pt_type = name_par;
  249. if (!checkToken(pm, ass, NULL)) {
  250. freeStatement(st);
  251. goto error_;
  252. }
  253. break;
  254. case s_3:
  255. pt_type = args_par;
  256. if (!checkToken(pm, sep, NULL))
  257. last_pt = true;
  258. break;
  259. default:
  260. pt_type = kwargs_par;
  261. if (!checkToken(pm, sep, NULL))
  262. last_pt = true;
  263. break;
  264. }
  265. if (pt_type == value_par)
  266. new_pt = connectValueParameter(st, new_pt, is_sep == 1);
  267. else if (pt_type == name_par){
  268. Statement *tmp_value;
  269. if (!callChildStatement(CP_FUNC, parserOr, T_OR, &tmp_value, "Don't get a parameter value")) {
  270. freeStatement(st);
  271. goto error_;
  272. }
  273. new_pt = connectNameParameter(tmp_value, st, new_pt);
  274. if (!checkToken(pm, sep, NULL))
  275. last_pt = true;
  276. }
  277. else if (pt_type == args_par){
  278. new_pt = connectArgsParameter(st, new_pt, is_sep == 1);
  279. if (is_formal)
  280. status = s_2; // 是否规定*args只出现一次
  281. else
  282. status = s_1;
  283. }
  284. else {
  285. new_pt = connectKwargsParameter(st, new_pt);
  286. if (is_formal)
  287. last_pt = true; // 是否规定**kwargs只出现一次
  288. else
  289. status = s_2;
  290. }
  291. }
  292. *pt = new_pt;
  293. if (enter)
  294. lexEnter(pm, false);
  295. return true;
  296. error_:
  297. freeParameter(new_pt, true);
  298. *pt = NULL;
  299. if (enter)
  300. lexEnter(pm, false);
  301. return false;
  302. }
  303. void lexEnter(ParserMessage *pm, bool lock){
  304. if (lock)
  305. pm->tm->file->filter_data.enter ++;
  306. else
  307. pm->tm->file->filter_data.enter --;
  308. }