1
0

parser-parser.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef AFUN_PARSER_PARSER_H
  2. #define AFUN_PARSER_PARSER_H
  3. #include "aFunParserExport.h"
  4. #include "parser-reader.h"
  5. #include "aFuncode.h"
  6. #include <queue>
  7. namespace aFunparser {
  8. class AFUN_PARSER_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. Parser(const Parser &) = delete;
  68. Parser &operator=(const Parser &) = delete;
  69. TokenType getTokenFromLexical(std::string &text);
  70. bool parserCode(aFuncode::Code &code);
  71. [[nodiscard]] AFUN_INLINE size_t countEvent() const;
  72. AFUN_INLINE ParserEvent popEvent();
  73. [[nodiscard]] AFUN_INLINE const ParserEvent &checkEvent() const;
  74. private:
  75. typedef enum DoneStatus {
  76. DEL_TOKEN = 0,
  77. FINISH_TOKEN = -1,
  78. CONTINUE_TOKEN = 1,
  79. ERROR_TOKEN = -2
  80. } DoneStatus;
  81. std::queue<ParserEvent> event;
  82. Reader &reader;
  83. struct {
  84. LexicalStatus status;
  85. TokenType token; // token类型
  86. size_t last; // 最后一次词法匹配的有效长度
  87. size_t mutli_comment; // 多行注释嵌套等级
  88. bool is_end;
  89. bool is_error;
  90. } lexical;
  91. struct {
  92. bool back;
  93. TokenType token;
  94. std::string text;
  95. bool is_error;
  96. } syntactic;
  97. void setLexicalLast(LexicalStatus status, TokenType token);
  98. DoneStatus doneBegin(char ch);
  99. DoneStatus donePrefixBlock(char ch);
  100. DoneStatus doneCommentBefore(char ch);
  101. DoneStatus doneUniComment(char ch);
  102. DoneStatus doneMutliComment(char ch);
  103. DoneStatus doneMutliCommentBeforeEnd(char ch);
  104. DoneStatus doneElementLong(char ch);
  105. DoneStatus doneElementLongEnd(char ch);
  106. DoneStatus doneElementShort(char ch);
  107. DoneStatus doneSpace(char ch);
  108. bool getToken();
  109. bool goBackToken();
  110. AFUN_STATIC const size_t SYNTACTIC_MAX_DEPTH = 218;
  111. aFuncode::Code::ByteCode *codeSelf(aFuncode::Code &code, size_t deep, char prefix);
  112. aFuncode::Code::ByteCode *codePrefix(aFuncode::Code &code, size_t deep);
  113. aFuncode::Code::ByteCode *codeList(aFuncode::Code &code, size_t deep);
  114. aFuncode::Code::ByteCode *codeListEnd(aFuncode::Code &code);
  115. AFUN_INLINE void pushEvent(const ParserEvent &new_event);
  116. AFUN_INLINE void pushEvent(ParserEvent &&new_event);
  117. };
  118. }
  119. #include "parser-parser.inline.h"
  120. #endif //AFUN_PARSER_PARSER_H