simplekey.cpp 3.12 KB
Newer Older
1
2
3
4
5
6
7
8
#include "crt.h"
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"

namespace YAML
{
9
	Scanner::SimpleKey::SimpleKey(const Mark& mark_, int flowLevel_)
10
		: mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0)
11
12
13
14
15
	{
	}

	void Scanner::SimpleKey::Validate()
	{
16
17
18
		// Note: pIndent will *not* be garbage here; see below
		if(pIndent)
			pIndent->isValid = true;
19
		if(pMapStart)
20
			pMapStart->status = Token::VALID;
21
		if(pKey)
22
			pKey->status = Token::VALID;
23
24
25
26
	}

	void Scanner::SimpleKey::Invalidate()
	{
27
28
		// Note: pIndent might be a garbage pointer here, but that's ok
		//       An indent will only be popped if the simple key is invalid
29
		if(pMapStart)
30
			pMapStart->status = Token::INVALID;
31
		if(pKey)
32
			pKey->status = Token::INVALID;
33
	}
34
35
36
37
38
39
40
41
42
43
44
45
	
	// CanInsertPotentialSimpleKey
	bool Scanner::CanInsertPotentialSimpleKey() const
	{
		if(!m_simpleKeyAllowed)
			return false;
		
		if(InFlowContext() && m_flows.top() != FLOW_MAP)
			return false;

		return !ExistsActiveSimpleKey();
	}
46

47
48
49
50
	// ExistsActiveSimpleKey
	// . Returns true if there's a potential simple key at our flow level
	//   (there's allowed at most one per flow level, i.e., at the start of the flow start token)
	bool Scanner::ExistsActiveSimpleKey() const
51
	{
52
53
54
55
		if(m_simpleKeys.empty())
			return false;
		
		const SimpleKey& key = m_simpleKeys.top();
56
		return key.flowLevel == GetFlowLevel();
57
58
59
60
61
62
63
	}

	// InsertPotentialSimpleKey
	// . If we can, add a potential simple key to the queue,
	//   and save it on a stack.
	void Scanner::InsertPotentialSimpleKey()
	{
64
		if(!CanInsertPotentialSimpleKey())
65
66
			return;
		
67
		SimpleKey key(INPUT.mark(), GetFlowLevel());
68
69

		// first add a map start, if necessary
70
71
72
73
		key.pIndent = PushIndentTo(INPUT.column(), IndentMarker::MAP);
		if(key.pIndent) {
			key.pIndent->isValid = false;
			key.pMapStart = key.pIndent->pStartToken;
74
			key.pMapStart->status = Token::UNVERIFIED;
75
		}
76
77

		// then add the (now unverified) key
78
		m_tokens.push(Token(Token::KEY, INPUT.mark()));
79
		key.pKey = &m_tokens.back();
80
		key.pKey->status = Token::UNVERIFIED;
81
82
83
84

		m_simpleKeys.push(key);
	}

85
86
87
88
89
90
91
92
93
	// InvalidateSimpleKey
	// . Automatically invalidate the simple key in our flow level
	void Scanner::InvalidateSimpleKey()
	{
		if(m_simpleKeys.empty())
			return;
		
		// grab top key
		SimpleKey& key = m_simpleKeys.top();
94
		if(key.flowLevel != GetFlowLevel())
95
96
97
98
99
100
			return;
		
		key.Invalidate();
		m_simpleKeys.pop();
	}

101
102
103
	// VerifySimpleKey
	// . Determines whether the latest simple key to be added is valid,
	//   and if so, makes it valid.
104
	bool Scanner::VerifySimpleKey()
105
106
	{
		if(m_simpleKeys.empty())
107
			return false;
108
109
110
111
112

		// grab top key
		SimpleKey key = m_simpleKeys.top();

		// only validate if we're in the correct flow level
113
		if(key.flowLevel != GetFlowLevel())
114
115
116
117
118
119
			return false;

		m_simpleKeys.pop();

		bool isValid = true;

120
		// needs to be less than 1024 characters and inline
121
		if(INPUT.line() != key.mark.line || INPUT.pos() - key.mark.pos > 1024)
122
123
124
125
126
127
128
129
130
131
132
			isValid = false;

		// invalidate key
		if(isValid)
			key.Validate();
		else
			key.Invalidate();

		return isValid;
	}

133
	void Scanner::PopAllSimpleKeys()
134
135
	{
		while(!m_simpleKeys.empty())
136
			m_simpleKeys.pop();
137
138
	}
}