parser.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef AFUN_PARSER_H
  2. #define AFUN_PARSER_H
  3. #include "aFunToolExport.h"
  4. #include "reader.h"
  5. namespace aFuncore {
  6. class AFUN_CORE_EXPORT Parser {
  7. public:
  8. typedef enum LexicalStatus {
  9. lex_begin = 0, // 起始类型
  10. lex_prefix_block_p = 1, // 前缀括号 !
  11. lex_prefix_block_b = 2, // 前缀括号 @
  12. lex_prefix_block_c = 3, // 前缀括号 #
  13. lex_comment_before = 4, // 注释
  14. lex_element_long = 5,
  15. lex_mutli_comment = 6, // 多行注释
  16. lex_uni_comment = 7, // 当行注释
  17. lex_mutli_comment_end_before = 8, // 多行注释遇到;
  18. lex_prefix = -1, // prefix类型
  19. lex_lp = -2,
  20. lex_lb = -3,
  21. lex_lc = -4,
  22. lex_rp = -5,
  23. lex_rb = -6,
  24. lex_rc = -7,
  25. lex_space = -8,
  26. lex_uni_comment_end = -9,
  27. lex_mutli_comment_end = -10,
  28. lex_nul = -11,
  29. lex_element_short = -12,
  30. lex_element_long_end = -13,
  31. } LexicalStatus;
  32. typedef enum TokenType {
  33. TK_ERROR = -1,
  34. TK_PREFIX = 0, // 前缀
  35. TK_LP = 1,
  36. TK_LB = 2,
  37. TK_LC = 3,
  38. TK_RP = 4,
  39. TK_RB = 5,
  40. TK_RC = 6,
  41. TK_ELEMENT_SHORT = 7,
  42. TK_ELEMENT_LONG = 8,
  43. TK_COMMENT = 9,
  44. TK_SPACE = 10,
  45. TK_EOF = 11,
  46. } TokenType;
  47. inline explicit Parser(Reader &reader_);
  48. TokenType getTokenFromLexical(std::string &text);
  49. private:
  50. Reader &reader;
  51. struct {
  52. LexicalStatus status;
  53. TokenType token; // token类型
  54. size_t last; // 最后一次词法匹配的有效长度
  55. size_t mutli_comment; // 多行注释嵌套等级
  56. bool is_end;
  57. bool is_error;
  58. } lexical;
  59. void setLexicalLast(LexicalStatus status, TokenType token);
  60. int doneBegin(char ch);
  61. int donePrefixBlock(char ch);
  62. int doneCommentBefore(char ch);
  63. int doneUniComment(char ch);
  64. int doneMutliComment(char ch);
  65. int doneMutliCommentBeforeEnd(char ch);
  66. int doneElementLong(char ch);
  67. int doneElementLongEnd(char ch);
  68. int doneElementShort(char ch);
  69. int doneSpace(char ch);
  70. };
  71. }
  72. #include "parser.inline.h"
  73. #endif //AFUN_PARSER_H