parse_enum.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/stream/input.h>
  4. #include <util/stream/mem.h>
  5. #include <util/string/strip.h>
  6. #include <util/generic/maybe.h>
  7. #include <util/generic/string.h>
  8. #include <util/generic/vector.h>
  9. class TEnumParser {
  10. public:
  11. struct TItem {
  12. TMaybe<TString> Value;
  13. TString CppName;
  14. TVector<TString> Aliases;
  15. TString CommentText;
  16. void Clear() {
  17. *this = TItem();
  18. }
  19. void NormalizeValue() {
  20. if (!Value)
  21. return;
  22. StripInPlace(*Value);
  23. }
  24. };
  25. // vector is to preserve declaration order
  26. typedef TVector<TItem> TItems;
  27. typedef TVector<TString> TScope;
  28. struct TEnum {
  29. TItems Items;
  30. TString CppName;
  31. TScope Scope;
  32. // enum or enum class
  33. bool EnumClass = false;
  34. bool BodyDetected = false;
  35. bool ForwardDeclaration = false;
  36. void Clear() {
  37. *this = TEnum();
  38. }
  39. };
  40. typedef TVector<TEnum> TEnums;
  41. /// Parse results stored here
  42. TEnums Enums;
  43. /// Parse enums from file containing C++ code
  44. TEnumParser(const TString& fileName);
  45. /// Parse enums from memory buffer containing C++ code
  46. TEnumParser(const char* data, size_t length);
  47. /// Parse enums from input stream
  48. TEnumParser(IInputStream& in);
  49. static TString ScopeStr(const TScope& scope) {
  50. TString result;
  51. for (const TString& name : scope) {
  52. result += name;
  53. result += "::";
  54. }
  55. return result;
  56. }
  57. private:
  58. void Parse(const char* data, size_t length);
  59. protected:
  60. TString SourceFileName;
  61. };