"docs/vscode:/vscode.git/clone" did not exist on "6237bd12476fa2f1c39ea47c9113e05faf6bf307"
simplekey.cpp 3.13 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;
		//       we "garbage collect" them so we can
		//       always refer to them
18
		if(pIndent)
19
			pIndent->status = IndentMarker::VALID;
20
		if(pMapStart)
21
			pMapStart->status = Token::VALID;
22
		if(pKey)
23
			pKey->status = Token::VALID;
24
25
26
27
	}

	void Scanner::SimpleKey::Invalidate()
	{
28
29
		if(pIndent)
			pIndent->status = IndentMarker::INVALID;
30
		if(pMapStart)
31
			pMapStart->status = Token::INVALID;
32
		if(pKey)
33
			pKey->status = Token::INVALID;
34
	}
35
36
37
38
39
40
41
42
43
44
45
46
	
	// CanInsertPotentialSimpleKey
	bool Scanner::CanInsertPotentialSimpleKey() const
	{
		if(!m_simpleKeyAllowed)
			return false;
		
		if(InFlowContext() && m_flows.top() != FLOW_MAP)
			return false;

		return !ExistsActiveSimpleKey();
	}
47

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

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

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

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

		m_simpleKeys.push(key);
	}

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

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

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

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

		m_simpleKeys.pop();

		bool isValid = true;

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

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

		return isValid;
	}

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