common.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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