core-parser.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <list>
  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. inline explicit Parser(Reader &reader_);
  51. TokenType getTokenFromLexical(std::string &text);
  52. bool parserCode(Code &code);
  53. [[nodiscard]] inline size_t countEvent() const;
  54. inline ParserEvent popEvent();
  55. [[nodiscard]] inline const ParserEvent &checkEvent() const;
  56. private:
  57. typedef enum DoneStatus {
  58. DEL_TOKEN = 0,
  59. FINISH_TOKEN = -1,
  60. CONTINUE_TOKEN = 1,
  61. ERROR_TOKEN = -2
  62. } DoneStatus;
  63. std::list<ParserEvent> event;
  64. Reader &reader;
  65. struct {
  66. LexicalStatus status;
  67. TokenType token; // token类型
  68. size_t last; // 最后一次词法匹配的有效长度
  69. size_t mutli_comment; // 多行注释嵌套等级
  70. bool is_end;
  71. bool is_error;
  72. } lexical;
  73. struct {
  74. bool back;
  75. TokenType token;
  76. std::string text;
  77. bool is_error;
  78. } syntactic;
  79. void setLexicalLast(LexicalStatus status, TokenType token);
  80. DoneStatus doneBegin(char ch);
  81. DoneStatus donePrefixBlock(char ch);
  82. DoneStatus doneCommentBefore(char ch);
  83. DoneStatus doneUniComment(char ch);
  84. DoneStatus doneMutliComment(char ch);
  85. DoneStatus doneMutliCommentBeforeEnd(char ch);
  86. DoneStatus doneElementLong(char ch);
  87. DoneStatus doneElementLongEnd(char ch);
  88. DoneStatus doneElementShort(char ch);
  89. DoneStatus doneSpace(char ch);
  90. bool getToken();
  91. bool goBackToken();
  92. static const size_t SYNTACTIC_MAX_DEPTH = 218;
  93. Code::ByteCode *codeSelf(Code &code, size_t deep, char prefix);
  94. Code::ByteCode *codePrefix(Code &code, size_t deep);
  95. Code::ByteCode *codeList(Code &code, size_t deep);
  96. Code::ByteCode *codeListEnd(Code &code);
  97. inline void pushEvent(const ParserEvent &new_event);
  98. inline void pushEvent(ParserEvent &&new_event);
  99. };
  100. struct Parser::ParserEvent {
  101. typedef enum EventType {
  102. parser_error_unknown = -1, // 遇到未知错误
  103. lexical_error_char = -2, // 遇到非正常符号
  104. lexical_error_element_end = -3, // 注释未结束
  105. syntactic_error_nested_too_deep = -4,
  106. syntactic_error_block_p_end = -5,
  107. syntactic_error_block_b_end = -6,
  108. syntactic_error_block_c_end = -7,
  109. syntactic_error_prefix = -8,
  110. lexical_warning_comment_end = 1, // 注释未结束
  111. } EventType;
  112. EventType type;
  113. aFuntool::FileLine line;
  114. std::string info;
  115. };
  116. }
  117. #include "core-parser.inline.h"
  118. #endif //AFUN_CORE_PARSER_H