3rdparty
libpointmatcher
contrib
yaml-cpp-pm
src
simplekey.cpp
Go to the documentation of this file.
1
#include "
scanner.h
"
2
#include "
token.h
"
3
#include "
yaml-cpp-pm/exceptions.h
"
4
#include "
exp.h
"
5
6
namespace
YAML_PM
7
{
8
Scanner::SimpleKey::SimpleKey
(
const
Mark
& mark_,
int
flowLevel_)
9
: mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0)
10
{
11
}
12
13
void
Scanner::SimpleKey::Validate
()
14
{
15
// Note: pIndent will *not* be garbage here;
16
// we "garbage collect" them so we can
17
// 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
void
Scanner::SimpleKey::Invalidate
()
27
{
28
if
(pIndent)
29
pIndent->status =
IndentMarker::INVALID
;
30
if
(pMapStart)
31
pMapStart->status =
Token::INVALID
;
32
if
(pKey)
33
pKey->status =
Token::INVALID
;
34
}
35
36
// CanInsertPotentialSimpleKey
37
bool
Scanner::CanInsertPotentialSimpleKey
()
const
38
{
39
if
(!
m_simpleKeyAllowed
)
40
return
false
;
41
42
return
!
ExistsActiveSimpleKey
();
43
}
44
45
// ExistsActiveSimpleKey
46
// . Returns true if there's a potential simple key at our flow level
47
// (there's allowed at most one per flow level, i.e., at the start of the flow start token)
48
bool
Scanner::ExistsActiveSimpleKey
()
const
49
{
50
if
(
m_simpleKeys
.empty())
51
return
false
;
52
53
const
SimpleKey
& key =
m_simpleKeys
.top();
54
return
key.
flowLevel
==
GetFlowLevel
();
55
}
56
57
// InsertPotentialSimpleKey
58
// . If we can, add a potential simple key to the queue,
59
// and save it on a stack.
60
void
Scanner::InsertPotentialSimpleKey
()
61
{
62
if
(!
CanInsertPotentialSimpleKey
())
63
return
;
64
65
SimpleKey
key(
INPUT
.
mark
(),
GetFlowLevel
());
66
67
// first add a map start, if necessary
68
if
(
InBlockContext
()) {
69
key.
pIndent
=
PushIndentTo
(
INPUT
.
column
(),
IndentMarker::MAP
);
70
if
(key.
pIndent
) {
71
key.
pIndent
->
status
=
IndentMarker::UNKNOWN
;
72
key.
pMapStart
= key.
pIndent
->
pStartToken
;
73
key.
pMapStart
->
status
=
Token::UNVERIFIED
;
74
}
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
m_simpleKeys
.push(key);
83
}
84
85
// InvalidateSimpleKey
86
// . Automatically invalidate the simple key in our flow level
87
void
Scanner::InvalidateSimpleKey
()
88
{
89
if
(
m_simpleKeys
.empty())
90
return
;
91
92
// grab top key
93
SimpleKey
& key =
m_simpleKeys
.top();
94
if
(key.
flowLevel
!=
GetFlowLevel
())
95
return
;
96
97
key.
Invalidate
();
98
m_simpleKeys
.pop();
99
}
100
101
// VerifySimpleKey
102
// . Determines whether the latest simple key to be added is valid,
103
// and if so, makes it valid.
104
bool
Scanner::VerifySimpleKey
()
105
{
106
if
(
m_simpleKeys
.empty())
107
return
false
;
108
109
// grab top key
110
SimpleKey
key =
m_simpleKeys
.top();
111
112
// only validate if we're in the correct flow level
113
if
(key.
flowLevel
!=
GetFlowLevel
())
114
return
false
;
115
116
m_simpleKeys
.pop();
117
118
bool
isValid =
true
;
119
120
// needs to be less than 1024 characters and inline
121
if
(
INPUT
.
line
() != key.
mark
.
line
||
INPUT
.
pos
() - key.
mark
.
pos
> 1024)
122
isValid =
false
;
123
124
// invalidate key
125
if
(isValid)
126
key.
Validate
();
127
else
128
key.
Invalidate
();
129
130
return
isValid;
131
}
132
133
void
Scanner::PopAllSimpleKeys
()
134
{
135
while
(!
m_simpleKeys
.empty())
136
m_simpleKeys
.pop();
137
}
138
}
139
YAML_PM::Scanner::SimpleKey::pIndent
IndentMarker * pIndent
Definition:
scanner.h:90
YAML_PM::Scanner::INPUT
Stream INPUT
Definition:
scanner.h:115
YAML_PM::Token::KEY
@ KEY
Definition:
token.h:57
YAML_PM::Scanner::InBlockContext
bool InBlockContext() const
Definition:
scanner.h:59
exceptions.h
YAML_PM::Stream::column
int column() const
Definition:
stream.h:41
YAML_PM::Scanner::IndentMarker::VALID
@ VALID
Definition:
scanner.h:38
YAML_PM::Scanner::IndentMarker::MAP
@ MAP
Definition:
scanner.h:37
YAML_PM::Scanner::SimpleKey
Definition:
scanner.h:82
YAML_PM::Token::status
STATUS status
Definition:
token.h:76
YAML_PM
Definition:
aliasmanager.h:11
YAML_PM::Token::UNVERIFIED
@ UNVERIFIED
Definition:
token.h:41
YAML_PM::Scanner::SimpleKey::pKey
Token * pKey
Definition:
scanner.h:91
YAML_PM::Stream::pos
int pos() const
Definition:
stream.h:39
YAML_PM::Scanner::ExistsActiveSimpleKey
bool ExistsActiveSimpleKey() const
Definition:
simplekey.cpp:48
YAML_PM::Scanner::SimpleKey::Invalidate
void Invalidate()
Definition:
simplekey.cpp:26
exp.h
YAML_PM::Mark::line
int line
Definition:
mark.h:19
YAML_PM::Scanner::SimpleKey::Validate
void Validate()
Definition:
simplekey.cpp:13
YAML_PM::Scanner::PushIndentTo
IndentMarker * PushIndentTo(int column, IndentMarker::INDENT_TYPE type)
Definition:
scanner.cpp:278
YAML_PM::Scanner::m_simpleKeys
std::stack< SimpleKey > m_simpleKeys
Definition:
scanner.h:124
YAML_PM::Scanner::VerifySimpleKey
bool VerifySimpleKey()
Definition:
simplekey.cpp:104
YAML_PM::Token::INVALID
@ INVALID
Definition:
token.h:41
YAML_PM::Scanner::SimpleKey::SimpleKey
SimpleKey(const Mark &mark_, int flowLevel_)
Definition:
simplekey.cpp:8
YAML_PM::Stream::line
int line() const
Definition:
stream.h:40
YAML_PM::Scanner::IndentMarker::status
STATUS status
Definition:
scanner.h:43
YAML_PM::Scanner::IndentMarker::pStartToken
Token * pStartToken
Definition:
scanner.h:44
YAML_PM::Scanner::m_simpleKeyAllowed
bool m_simpleKeyAllowed
Definition:
scanner.h:122
YAML_PM::Scanner::SimpleKey::flowLevel
int flowLevel
Definition:
scanner.h:89
YAML_PM::Scanner::IndentMarker::INVALID
@ INVALID
Definition:
scanner.h:38
YAML_PM::Token::VALID
@ VALID
Definition:
token.h:41
YAML_PM::Mark::pos
int pos
Definition:
mark.h:18
YAML_PM::Scanner::SimpleKey::pMapStart
Token * pMapStart
Definition:
scanner.h:91
YAML_PM::Scanner::GetFlowLevel
int GetFlowLevel() const
Definition:
scanner.h:60
YAML_PM::Scanner::InsertPotentialSimpleKey
void InsertPotentialSimpleKey()
Definition:
simplekey.cpp:60
YAML_PM::Mark
Definition:
mark.h:13
YAML_PM::Scanner::m_tokens
std::queue< Token > m_tokens
Definition:
scanner.h:118
YAML_PM::Scanner::CanInsertPotentialSimpleKey
bool CanInsertPotentialSimpleKey() const
Definition:
simplekey.cpp:37
YAML_PM::Scanner::InvalidateSimpleKey
void InvalidateSimpleKey()
Definition:
simplekey.cpp:87
YAML_PM::Token
Definition:
token.h:39
scanner.h
YAML_PM::Scanner::PopAllSimpleKeys
void PopAllSimpleKeys()
Definition:
simplekey.cpp:133
YAML_PM::Scanner::SimpleKey::mark
Mark mark
Definition:
scanner.h:88
token.h
YAML_PM::Stream::mark
const Mark mark() const
Definition:
stream.h:38
YAML_PM::Scanner::IndentMarker::UNKNOWN
@ UNKNOWN
Definition:
scanner.h:38
mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:46:00