parser.h 2.1 KB

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