Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
7d2873ce
Commit
7d2873ce
authored
Apr 02, 2016
by
Jesse Beder
Browse files
Fix scalar parsing when a line starts with a comment.
parent
091ddfa5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
26 deletions
+81
-26
src/scanscalar.cpp
src/scanscalar.cpp
+51
-26
test/integration/handler_test.cpp
test/integration/handler_test.cpp
+30
-0
No files found.
src/scanscalar.cpp
View file @
7d2873ce
...
...
@@ -39,16 +39,18 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
std
::
size_t
lastNonWhitespaceChar
=
scalar
.
size
();
bool
escapedNewline
=
false
;
while
(
!
params
.
end
->
Matches
(
INPUT
)
&&
!
Exp
::
Break
().
Matches
(
INPUT
))
{
if
(
!
INPUT
)
if
(
!
INPUT
)
{
break
;
}
// document indicator?
if
(
INPUT
.
column
()
==
0
&&
Exp
::
DocIndicator
().
Matches
(
INPUT
))
{
if
(
params
.
onDocIndicator
==
BREAK
)
if
(
params
.
onDocIndicator
==
BREAK
)
{
break
;
else
if
(
params
.
onDocIndicator
==
THROW
)
}
else
if
(
params
.
onDocIndicator
==
THROW
)
{
throw
ParserException
(
INPUT
.
mark
(),
ErrorMsg
::
DOC_IN_SCALAR
);
}
}
foundNonEmptyLine
=
true
;
pastOpeningBreak
=
true
;
...
...
@@ -74,27 +76,31 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
// otherwise, just add the damn character
char
ch
=
INPUT
.
get
();
scalar
+=
ch
;
if
(
ch
!=
' '
&&
ch
!=
'\t'
)
if
(
ch
!=
' '
&&
ch
!=
'\t'
)
{
lastNonWhitespaceChar
=
scalar
.
size
();
}
}
// eof? if we're looking to eat something, then we throw
if
(
!
INPUT
)
{
if
(
params
.
eatEnd
)
if
(
params
.
eatEnd
)
{
throw
ParserException
(
INPUT
.
mark
(),
ErrorMsg
::
EOF_IN_SCALAR
);
}
break
;
}
// doc indicator?
if
(
params
.
onDocIndicator
==
BREAK
&&
INPUT
.
column
()
==
0
&&
Exp
::
DocIndicator
().
Matches
(
INPUT
))
Exp
::
DocIndicator
().
Matches
(
INPUT
))
{
break
;
}
// are we done via character match?
int
n
=
params
.
end
->
Match
(
INPUT
);
if
(
n
>=
0
)
{
if
(
params
.
eatEnd
)
if
(
params
.
eatEnd
)
{
INPUT
.
eat
(
n
);
}
break
;
}
...
...
@@ -111,23 +117,33 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
// Phase #3: scan initial spaces
// first the required indentation
while
(
INPUT
.
peek
()
==
' '
&&
(
INPUT
.
column
()
<
params
.
indent
||
(
params
.
detectIndent
&&
!
foundNonEmptyLine
)))
while
(
INPUT
.
peek
()
==
' '
&&
(
INPUT
.
column
()
<
params
.
indent
||
(
params
.
detectIndent
&&
!
foundNonEmptyLine
))
&&
!
params
.
end
->
Matches
(
INPUT
))
{
INPUT
.
eat
(
1
);
}
// update indent if we're auto-detecting
if
(
params
.
detectIndent
&&
!
foundNonEmptyLine
)
if
(
params
.
detectIndent
&&
!
foundNonEmptyLine
)
{
params
.
indent
=
std
::
max
(
params
.
indent
,
INPUT
.
column
());
}
// and then the rest of the whitespace
while
(
Exp
::
Blank
().
Matches
(
INPUT
))
{
// we check for tabs that masquerade as indentation
if
(
INPUT
.
peek
()
==
'\t'
&&
INPUT
.
column
()
<
params
.
indent
&&
params
.
onTabInIndentation
==
THROW
)
params
.
onTabInIndentation
==
THROW
)
{
throw
ParserException
(
INPUT
.
mark
(),
ErrorMsg
::
TAB_IN_INDENTATION
);
}
if
(
!
params
.
eatLeadingWhitespace
)
if
(
!
params
.
eatLeadingWhitespace
)
{
break
;
}
if
(
params
.
end
->
Matches
(
INPUT
))
{
break
;
}
INPUT
.
eat
(
1
);
}
...
...
@@ -147,26 +163,29 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
break
;
case
FOLD_BLOCK
:
if
(
!
emptyLine
&&
!
nextEmptyLine
&&
!
moreIndented
&&
!
nextMoreIndented
&&
INPUT
.
column
()
>=
params
.
indent
)
!
nextMoreIndented
&&
INPUT
.
column
()
>=
params
.
indent
)
{
scalar
+=
" "
;
else
if
(
nextEmptyLine
)
}
else
if
(
nextEmptyLine
)
{
foldedNewlineCount
++
;
else
}
else
{
scalar
+=
"
\n
"
;
}
if
(
!
nextEmptyLine
&&
foldedNewlineCount
>
0
)
{
scalar
+=
std
::
string
(
foldedNewlineCount
-
1
,
'\n'
);
if
(
foldedNewlineStartedMoreIndented
||
nextMoreIndented
|
!
foundNonEmptyLine
)
nextMoreIndented
|
!
foundNonEmptyLine
)
{
scalar
+=
"
\n
"
;
}
foldedNewlineCount
=
0
;
}
break
;
case
FOLD_FLOW
:
if
(
nextEmptyLine
)
if
(
nextEmptyLine
)
{
scalar
+=
"
\n
"
;
else
if
(
!
emptyLine
&&
!
nextEmptyLine
&&
!
escapedNewline
)
}
else
if
(
!
emptyLine
&&
!
nextEmptyLine
&&
!
escapedNewline
)
{
scalar
+=
" "
;
}
break
;
}
}
...
...
@@ -186,35 +205,41 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
if
(
params
.
trimTrailingSpaces
)
{
std
::
size_t
pos
=
scalar
.
find_last_not_of
(
' '
);
if
(
lastEscapedChar
!=
std
::
string
::
npos
)
{
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
{
pos
=
lastEscapedChar
;
}
if
(
pos
<
scalar
.
size
())
}
if
(
pos
<
scalar
.
size
())
{
scalar
.
erase
(
pos
+
1
);
}
}
switch
(
params
.
chomp
)
{
case
CLIP
:
{
std
::
size_t
pos
=
scalar
.
find_last_not_of
(
'\n'
);
if
(
lastEscapedChar
!=
std
::
string
::
npos
)
{
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
{
pos
=
lastEscapedChar
;
}
if
(
pos
==
std
::
string
::
npos
)
}
if
(
pos
==
std
::
string
::
npos
)
{
scalar
.
erase
();
else
if
(
pos
+
1
<
scalar
.
size
())
}
else
if
(
pos
+
1
<
scalar
.
size
())
{
scalar
.
erase
(
pos
+
2
);
}
}
break
;
case
STRIP
:
{
std
::
size_t
pos
=
scalar
.
find_last_not_of
(
'\n'
);
if
(
lastEscapedChar
!=
std
::
string
::
npos
)
{
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
if
(
pos
<
lastEscapedChar
||
pos
==
std
::
string
::
npos
)
{
pos
=
lastEscapedChar
;
}
if
(
pos
==
std
::
string
::
npos
)
}
if
(
pos
==
std
::
string
::
npos
)
{
scalar
.
erase
();
else
if
(
pos
<
scalar
.
size
())
}
else
if
(
pos
<
scalar
.
size
())
{
scalar
.
erase
(
pos
+
1
);
}
}
break
;
default:
break
;
...
...
test/integration/handler_test.cpp
View file @
7d2873ce
...
...
@@ -42,5 +42,35 @@ TEST_F(HandlerTest, NullStringScalar) {
EXPECT_CALL
(
handler
,
OnDocumentEnd
());
Parse
(
"foo: null"
);
}
TEST_F
(
HandlerTest
,
CommentOnNewlineOfMapValueWithNoSpaces
)
{
EXPECT_CALL
(
handler
,
OnDocumentStart
(
_
));
EXPECT_CALL
(
handler
,
OnMapStart
(
_
,
"?"
,
0
,
EmitterStyle
::
Block
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"key"
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"value"
));
EXPECT_CALL
(
handler
,
OnMapEnd
());
EXPECT_CALL
(
handler
,
OnDocumentEnd
());
Parse
(
"key: value
\n
# comment"
);
}
TEST_F
(
HandlerTest
,
CommentOnNewlineOfMapValueWithOneSpace
)
{
EXPECT_CALL
(
handler
,
OnDocumentStart
(
_
));
EXPECT_CALL
(
handler
,
OnMapStart
(
_
,
"?"
,
0
,
EmitterStyle
::
Block
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"key"
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"value"
));
EXPECT_CALL
(
handler
,
OnMapEnd
());
EXPECT_CALL
(
handler
,
OnDocumentEnd
());
Parse
(
"key: value
\n
# comment"
);
}
TEST_F
(
HandlerTest
,
CommentOnNewlineOfMapValueWithManySpace
)
{
EXPECT_CALL
(
handler
,
OnDocumentStart
(
_
));
EXPECT_CALL
(
handler
,
OnMapStart
(
_
,
"?"
,
0
,
EmitterStyle
::
Block
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"key"
));
EXPECT_CALL
(
handler
,
OnScalar
(
_
,
"?"
,
0
,
"value"
));
EXPECT_CALL
(
handler
,
OnMapEnd
());
EXPECT_CALL
(
handler
,
OnDocumentEnd
());
Parse
(
"key: value
\n
# comment"
);
}
}
// namespace
}
// namespace YAML
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment