__grammar.c 11 KB

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