read_ops.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "read_ops.h"
  2. using namespace NYsonPull;
  3. using namespace NYsonPull::NReadOps;
  4. namespace {
  5. bool TrySkipValueUntil(EEventType end, TReader& reader) {
  6. const auto& event = reader.NextEvent();
  7. if (event.Type() == end) {
  8. return false;
  9. }
  10. SkipCurrentValue(event, reader);
  11. return true;
  12. }
  13. bool TrySkipKeyValueUntil(EEventType end, TReader& reader) {
  14. const auto& event = reader.NextEvent();
  15. if (event.Type() == end) {
  16. return false;
  17. }
  18. Expect(event, EEventType::Key);
  19. SkipValue(reader);
  20. return true;
  21. }
  22. }
  23. void NYsonPull::NReadOps::SkipCurrentValue(const TEvent& event, TReader& reader) {
  24. switch (event.Type()) {
  25. case EEventType::BeginList:
  26. while (TrySkipValueUntil(EEventType::EndList, reader)) {
  27. }
  28. return;
  29. case EEventType::BeginMap:
  30. while (TrySkipKeyValueUntil(EEventType::EndMap, reader)) {
  31. }
  32. return;
  33. case EEventType::BeginAttributes:
  34. while (TrySkipKeyValueUntil(EEventType::EndAttributes, reader)) {
  35. }
  36. // attributes after attributes are disallowed in TReader
  37. SkipValue(reader);
  38. return;
  39. case EEventType::Scalar:
  40. return;
  41. default:
  42. throw yexception() << "Unexpected event: " << event;
  43. }
  44. }
  45. void NYsonPull::NReadOps::SkipValue(TReader& reader) {
  46. const auto& event = reader.NextEvent();
  47. SkipCurrentValue(event, reader);
  48. }
  49. void NYsonPull::NReadOps::SkipControlRecords(TReader& reader) {
  50. const auto* event = &reader.LastEvent();
  51. while (event->Type() == EEventType::BeginAttributes) {
  52. SkipCurrentValue(*event, reader);
  53. event = &reader.NextEvent();
  54. }
  55. Expect(*event, EEventType::BeginMap);
  56. }