parser.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/generic/string.h>
  4. #include <util/stream/output.h>
  5. class TCppSaxParser: public IOutputStream {
  6. public:
  7. struct TText {
  8. TText();
  9. TText(ui64 offset);
  10. TText(const TString& data, ui64 offset);
  11. ~TText();
  12. void Reset() noexcept;
  13. TString Data;
  14. ui64 Offset;
  15. };
  16. class TWorker {
  17. public:
  18. typedef TCppSaxParser::TText TText;
  19. TWorker() noexcept;
  20. virtual ~TWorker();
  21. virtual void DoEnd() = 0;
  22. virtual void DoStart() = 0;
  23. virtual void DoString(const TText& text) = 0;
  24. virtual void DoCharacter(const TText& text) = 0;
  25. virtual void DoCode(const TText& text) = 0;
  26. virtual void DoOneLineComment(const TText& text) = 0;
  27. virtual void DoMultiLineComment(const TText& text) = 0;
  28. virtual void DoPreprocessor(const TText& text) = 0;
  29. };
  30. TCppSaxParser(TWorker* worker);
  31. ~TCppSaxParser() override;
  32. private:
  33. void DoWrite(const void* data, size_t len) override;
  34. void DoFinish() override;
  35. private:
  36. class TImpl;
  37. THolder<TImpl> Impl_;
  38. };
  39. class TCppSimpleSax: public TCppSaxParser::TWorker {
  40. public:
  41. TCppSimpleSax() noexcept;
  42. ~TCppSimpleSax() override;
  43. void DoEnd() override = 0;
  44. void DoStart() override = 0;
  45. void DoString(const TText& text) override = 0;
  46. void DoCharacter(const TText& text) override = 0;
  47. virtual void DoWhiteSpace(const TText& text) = 0;
  48. virtual void DoIdentifier(const TText& text) = 0;
  49. virtual void DoSyntax(const TText& text) = 0;
  50. void DoOneLineComment(const TText& text) override = 0;
  51. void DoMultiLineComment(const TText& text) override = 0;
  52. void DoPreprocessor(const TText& text) override = 0;
  53. private:
  54. void DoCode(const TText& text) override;
  55. };
  56. class TCppFullSax: public TCppSimpleSax {
  57. public:
  58. TCppFullSax();
  59. ~TCppFullSax() override;
  60. void DoEnd() override;
  61. void DoStart() override;
  62. void DoString(const TText& text) override;
  63. void DoCharacter(const TText& text) override;
  64. void DoWhiteSpace(const TText& text) override;
  65. virtual void DoKeyword(const TText& text);
  66. virtual void DoName(const TText& text);
  67. virtual void DoOctNumber(const TText& text);
  68. virtual void DoHexNumber(const TText& text);
  69. virtual void DoDecNumber(const TText& text);
  70. virtual void DoFloatNumber(const TText& text);
  71. void DoSyntax(const TText& text) override;
  72. void DoOneLineComment(const TText& text) override;
  73. void DoMultiLineComment(const TText& text) override;
  74. void DoPreprocessor(const TText& text) override;
  75. void AddKeyword(const TString& keyword);
  76. private:
  77. void DoIdentifier(const TText& text) override;
  78. private:
  79. class TImpl;
  80. THolder<TImpl> Impl_;
  81. };