core-parser.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef AFUN_CORE_PARSER_H
  2. #define AFUN_CORE_PARSER_H
  3. #include "aFunToolExport.h"
  4. #include "reader.h"
  5. #include "code.h"
  6. #include <queue>
  7. namespace aFuncore {
  8. class AFUN_CORE_EXPORT Parser {
  9. public:
  10. typedef enum LexicalStatus {
  11. lex_begin = 0, // 起始类型
  12. lex_prefix_block_p = 1, // 前缀括号 !
  13. lex_prefix_block_b = 2, // 前缀括号 @
  14. lex_prefix_block_c = 3, // 前缀括号 #
  15. lex_comment_before = 4, // 注释
  16. lex_element_long = 5,
  17. lex_mutli_comment = 6, // 多行注释
  18. lex_uni_comment = 7, // 当行注释
  19. lex_mutli_comment_end_before = 8, // 多行注释遇到;
  20. lex_prefix = -1, // prefix类型
  21. lex_lp = -2,
  22. lex_lb = -3,
  23. lex_lc = -4,
  24. lex_rp = -5,
  25. lex_rb = -6,
  26. lex_rc = -7,
  27. lex_space = -8,
  28. lex_uni_comment_end = -9,
  29. lex_mutli_comment_end = -10,
  30. lex_nul = -11,
  31. lex_element_short = -12,
  32. lex_element_long_end = -13,
  33. } LexicalStatus;
  34. typedef enum TokenType {
  35. TK_ERROR = -1,
  36. TK_PREFIX = 0, // 前缀
  37. TK_LP = 1,
  38. TK_LB = 2,
  39. TK_LC = 3,
  40. TK_RP = 4,
  41. TK_RB = 5,
  42. TK_RC = 6,
  43. TK_ELEMENT_SHORT = 7,
  44. TK_ELEMENT_LONG = 8,
  45. TK_COMMENT = 9,
  46. TK_SPACE = 10,
  47. TK_EOF = 11,
  48. } TokenType;
  49. struct ParserEvent {
  50. typedef enum EventType {
  51. parser_error_unknown = -1, // 遇到未知错误
  52. lexical_error_char = -2, // 遇到非正常符号
  53. lexical_error_element_end = -3, // 注释未结束
  54. syntactic_error_nested_too_deep = -4,
  55. syntactic_error_block_p_end = -5,
  56. syntactic_error_block_b_end = -6,
  57. syntactic_error_block_c_end = -7,
  58. syntactic_error_prefix = -8,
  59. reader_error = -9,
  60. lexical_warning_comment_end = 1, // 注释未结束
  61. } EventType;
  62. EventType type;
  63. aFuntool::FileLine line;
  64. std::string info;
  65. };
  66. AFUN_INLINE explicit Parser(Reader &reader_);
  67. TokenType getTokenFromLexical(std::string &text);
  68. bool parserCode(Code &code);
  69. [[nodiscard]] AFUN_INLINE size_t countEvent() const;
  70. AFUN_INLINE ParserEvent popEvent();
  71. [[nodiscard]] AFUN_INLINE const ParserEvent &checkEvent() const;
  72. private:
  73. typedef enum DoneStatus {
  74. DEL_TOKEN = 0,
  75. FINISH_TOKEN = -1,
  76. CONTINUE_TOKEN = 1,
  77. ERROR_TOKEN = -2
  78. } DoneStatus;
  79. std::queue<ParserEvent> event;
  80. Reader &reader;
  81. struct {
  82. LexicalStatus status;
  83. TokenType token; // token类型
  84. size_t last; // 最后一次词法匹配的有效长度
  85. size_t mutli_comment; // 多行注释嵌套等级
  86. bool is_end;
  87. bool is_error;
  88. } lexical;
  89. struct {
  90. bool back;
  91. TokenType token;
  92. std::string text;
  93. bool is_error;
  94. } syntactic;
  95. void setLexicalLast(LexicalStatus status, TokenType token);
  96. DoneStatus doneBegin(char ch);
  97. DoneStatus donePrefixBlock(char ch);
  98. DoneStatus doneCommentBefore(char ch);
  99. DoneStatus doneUniComment(char ch);
  100. DoneStatus doneMutliComment(char ch);
  101. DoneStatus doneMutliCommentBeforeEnd(char ch);
  102. DoneStatus doneElementLong(char ch);
  103. DoneStatus doneElementLongEnd(char ch);
  104. DoneStatus doneElementShort(char ch);
  105. DoneStatus doneSpace(char ch);
  106. bool getToken();
  107. bool goBackToken();
  108. AFUN_STATIC const size_t SYNTACTIC_MAX_DEPTH = 218;
  109. Code::ByteCode *codeSelf(Code &code, size_t deep, char prefix);
  110. Code::ByteCode *codePrefix(Code &code, size_t deep);
  111. Code::ByteCode *codeList(Code &code, size_t deep);
  112. Code::ByteCode *codeListEnd(Code &code);
  113. AFUN_INLINE void pushEvent(const ParserEvent &new_event);
  114. AFUN_INLINE void pushEvent(ParserEvent &&new_event);
  115. };
  116. }
  117. #include "core-parser.inline.h"
  118. #endif //AFUN_CORE_PARSER_H