parser.cpp 5.9 KB

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