"...composable_kernel.git" did not exist on "f16356d4ff7d7f4f0c28c48c89a55d2372b2a3f7"
simplekey.cpp 2.61 KB
Newer Older
1
#include "crt.h"
beder's avatar
beder committed
2
3
4
5
6
7
8
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"

namespace YAML
{
9
	Scanner::SimpleKey::SimpleKey(int pos_, int line_, int column_, int flowLevel_)
10
		: pos(pos_), line(line_), column(column_), flowLevel(flowLevel_), pMapStart(0), pKey(0)
beder's avatar
beder committed
11
12
13
14
15
16
	{
	}

	void Scanner::SimpleKey::Validate()
	{
		if(pMapStart)
17
			pMapStart->status = TS_VALID;
beder's avatar
beder committed
18
		if(pKey)
19
			pKey->status = TS_VALID;
beder's avatar
beder committed
20
21
22
23
24
	}

	void Scanner::SimpleKey::Invalidate()
	{
		if(pMapStart)
25
			pMapStart->status = TS_INVALID;
beder's avatar
beder committed
26
		if(pKey)
27
			pKey->status = TS_INVALID;
beder's avatar
beder committed
28
29
30
31
32
33
34
	}

	// InsertSimpleKey
	// . Adds a potential simple key to the queue,
	//   and saves it on a stack.
	void Scanner::InsertSimpleKey()
	{
35
		SimpleKey key(INPUT.pos(), INPUT.line, INPUT.column, m_flowLevel);
beder's avatar
beder committed
36
37

		// first add a map start, if necessary
38
		key.pMapStart = PushIndentTo(INPUT.column, false);
beder's avatar
beder committed
39
		if(key.pMapStart)
40
			key.pMapStart->status = TS_UNVERIFIED;
beder's avatar
beder committed
41

42
		// then add the (now unverified) key
43
44
		m_tokens.push(Token(TT_KEY, INPUT.line, INPUT.column));
		key.pKey = &m_tokens.back();
45
		key.pKey->status = TS_UNVERIFIED;
beder's avatar
beder committed
46
47
48
49

		m_simpleKeys.push(key);
	}

50
	// VerifySimpleKey
beder's avatar
beder committed
51
52
	// . Determines whether the latest simple key to be added is valid,
	//   and if so, makes it valid.
53
	bool Scanner::VerifySimpleKey()
beder's avatar
beder committed
54
	{
55
		m_isLastKeyValid = false;
beder's avatar
beder committed
56
		if(m_simpleKeys.empty())
57
			return m_isLastKeyValid;
beder's avatar
beder committed
58
59
60

		// grab top key
		SimpleKey key = m_simpleKeys.top();
61
62
63
64
65

		// only validate if we're in the correct flow level
		if(key.flowLevel != m_flowLevel)
			return false;

beder's avatar
beder committed
66
67
68
69
70
71
72
73
74
75
76
		m_simpleKeys.pop();

		bool isValid = true;

		// needs to be followed immediately by a value
		if(m_flowLevel > 0 && !Exp::ValueInFlow.Matches(INPUT))
			isValid = false;
		if(m_flowLevel == 0 && !Exp::Value.Matches(INPUT))
			isValid = false;

		// also needs to be less than 1024 characters and inline
77
		if(INPUT.line != key.line || INPUT.pos() - key.pos > 1024)
beder's avatar
beder committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
			isValid = false;

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

		// In block style, remember that we've pushed an indent for this potential simple key (if it was starting).
		// If it was invalid, then we need to pop it off.
		// Note: we're guaranteed to be popping the right one (i.e., there couldn't have been anything in
		//       between) since keys have to be inline, and will be invalidated immediately on a newline.
		if(!isValid && m_flowLevel == 0)
			m_indents.pop();

93
		m_isLastKeyValid = isValid;
beder's avatar
beder committed
94
95
96
		return isValid;
	}

97
	void Scanner::VerifyAllSimpleKeys()
beder's avatar
beder committed
98
99
	{
		while(!m_simpleKeys.empty())
100
			VerifySimpleKey();
beder's avatar
beder committed
101
102
	}
}