Compare commits

...

18 Commits

Author SHA1 Message Date
Jesse Beder
22410f46f5 Updated the CMake globbing so it only compiles sources starting with a lowercase letter (apparently Mac OS auto-generates files looking like ._whatever and it was trying to compile those too) 2009-10-25 18:01:48 +00:00
Jesse Beder
9559a661aa Tagged version 0.2.3 2009-10-22 21:55:44 +00:00
Jesse Beder
beb524489c Small refactoring 2009-10-22 21:51:32 +00:00
Jesse Beder
4ffb93c12b Switch to flow map when emitting an empty block map 2009-10-22 14:21:12 +00:00
Jesse Beder
ae06a40fe6 Switch to flow sequence when emitting an empty sequence 2009-10-22 14:17:12 +00:00
Jesse Beder
315b00065b Fixed bug in plain scalar folding 2009-10-20 14:47:16 +00:00
Jesse Beder
6f02f7556e Added a bunch of tests, simplified the testing code 2009-10-20 14:43:24 +00:00
Jesse Beder
fa0af88dfe Merged r270:HEAD of the emitting-unicode branch 2009-10-19 23:31:11 +00:00
Jesse Beder
bce845bb1f Fixed little bug in parser commit 2009-10-19 22:42:30 +00:00
Jesse Beder
ed570b9f7c Added default constructor to Parser, and cleaned it up a bit 2009-10-19 22:40:46 +00:00
Jesse Beder
59b0e986bf Update CMakeLists.txt to append, not overwrite CMAKE_CXX_FLAGS 2009-10-19 22:32:26 +00:00
Jesse Beder
cffb98d15b Patched for optional building of tests and tools 2009-10-12 05:21:00 +00:00
Jesse Beder
3e1ba0f3b4 Refactored the UTF-8 emitting 2009-10-08 21:05:56 +00:00
Jesse Beder
d0b5bf4b7b Fixed the emitter unicode output 2009-10-07 06:46:05 +00:00
Jesse Beder
7db39e66b8 Updated signature of Parser::GetNextDocument (issue 45) 2009-09-29 18:25:11 +00:00
Jesse Beder
94eb7f1dbd Modified old gcc version patch so it still uses the new Node::Read in Visual Studio. Also broke up the \uNNNN characters in the spec tests into \xNN-type strings. 2009-09-16 05:31:28 +00:00
Jesse Beder
5733b77b84 Patched for gcc version <= 3.3 (just fall back to original version of Node::Read) 2009-09-16 04:01:40 +00:00
Jesse Beder
98bebfb628 Tagged version 0.2.2 2009-09-09 01:37:23 +00:00
19 changed files with 590 additions and 330 deletions

View File

