parser.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "parser.h"
  2. #include "consumer.h"
  3. #include "format.h"
  4. #include "parser_detail.h"
  5. #include <util/stream/input.h>
  6. #include <util/generic/buffer.h>
  7. namespace NYson {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. class TYsonParser::TImpl {
  10. public:
  11. TImpl(
  12. NYT::NYson::IYsonConsumer* consumer,
  13. IInputStream* stream,
  14. EYsonType type,
  15. bool enableLinePositionInfo,
  16. TMaybe<ui64> memoryLimit = Nothing())
  17. : Consumer_(consumer)
  18. , Stream_(stream)
  19. , Type_(type)
  20. , EnableLinePositionInfo_(enableLinePositionInfo)
  21. , MemoryLimit_(memoryLimit)
  22. {
  23. }
  24. void Parse() {
  25. TBuffer buffer(64 << 10);
  26. ParseYsonStreamImpl<NYT::NYson::IYsonConsumer, TStreamReader>(
  27. TStreamReader(Stream_, buffer.Data(), buffer.Capacity()),
  28. Consumer_,
  29. Type_,
  30. EnableLinePositionInfo_,
  31. MemoryLimit_);
  32. }
  33. private:
  34. NYT::NYson::IYsonConsumer* Consumer_;
  35. IInputStream* Stream_;
  36. EYsonType Type_;
  37. bool EnableLinePositionInfo_;
  38. TMaybe<ui64> MemoryLimit_;
  39. };
  40. ////////////////////////////////////////////////////////////////////////////////
  41. TYsonParser::TYsonParser(
  42. NYT::NYson::IYsonConsumer* consumer,
  43. IInputStream* stream,
  44. EYsonType type,
  45. bool enableLinePositionInfo,
  46. TMaybe<ui64> memoryLimit)
  47. : Impl(new TImpl(consumer, stream, type, enableLinePositionInfo, memoryLimit))
  48. {
  49. }
  50. TYsonParser::~TYsonParser() {
  51. }
  52. void TYsonParser::Parse() {
  53. Impl->Parse();
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////
  56. class TStatelessYsonParser::TImpl {
  57. private:
  58. THolder<TStatelessYsonParserImplBase> Impl;
  59. public:
  60. TImpl(
  61. NYT::NYson::IYsonConsumer* consumer,
  62. bool enableLinePositionInfo,
  63. TMaybe<ui64> memoryLimit)
  64. : Impl(
  65. enableLinePositionInfo
  66. ? static_cast<TStatelessYsonParserImplBase*>(new TStatelessYsonParserImpl<NYT::NYson::IYsonConsumer, true>(consumer, memoryLimit))
  67. : static_cast<TStatelessYsonParserImplBase*>(new TStatelessYsonParserImpl<NYT::NYson::IYsonConsumer, false>(consumer, memoryLimit)))
  68. {
  69. }
  70. void Parse(const TStringBuf& data, EYsonType type = ::NYson::EYsonType::Node) {
  71. Impl->Parse(data, type);
  72. }
  73. };
  74. ////////////////////////////////////////////////////////////////////////////////
  75. TStatelessYsonParser::TStatelessYsonParser(
  76. NYT::NYson::IYsonConsumer* consumer,
  77. bool enableLinePositionInfo,
  78. TMaybe<ui64> memoryLimit)
  79. : Impl(new TImpl(consumer, enableLinePositionInfo, memoryLimit))
  80. {
  81. }
  82. TStatelessYsonParser::~TStatelessYsonParser() {
  83. }
  84. void TStatelessYsonParser::Parse(const TStringBuf& data, EYsonType type) {
  85. Impl->Parse(data, type);
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////
  88. void ParseYsonStringBuffer(
  89. const TStringBuf& buffer,
  90. NYT::NYson::IYsonConsumer* consumer,
  91. EYsonType type,
  92. bool enableLinePositionInfo,
  93. TMaybe<ui64> memoryLimit) {
  94. ParseYsonStreamImpl<NYT::NYson::IYsonConsumer, TStringReader>(
  95. TStringReader(buffer.begin(), buffer.end()),
  96. consumer,
  97. type,
  98. enableLinePositionInfo,
  99. memoryLimit);
  100. }
  101. ////////////////////////////////////////////////////////////////////////////////
  102. class TYsonListParser::TImpl {
  103. public:
  104. TImpl(
  105. NYT::NYson::IYsonConsumer* consumer,
  106. IInputStream* stream,
  107. bool enableLinePositionInfo,
  108. TMaybe<ui64> memoryLimit = Nothing())
  109. : Consumer_(consumer)
  110. , Stream_(stream)
  111. , EnableLinePositionInfo_(enableLinePositionInfo)
  112. , MemoryLimit_(memoryLimit)
  113. , Buffer_(64 << 10)
  114. , Reader_(Stream_, Buffer_.Data(), Buffer_.Capacity())
  115. {
  116. }
  117. bool Parse() {
  118. if (!Impl_) {
  119. Impl_.Reset(
  120. EnableLinePositionInfo_
  121. ? static_cast<TYsonListParserImplBase*>(new TYsonListParserImpl<NYT::NYson::IYsonConsumer, TStreamReader, true>(Reader_, Consumer_, MemoryLimit_))
  122. : static_cast<TYsonListParserImplBase*>(new TYsonListParserImpl<NYT::NYson::IYsonConsumer, TStreamReader, false>(Reader_, Consumer_, MemoryLimit_)));
  123. }
  124. return Impl_->Parse();
  125. }
  126. private:
  127. NYT::NYson::IYsonConsumer* Consumer_;
  128. IInputStream* Stream_;
  129. bool EnableLinePositionInfo_;
  130. TMaybe<ui64> MemoryLimit_;
  131. TBuffer Buffer_;
  132. TStreamReader Reader_;
  133. THolder<TYsonListParserImplBase> Impl_;
  134. };
  135. ////////////////////////////////////////////////////////////////////////////////
  136. TYsonListParser::TYsonListParser(
  137. NYT::NYson::IYsonConsumer* consumer,
  138. IInputStream* stream,
  139. bool enableLinePositionInfo,
  140. TMaybe<ui64> memoryLimit)
  141. : Impl(new TImpl(consumer, stream, enableLinePositionInfo, memoryLimit))
  142. {
  143. }
  144. TYsonListParser::~TYsonListParser() {
  145. }
  146. bool TYsonListParser::Parse() {
  147. return Impl->Parse();
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. } // namespace NYson