tokenizer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "token.h"
  3. namespace NYT::NYPath {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. class TTokenizer
  6. {
  7. public:
  8. explicit TTokenizer(TYPathBuf path = {});
  9. TTokenizer(const TTokenizer&) = delete;
  10. TTokenizer& operator=(const TTokenizer&) = delete;
  11. TTokenizer(TTokenizer&&) = default;
  12. TTokenizer& operator=(TTokenizer&&) = default;
  13. void Reset(TYPathBuf path);
  14. ETokenType Advance();
  15. ETokenType GetType() const;
  16. ETokenType GetPreviousType() const;
  17. TStringBuf GetToken() const;
  18. TYPathBuf GetPrefix() const;
  19. TYPathBuf GetPrefixPlusToken() const;
  20. TStringBuf GetSuffix() const;
  21. TStringBuf GetInput() const;
  22. TYPathBuf GetPath() const;
  23. const TString& GetLiteralValue() const;
  24. void Expect(ETokenType expectedType) const;
  25. void ExpectListIndex() const;
  26. bool Skip(ETokenType expectedType);
  27. [[noreturn]] void ThrowUnexpected() const;
  28. // For iterations. Restores tokenizer to current state on destruction.
  29. // Does not restore LiteralValue_.
  30. class TCheckpoint
  31. {
  32. public:
  33. explicit TCheckpoint(TTokenizer& tokenizer);
  34. ~TCheckpoint();
  35. private:
  36. TTokenizer& Tokenizer_;
  37. TYPathBuf Path_;
  38. ETokenType Type_;
  39. ETokenType PreviousType_;
  40. TStringBuf Token_;
  41. TStringBuf Input_;
  42. };
  43. private:
  44. friend class TTokenizer::TCheckpoint;
  45. TYPathBuf Path_;
  46. ETokenType Type_;
  47. ETokenType PreviousType_;
  48. TStringBuf Token_;
  49. TStringBuf Input_;
  50. TString LiteralValue_;
  51. void SetType(ETokenType type);
  52. const char* AdvanceEscaped(const char* current);
  53. static int ParseHexDigit(char ch, TStringBuf context);
  54. static void ThrowMalformedEscapeSequence(TStringBuf context);
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. bool HasPrefix(TYPathBuf fullPath, TYPathBuf prefixPath);
  58. ////////////////////////////////////////////////////////////////////////////////
  59. } // namespace NYT::NYPath