parser.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include <ios>
  9. #include <memory>
  10. #include "yaml-cpp/dll.h"
  11. namespace YAML {
  12. class EventHandler;
  13. class Node;
  14. class Scanner;
  15. struct Directives;
  16. struct Token;
  17. /**
  18. * A parser turns a stream of bytes into one stream of "events" per YAML
  19. * document in the input stream.
  20. */
  21. class YAML_CPP_API Parser {
  22. public:
  23. /** Constructs an empty parser (with no input. */
  24. Parser();
  25. Parser(const Parser&) = delete;
  26. Parser(Parser&&) = delete;
  27. Parser& operator=(const Parser&) = delete;
  28. Parser& operator=(Parser&&) = delete;
  29. /**
  30. * Constructs a parser from the given input stream. The input stream must
  31. * live as long as the parser.
  32. */
  33. explicit Parser(std::istream& in);
  34. ~Parser();
  35. /** Evaluates to true if the parser has some valid input to be read. */
  36. explicit operator bool() const;
  37. /**
  38. * Resets the parser with the given input stream. Any existing state is
  39. * erased.
  40. */
  41. void Load(std::istream& in);
  42. /**
  43. * Handles the next document by calling events on the {@code eventHandler}.
  44. *
  45. * @throw a ParserException on error.
  46. * @return false if there are no more documents
  47. */
  48. bool HandleNextDocument(EventHandler& eventHandler);
  49. void PrintTokens(std::ostream& out);
  50. private:
  51. /**
  52. * Reads any directives that are next in the queue, setting the internal
  53. * {@code m_pDirectives} state.
  54. */
  55. void ParseDirectives();
  56. void HandleDirective(const Token& token);
  57. /**
  58. * Handles a "YAML" directive, which should be of the form 'major.minor' (like
  59. * a version number).
  60. */
  61. void HandleYamlDirective(const Token& token);
  62. /**
  63. * Handles a "TAG" directive, which should be of the form 'handle prefix',
  64. * where 'handle' is converted to 'prefix' in the file.
  65. */
  66. void HandleTagDirective(const Token& token);
  67. private:
  68. std::unique_ptr<Scanner> m_pScanner;
  69. std::unique_ptr<Directives> m_pDirectives;
  70. };
  71. } // namespace YAML
  72. #endif // PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66