parser.h 979 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

#include <ios>
#include <string>
#include <stack>

namespace YAML
{
	class Node;

	const std::string DocStart = "---";
	const std::string DocEnd = "...";

	const char SeqToken = '-';

	enum CONTEXT { C_BLOCK, C_FLOW };

	class Parser
	{
	public:
		struct State
		{
			State(CONTEXT context_, int indent_, bool startingNewLine_)
			: context(context_), indent(indent_), startingNewLine(startingNewLine_) {}

			CONTEXT context;
			int indent;
			bool startingNewLine;
		};

	public:
		Parser(std::istream& in);
		~Parser();

		operator bool () const;
		bool operator !() const;

		// parse helpers
		static bool IsWhitespace(char ch);
		void Putback(const std::string& str);
		void StripWhitespace(int n = -1);
		int GetNumOfSpaces();
		bool SeqContinues();

		// readers
		Node *ReadNextNode();
		std::string ReadNextToken();

	private:
		bool m_ok;

		std::istream& INPUT;
		std::stack <State> m_state;
	};
}