"src/main.cpp" did not exist on "dacc6319682a58f1953f0cb80d8c952d08c0d299"
simplekey.cpp 3.11 KB
Newer Older
1
2
3
4
5
6
7
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"

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

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

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

		return !ExistsActiveSimpleKey();
	}
45

46
47
48
49
	// 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
50
	{
51
52
53
54
		if(m_simpleKeys.empty())
			return false;
		
		const SimpleKey& key = m_simpleKeys.top();
55
		return key.flowLevel == GetFlowLevel();
56
57
58
59
60
61
62
	}

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

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

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

		m_simpleKeys.push(key);
	}

84
85
86
87
88
89
90
91
92
	// 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();
93
		if(key.flowLevel != GetFlowLevel())
94
95
96
97
98
99
			return;
		
		key.Invalidate();
		m_simpleKeys.pop();
	}

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

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

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

		m_simpleKeys.pop();

		bool isValid = true;

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

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

		return isValid;
	}

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