__grammar.c 11 KB

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