__grammar.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef VIRTUALMATH___GRAMMAR_H
  2. #define VIRTUALMATH___GRAMMAR_H
  3. #include "__virtualmath.h"
  4. #if OUT_LOG && OUT_PASERS_LOG
  5. #define doubleLog(pm, grammar_level, pasers_level, message, ...) do{ \
  6. writeLog(pm->grammar_debug, grammar_level, message, __VA_ARGS__); \
  7. writeLog(pm->paser_debug, pasers_level, "\n"message, __VA_ARGS__); \
  8. } while(0)
  9. #else
  10. #define doubleLog(...) PASS
  11. #endif
  12. #if OUT_LOG && OUT_GRAMMER_LOG
  13. #define writeLog_(...) writeLog(__VA_ARGS__)
  14. #else
  15. #define writeLog_(...) PASS
  16. #endif
  17. // TODO-szh 优化token操作, 减少内存操作
  18. #define popAheadToken(token_var, pm) do{ \
  19. doubleLog(pm, GRAMMAR_DEBUG, DEBUG, "token operation number : %d\n", pm->count); \
  20. pm->count ++; \
  21. safeGetToken(pm->tm, pm->paser_debug); \
  22. /* 执行popheanToken之前执行readBackToken因此不必再检查status */ \
  23. token_var = popToken(pm->tm->ts, pm->paser_debug); \
  24. } while(0) /*弹出预读的token*/
  25. #define addStatementToken(type, st, pm) do{\
  26. Token *tmp_new_token; \
  27. tmp_new_token = makeStatementToken(type, st); \
  28. addToken(pm->tm->ts, tmp_new_token, pm->paser_debug); \
  29. backToken(pm->tm->ts, pm->paser_debug); \
  30. } while(0)
  31. #define backToken_(pm, token) do{ \
  32. addToken(pm->tm->ts, (token), pm->paser_debug); \
  33. backToken(pm->tm->ts, pm->paser_debug); \
  34. }while(0)
  35. #define addToken_ backToken_
  36. #define call_success(pm) (pm->status == success)
  37. #define delToken(pm) do{ \
  38. Token *tmp_token; \
  39. popAheadToken(tmp_token, pm); \
  40. freeToken(tmp_token, true, false); \
  41. }while(0)
  42. #define checkToken(pm, type, error_) do{ \
  43. int token = readBackToken(pm); \
  44. if (token != type){ \
  45. goto error_; \
  46. } \
  47. delToken(pm); \
  48. }while(0)
  49. // pasersCommand专属macro
  50. #define commandCallBack(pm, st, call, type, return_) do{ \
  51. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: call "#call"\n", NULL); \
  52. Token *tmp_token = NULL; \
  53. call(CALLPASERSSIGNATURE); \
  54. if (!call_success(pm) || readBackToken(pm) != type) \
  55. goto return_; \
  56. popAheadToken(tmp_token, pm); \
  57. st = tmp_token->data.st; \
  58. freeToken(tmp_token, true, false); \
  59. } while(0)
  60. #define commandCallControl(pm, st, call, type, return_) do{ \
  61. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: call pasers"#call"\n", NULL); \
  62. Token *tmp_token = NULL; \
  63. parserControl(CALLPASERSSIGNATURE, call, type); \
  64. if (!call_success(pm) || readBackToken(pm) != type) \
  65. goto return_; \
  66. popAheadToken(tmp_token, pm); \
  67. st = tmp_token->data.st; \
  68. freeToken(tmp_token, true, false); \
  69. } while(0)
  70. void parserCommand(PASERSSIGNATURE);
  71. void parserControl(PASERSSIGNATURE, Statement *(*callBack)(Statement *), int type);
  72. void parserDef(PASERSSIGNATURE);
  73. void parserIf(PASERSSIGNATURE);
  74. void parserWhile(PASERSSIGNATURE);
  75. void parserCode(PASERSSIGNATURE);
  76. void parserOperation(PASERSSIGNATURE);
  77. void parserPolynomial(PASERSSIGNATURE);
  78. void parserBaseValue(PASERSSIGNATURE);
  79. void parserCallBack(PASERSSIGNATURE);
  80. void parserFactor(PASERSSIGNATURE);
  81. void parserAssignment(PASERSSIGNATURE);
  82. void twoOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*getSymbol)(PASERSSIGNATURE, int symbol, Statement **st), int, int, char *, char *);
  83. void tailOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*tailFunction)(PASERSSIGNATURE, Token *left_token, Statement **st), int , int , char *, char *);
  84. void syntaxError(ParserMessage *pm, char *message, int status);
  85. int readBackToken(ParserMessage *pm);
  86. #endif //VIRTUALMATH___GRAMMAR_H