Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
0683cbf8
Commit
0683cbf8
authored
Jun 30, 2008
by
Jesse Beder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mostly finished refactoring the scalar scanning.
parent
5f8252ee
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
183 additions
and
369 deletions
+183
-369
exceptions.h
+3
-3
exp.h
+1
-0
scanner.h
+4
-3
scanscalar.cpp
+152
-337
scanscalar.h
+11
-17
test.yaml
+12
-9
No files found.
exceptions.h
View file @
0683cbf8
...
@@ -11,10 +11,10 @@ namespace YAML
...
@@ -11,10 +11,10 @@ namespace YAML
class
IllegalMapKey
:
public
Exception
{};
class
IllegalMapKey
:
public
Exception
{};
class
IllegalMapValue
:
public
Exception
{};
class
IllegalMapValue
:
public
Exception
{};
class
IllegalScalar
:
public
Exception
{};
class
IllegalScalar
:
public
Exception
{};
class
IllegalTabIn
Scalar
:
public
Exception
{};
class
IllegalTabIn
Indentation
:
public
Exception
{};
class
IllegalFlowEnd
:
public
Exception
{};
class
IllegalFlowEnd
:
public
Exception
{};
class
DocIndicatorInQuote
:
public
Exception
{};
class
IllegalDocIndicator
:
public
Exception
{};
class
EOFInQuote
:
public
Exception
{};
class
IllegalEOF
:
public
Exception
{};
class
RequiredSimpleKeyNotFound
:
public
Exception
{};
class
RequiredSimpleKeyNotFound
:
public
Exception
{};
class
ZeroIndentationInBlockScalar
:
public
Exception
{};
class
ZeroIndentationInBlockScalar
:
public
Exception
{};
class
UnexpectedCharacterInBlockScalar
:
public
Exception
{};
class
UnexpectedCharacterInBlockScalar
:
public
Exception
{};
...
...
exp.h
View file @
0683cbf8
...
@@ -25,6 +25,7 @@ namespace YAML
...
@@ -25,6 +25,7 @@ namespace YAML
const
RegEx
DocStart
=
RegEx
(
"---"
)
+
(
BlankOrBreak
||
RegEx
(
EOF
)
||
RegEx
());
const
RegEx
DocStart
=
RegEx
(
"---"
)
+
(
BlankOrBreak
||
RegEx
(
EOF
)
||
RegEx
());
const
RegEx
DocEnd
=
RegEx
(
"..."
)
+
(
BlankOrBreak
||
RegEx
(
EOF
)
||
RegEx
());
const
RegEx
DocEnd
=
RegEx
(
"..."
)
+
(
BlankOrBreak
||
RegEx
(
EOF
)
||
RegEx
());
const
RegEx
DocIndicator
=
DocStart
||
DocEnd
;
const
RegEx
BlockEntry
=
RegEx
(
'-'
)
+
(
BlankOrBreak
||
RegEx
(
EOF
));
const
RegEx
BlockEntry
=
RegEx
(
'-'
)
+
(
BlankOrBreak
||
RegEx
(
EOF
));
const
RegEx
Key
=
RegEx
(
'?'
),
const
RegEx
Key
=
RegEx
(
'?'
),
KeyInFlow
=
RegEx
(
'?'
)
+
BlankOrBreak
;
KeyInFlow
=
RegEx
(
'?'
)
+
BlankOrBreak
;
...
...
scanner.h
View file @
0683cbf8
...
@@ -19,19 +19,20 @@ namespace YAML
...
@@ -19,19 +19,20 @@ namespace YAML
~
Scanner
();
~
Scanner
();
Token
*
GetNextToken
();
Token
*
GetNextToken
();
void
Scan
();
private
:
// scanning
void
ScanNextToken
();
void
ScanNextToken
();
void
ScanToNextToken
();
void
ScanToNextToken
();
Token
*
PushIndentTo
(
int
column
,
bool
sequence
);
Token
*
PushIndentTo
(
int
column
,
bool
sequence
);
void
PopIndentTo
(
int
column
);
void
PopIndentTo
(
int
column
);
// checking input
void
InsertSimpleKey
();
void
InsertSimpleKey
();
bool
VerifySimpleKey
();
bool
VerifySimpleKey
();
void
VerifyAllSimpleKeys
();
void
VerifyAllSimpleKeys
();
void
Scan
();
private
:
bool
IsWhitespaceToBeEaten
(
char
ch
);
bool
IsWhitespaceToBeEaten
(
char
ch
);
bool
IsDocumentStart
();
bool
IsDocumentStart
();
bool
IsDocumentEnd
();
bool
IsDocumentEnd
();
...
...
scanscalar.cpp
View file @
0683cbf8
...
@@ -6,135 +6,138 @@
...
@@ -6,135 +6,138 @@
namespace
YAML
namespace
YAML
{
{
//////////////////////////////////////////////////////////
// ScanScalar
// WhitespaceInfo
std
::
string
ScanScalar
(
Stream
&
INPUT
,
ScanScalarInfo
&
info
)
WhitespaceInfo
::
WhitespaceInfo
()
:
leadingBlanks
(
false
),
fold
(
true
),
chomp
(
0
),
increment
(
0
)
{
{
bool
foundNonEmptyLine
=
false
;
bool
emptyLine
=
false
,
moreIndented
=
false
;
std
::
string
scalar
;
info
.
leadingSpaces
=
false
;
while
(
INPUT
)
{
// ********************************
// Phase #1: scan until line ending
while
(
!
info
.
end
.
Matches
(
INPUT
)
&&
!
Exp
::
Break
.
Matches
(
INPUT
))
{
if
(
INPUT
.
peek
()
==
EOF
)
break
;
// document indicator?
if
(
INPUT
.
column
==
0
&&
Exp
::
DocIndicator
.
Matches
(
INPUT
))
{
if
(
info
.
onDocIndicator
==
BREAK
)
break
;
else
if
(
info
.
onDocIndicator
==
THROW
)
throw
IllegalDocIndicator
();
}
}
void
WhitespaceInfo
::
SetChompers
(
char
ch
)
foundNonEmptyLine
=
true
;
{
if
(
ch
==
'+'
)
// escaped newline? (only if we're escaping on slash)
chomp
=
1
;
if
(
info
.
escape
==
'\\'
&&
Exp
::
EscBreak
.
Matches
(
INPUT
))
{
else
if
(
ch
==
'-'
)
int
n
=
Exp
::
EscBreak
.
Match
(
INPUT
);
chomp
=
-
1
;
INPUT
.
Eat
(
n
);
else
if
(
Exp
::
Digit
.
Matches
(
ch
))
{
continue
;
increment
=
ch
-
'0'
;
if
(
increment
==
0
)
throw
ZeroIndentationInBlockScalar
();
}
}
// escape this?
if
(
INPUT
.
peek
()
==
info
.
escape
)
{
scalar
+=
Exp
::
Escape
(
INPUT
);
continue
;
}
}
void
WhitespaceInfo
::
AddBlank
(
char
ch
)
// otherwise, just add the damn character
{
scalar
+=
INPUT
.
GetChar
();
if
(
!
leadingBlanks
)
whitespace
+=
ch
;
}
}
void
WhitespaceInfo
::
AddBreak
(
const
std
::
string
&
line
)
// eof? if we're looking to eat something, then we throw
{
if
(
INPUT
.
peek
()
==
EOF
)
{
// where to store this character?
if
(
info
.
eatEnd
)
if
(
!
leadingBlanks
)
{
throw
IllegalEOF
();
leadingBlanks
=
true
;
break
;
whitespace
=
""
;
leadingBreaks
+=
line
;
}
else
trailingBreaks
+=
line
;
}
}
std
::
string
WhitespaceInfo
::
Join
(
bool
lastLine
)
// doc indicator?
{
if
(
info
.
onDocIndicator
==
BREAK
&&
INPUT
.
column
==
0
&&
Exp
::
DocIndicator
.
Matches
(
INPUT
))
std
::
string
ret
;
break
;
if
(
leadingBlanks
)
{
// are we done via character match?
// fold line break?
int
n
=
info
.
end
.
Match
(
INPUT
);
if
(
fold
&&
Exp
::
Break
.
Matches
(
leadingBreaks
)
&&
trailingBreaks
.
empty
()
&&
!
lastLine
)
if
(
n
>=
0
)
{
ret
=
" "
;
if
(
info
.
eatEnd
)
else
if
(
!
lastLine
||
chomp
!=
-
1
)
INPUT
.
Eat
(
n
);
ret
=
leadingBreaks
;
break
;
if
(
!
lastLine
||
chomp
==
1
)
ret
+=
trailingBreaks
;
leadingBlanks
=
false
;
leadingBreaks
=
""
;
trailingBreaks
=
""
;
}
else
if
(
!
whitespace
.
empty
())
{
ret
=
whitespace
;
whitespace
=
""
;
}
}
return
ret
;
// ********************************
// Phase #2: eat line ending
n
=
Exp
::
Break
.
Match
(
INPUT
);
INPUT
.
Eat
(
n
);
// ********************************
// Phase #3: scan initial spaces
// first the required indentation
while
(
INPUT
.
peek
()
==
' '
&&
(
INPUT
.
column
<
info
.
indent
||
(
info
.
detectIndent
&&
!
foundNonEmptyLine
)))
INPUT
.
Eat
(
1
);
// update indent if we're auto-detecting
if
(
info
.
detectIndent
&&
!
foundNonEmptyLine
)
info
.
indent
=
std
::
max
(
info
.
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
<
info
.
indent
&&
info
.
onTabInIndentation
==
THROW
)
throw
IllegalTabInIndentation
();
if
(
!
info
.
eatLeadingWhitespace
)
break
;
INPUT
.
Eat
(
1
);
}
// was this an empty line?
bool
nextEmptyLine
=
Exp
::
Break
.
Matches
(
INPUT
);
bool
nextMoreIndented
=
(
INPUT
.
peek
()
==
' '
);
// TODO: for block scalars, we always start with a newline, so we should fold OR keep that
if
(
info
.
fold
&&
!
emptyLine
&&
!
nextEmptyLine
&&
!
moreIndented
&&
!
nextMoreIndented
)
scalar
+=
" "
;
else
scalar
+=
"
\n
"
;
emptyLine
=
nextEmptyLine
;
moreIndented
=
nextMoreIndented
;
// are we done via indentation?
if
(
!
emptyLine
&&
INPUT
.
column
<
info
.
indent
)
{
info
.
leadingSpaces
=
true
;
break
;
}
}
// post-processing
if
(
info
.
trimTrailingSpaces
)
{
unsigned
pos
=
scalar
.
find_last_not_of
(
' '
);
if
(
pos
<
scalar
.
size
())
scalar
.
erase
(
pos
+
1
);
}
if
(
info
.
chomp
<=
0
)
{
unsigned
pos
=
scalar
.
find_last_not_of
(
'\n'
);
if
(
info
.
chomp
==
0
&&
pos
+
1
<
scalar
.
size
())
scalar
.
erase
(
pos
+
2
);
else
if
(
info
.
chomp
==
-
1
&&
pos
<
scalar
.
size
())
scalar
.
erase
(
pos
+
1
);
}
return
scalar
;
}
}
// PlainScalarToken
// PlainScalarToken
// . We scan these in passes of two steps each: First, grab all non-whitespace
// characters we can, and then grab all whitespace characters we can.
// . This has the benefit of letting us handle leading whitespace (which is chomped)
// and in-line whitespace (which is kept) separately.
template
<>
PlainScalarToken
*
Scanner
::
ScanToken
(
PlainScalarToken
*
pToken
)
template
<>
PlainScalarToken
*
Scanner
::
ScanToken
(
PlainScalarToken
*
pToken
)
{
{
//// now eat and store the scalar
// set up the scanning parameters
//std::string scalar;
//WhitespaceInfo info;
//while(INPUT) {
// // doc start/end tokens
// if(IsDocumentStart() || IsDocumentEnd())
// break;
// // comment
// if(Exp::Comment.Matches(INPUT))
// break;
// // first eat non-blanks
// while(INPUT && !Exp::BlankOrBreak.Matches(INPUT)) {
// // illegal colon in flow context
// if(m_flowLevel > 0 && Exp::IllegalColonInScalar.Matches(INPUT))
// throw IllegalScalar();
// // characters that might end the scalar
// if(m_flowLevel > 0 && Exp::EndScalarInFlow.Matches(INPUT))
// break;
// if(m_flowLevel == 0 && Exp::EndScalar.Matches(INPUT))
// break;
// // finally, read the character!
// scalar += GetChar();
// }
// // did we hit a non-blank character that ended us?
// if(!Exp::BlankOrBreak.Matches(INPUT))
// break;
// // now eat blanks
// while(INPUT && Exp::BlankOrBreak.Matches(INPUT)) {
// if(Exp::Blank.Matches(INPUT)) {
// // can't use tabs as indentation! only spaces!
// if(INPUT.peek() == '\t' && info.leadingBlanks && m_column <= m_indents.top())
// throw IllegalTabInScalar();
// info.AddBlank(GetChar());
// } else {
// // we know it's a line break; see how many characters to read
// int n = Exp::Break.Match(INPUT);
// std::string line = GetChar(n);
// info.AddBreak(line);
// // and we can't continue a simple key to the next line
// ValidateSimpleKey();
// }
// }
// // break if we're below the indentation level
// if(m_flowLevel == 0 && m_column <= m_indents.top())
// break;
// // finally join whitespace
// scalar += info.Join();
//}
ScanScalarInfo
info
;
ScanScalarInfo
info
;
info
.
end
=
(
m_flowLevel
>
0
?
Exp
::
EndScalarInFlow
:
Exp
::
EndScalar
)
||
(
RegEx
(
' '
)
+
Exp
::
Comment
);
info
.
end
=
(
m_flowLevel
>
0
?
Exp
::
EndScalarInFlow
:
Exp
::
EndScalar
)
||
(
RegEx
(
' '
)
+
Exp
::
Comment
);
info
.
eatEnd
=
false
;
info
.
eatEnd
=
false
;
...
@@ -143,6 +146,8 @@ namespace YAML
...
@@ -143,6 +146,8 @@ namespace YAML
info
.
eatLeadingWhitespace
=
true
;
info
.
eatLeadingWhitespace
=
true
;
info
.
trimTrailingSpaces
=
true
;
info
.
trimTrailingSpaces
=
true
;
info
.
chomp
=
CLIP
;
info
.
chomp
=
CLIP
;
info
.
onDocIndicator
=
BREAK
;
info
.
onTabInIndentation
=
THROW
;
// insert a potential simple key
// insert a potential simple key
if
(
m_simpleKeyAllowed
)
if
(
m_simpleKeyAllowed
)
...
@@ -150,9 +155,13 @@ namespace YAML
...
@@ -150,9 +155,13 @@ namespace YAML
pToken
->
value
=
ScanScalar
(
INPUT
,
info
);
pToken
->
value
=
ScanScalar
(
INPUT
,
info
);
m_simpleKeyAllowed
=
false
;
// can have a simple key only if we ended the scalar by starting a new line
if
(
true
/*info.leadingBlanks*/
)
m_simpleKeyAllowed
=
info
.
leadingSpaces
;
m_simpleKeyAllowed
=
true
;
// finally, we can't have any colons in a scalar, so if we ended on a colon, there
// had better be a break after it
if
(
Exp
::
IllegalColonInScalar
.
Matches
(
INPUT
))
throw
IllegalScalar
();
return
pToken
;
return
pToken
;
}
}
...
@@ -160,76 +169,11 @@ namespace YAML
...
@@ -160,76 +169,11 @@ namespace YAML
// QuotedScalarToken
// QuotedScalarToken
template
<>
QuotedScalarToken
*
Scanner
::
ScanToken
(
QuotedScalarToken
*
pToken
)
template
<>
QuotedScalarToken
*
Scanner
::
ScanToken
(
QuotedScalarToken
*
pToken
)
{
{
//// now eat and store the scalar
//std::string scalar;
//WhitespaceInfo info;
//while(INPUT) {
// if(IsDocumentStart() || IsDocumentEnd())
// throw DocIndicatorInQuote();
// if(INPUT.peek() == EOF)
// throw EOFInQuote();
// // first eat non-blanks
// while(INPUT && !Exp::BlankOrBreak.Matches(INPUT)) {
// // escaped single quote?
// if(pToken->single && Exp::EscSingleQuote.Matches(INPUT)) {
// int n = Exp::EscSingleQuote.Match(INPUT);
// scalar += GetChar(n);
// continue;
// }
// // is the quote ending?
// if(INPUT.peek() == quote)
// break;
// // escaped newline?
// if(Exp::EscBreak.Matches(INPUT))
// break;
// // other escape sequence
// if(INPUT.peek() == '\\') {
// int length = 0;
// scalar += Exp::Escape(INPUT, length);
// m_column += length;
// continue;
// }
// // and finally, just add the damn character
// scalar += GetChar();
// }
// // is the quote ending?
// if(INPUT.peek() == quote) {
// // eat and go
// GetChar();
// break;
// }
// // now we eat blanks
// while(Exp::BlankOrBreak.Matches(INPUT)) {
// if(Exp::Blank.Matches(INPUT)) {
// info.AddBlank(GetChar());
// } else {
// // we know it's a line break; see how many characters to read
// int n = Exp::Break.Match(INPUT);
// std::string line = GetChar(n);
// info.AddBreak(line);
// // and we can't continue a simple key to the next line
// ValidateSimpleKey();
// }
// }
// // and finally join the whitespace
// scalar += info.Join();
//}
// eat single or double quote
// eat single or double quote
char
quote
=
INPUT
.
GetChar
();
char
quote
=
INPUT
.
GetChar
();
pToken
->
single
=
(
quote
==
'\''
);
pToken
->
single
=
(
quote
==
'\''
);
// setup the scanning parameters
ScanScalarInfo
info
;
ScanScalarInfo
info
;
info
.
end
=
(
pToken
->
single
?
RegEx
(
quote
)
&&
!
Exp
::
EscSingleQuote
:
RegEx
(
quote
));
info
.
end
=
(
pToken
->
single
?
RegEx
(
quote
)
&&
!
Exp
::
EscSingleQuote
:
RegEx
(
quote
));
info
.
eatEnd
=
true
;
info
.
eatEnd
=
true
;
...
@@ -239,6 +183,7 @@ namespace YAML
...
@@ -239,6 +183,7 @@ namespace YAML
info
.
eatLeadingWhitespace
=
true
;
info
.
eatLeadingWhitespace
=
true
;
info
.
trimTrailingSpaces
=
false
;
info
.
trimTrailingSpaces
=
false
;
info
.
chomp
=
CLIP
;
info
.
chomp
=
CLIP
;
info
.
onDocIndicator
=
THROW
;
// insert a potential simple key
// insert a potential simple key
if
(
m_simpleKeyAllowed
)
if
(
m_simpleKeyAllowed
)
...
@@ -251,9 +196,14 @@ namespace YAML
...
@@ -251,9 +196,14 @@ namespace YAML
}
}
// BlockScalarToken
// BlockScalarToken
// . These need a little extra processing beforehand.
// . We need to scan the line where the indicator is (this doesn't count as part of the scalar),
// and then we need to figure out what level of indentation we'll be using.
template
<>
BlockScalarToken
*
Scanner
::
ScanToken
(
BlockScalarToken
*
pToken
)
template
<>
BlockScalarToken
*
Scanner
::
ScanToken
(
BlockScalarToken
*
pToken
)
{
{
WhitespaceInfo
info
;
ScanScalarInfo
info
;
info
.
indent
=
1
;
info
.
detectIndent
=
true
;
// eat block indicator ('|' or '>')
// eat block indicator ('|' or '>')
char
indicator
=
INPUT
.
GetChar
();
char
indicator
=
INPUT
.
GetChar
();
...
@@ -261,10 +211,21 @@ namespace YAML
...
@@ -261,10 +211,21 @@ namespace YAML
// eat chomping/indentation indicators
// eat chomping/indentation indicators
int
n
=
Exp
::
Chomp
.
Match
(
INPUT
);
int
n
=
Exp
::
Chomp
.
Match
(
INPUT
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
info
.
SetChompers
(
INPUT
.
GetChar
());
char
ch
=
INPUT
.
GetChar
();
if
(
ch
==
'+'
)
info
.
chomp
=
KEEP
;
else
if
(
ch
==
'-'
)
info
.
chomp
=
STRIP
;
else
if
(
Exp
::
Digit
.
Matches
(
ch
))
{
info
.
indent
=
ch
-
'0'
;
info
.
detectIndent
=
false
;
if
(
info
.
indent
==
0
)
throw
ZeroIndentationInBlockScalar
();
}
}
//
first
eat whitespace
//
now
eat whitespace
while
(
Exp
::
Blank
.
Matches
(
INPUT
))
while
(
Exp
::
Blank
.
Matches
(
INPUT
))
INPUT
.
Eat
(
1
);
INPUT
.
Eat
(
1
);
...
@@ -277,164 +238,18 @@ namespace YAML
...
@@ -277,164 +238,18 @@ namespace YAML
if
(
INPUT
&&
!
Exp
::
Break
.
Matches
(
INPUT
))
if
(
INPUT
&&
!
Exp
::
Break
.
Matches
(
INPUT
))
throw
UnexpectedCharacterInBlockScalar
();
throw
UnexpectedCharacterInBlockScalar
();
// and eat that baby
INPUT
.
EatLineBreak
();
// set the initial indentation
// set the initial indentation
int
indent
=
info
.
increment
;
if
(
m_indents
.
top
()
>=
0
)
if
(
info
.
increment
&&
m_indents
.
top
()
>=
0
)
info
.
indent
+=
m_indents
.
top
();
indent
+=
m_indents
.
top
();
GetBlockIndentation
(
INPUT
,
indent
,
info
.
trailingBreaks
,
m_indents
.
top
());
ScanScalarInfo
sinfo
;
info
.
eatLeadingWhitespace
=
false
;
sinfo
.
indent
=
indent
;
info
.
trimTrailingSpaces
=
false
;
sinfo
.
fold
=
info
.
fold
;
info
.
onTabInIndentation
=
THROW
;
sinfo
.
eatLeadingWhitespace
=
false
;
sinfo
.
trimTrailingSpaces
=
false
;
sinfo
.
chomp
=
(
CHOMP
)
info
.
chomp
;
pToken
->
value
=
ScanScalar
(
INPUT
,
s
info
);
pToken
->
value
=
ScanScalar
(
INPUT
,
info
);
// simple keys always ok after block scalars (since we're gonna start a new line anyways)
// simple keys always ok after block scalars (since we're gonna start a new line anyways)
m_simpleKeyAllowed
=
true
;
m_simpleKeyAllowed
=
true
;
return
pToken
;
return
pToken
;
}
}
// GetBlockIndentation
// . Helper to scanning a block scalar.
// . Eats leading *indentation* zeros (i.e., those that come before 'indent'),
// and updates 'indent' (if it hasn't been set yet).
void
GetBlockIndentation
(
Stream
&
INPUT
,
int
&
indent
,
std
::
string
&
breaks
,
int
topIndent
)
{
int
maxIndent
=
0
;
while
(
1
)
{
// eat as many indentation spaces as we can
while
((
indent
==
0
||
INPUT
.
column
<
indent
)
&&
INPUT
.
peek
()
==
' '
)
INPUT
.
Eat
(
1
);
if
(
INPUT
.
column
>
maxIndent
)
maxIndent
=
INPUT
.
column
;
// do we need more indentation, but we've got a tab?
if
((
indent
==
0
||
INPUT
.
column
<
indent
)
&&
INPUT
.
peek
()
==
'\t'
)
throw
IllegalTabInScalar
();
// TODO: are literal scalar lines allowed to have tabs here?
// is this a non-empty line?
if
(
!
Exp
::
Break
.
Matches
(
INPUT
))
break
;
// otherwise, eat the line break and move on
int
n
=
Exp
::
Break
.
Match
(
INPUT
);
breaks
+=
INPUT
.
GetChar
(
n
);
}
// finally, set the indentation
if
(
indent
==
0
)
{
indent
=
maxIndent
;
if
(
indent
<
topIndent
+
1
)
indent
=
topIndent
+
1
;
if
(
indent
<
1
)
indent
=
1
;
}
}
// ScanScalar
std
::
string
ScanScalar
(
Stream
&
INPUT
,
ScanScalarInfo
info
)
{
bool
emptyLine
=
false
,
moreIndented
=
false
;
std
::
string
scalar
;
while
(
INPUT
)
{
// ********************************
// Phase #1: scan until line ending
while
(
!
info
.
end
.
Matches
(
INPUT
)
&&
!
Exp
::
Break
.
Matches
(
INPUT
))
{
if
(
INPUT
.
peek
()
==
EOF
)
break
;
// escaped newline? (only if we're escaping on slash)
if
(
info
.
escape
==
'\\'
&&
Exp
::
EscBreak
.
Matches
(
INPUT
))
{
int
n
=
Exp
::
EscBreak
.
Match
(
INPUT
);
INPUT
.
Eat
(
n
);
continue
;
}
// escape this?
if
(
INPUT
.
peek
()
==
info
.
escape
)
{
scalar
+=
Exp
::
Escape
(
INPUT
);
continue
;
}
// otherwise, just add the damn character
scalar
+=
INPUT
.
GetChar
();
}
// eof? if we're looking to eat something, then we throw
if
(
INPUT
.
peek
()
==
EOF
)
{
if
(
info
.
eatEnd
)
throw
EOFInQuote
();
break
;
}
// are we done via character match?
int
n
=
info
.
end
.
Match
(
INPUT
);
if
(
n
>=
0
)
{
if
(
info
.
eatEnd
)
INPUT
.
Eat
(
n
);
break
;
}
// ********************************
// Phase #2: eat line ending
n
=
Exp
::
Break
.
Match
(
INPUT
);
INPUT
.
Eat
(
n
);
// ********************************
// Phase #3: scan initial spaces
// first the required indentation
while
(
INPUT
.
peek
()
==
' '
&&
INPUT
.
column
<
info
.
indent
)
INPUT
.
Eat
(
1
);
// and then the rest of the whitespace
if
(
info
.
eatLeadingWhitespace
)
{
while
(
Exp
::
Blank
.
Matches
(
INPUT
))
INPUT
.
Eat
(
1
);
}
// was this an empty line?
bool
nextEmptyLine
=
Exp
::
Break
.
Matches
(
INPUT
);
bool
nextMoreIndented
=
(
INPUT
.
peek
()
==
' '
);
if
(
info
.
fold
&&
!
emptyLine
&&
!
nextEmptyLine
&&
!
moreIndented
&&
!
nextMoreIndented
)
scalar
+=
" "
;
else
scalar
+=
"
\n
"
;
emptyLine
=
nextEmptyLine
;
moreIndented
=
nextMoreIndented
;
// are we done via indentation?
if
(
!
emptyLine
&&
INPUT
.
column
<
info
.
indent
)
break
;
}
// post-processing
if
(
info
.
trimTrailingSpaces
)
{
unsigned
pos
=
scalar
.
find_last_not_of
(
' '
);
if
(
pos
<
scalar
.
size
())
scalar
.
erase
(
pos
+
1
);
}
if
(
info
.
chomp
<=
0
)
{
unsigned
pos
=
scalar
.
find_last_not_of
(
'\n'
);
if
(
info
.
chomp
==
0
&&
pos
+
1
<
scalar
.
size
())
scalar
.
erase
(
pos
+
2
);
else
if
(
info
.
chomp
==
-
1
&&
pos
<
scalar
.
size
())
scalar
.
erase
(
pos
+
1
);
}
return
scalar
;
}
}
}
scanscalar.h
View file @
0683cbf8
...
@@ -7,35 +7,29 @@
...
@@ -7,35 +7,29 @@
namespace
YAML
namespace
YAML
{
{
enum
CHOMP
{
STRIP
=
-
1
,
CLIP
,
KEEP
};
enum
CHOMP
{
STRIP
=
-
1
,
CLIP
,
KEEP
};
enum
ACTION
{
NONE
,
BREAK
,
THROW
};
struct
ScanScalarInfo
{
struct
ScanScalarInfo
{
ScanScalarInfo
()
:
eatEnd
(
false
),
indent
(
0
),
eatLeadingWhitespace
(
0
),
escape
(
0
),
fold
(
false
),
trimTrailingSpaces
(
0
),
chomp
(
CLIP
)
{}
ScanScalarInfo
()
:
eatEnd
(
false
),
indent
(
0
),
detectIndent
(
false
),
eatLeadingWhitespace
(
0
),
escape
(
0
),
fold
(
false
),
trimTrailingSpaces
(
0
),
chomp
(
CLIP
),
onDocIndicator
(
NONE
),
onTabInIndentation
(
NONE
),
leadingSpaces
(
false
)
{}
// input:
RegEx
end
;
// what condition ends this scalar?
RegEx
end
;
// what condition ends this scalar?
bool
eatEnd
;
// should we eat that condition when we see it?
bool
eatEnd
;
// should we eat that condition when we see it?
int
indent
;
// what level of indentation should be eaten and ignored?
int
indent
;
// what level of indentation should be eaten and ignored?
bool
detectIndent
;
// should we try to autodetect the indent?
bool
eatLeadingWhitespace
;
// should we continue eating this delicious indentation after 'indent' spaces?
bool
eatLeadingWhitespace
;
// should we continue eating this delicious indentation after 'indent' spaces?
char
escape
;
// what character do we escape on (i.e., slash or single quote) (0 for none)
char
escape
;
// what character do we escape on (i.e., slash or single quote) (0 for none)
bool
fold
;
// do we fold line ends?
bool
fold
;
// do we fold line ends?
bool
trimTrailingSpaces
;
// do we remove all trailing spaces (at the very end)
bool
trimTrailingSpaces
;
// do we remove all trailing spaces (at the very end)
CHOMP
chomp
;
// do we strip, clip, or keep trailing newlines (at the very end)
CHOMP
chomp
;
// do we strip, clip, or keep trailing newlines (at the very end)
// Note: strip means kill all, clip means keep at most one, keep means keep all
// Note: strip means kill all, clip means keep at most one, keep means keep all
};
ACTION
onDocIndicator
;
// what do we do if we see a document indicator?
ACTION
onTabInIndentation
;
// what do we do if we see a tab where we should be seeing indentation spaces
void
GetBlockIndentation
(
Stream
&
INPUT
,
int
&
indent
,
std
::
string
&
breaks
,
int
topIndent
);
std
::
string
ScanScalar
(
Stream
&
INPUT
,
ScanScalarInfo
info
);
struct
WhitespaceInfo
{
WhitespaceInfo
();
void
SetChompers
(
char
ch
);
// output:
void
AddBlank
(
char
ch
);
bool
leadingSpaces
;
void
AddBreak
(
const
std
::
string
&
line
);
std
::
string
Join
(
bool
lastline
=
false
);
bool
leadingBlanks
;
bool
fold
;
std
::
string
whitespace
,
leadingBreaks
,
trailingBreaks
;
int
chomp
,
increment
;
};
};
std
::
string
ScanScalar
(
Stream
&
INPUT
,
ScanScalarInfo
&
info
);
}
}
test.yaml
View file @
0683cbf8
---
---
-
"
quoted
scalar
\t
with
a
tab
\n
and
a
newline"
-
here's a key
:
value
-
'
This
is
Jesse'
'
s
single
quote!'
here's the first block
:
|
-
|
after the block: value
here's a literal:
and here's a block: |-
#include <iostream>
What's going on?
How are you doing?
Here's some code:
#include <iostream>
int main()
int main()
{
{
std::cout << "Hello World!\n";
std::cout << "Hello World!\n";
return 0;
}
}
-
key1
:
value1
key2
:
value2
I'm doing fine!
\ No newline at end of file
and last key: value
\ No newline at end of file
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