common.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <yql/essentials/parser/lexer_common/lexer.h>
  3. #include <google/protobuf/message.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/vector.h>
  6. #include <util/charset/utf8.h>
  7. namespace NProtoAST {
  8. static const char* INVALID_TOKEN_NAME = "nothing";
  9. static const char* ABSENCE = " absence";
  10. template <typename InputType>
  11. void InvalidCharacter(IOutputStream& err, const InputType* input) {
  12. wchar32 rune = 0;
  13. size_t runeLen = 0;
  14. auto begin = input->get_nextChar();
  15. auto end = begin + input->get_sizeBuf();
  16. if (begin != end && SafeReadUTF8Char(rune, runeLen, begin, end) == RECODE_OK) {
  17. err << " '" << TStringBuf((const char*)begin, runeLen) << "' (Unicode character <" << ui32(rune) << ">)";
  18. }
  19. }
  20. template <typename TokenType>
  21. inline void InvalidToken(IOutputStream& err, const TokenType* token) {
  22. if (token) {
  23. if (token->get_input()) {
  24. err << " '" << token->getText() << "'";
  25. } else {
  26. err << ABSENCE;
  27. }
  28. }
  29. }
  30. class TTooManyErrors : public yexception {
  31. };
  32. class IErrorCollector {
  33. public:
  34. explicit IErrorCollector(size_t maxErrors);
  35. virtual ~IErrorCollector();
  36. // throws TTooManyErrors
  37. void Error(ui32 line, ui32 col, const TString& message);
  38. private:
  39. virtual void AddError(ui32 line, ui32 col, const TString& message) = 0;
  40. protected:
  41. const size_t MaxErrors;
  42. size_t NumErrors;
  43. };
  44. class TErrorOutput: public IErrorCollector {
  45. public:
  46. TErrorOutput(IOutputStream& err, const TString& name, size_t maxErrors);
  47. virtual ~TErrorOutput();
  48. private:
  49. void AddError(ui32 line, ui32 col, const TString& message) override;
  50. public:
  51. IOutputStream& Err;
  52. TString Name;
  53. };
  54. } // namespace NProtoAST
  55. namespace NSQLTranslation {
  56. class IParser {
  57. public:
  58. virtual ~IParser() = default;
  59. virtual google::protobuf::Message* Parse(
  60. const TString& query, const TString& queryName, NProtoAST::IErrorCollector& err,
  61. google::protobuf::Arena* arena) = 0;
  62. };
  63. class IParserFactory : public TThrRefBase {
  64. public:
  65. virtual ~IParserFactory() = default;
  66. virtual std::unique_ptr<IParser> MakeParser() const = 0;
  67. };
  68. using TParserFactoryPtr = TIntrusivePtr<IParserFactory>;
  69. } // namespace NSQLTranslation