core-parser.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. namespace aFuncore {
  7. class AFUN_CORE_EXPORT Parser {
  8. public:
  9. typedef enum LexicalStatus {
  10. lex_begin = 0, // 起始类型
  11. lex_prefix_block_p = 1, // 前缀括号 !
  12. lex_prefix_block_b = 2, // 前缀括号 @
  13. lex_prefix_block_c = 3, // 前缀括号 #
  14. lex_comment_before = 4, // 注释
  15. lex_element_long = 5,
  16. lex_mutli_comment = 6, // 多行注释
  17. lex_uni_comment = 7, // 当行注释
  18. lex_mutli_comment_end_before = 8, // 多行注释遇到;
  19. lex_prefix = -1, // prefix类型
  20. lex_lp = -2,
  21. lex_lb = -3,
  22. lex_lc = -4,
  23. lex_rp = -5,
  24. lex_rb = -6,
  25. lex_rc = -7,
  26. lex_space = -8,
  27. lex_uni_comment_end = -9,
  28. lex_mutli_comment_end = -10,
  29. lex_nul = -11,
  30. lex_element_short = -12,
  31. lex_element_long_end = -13,
  32. } LexicalStatus;
  33. typedef enum TokenType {
  34. TK_ERROR = -1,
  35. TK_PREFIX = 0, // 前缀
  36. TK_LP = 1,
  37. TK_LB = 2,
  38. TK_LC = 3,
  39. TK_RP = 4,
  40. TK_RB = 5,
  41. TK_RC = 6,
  42. TK_ELEMENT_SHORT = 7,
  43. TK_ELEMENT_LONG = 8,
  44. TK_COMMENT = 9,
  45. TK_SPACE = 10,
  46. TK_EOF = 11,
  47. } TokenType;
  48. inline explicit Parser(Reader &reader_);
  49. TokenType getTokenFromLexical(std::string &text);
  50. bool parserCode(Code &code);
  51. private:
  52. typedef enum DoneStatus {
  53. DEL_TOKEN = 0,
  54. FINISH_TOKEN = -1,
  55. CONTINUE_TOKEN = 1,
  56. ERROR_TOKEN = -2
  57. } DoneStatus;
  58. Reader &reader;
  59. struct {
  60. LexicalStatus status;
  61. TokenType token; // token类型
  62. size_t last; // 最后一次词法匹配的有效长度
  63. size_t mutli_comment; // 多行注释嵌套等级
  64. bool is_end;
  65. bool is_error;
  66. } lexical;
  67. struct {
  68. bool back;
  69. TokenType token;
  70. std::string text;
  71. bool is_error;
  72. } syntactic;
  73. void setLexicalLast(LexicalStatus status, TokenType token);
  74. DoneStatus doneBegin(char ch);
  75. DoneStatus donePrefixBlock(char ch);
  76. DoneStatus doneCommentBefore(char ch);
  77. DoneStatus doneUniComment(char ch);
  78. DoneStatus doneMutliComment(char ch);
  79. DoneStatus doneMutliCommentBeforeEnd(char ch);
  80. DoneStatus doneElementLong(char ch);
  81. DoneStatus doneElementLongEnd(char ch);
  82. DoneStatus doneElementShort(char ch);
  83. DoneStatus doneSpace(char ch);
  84. bool getToken();
  85. bool goBackToken();
  86. static const size_t SYNTACTIC_MAX_DEPTH = 218;
  87. Code::ByteCode *codeSelf(Code &code, size_t deep, char prefix);
  88. Code::ByteCode *codePrefix(Code &code, size_t deep);
  89. Code::ByteCode *codeList(Code &code, size_t deep);
  90. Code::ByteCode *codeListEnd(Code &code);
  91. };
  92. }
  93. #include "core-parser.inline.h"
  94. #endif //AFUN_CORE_PARSER_H