@@ -10,16 +10,18 @@ if(IPHONE)
endif(IPHONE)
if(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_CXX_FLAGS "-O2 -Wall -pedantic -Wextra")
set(CMAKE_CXX_FLAGS "-O2 -Wall -pedantic -Wextra ${CMAKE_CXX_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCC)
set(YAML_CPP_VERSION_MAJOR "0")
set(YAML_CPP_VERSION_MINOR "2")
set(YAML_CPP_VERSION_PATCH "1")
set(YAML_CPP_VERSION_PATCH "3")
set(YAML_CPP_VERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}.${YAML_CPP_VERSION_PATCH}")
enable_testing()
option(YAML_CPP_BUILD_TOOLS "Enables or disables yaml-reader and parse tools" true)
if(WIN32)
set(_library_dir bin) # .dll are in PATH, like executables
else(WIN32)
@@ -36,9 +38,9 @@ set(_INSTALL_DESTINATIONS
ARCHIVE DESTINATION lib${LIB_SUFFIX}
)
#
file(GLOB public_headers include/*.h)
file(GLOB private_headers src/*.h)
file(GLOB sources src/*.cpp)
file(GLOB public_headers include/[a-z]*.h)
file(GLOB private_headers src/[a-z]*.h)
file(GLOB sources src/[a-z]*.cpp)
include_directories(${YAML_CPP_SOURCE_DIR}/include)
add_library(yaml-cpp
@@ -64,5 +66,7 @@ if(UNIX)
install(FILES ${PC_FILE} DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
endif(UNIX)
add_subdirectory (yaml-reader)
add_subdirectory (util)
if(YAML_CPP_BUILD_TOOLS)
add_subdirectory (yaml-reader)
add_subdirectory (util)
endif(YAML_CPP_BUILD_TOOLS)

View File

@@ -29,6 +29,7 @@ namespace YAML
const std::string GetLastError() const;
// global setters
bool SetOutputCharset(EMITTER_MANIP value);
bool SetStringFormat(EMITTER_MANIP value);
bool SetBoolFormat(EMITTER_MANIP value);
bool SetIntBase(EMITTER_MANIP value);

View File

@@ -12,6 +12,10 @@ namespace YAML
// general manipulators
Auto,
// output character set
EmitNonAscii,
EscapeNonAscii,
// string manipulators
// Auto, // duplicate
SingleQuoted,

View File

@@ -79,22 +79,22 @@ namespace YAML
template <typename T>
inline bool operator == (const T& value, const Node& node) {
return value == node.Read<T>();
return value == node.operator T();
}
template <typename T>
inline bool operator == (const Node& node, const T& value) {
return value == node.Read<T>();
return value == node.operator T();
}
template <typename T>
inline bool operator != (const T& value, const Node& node) {
return value != node.Read<T>();
return value != node.operator T();
}
template <typename T>
inline bool operator != (const Node& node, const T& value) {
return value != node.Read<T>();
return value != node.operator T();
}
inline bool operator == (const char *value, const Node& node) {

View File

@@ -7,6 +7,17 @@ namespace YAML
// thanks to litb from stackoverflow.com
// http://stackoverflow.com/questions/1386183/how-to-call-a-templated-function-if-it-exists-and-something-else-otherwise/1386390#1386390
// Note: this doesn't work on gcc 3.2, but does on gcc 3.4 and above. I'm not sure about 3.3.
#if __GNUC__ && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ <= 3))
// trick doesn't work? Just fall back to ConvertScalar.
// This means that we can't use any user-defined types as keys in a map
template <typename T>
inline bool Node::Read(T& value) const {
return ConvertScalar(*this, value);
}
#else
// usual case: the trick!
template<bool>
struct read_impl;
@@ -52,6 +63,7 @@ namespace YAML
return read_impl<sizeof (fallback::flag(), Convert(std::string(), value), fallback::flag()) != 1>::read(*this, value);
}
#endif // done with trick
// the main conversion function
template <typename T>

View File

@@ -4,28 +4,31 @@
#define PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include "node.h"
#include "parserstate.h"
#include "noncopyable.h"
#include <ios>
#include <string>
#include <vector>
#include <map>
#include "node.h"
#include "parserstate.h"
#include <memory>
namespace YAML
{
class Scanner;
struct Token;
class Parser
class Parser: private noncopyable
{
public:
Parser();
Parser(std::istream& in);
~Parser();
operator bool() const;
void Load(std::istream& in);
void GetNextDocument(Node& document);
bool GetNextDocument(Node& document);
void PrintTokens(std::ostream& out);
private:
@@ -35,12 +38,7 @@ namespace YAML
void HandleTagDirective(Token *pToken);
private:
// can't copy this
Parser(const Parser&) {}
Parser& operator = (const Parser&) { return *this; }
private:
Scanner *m_pScanner;
std::auto_ptr<Scanner> m_pScanner;
ParserState m_state;
};
}

View File

@@ -37,6 +37,11 @@ namespace YAML
}
// global setters
bool Emitter::SetOutputCharset(EMITTER_MANIP value)
{
return m_pState->SetOutputCharset(value, GLOBAL);
}
bool Emitter::SetStringFormat(EMITTER_MANIP value)
{
return m_pState->SetStringFormat(value, GLOBAL);
@@ -344,12 +349,18 @@ namespace YAML
EMITTER_STATE curState = m_pState->GetCurState();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
if(flowType == FT_BLOCK)
assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY);
else if(flowType == FT_FLOW) {
m_stream << "]";
if(flowType == FT_BLOCK) {
// Note: block sequences are *not* allowed to be empty, but we convert it
// to a flow sequence if it is
assert(curState == ES_DONE_WITH_BLOCK_SEQ_ENTRY || curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
if(curState == ES_WAITING_FOR_BLOCK_SEQ_ENTRY) {
unsigned curIndent = m_pState->GetCurIndent();
m_stream << IndentTo(curIndent) << "[]";
}
} else if(flowType == FT_FLOW) {
// Note: flow sequences are allowed to be empty
assert(curState == ES_DONE_WITH_FLOW_SEQ_ENTRY || curState == ES_WAITING_FOR_FLOW_SEQ_ENTRY);
m_stream << "]";
} else
assert(false);
@@ -399,12 +410,18 @@ namespace YAML
EMITTER_STATE curState = m_pState->GetCurState();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
if(flowType == FT_BLOCK)
assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE);
else if(flowType == FT_FLOW) {
m_stream << "}";
if(flowType == FT_BLOCK) {
// Note: block sequences are *not* allowed to be empty, but we convert it
// to a flow sequence if it is
assert(curState == ES_DONE_WITH_BLOCK_MAP_VALUE || curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY);
if(curState == ES_WAITING_FOR_BLOCK_MAP_ENTRY) {
unsigned curIndent = m_pState->GetCurIndent();
m_stream << IndentTo(curIndent) << "{}";
}
} else if(flowType == FT_FLOW) {
// Note: flow maps are allowed to be empty
assert(curState == ES_DONE_WITH_FLOW_MAP_VALUE || curState == ES_WAITING_FOR_FLOW_MAP_ENTRY);
m_stream << "}";
} else
assert(false);
@@ -485,13 +502,14 @@ namespace YAML
PreAtomicWrite();
EmitSeparationIfNecessary();
bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii;
EMITTER_MANIP strFmt = m_pState->GetStringFormat();
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
unsigned curIndent = m_pState->GetCurIndent();
switch(strFmt) {
case Auto:
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii);
break;
case SingleQuoted:
if(!Utils::WriteSingleQuotedString(m_stream, str)) {
@@ -500,11 +518,11 @@ namespace YAML
}
break;
case DoubleQuoted:
Utils::WriteDoubleQuotedString(m_stream, str);
Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii);
break;
case Literal:
if(flowType == FT_FLOW)
Utils::WriteString(m_stream, str, flowType == FT_FLOW);
Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii);
else
Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent());
break;
@@ -679,3 +697,4 @@ namespace YAML
return *this;
}
}

View File

@@ -9,6 +9,7 @@ namespace YAML
m_stateStack.push(ES_WAITING_FOR_DOC);
// set default global manipulators
m_charset.set(EmitNonAscii);
m_strFmt.set(Auto);
m_boolFmt.set(TrueFalseBool);
m_boolLengthFmt.set(LongBool);
@@ -43,6 +44,7 @@ namespace YAML
// . Only the ones that make sense will be accepted
void EmitterState::SetLocalValue(EMITTER_MANIP value)
{
SetOutputCharset(value, LOCAL);
SetStringFormat(value, LOCAL);
SetBoolFormat(value, LOCAL);
SetBoolCaseFormat(value, LOCAL);
@@ -133,6 +135,18 @@ namespace YAML
m_modifiedSettings.clear();
}
bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope)
{
switch(value) {
case EmitNonAscii:
case EscapeNonAscii:
_Set(m_charset, value, scope);
return true;
default:
return false;
}
}
bool EmitterState::SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope)
{
switch(value) {

View File

@@ -108,6 +108,9 @@ namespace YAML
void ClearModifiedSettings();
// formatters
bool SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope);
EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); }
bool SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope);
EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
@@ -149,6 +152,7 @@ namespace YAML
// other state
std::stack <EMITTER_STATE> m_stateStack;
Setting <EMITTER_MANIP> m_charset;
Setting <EMITTER_MANIP> m_strFmt;
Setting <EMITTER_MANIP> m_boolFmt;
Setting <EMITTER_MANIP> m_boolLengthFmt;

View File

@@ -11,11 +11,123 @@ namespace YAML
namespace Utils
{
namespace {
bool IsPrintable(char ch) {
return (0x20 <= ch && ch <= 0x7E);
enum {REPLACEMENT_CHARACTER = 0xFFFD};
bool IsAnchorChar(int ch) { // test for ns-anchor-char
switch (ch) {
case ',': case '[': case ']': case '{': case '}': // c-flow-indicator
case ' ': case '\t': // s-white
case 0xFEFF: // c-byte-order-mark
case 0xA: case 0xD: // b-char
return false;
case 0x85:
return true;
}
if (ch < 0x20)
return false;
if (ch < 0x7E)
return true;
if (ch < 0xA0)
return false;
if (ch >= 0xD800 && ch <= 0xDFFF)
return false;
if ((ch & 0xFFFE) == 0xFFFE)
return false;
if ((ch >= 0xFDD0) && (ch <= 0xFDEF))
return false;
if (ch > 0x10FFFF)
return false;
return true;
}
bool IsValidPlainScalar(const std::string& str, bool inFlow) {
int Utf8BytesIndicated(char ch) {
int byteVal = static_cast<unsigned char>(ch);
switch (byteVal >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
return 1;
case 12: case 13:
return 2;
case 14:
return 3;
case 15:
return 4;
default:
return -1;
}
}
bool IsTrailingByte(char ch) {
return (ch & 0xC0) == 0x80;
}
bool GetNextCodePointAndAdvance(int& codePoint, std::string::const_iterator& first, std::string::const_iterator last) {
if (first == last)
return false;
int nBytes = Utf8BytesIndicated(*first);
if (nBytes < 1) {
// Bad lead byte
++first;
codePoint = REPLACEMENT_CHARACTER;
return true;
}
if (nBytes == 1) {
codePoint = *first++;
return true;
}
// Gather bits from trailing bytes
codePoint = static_cast<unsigned char>(*first) & ~(0xFF << (7 - nBytes));
++first;
--nBytes;
for (; nBytes > 0; ++first, --nBytes) {
if ((first == last) || !IsTrailingByte(*first)) {
codePoint = REPLACEMENT_CHARACTER;
break;
}
codePoint <<= 6;
codePoint |= *first & 0x3F;
}
// Check for illegal code points
if (codePoint > 0x10FFFF)
codePoint = REPLACEMENT_CHARACTER;
else if (codePoint >= 0xD800 && codePoint <= 0xDFFF)
codePoint = REPLACEMENT_CHARACTER;
else if ((codePoint & 0xFFFE) == 0xFFFE)
codePoint = REPLACEMENT_CHARACTER;
else if (codePoint >= 0xFDD0 && codePoint <= 0xFDEF)
codePoint = REPLACEMENT_CHARACTER;
return true;
}
void WriteCodePoint(ostream& out, int codePoint) {
if (codePoint < 0 || codePoint > 0x10FFFF) {
codePoint = REPLACEMENT_CHARACTER;
}
if (codePoint < 0x7F) {
out << static_cast<char>(codePoint);
} else if (codePoint < 0x7FF) {
out << static_cast<char>(0xC0 | (codePoint >> 6))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
} else if (codePoint < 0xFFFF) {
out << static_cast<char>(0xE0 | (codePoint >> 12))
<< static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
} else {
out << static_cast<char>(0xF0 | (codePoint >> 18))
<< static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F))
<< static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F))
<< static_cast<char>(0x80 | (codePoint & 0x3F));
}
}
bool IsValidPlainScalar(const std::string& str, bool inFlow, bool allowOnlyAscii) {
// first check the start
const RegEx& start = (inFlow ? Exp::PlainScalarInFlow : Exp::PlainScalar);
if(!start.Matches(str))
@@ -28,64 +140,109 @@ namespace YAML
// then check until something is disallowed
const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar)
|| (Exp::BlankOrBreak + Exp::Comment)
|| (!Exp::Printable)
|| Exp::NotPrintable
|| Exp::Utf8_ByteOrderMark
|| Exp::Break
|| Exp::Tab;
StringCharSource buffer(str.c_str(), str.size());
while(buffer) {
if(disallowed.Matches(buffer))
return false;
if(allowOnlyAscii && (0x7F < static_cast<unsigned char>(buffer[0])))
return false;
++buffer;
}
return true;
}
void WriteDoubleQuoteEscapeSequence(ostream& out, int codePoint) {
static const char hexDigits[] = "0123456789abcdef";
char escSeq[] = "\\U00000000";
int digits = 8;
if (codePoint < 0xFF) {
escSeq[1] = 'x';
digits = 2;
} else if (codePoint < 0xFFFF) {
escSeq[1] = 'u';
digits = 4;
}
// Write digits into the escape sequence
int i = 2;
for (; digits > 0; --digits, ++i) {
escSeq[i] = hexDigits[(codePoint >> (4 * (digits - 1))) & 0xF];
}
escSeq[i] = 0; // terminate with NUL character
out << escSeq;
}
bool WriteAliasName(ostream& out, const std::string& str) {
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (!IsAnchorChar(codePoint))
return false;
WriteCodePoint(out, codePoint);
}
return true;
}
}
bool WriteString(ostream& out, const std::string& str, bool inFlow)
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii)
{
if(IsValidPlainScalar(str, inFlow)) {
if(IsValidPlainScalar(str, inFlow, escapeNonAscii)) {
out << str;
return true;
} else
return WriteDoubleQuotedString(out, str);
return WriteDoubleQuotedString(out, str, escapeNonAscii);
}
bool WriteSingleQuotedString(ostream& out, const std::string& str)
{
out << "'";
for(std::size_t i=0;i<str.size();i++) {
char ch = str[i];
if(!IsPrintable(ch))
return false;
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\n')
return false; // We can't handle a new line and the attendant indentation yet
if(ch == '\'')
if (codePoint == '\'')
out << "''";
else
out << ch;
WriteCodePoint(out, codePoint);
}
out << "'";
return true;
}
bool WriteDoubleQuotedString(ostream& out, const std::string& str)
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii)
{
out << "\"";
for(std::size_t i=0;i<str.size();i++) {
char ch = str[i];
if(IsPrintable(ch)) {
if(ch == '\"')
out << "\\\"";
else if(ch == '\\')
out << "\\\\";
else
out << ch;
} else {
// TODO: for the common escaped characters, give their usual symbol
std::stringstream str;
str << "\\x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned int>(static_cast<unsigned char>(ch));
out << str.str();
}
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\"')
out << "\\\"";
else if (codePoint == '\\')
out << "\\\\";
else if (codePoint < 0x20 || (codePoint >= 0x80 && codePoint <= 0xA0)) // Control characters and non-breaking space
WriteDoubleQuoteEscapeSequence(out, codePoint);
else if (codePoint == 0xFEFF) // Byte order marks (ZWNS) should be escaped (YAML 1.2, sec. 5.2)
WriteDoubleQuoteEscapeSequence(out, codePoint);
else if (escapeNonAscii && codePoint > 0x7E)
WriteDoubleQuoteEscapeSequence(out, codePoint);
else
WriteCodePoint(out, codePoint);
}
out << "\"";
return true;
@@ -95,11 +252,15 @@ namespace YAML
{
out << "|\n";
out << IndentTo(indent);
for(std::size_t i=0;i<str.size();i++) {
if(str[i] == '\n')
out << "\n" << IndentTo(indent);
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if (codePoint == '\n')
out << "\n" << IndentTo(indent);
else
out << str[i];
WriteCodePoint(out, codePoint);
}
return true;
}
@@ -108,11 +269,15 @@ namespace YAML
{
unsigned curIndent = out.col();
out << "#" << Indentation(postCommentIndent);
for(std::size_t i=0;i<str.size();i++) {
if(str[i] == '\n')
int codePoint;
for(std::string::const_iterator i = str.begin();
GetNextCodePointAndAdvance(codePoint, i, str.end());
)
{
if(codePoint == '\n')
out << "\n" << IndentTo(curIndent) << "#" << Indentation(postCommentIndent);
else
out << str[i];
WriteCodePoint(out, codePoint);
}
return true;
}
@@ -120,25 +285,13 @@ namespace YAML
bool WriteAlias(ostream& out, const std::string& str)
{
out << "*";
for(std::size_t i=0;i<str.size();i++) {
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
return false;
out << str[i];
}
return true;
return WriteAliasName(out, str);
}
bool WriteAnchor(ostream& out, const std::string& str)
{
out << "&";
for(std::size_t i=0;i<str.size();i++) {
if(!IsPrintable(str[i]) || str[i] == ' ' || str[i] == '\t' || str[i] == '\n' || str[i] == '\r')
return false;
out << str[i];
}
return true;
return WriteAliasName(out, str);
}
}
}

View File

@@ -11,9 +11,9 @@ namespace YAML
{
namespace Utils
{
bool WriteString(ostream& out, const std::string& str, bool inFlow);
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii);
bool WriteSingleQuotedString(ostream& out, const std::string& str);
bool WriteDoubleQuotedString(ostream& out, const std::string& str);
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii);
bool WriteLiteralString(ostream& out, const std::string& str, int indent);
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent);
bool WriteAlias(ostream& out, const std::string& str);

View File

@@ -28,9 +28,9 @@ namespace YAML
return value;
}
std::string Str(char ch)
std::string Str(unsigned ch)
{
return std::string("") + ch;
return std::string(1, static_cast<char>(ch));
}
// Escape

View File

@@ -26,7 +26,12 @@ namespace YAML
const RegEx Alpha = RegEx('a', 'z') || RegEx('A', 'Z');
const RegEx AlphaNumeric = Alpha || Digit;
const RegEx Hex = Digit || RegEx('A', 'F') || RegEx('a', 'f');
const RegEx Printable = RegEx(0x20, 0x7E);
// Valid Unicode code points that are not part of c-printable (YAML 1.2, sec. 5.1)
const RegEx NotPrintable = RegEx(0) ||
RegEx("\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x7F", REGEX_OR) ||
RegEx(0x0E, 0x1F) ||
(RegEx('\xC2') + (RegEx('\x80', '\x84') || RegEx('\x86', '\x9F')));
const RegEx Utf8_ByteOrderMark = RegEx("\xEF\xBB\xBF");
// actual tags

View File

@@ -8,33 +8,38 @@
namespace YAML
{
Parser::Parser(std::istream& in): m_pScanner(0)
Parser::Parser()
{
}
Parser::Parser(std::istream& in)
{
Load(in);
}
Parser::~Parser()
{
delete m_pScanner;
}
Parser::operator bool() const
{
return !m_pScanner->empty();
return m_pScanner.get() && !m_pScanner->empty();
}
void Parser::Load(std::istream& in)
{
delete m_pScanner;
m_pScanner = new Scanner(in);
m_pScanner.reset(new Scanner(in));
m_state.Reset();
}
// GetNextDocument
// . Reads the next document in the queue (of tokens).
// . Throws a ParserException on error.
void Parser::GetNextDocument(Node& document)
bool Parser::GetNextDocument(Node& document)
{
if(!m_pScanner.get())
return false;
// clear node
document.Clear();
@@ -43,14 +48,14 @@ namespace YAML
// we better have some tokens in the queue
if(m_pScanner->empty())
return;
return false;
// first eat doc start (optional)
if(m_pScanner->peek().type == Token::DOC_START)
m_pScanner->pop();
// now parse our root node
document.Parse(m_pScanner, m_state);
document.Parse(m_pScanner.get(), m_state);
// and finally eat any doc ends we see
while(!m_pScanner->empty() && m_pScanner->peek().type == Token::DOC_END)
@@ -58,6 +63,8 @@ namespace YAML
// clear anchors from the scanner, which are no longer relevant
m_pScanner->ClearAnchors();
return true;
}
// ParseDirectives
@@ -126,6 +133,9 @@ namespace YAML
void Parser::PrintTokens(std::ostream& out)
{
if(!m_pScanner.get())
return;
while(1) {
if(m_pScanner->empty())
break;

View File

@@ -287,7 +287,7 @@ namespace YAML
params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
params.eatEnd = false;
params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
params.fold = FOLD_BLOCK;
params.fold = FOLD_FLOW;
params.eatLeadingWhitespace = true;
params.trimTrailingSpaces = true;
params.chomp = STRIP;

View File

@@ -11,9 +11,8 @@ int main(int argc, char **argv)
std::istream& input = (argc > 1 ? fin : std::cin);
try {
YAML::Parser parser(input);
while(parser) {
YAML::Node doc;
parser.GetNextDocument(doc);
YAML::Node doc;
while(parser.GetNextDocument(doc)) {
YAML::Emitter emitter;
emitter << doc;
std::cout << emitter.c_str() << "\n";

View File

@@ -1,5 +1,5 @@
file(GLOB yaml-reader_headers *.h)
file(GLOB yaml-reader_sources *.cpp)
file(GLOB yaml-reader_headers [a-z]*.h)
file(GLOB yaml-reader_sources [a-z]*.cpp)
add_executable(yaml-reader
${yaml-reader_sources}

View File

@@ -448,6 +448,26 @@ namespace Test
desiredOutput = "- ~\n-\n null value: ~\n ~: null key";
}
void EscapedUnicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::EscapeNonAscii << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\"$ \\xa2 \\u20ac \\U00024b62\"";
}
void Unicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
}
void DoubleQuotedUnicode(YAML::Emitter& out, std::string& desiredOutput)
{
out << YAML::DoubleQuoted << "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
desiredOutput = "\"\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2\"";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// incorrect emitting
@@ -609,6 +629,9 @@ namespace Test
RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed, total);
RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed, total);
RunEmitterTest(&Emitter::Null, "null", passed, total);
RunEmitterTest(&Emitter::EscapedUnicode, "escaped unicode", passed, total);
RunEmitterTest(&Emitter::Unicode, "unicode", passed, total);
RunEmitterTest(&Emitter::DoubleQuotedUnicode, "double quoted unicode", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed, total);
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed, total);

View File

@@ -17,6 +17,12 @@ namespace {
}
#define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false)
#define PARSE(doc, input) \
std::stringstream stream(input);\
YAML::Parser parser(stream);\
YAML::Node doc;\
parser.GetNextDocument(doc)
#define PARSE_NEXT(doc) parser.GetNextDocument(doc)
namespace Test {
namespace {
@@ -47,11 +53,8 @@ namespace Test {
"- Mark McGwire\n"
"- Sammy Sosa\n"
"- Ken Griffey";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc[0] == "Mark McGwire");
YAML_ASSERT(doc[1] == "Sammy Sosa");
@@ -65,11 +68,8 @@ namespace Test {
"hr: 65 # Home runs\n"
"avg: 0.278 # Batting average\n"
"rbi: 147 # Runs Batted In";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["hr"] == "65");
YAML_ASSERT(doc["avg"] == "0.278");
@@ -88,11 +88,8 @@ namespace Test {
"- New York Mets\n"
"- Chicago Cubs\n"
"- Atlanta Braves";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["american"].size() == 3);
YAML_ASSERT(doc["american"][0] == "Boston Red Sox");
@@ -117,11 +114,8 @@ namespace Test {
" name: Sammy Sosa\n"
" hr: 63\n"
" avg: 0.288";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc[0].size() == 3);
YAML_ASSERT(doc[0]["name"] == "Mark McGwire");
@@ -141,11 +135,8 @@ namespace Test {
"- [name , hr, avg ]\n"
"- [Mark McGwire, 65, 0.278]\n"
"- [Sammy Sosa , 63, 0.288]";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc[0].size() == 3);
YAML_ASSERT(doc[0][0] == "name");
@@ -171,11 +162,8 @@ namespace Test {
" hr: 63,\n"
" avg: 0.288\n"
" }";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["Mark McGwire"].size() == 2);
YAML_ASSERT(doc["Mark McGwire"]["hr"] == "65");
@@ -200,17 +188,14 @@ namespace Test {
"---\n"
"- Chicago Cubs\n"
"- St Louis Cardinals";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc[0] == "Mark McGwire");
YAML_ASSERT(doc[1] == "Sammy Sosa");
YAML_ASSERT(doc[2] == "Ken Griffey");
parser.GetNextDocument(doc);
PARSE_NEXT(doc);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc[0] == "Chicago Cubs");
YAML_ASSERT(doc[1] == "St Louis Cardinals");
@@ -231,17 +216,14 @@ namespace Test {
"player: Sammy Sosa\n"
"action: grand slam\n"
"...";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["time"] == "20:03:20");
YAML_ASSERT(doc["player"] == "Sammy Sosa");
YAML_ASSERT(doc["action"] == "strike (miss)");
parser.GetNextDocument(doc);
PARSE_NEXT(doc);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["time"] == "20:03:47");
YAML_ASSERT(doc["player"] == "Sammy Sosa");
@@ -261,11 +243,8 @@ namespace Test {
" # 1998 rbi ranking\n"
" - Sammy Sosa\n"
" - Ken Griffey";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["hr"].size() == 2);
YAML_ASSERT(doc["hr"][0] == "Mark McGwire");
@@ -288,11 +267,8 @@ namespace Test {
"rbi:\n"
" - *SS # Subsequent occurrence\n"
" - Ken Griffey";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["hr"].size() == 2);
YAML_ASSERT(doc["hr"][0] == "Mark McGwire");
@@ -331,11 +307,8 @@ namespace Test {
" Atlanta Braves ]\n"
": [ 2001-07-02, 2001-08-12,\n"
" 2001-08-14 ]";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")].size() == 1);
YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")][0] == "2001-07-23");
@@ -358,11 +331,8 @@ namespace Test {
" quantity: 4\n"
"- item : Big Shoes\n"
" quantity: 1";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc[0].size() == 2);
YAML_ASSERT(doc[0]["item"] == "Super Hoop");
@@ -384,11 +354,8 @@ namespace Test {
"--- |\n"
" \\//||\\/||\n"
" // || ||__";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc ==
"\\//||\\/||\n"
"// || ||__");
@@ -403,11 +370,8 @@ namespace Test {
" Mark McGwire's\n"
" year was crippled\n"
" by a knee injury.";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc == "Mark McGwire's year was crippled by a knee injury.");
return true;
}
@@ -424,11 +388,8 @@ namespace Test {
" 0.288 Batting Average\n"
" \n"
" What a year!";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc ==
"Sammy Sosa completed another fine season with great stats.\n\n"
" 63 Home Runs\n"
@@ -448,11 +409,8 @@ namespace Test {
"stats: |\n"
" 65 Home Runs\n"
" 0.278 Batting Average\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["name"] == "Mark McGwire");
YAML_ASSERT(doc["accomplishment"] == "Mark set a major league home run record in 1998.\n");
@@ -471,13 +429,10 @@ namespace Test {
"single: '\"Howdy!\" he cried.'\n"
"quoted: ' # Not a ''comment''.'\n"
"tie-fighter: '|\\-*-/|'";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 6);
YAML_ASSERT(doc["unicode"] == "Sosa did fine.\u263A");
YAML_ASSERT(doc["unicode"] == "Sosa did fine.\xe2\x98\xba");
YAML_ASSERT(doc["control"] == "\b1998\t1999\t2000\n");
YAML_ASSERT(doc["hex esc"] == "\x0d\x0a is \r\n");
YAML_ASSERT(doc["single"] == "\"Howdy!\" he cried.");
@@ -496,11 +451,8 @@ namespace Test {
"\n"
"quoted: \"So does this\n"
" quoted scalar.\\n\"";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["plain"] == "This unquoted scalar spans many lines.");
YAML_ASSERT(doc["quoted"] == "So does this quoted scalar.\n");
@@ -542,11 +494,8 @@ namespace Test {
" Late afternoon is best.\n"
" Backup contact is Nancy\n"
" Billsmer @ 338-4338.";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 8);
YAML_ASSERT(doc["invoice"] == 34843);
YAML_ASSERT(doc["date"] == "2001-01-23");
@@ -613,23 +562,20 @@ namespace Test {
" line: 58\n"
" code: |-\n"
" foo = bar";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["Time"] == "2001-11-23 15:01:42 -5");
YAML_ASSERT(doc["User"] == "ed");
YAML_ASSERT(doc["Warning"] == "This is an error message for the log file");
parser.GetNextDocument(doc);
PARSE_NEXT(doc);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["Time"] == "2001-11-23 15:02:31 -5");
YAML_ASSERT(doc["User"] == "ed");
YAML_ASSERT(doc["Warning"] == "A slightly different error message.");
parser.GetNextDocument(doc);
PARSE_NEXT(doc);
YAML_ASSERT(doc.size() == 4);
YAML_ASSERT(doc["Date"] == "2001-11-23 15:03:17 -5");
YAML_ASSERT(doc["User"] == "ed");
@@ -659,11 +605,8 @@ namespace Test {
" ? sky\n"
" : blue\n"
" sea : green";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["sequence"].size() == 2);
YAML_ASSERT(doc["sequence"][0] == "one");
@@ -680,11 +623,8 @@ namespace Test {
std::string input =
"sequence: [ one, two, ]\n"
"mapping: { sky: blue, sea: green }";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["sequence"].size() == 2);
YAML_ASSERT(doc["sequence"][0] == "one");
@@ -703,11 +643,8 @@ namespace Test {
std::string input =
"anchored: !local &anchor value\n"
"alias: *anchor";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["anchored"] == "value"); // TODO: assert tag
YAML_ASSERT(doc["alias"] == "value");
@@ -724,11 +661,8 @@ namespace Test {
"folded: >\n"
" some\n"
" text\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["literal"] == "some\ntext\n");
YAML_ASSERT(doc["folded"] == "some text\n");
@@ -741,11 +675,8 @@ namespace Test {
std::string input =
"single: 'text'\n"
"double: \"text\"";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["single"] == "text");
YAML_ASSERT(doc["double"] == "text");
@@ -762,11 +693,8 @@ namespace Test {
"|\n"
" Line break (no glyph)\n"
" Line break (glyphed)\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc == "Line break (no glyph)\nLine break (glyphed)\n");
return true;
}
@@ -781,11 +709,8 @@ namespace Test {
" void main() {\n"
" \tprintf(\"Hello, world!\\n\");\n"
" }";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["quoted"] == "Quoted\t");
YAML_ASSERT(doc["block"] ==
@@ -804,12 +729,9 @@ namespace Test {
"\\n \\r \\t \\v \\0 \\\n"
"\\ \\_ \\N \\L \\P \\\n"
"\\x41 \\u0041 \\U00000041\"";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
YAML_ASSERT(doc == "Fun with \x5C \x22 \x07 \x08 \x1B \x0C \x0A \x0D \x09 \x0B " + std::string("\x00", 1) + " \x20 \xA0 \x85 \u2028 \u2029 A A A");
PARSE(doc, input);
YAML_ASSERT(doc == "Fun with \x5C \x22 \x07 \x08 \x1B \x0C \x0A \x0D \x09 \x0B " + std::string("\x00", 1) + " \x20 \xA0 \x85 \xe2\x80\xa8 \xe2\x80\xa9 A A A");
return true;
}
@@ -850,11 +772,8 @@ namespace Test {
" Also by two, # are neither\n"
" \tStill by two # content nor\n"
" ] # indentation.";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["Not indented"].size() == 2);
YAML_ASSERT(doc["Not indented"]["By one space"] == "By four\n spaces\n");
@@ -873,11 +792,8 @@ namespace Test {
": -\tb\n"
" - -\tc\n"
" - d";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["a"].size() == 2);
YAML_ASSERT(doc["a"][0] == "b");
@@ -894,11 +810,8 @@ namespace Test {
"- foo:\t bar\n"
"- - baz\n"
" -\tbaz";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc[0].size() == 1);
YAML_ASSERT(doc[0]["foo"] == "bar");
@@ -919,11 +832,8 @@ namespace Test {
"block: |\n"
" text\n"
" \tlines\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 3);
YAML_ASSERT(doc["plain"] == "text lines");
YAML_ASSERT(doc["quoted"] == "text lines");
@@ -942,11 +852,8 @@ namespace Test {
"Chomping: |\n"
" Clipped empty lines\n"
" ";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["Folding"] == "Empty line\nas a line feed");
YAML_ASSERT(doc["Chomping"] == "Clipped empty lines\n");
@@ -964,11 +871,8 @@ namespace Test {
"\n"
" as\n"
" space";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc == "trimmed\n\n\nas space");
return true;
}
@@ -983,11 +887,8 @@ namespace Test {
" \t bar\n"
"\n"
" baz\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc == "foo \n\n\t bar\n\nbaz\n");
return true;
}
@@ -1003,11 +904,8 @@ namespace Test {
"\n"
" baz\n"
"\"";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc == " foo\nbar\nbaz ");
return true;
}
@@ -1018,11 +916,8 @@ namespace Test {
std::string input =
"key: # Comment\n"
" value";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["key"] == "value");
return true;
@@ -1050,11 +945,8 @@ namespace Test {
" # lines\n"
" value\n"
"\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["key"] == "value");
return true;
@@ -1089,11 +981,8 @@ namespace Test {
" 65\n"
" avg: # Average\n"
" 0.278";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
StringMap key;
key._["first"] = "Sammy";
key._["last"] = "Sosa";
@@ -1113,11 +1002,8 @@ namespace Test {
std::string input =
"First occurrence: &anchor Value\n"
"Second occurrence: *anchor";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["First occurrence"] == "Value");
YAML_ASSERT(doc["Second occurrence"] == "Value");
@@ -1132,11 +1018,8 @@ namespace Test {
"Second occurrence: *anchor\n"
"Override anchor: &anchor Bar\n"
"Reuse anchor: *anchor";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 4);
YAML_ASSERT(doc["First occurrence"] == "Foo");
YAML_ASSERT(doc["Second occurrence"] == "Foo");
@@ -1153,11 +1036,8 @@ namespace Test {
" foo : !!str,\n"
" !!str : bar,\n"
"}";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(doc["foo"] == ""); // TODO: check tag
YAML_ASSERT(doc[""] == "bar");
@@ -1172,11 +1052,8 @@ namespace Test {
" ? foo :,\n"
" : bar,\n"
"}\n";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 2);
YAML_ASSERT(IsNull(doc["foo"]));
YAML_ASSERT(doc[YAML::Null] == "bar");
@@ -1190,17 +1067,146 @@ namespace Test {
"\"implicit block key\" : [\n"
" \"implicit flow key\" : value,\n"
" ]";
std::stringstream stream(input);
YAML::Parser parser(stream);
YAML::Node doc;
parser.GetNextDocument(doc);
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["implicit block key"].size() == 1);
YAML_ASSERT(doc["implicit block key"][0].size() == 1);
YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"] == "value");
return true;
}
// 7.5
TEST DoubleQuotedLineBreaks()
{
std::string input =
"\"folded \n"
"to a space,\t\n"
" \n"
"to a line feed, or \t\\\n"
" \\ \tnon-content\"";
PARSE(doc, input);
YAML_ASSERT(doc == "folded to a space,\nto a line feed, or \t \tnon-content");
return true;
}
// 7.6
TEST DoubleQuotedLines()
{
std::string input =
"\" 1st non-empty\n"
"\n"
" 2nd non-empty \n"
"\t3rd non-empty \"";
PARSE(doc, input);
YAML_ASSERT(doc == " 1st non-empty\n2nd non-empty 3rd non-empty ");
return true;
}
// 7.7
TEST SingleQuotedCharacters()
{
std::string input = " 'here''s to \"quotes\"'";
PARSE(doc, input);
YAML_ASSERT(doc == "here's to \"quotes\"");
return true;
}
// 7.8
TEST SingleQuotedImplicitKeys()
{
std::string input =
"'implicit block key' : [\n"
" 'implicit flow key' : value,\n"
" ]";
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["implicit block key"].size() == 1);
YAML_ASSERT(doc["implicit block key"][0].size() == 1);
YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"] == "value");
return true;
}
// 7.9
TEST SingleQuotedLines()
{
std::string input =
"' 1st non-empty\n"
"\n"
" 2nd non-empty \n"
"\t3rd non-empty '";
PARSE(doc, input);
YAML_ASSERT(doc == " 1st non-empty\n2nd non-empty 3rd non-empty ");
return true;
}
// 7.10
TEST PlainCharacters()
{
std::string input =
"# Outside flow collection:\n"
"- ::vector\n"
"- \": - ()\"\n"
"- Up, up, and away!\n"
"- -123\n"
"- http://example.com/foo#bar\n"
"# Inside flow collection:\n"
"- [ ::vector,\n"
" \": - ()\",\n"
" \"Up, up, and away!\",\n"
" -123,\n"
" http://example.com/foo#bar ]";
PARSE(doc, input);
YAML_ASSERT(doc.size() == 6);
YAML_ASSERT(doc[0] == "::vector");
YAML_ASSERT(doc[1] == ": - ()");
YAML_ASSERT(doc[2] == "Up, up, and away!");
YAML_ASSERT(doc[3] == -123);
YAML_ASSERT(doc[4] == "http://example.com/foo#bar");
YAML_ASSERT(doc[5].size() == 5);
YAML_ASSERT(doc[5][0] == "::vector");
YAML_ASSERT(doc[5][1] == ": - ()");
YAML_ASSERT(doc[5][2] == "Up, up, and away!");
YAML_ASSERT(doc[5][3] == -123);
YAML_ASSERT(doc[5][4] == "http://example.com/foo#bar");
return true;
}
// 7.11
TEST PlainImplicitKeys()
{
std::string input =
"implicit block key : [\n"
" implicit flow key : value,\n"
" ]";
PARSE(doc, input);
YAML_ASSERT(doc.size() == 1);
YAML_ASSERT(doc["implicit block key"].size() == 1);
YAML_ASSERT(doc["implicit block key"][0].size() == 1);
YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"] == "value");
return true;
}
// 7.12
TEST PlainLines()
{
std::string input =
"1st non-empty\n"
"\n"
" 2nd non-empty \n"
"\t3rd non-empty";
PARSE(doc, input);
YAML_ASSERT(doc == "1st non-empty\n2nd non-empty 3rd non-empty");
return true;
}
}
bool RunSpecTests()
@@ -1257,6 +1263,14 @@ namespace Test {
RunSpecTest(&Spec::EmptyNodes, "7.2", "Empty Nodes", passed, total);
RunSpecTest(&Spec::CompletelyEmptyNodes, "7.3", "Completely Empty Nodes", passed, total);
RunSpecTest(&Spec::DoubleQuotedImplicitKeys, "7.4", "Double Quoted Implicit Keys", passed, total);
RunSpecTest(&Spec::DoubleQuotedLineBreaks, "7.5", "Double Quoted Line Breaks", passed, total);
RunSpecTest(&Spec::DoubleQuotedLines, "7.6", "Double Quoted Lines", passed, total);
RunSpecTest(&Spec::SingleQuotedCharacters, "7.7", "Single Quoted Characters", passed, total);
RunSpecTest(&Spec::SingleQuotedImplicitKeys, "7.8", "Single Quoted Implicit Keys", passed, total);
RunSpecTest(&Spec::SingleQuotedLines, "7.9", "Single Quoted Lines", passed, total);
RunSpecTest(&Spec::PlainCharacters, "7.10", "Plain Characters", passed, total);
RunSpecTest(&Spec::PlainImplicitKeys, "7.11", "Plain Implicit Keys", passed, total);
RunSpecTest(&Spec::PlainLines, "7.12", "Plain Lines", passed, total);
std::cout << "Spec tests: " << passed << "/" << total << " passed\n";
return passed == total;