mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Compare commits
33 Commits
release-0.
...
release-0.
Author | SHA1 | Date | |
---|---|---|---|
![]() |
22410f46f5 | ||
![]() |
9559a661aa | ||
![]() |
beb524489c | ||
![]() |
4ffb93c12b | ||
![]() |
ae06a40fe6 | ||
![]() |
315b00065b | ||
![]() |
6f02f7556e | ||
![]() |
fa0af88dfe | ||
![]() |
bce845bb1f | ||
![]() |
ed570b9f7c | ||
![]() |
59b0e986bf | ||
![]() |
cffb98d15b | ||
![]() |
3e1ba0f3b4 | ||
![]() |
d0b5bf4b7b | ||
![]() |
7db39e66b8 | ||
![]() |
94eb7f1dbd | ||
![]() |
5733b77b84 | ||
![]() |
98bebfb628 | ||
![]() |
7c4cc9bf5f | ||
![]() |
fa885d1813 | ||
![]() |
da4614eb8b | ||
![]() |
4dcd222d1f | ||
![]() |
7bdd31b34b | ||
![]() |
a4b8521efe | ||
![]() |
7037562998 | ||
![]() |
f3ff6ffc55 | ||
![]() |
e3ff87ecde | ||
![]() |
45ac700fff | ||
![]() |
2aab5acab4 | ||
![]() |
e9d760eea9 | ||
![]() |
d485d0a834 | ||
![]() |
973ac4b3bd | ||
![]() |
e91a152e06 |
@@ -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 "0")
|
||||
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)
|
||||
|
@@ -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);
|
||||
|
@@ -12,6 +12,10 @@ namespace YAML
|
||||
// general manipulators
|
||||
Auto,
|
||||
|
||||
// output character set
|
||||
EmitNonAscii,
|
||||
EscapeNonAscii,
|
||||
|
||||
// string manipulators
|
||||
// Auto, // duplicate
|
||||
SingleQuoted,
|
||||
|
@@ -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) {
|
||||
|
@@ -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;
|
||||
|
||||
@@ -38,12 +49,12 @@ namespace YAML
|
||||
struct flag { char c[2]; };
|
||||
flag Convert(...);
|
||||
|
||||
char (& operator,(flag, flag) )[1];
|
||||
int operator,(flag, flag);
|
||||
|
||||
template<typename T>
|
||||
void operator,(flag, T const&);
|
||||
|
||||
char (& operator,(char(&)[1], flag) )[1];
|
||||
char operator,(int, flag);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -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>
|
||||
|
@@ -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;
|
||||
};
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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) {
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
bool IsValidPlainScalar(const std::string& str, bool inFlow) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool WriteString(ostream& out, const std::string& str, bool inFlow)
|
||||
// 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(IsValidPlainScalar(str, inFlow)) {
|
||||
if (!IsAnchorChar(codePoint))
|
||||
return false;
|
||||
|
||||
WriteCodePoint(out, codePoint);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool WriteString(ostream& out, const std::string& str, bool inFlow, bool escapeNonAscii)
|
||||
{
|
||||
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 == '\"')
|
||||
int codePoint;
|
||||
for(std::string::const_iterator i = str.begin();
|
||||
GetNextCodePointAndAdvance(codePoint, i, str.end());
|
||||
)
|
||||
{
|
||||
if (codePoint == '\"')
|
||||
out << "\\\"";
|
||||
else if(ch == '\\')
|
||||
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
|
||||
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 <int>(ch);
|
||||
out << str.str();
|
||||
}
|
||||
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')
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
|
11
src/exp.cpp
11
src/exp.cpp
@@ -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
|
||||
@@ -83,7 +83,7 @@ namespace YAML
|
||||
|
||||
// now do the slash (we're not gonna check if it's a slash - you better pass one!)
|
||||
switch(ch) {
|
||||
case '0': return "\0";
|
||||
case '0': return std::string(1, '\x00');
|
||||
case 'a': return "\x07";
|
||||
case 'b': return "\x08";
|
||||
case 't':
|
||||
@@ -97,8 +97,9 @@ namespace YAML
|
||||
case '\"': return "\"";
|
||||
case '\'': return "\'";
|
||||
case '\\': return "\\";
|
||||
case 'N': return "\xC2\x85"; // NEL (#x85)
|
||||
case '_': return "\xC2\xA0"; // #xA0
|
||||
case '/': return "/";
|
||||
case 'N': return "\x85";
|
||||
case '_': return "\xA0";
|
||||
case 'L': return "\xE2\x80\xA8"; // LS (#x2028)
|
||||
case 'P': return "\xE2\x80\xA9"; // PS (#x2029)
|
||||
case 'x': return Escape(in, 2);
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -171,8 +171,11 @@ namespace YAML
|
||||
{
|
||||
while(1) {
|
||||
// first eat whitespace
|
||||
while(INPUT && IsWhitespaceToBeEaten(INPUT.peek()))
|
||||
while(INPUT && IsWhitespaceToBeEaten(INPUT.peek())) {
|
||||
if(InBlockContext() && Exp::Tab.Matches(INPUT))
|
||||
m_simpleKeyAllowed = false;
|
||||
INPUT.eat(1);
|
||||
}
|
||||
|
||||
// then eat a comment
|
||||
if(Exp::Comment.Matches(INPUT)) {
|
||||
@@ -202,18 +205,19 @@ namespace YAML
|
||||
// Misc. helpers
|
||||
|
||||
// IsWhitespaceToBeEaten
|
||||
// . We can eat whitespace if:
|
||||
// 1. It's a space
|
||||
// 2. It's a tab, and we're either:
|
||||
// a. In the flow context
|
||||
// b. In the block context but not where a simple key could be allowed
|
||||
// (i.e., not at the beginning of a line, or following '-', '?', or ':')
|
||||
// . We can eat whitespace if it's a space or tab
|
||||
// . Note: originally tabs in block context couldn't be eaten
|
||||
// "where a simple key could be allowed
|
||||
// (i.e., not at the beginning of a line, or following '-', '?', or ':')"
|
||||
// I think this is wrong, since tabs can be non-content whitespace; it's just
|
||||
// that they can't contribute to indentation, so once you've seen a tab in a
|
||||
// line, you can't start a simple key
|
||||
bool Scanner::IsWhitespaceToBeEaten(char ch)
|
||||
{
|
||||
if(ch == ' ')
|
||||
return true;
|
||||
|
||||
if(ch == '\t' && (InFlowContext() || !m_simpleKeyAllowed))
|
||||
if(ch == '\t')
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
@@ -19,14 +19,19 @@ namespace YAML
|
||||
// and different places in the above flow.
|
||||
std::string ScanScalar(Stream& INPUT, ScanScalarParams& params)
|
||||
{
|
||||
bool foundNonEmptyLine = false, pastOpeningBreak = false;
|
||||
bool foundNonEmptyLine = false;
|
||||
bool pastOpeningBreak = (params.fold == FOLD_FLOW);
|
||||
bool emptyLine = false, moreIndented = false;
|
||||
int foldedNewlineCount = 0;
|
||||
bool foldedNewlineStartedMoreIndented = false;
|
||||
std::string scalar;
|
||||
params.leadingSpaces = false;
|
||||
|
||||
while(INPUT) {
|
||||
// ********************************
|
||||
// Phase #1: scan until line ending
|
||||
|
||||
std::size_t lastNonWhitespaceChar = scalar.size();
|
||||
while(!params.end.Matches(INPUT) && !Exp::Break.Matches(INPUT)) {
|
||||
if(!INPUT)
|
||||
break;
|
||||
@@ -46,17 +51,22 @@ namespace YAML
|
||||
if(params.escape == '\\' && Exp::EscBreak.Matches(INPUT)) {
|
||||
int n = Exp::EscBreak.Match(INPUT);
|
||||
INPUT.eat(n);
|
||||
lastNonWhitespaceChar = scalar.size();
|
||||
continue;
|
||||
}
|
||||
|
||||
// escape this?
|
||||
if(INPUT.peek() == params.escape) {
|
||||
scalar += Exp::Escape(INPUT);
|
||||
lastNonWhitespaceChar = scalar.size();
|
||||
continue;
|
||||
}
|
||||
|
||||
// otherwise, just add the damn character
|
||||
scalar += INPUT.get();
|
||||
char ch = INPUT.get();
|
||||
scalar += ch;
|
||||
if(ch != ' ' && ch != '\t')
|
||||
lastNonWhitespaceChar = scalar.size();
|
||||
}
|
||||
|
||||
// eof? if we're looking to eat something, then we throw
|
||||
@@ -78,6 +88,10 @@ namespace YAML
|
||||
break;
|
||||
}
|
||||
|
||||
// do we remove trailing whitespace?
|
||||
if(params.fold == FOLD_FLOW)
|
||||
scalar.erase(lastNonWhitespaceChar);
|
||||
|
||||
// ********************************
|
||||
// Phase #2: eat line ending
|
||||
n = Exp::Break.Match(INPUT);
|
||||
@@ -108,19 +122,38 @@ namespace YAML
|
||||
|
||||
// was this an empty line?
|
||||
bool nextEmptyLine = Exp::Break.Matches(INPUT);
|
||||
bool nextMoreIndented = (INPUT.peek() == ' ');
|
||||
bool nextMoreIndented = Exp::Blank.Matches(INPUT);
|
||||
if(params.fold == FOLD_BLOCK && foldedNewlineCount == 0 && nextEmptyLine)
|
||||
foldedNewlineStartedMoreIndented = moreIndented;
|
||||
|
||||
// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
|
||||
bool useNewLine = pastOpeningBreak;
|
||||
// and for folded scalars, we don't fold the very last newline to a space
|
||||
if(params.fold && !emptyLine && INPUT.column() < params.indent)
|
||||
useNewLine = false;
|
||||
|
||||
if(useNewLine) {
|
||||
if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
|
||||
if(pastOpeningBreak) {
|
||||
switch(params.fold) {
|
||||
case DONT_FOLD:
|
||||
scalar += "\n";
|
||||
break;
|
||||
case FOLD_BLOCK:
|
||||
if(!emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented && INPUT.column() >= params.indent)
|
||||
scalar += " ";
|
||||
else if(nextEmptyLine)
|
||||
foldedNewlineCount++;
|
||||
else
|
||||
scalar += "\n";
|
||||
|
||||
if(!nextEmptyLine && foldedNewlineCount > 0) {
|
||||
scalar += std::string(foldedNewlineCount - 1, '\n');
|
||||
if(foldedNewlineStartedMoreIndented || nextMoreIndented)
|
||||
scalar += "\n";
|
||||
foldedNewlineCount = 0;
|
||||
}
|
||||
break;
|
||||
case FOLD_FLOW:
|
||||
if(nextEmptyLine)
|
||||
scalar += "\n";
|
||||
else if(!emptyLine && !nextEmptyLine)
|
||||
scalar += " ";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
emptyLine = nextEmptyLine;
|
||||
@@ -141,11 +174,11 @@ namespace YAML
|
||||
scalar.erase(pos + 1);
|
||||
}
|
||||
|
||||
if(params.chomp <= 0) {
|
||||
if(params.chomp == STRIP || params.chomp == CLIP) {
|
||||
std::size_t pos = scalar.find_last_not_of('\n');
|
||||
if(params.chomp == 0 && pos + 1 < scalar.size())
|
||||
if(params.chomp == CLIP && pos + 1 < scalar.size())
|
||||
scalar.erase(pos + 2);
|
||||
else if(params.chomp == -1 && pos < scalar.size())
|
||||
else if(params.chomp == STRIP && pos < scalar.size())
|
||||
scalar.erase(pos + 1);
|
||||
}
|
||||
|
||||
|
@@ -12,9 +12,10 @@ namespace YAML
|
||||
{
|
||||
enum CHOMP { STRIP = -1, CLIP, KEEP };
|
||||
enum ACTION { NONE, BREAK, THROW };
|
||||
enum FOLD { DONT_FOLD, FOLD_BLOCK, FOLD_FLOW };
|
||||
|
||||
struct ScanScalarParams {
|
||||
ScanScalarParams(): eatEnd(false), indent(0), detectIndent(false), eatLeadingWhitespace(0), escape(0), fold(false),
|
||||
ScanScalarParams(): eatEnd(false), indent(0), detectIndent(false), eatLeadingWhitespace(0), escape(0), fold(DONT_FOLD),
|
||||
trimTrailingSpaces(0), chomp(CLIP), onDocIndicator(NONE), onTabInIndentation(NONE), leadingSpaces(false) {}
|
||||
|
||||
// input:
|
||||
@@ -24,7 +25,7 @@ namespace YAML
|
||||
bool detectIndent; // should we try to autodetect the indent?
|
||||
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)
|
||||
bool fold; // do we fold line ends?
|
||||
FOLD fold; // how do we fold line ends?
|
||||
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)
|
||||
// Note: strip means kill all, clip means keep at most one, keep means keep all
|
||||
|
@@ -287,10 +287,10 @@ namespace YAML
|
||||
params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
|
||||
params.eatEnd = false;
|
||||
params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
|
||||
params.fold = true;
|
||||
params.fold = FOLD_FLOW;
|
||||
params.eatLeadingWhitespace = true;
|
||||
params.trimTrailingSpaces = true;
|
||||
params.chomp = CLIP;
|
||||
params.chomp = STRIP;
|
||||
params.onDocIndicator = BREAK;
|
||||
params.onTabInIndentation = THROW;
|
||||
|
||||
@@ -327,7 +327,7 @@ namespace YAML
|
||||
params.eatEnd = true;
|
||||
params.escape = (single ? '\'' : '\\');
|
||||
params.indent = 0;
|
||||
params.fold = true;
|
||||
params.fold = FOLD_FLOW;
|
||||
params.eatLeadingWhitespace = true;
|
||||
params.trimTrailingSpaces = false;
|
||||
params.chomp = CLIP;
|
||||
@@ -365,9 +365,10 @@ namespace YAML
|
||||
// eat block indicator ('|' or '>')
|
||||
Mark mark = INPUT.mark();
|
||||
char indicator = INPUT.get();
|
||||
params.fold = (indicator == Keys::FoldedScalar);
|
||||
params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD);
|
||||
|
||||
// eat chomping/indentation indicators
|
||||
params.chomp = CLIP;
|
||||
int n = Exp::Chomp.Match(INPUT);
|
||||
for(int i=0;i<n;i++) {
|
||||
char ch = INPUT.get();
|
||||
|
@@ -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);
|
||||
while(parser.GetNextDocument(doc)) {
|
||||
YAML::Emitter emitter;
|
||||
emitter << doc;
|
||||
std::cout << emitter.c_str() << "\n";
|
||||
|
@@ -179,6 +179,10 @@
|
||||
RelativePath=".\yaml-reader\parsertests.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\spectests.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\tests.cpp"
|
||||
>
|
||||
@@ -189,6 +193,18 @@
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\emittertests.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\parsertests.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\spectests.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\tests.h"
|
||||
>
|
||||
|
@@ -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}
|
||||
|
@@ -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
|
||||
|
||||
@@ -534,4 +554,97 @@ namespace Test
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
void RunEmitterTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, int& passed, int& total) {
|
||||
YAML::Emitter out;
|
||||
std::string desiredOutput;
|
||||
test(out, desiredOutput);
|
||||
std::string output = out.c_str();
|
||||
|
||||
if(output == desiredOutput) {
|
||||
passed++;
|
||||
} else {
|
||||
std::cout << "Emitter test failed: " << name << "\n";
|
||||
std::cout << "Output:\n";
|
||||
std::cout << output << "<<<\n";
|
||||
std::cout << "Desired output:\n";
|
||||
std::cout << desiredOutput << "<<<\n";
|
||||
}
|
||||
total++;
|
||||
}
|
||||
|
||||
void RunEmitterErrorTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, int& passed, int& total) {
|
||||
YAML::Emitter out;
|
||||
std::string desiredError;
|
||||
test(out, desiredError);
|
||||
std::string lastError = out.GetLastError();
|
||||
if(!out.good() && lastError == desiredError) {
|
||||
passed++;
|
||||
} else {
|
||||
std::cout << "Emitter test failed: " << name << "\n";
|
||||
if(out.good())
|
||||
std::cout << "No error detected\n";
|
||||
else
|
||||
std::cout << "Detected error: " << lastError << "\n";
|
||||
std::cout << "Expected error: " << desiredError << "\n";
|
||||
}
|
||||
total++;
|
||||
}
|
||||
}
|
||||
|
||||
bool RunEmitterTests()
|
||||
{
|
||||
int passed = 0;
|
||||
int total = 0;
|
||||
RunEmitterTest(&Emitter::SimpleScalar, "simple scalar", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleSeq, "simple seq", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleFlowSeq, "simple flow seq", passed, total);
|
||||
RunEmitterTest(&Emitter::EmptyFlowSeq, "empty flow seq", passed, total);
|
||||
RunEmitterTest(&Emitter::NestedBlockSeq, "nested block seq", passed, total);
|
||||
RunEmitterTest(&Emitter::NestedFlowSeq, "nested flow seq", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleMap, "simple map", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleFlowMap, "simple flow map", passed, total);
|
||||
RunEmitterTest(&Emitter::MapAndList, "map and list", passed, total);
|
||||
RunEmitterTest(&Emitter::ListAndMap, "list and map", passed, total);
|
||||
RunEmitterTest(&Emitter::NestedBlockMap, "nested block map", passed, total);
|
||||
RunEmitterTest(&Emitter::NestedFlowMap, "nested flow map", passed, total);
|
||||
RunEmitterTest(&Emitter::MapListMix, "map list mix", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleLongKey, "simple long key", passed, total);
|
||||
RunEmitterTest(&Emitter::SingleLongKey, "single long key", passed, total);
|
||||
RunEmitterTest(&Emitter::ComplexLongKey, "complex long key", passed, total);
|
||||
RunEmitterTest(&Emitter::AutoLongKey, "auto long key", passed, total);
|
||||
RunEmitterTest(&Emitter::ScalarFormat, "scalar format", passed, total);
|
||||
RunEmitterTest(&Emitter::AutoLongKeyScalar, "auto long key scalar", passed, total);
|
||||
RunEmitterTest(&Emitter::LongKeyFlowMap, "long key flow map", passed, total);
|
||||
RunEmitterTest(&Emitter::BlockMapAsKey, "block map as key", passed, total);
|
||||
RunEmitterTest(&Emitter::AliasAndAnchor, "alias and anchor", passed, total);
|
||||
RunEmitterTest(&Emitter::AliasAndAnchorWithNull, "alias and anchor with null", passed, total);
|
||||
RunEmitterTest(&Emitter::ComplexDoc, "complex doc", passed, total);
|
||||
RunEmitterTest(&Emitter::STLContainers, "STL containers", passed, total);
|
||||
RunEmitterTest(&Emitter::SimpleComment, "simple comment", passed, total);
|
||||
RunEmitterTest(&Emitter::MultiLineComment, "multi-line comment", passed, total);
|
||||
RunEmitterTest(&Emitter::ComplexComments, "complex comments", passed, total);
|
||||
RunEmitterTest(&Emitter::Indentation, "indentation", passed, total);
|
||||
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);
|
||||
RunEmitterErrorTest(&Emitter::BadSingleQuoted, "bad single quoted string", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::InvalidAnchor, "invalid anchor", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::InvalidAlias, "invalid alias", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::MissingKey, "missing key", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::MissingValue, "missing value", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::UnexpectedKey, "unexpected key", passed, total);
|
||||
RunEmitterErrorTest(&Emitter::UnexpectedValue, "unexpected value", passed, total);
|
||||
|
||||
std::cout << "Emitter tests: " << passed << "/" << total << " passed\n";
|
||||
return passed == total;
|
||||
}
|
||||
}
|
||||
|
||||
|
11
yaml-reader/emittertests.h
Normal file
11
yaml-reader/emittertests.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
namespace Test {
|
||||
bool RunEmitterTests();
|
||||
}
|
||||
|
||||
#endif // EMITTERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -611,4 +611,278 @@ namespace Test
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, int& passed, int& total) {
|
||||
std::string error;
|
||||
std::string inputScalar, desiredOutput;
|
||||
std::string output;
|
||||
bool ok = true;
|
||||
try {
|
||||
test(inputScalar, desiredOutput);
|
||||
std::stringstream stream(inputScalar);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
doc >> output;
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok && output == desiredOutput) {
|
||||
passed++;
|
||||
} else {
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
else {
|
||||
std::cout << "Output:\n" << output << "<<<\n";
|
||||
std::cout << "Desired output:\n" << desiredOutput << "<<<\n";
|
||||
}
|
||||
}
|
||||
total++;
|
||||
}
|
||||
|
||||
void RunParserTest(bool (*test)(), const std::string& name, int& passed, int& total) {
|
||||
std::string error;
|
||||
bool ok = true;
|
||||
try {
|
||||
ok = test();
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok) {
|
||||
passed++;
|
||||
} else {
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
}
|
||||
total++;
|
||||
}
|
||||
|
||||
typedef void (*EncodingFn)(std::ostream&, int);
|
||||
|
||||
inline char Byte(int ch)
|
||||
{
|
||||
return static_cast<char>(static_cast<unsigned char>(static_cast<unsigned int>(ch)));
|
||||
}
|
||||
|
||||
void EncodeToUtf8(std::ostream& stream, int ch)
|
||||
{
|
||||
if (ch <= 0x7F)
|
||||
{
|
||||
stream << Byte(ch);
|
||||
}
|
||||
else if (ch <= 0x7FF)
|
||||
{
|
||||
stream << Byte(0xC0 | (ch >> 6));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
else if (ch <= 0xFFFF)
|
||||
{
|
||||
stream << Byte(0xE0 | (ch >> 12));
|
||||
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
else if (ch <= 0x1FFFFF)
|
||||
{
|
||||
stream << Byte(0xF0 | (ch >> 18));
|
||||
stream << Byte(0x80 | ((ch >> 12) & 0x3F));
|
||||
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
}
|
||||
|
||||
bool SplitUtf16HighChar(std::ostream& stream, EncodingFn encoding, int ch)
|
||||
{
|
||||
int biasedValue = ch - 0x10000;
|
||||
if (biasedValue < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int high = 0xD800 | (biasedValue >> 10);
|
||||
int low = 0xDC00 | (biasedValue & 0x3FF);
|
||||
encoding(stream, high);
|
||||
encoding(stream, low);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EncodeToUtf16LE(std::ostream& stream, int ch)
|
||||
{
|
||||
if (!SplitUtf16HighChar(stream, &EncodeToUtf16LE, ch))
|
||||
{
|
||||
stream << Byte(ch & 0xFF) << Byte(ch >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
void EncodeToUtf16BE(std::ostream& stream, int ch)
|
||||
{
|
||||
if (!SplitUtf16HighChar(stream, &EncodeToUtf16BE, ch))
|
||||
{
|
||||
stream << Byte(ch >> 8) << Byte(ch & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
void EncodeToUtf32LE(std::ostream& stream, int ch)
|
||||
{
|
||||
stream << Byte(ch & 0xFF) << Byte((ch >> 8) & 0xFF)
|
||||
<< Byte((ch >> 16) & 0xFF) << Byte((ch >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
void EncodeToUtf32BE(std::ostream& stream, int ch)
|
||||
{
|
||||
stream << Byte((ch >> 24) & 0xFF) << Byte((ch >> 16) & 0xFF)
|
||||
<< Byte((ch >> 8) & 0xFF) << Byte(ch & 0xFF);
|
||||
}
|
||||
|
||||
class EncodingTester
|
||||
{
|
||||
public:
|
||||
EncodingTester(EncodingFn encoding, bool declareEncoding)
|
||||
{
|
||||
if (declareEncoding)
|
||||
{
|
||||
encoding(m_yaml, 0xFEFF);
|
||||
}
|
||||
|
||||
AddEntry(encoding, 0x0021, 0x007E); // Basic Latin
|
||||
AddEntry(encoding, 0x00A1, 0x00FF); // Latin-1 Supplement
|
||||
AddEntry(encoding, 0x0660, 0x06FF); // Arabic (largest contiguous block)
|
||||
|
||||
// CJK unified ideographs (multiple lines)
|
||||
AddEntry(encoding, 0x4E00, 0x4EFF);
|
||||
AddEntry(encoding, 0x4F00, 0x4FFF);
|
||||
AddEntry(encoding, 0x5000, 0x51FF); // 512 character line
|
||||
AddEntry(encoding, 0x5200, 0x54FF); // 768 character line
|
||||
AddEntry(encoding, 0x5500, 0x58FF); // 1024 character line
|
||||
|
||||
AddEntry(encoding, 0x103A0, 0x103C3); // Old Persian
|
||||
|
||||
m_yaml.seekg(0, std::ios::beg);
|
||||
}
|
||||
|
||||
std::istream& stream() {return m_yaml;}
|
||||
const std::vector<std::string>& entries() {return m_entries;}
|
||||
|
||||
private:
|
||||
std::stringstream m_yaml;
|
||||
std::vector<std::string> m_entries;
|
||||
|
||||
void AddEntry(EncodingFn encoding, int startCh, int endCh)
|
||||
{
|
||||
encoding(m_yaml, '-');
|
||||
encoding(m_yaml, ' ');
|
||||
encoding(m_yaml, '|');
|
||||
encoding(m_yaml, '\n');
|
||||
encoding(m_yaml, ' ');
|
||||
encoding(m_yaml, ' ');
|
||||
|
||||
std::stringstream entry;
|
||||
for (int ch = startCh; ch <= endCh; ++ch)
|
||||
{
|
||||
encoding(m_yaml, ch);
|
||||
EncodeToUtf8(entry, ch);
|
||||
}
|
||||
encoding(m_yaml, '\n');
|
||||
|
||||
m_entries.push_back(entry.str());
|
||||
}
|
||||
};
|
||||
|
||||
void RunEncodingTest(EncodingFn encoding, bool declareEncoding, const std::string& name, int& passed, int& total)
|
||||
{
|
||||
EncodingTester tester(encoding, declareEncoding);
|
||||
std::string error;
|
||||
bool ok = true;
|
||||
try {
|
||||
YAML::Parser parser(tester.stream());
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
YAML::Iterator itNode = doc.begin();
|
||||
std::vector<std::string>::const_iterator itEntry = tester.entries().begin();
|
||||
for (; (itNode != doc.end()) && (itEntry != tester.entries().end()); ++itNode, ++itEntry)
|
||||
{
|
||||
std::string stScalarValue;
|
||||
if (!itNode->GetScalar(stScalarValue) && (stScalarValue == *itEntry))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((itNode != doc.end()) || (itEntry != tester.entries().end()))
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok) {
|
||||
passed++;
|
||||
} else {
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
}
|
||||
total++;
|
||||
}
|
||||
}
|
||||
|
||||
bool RunParserTests()
|
||||
{
|
||||
int passed = 0;
|
||||
int total = 0;
|
||||
RunScalarParserTest(&Parser::SimpleScalar, "simple scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::MultiLineScalar, "multi-line scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::LiteralScalar, "literal scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::FoldedScalar, "folded scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::ChompedFoldedScalar, "chomped folded scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::ChompedLiteralScalar, "chomped literal scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::FoldedScalarWithIndent, "folded scalar with indent", passed, total);
|
||||
RunScalarParserTest(&Parser::ColonScalar, "colon scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::QuotedScalar, "quoted scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::CommaScalar, "comma scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::DashScalar, "dash scalar", passed, total);
|
||||
RunScalarParserTest(&Parser::URLScalar, "url scalar", passed, total);
|
||||
|
||||
RunParserTest(&Parser::SimpleSeq, "simple seq", passed, total);
|
||||
RunParserTest(&Parser::SimpleMap, "simple map", passed, total);
|
||||
RunParserTest(&Parser::FlowSeq, "flow seq", passed, total);
|
||||
RunParserTest(&Parser::FlowMap, "flow map", passed, total);
|
||||
RunParserTest(&Parser::FlowMapWithOmittedKey, "flow map with omitted key", passed, total);
|
||||
RunParserTest(&Parser::FlowMapWithOmittedValue, "flow map with omitted value", passed, total);
|
||||
RunParserTest(&Parser::FlowMapWithSoloEntry, "flow map with solo entry", passed, total);
|
||||
RunParserTest(&Parser::FlowMapEndingWithSoloEntry, "flow map ending with solo entry", passed, total);
|
||||
RunParserTest(&Parser::QuotedSimpleKeys, "quoted simple keys", passed, total);
|
||||
RunParserTest(&Parser::CompressedMapAndSeq, "compressed map and seq", passed, total);
|
||||
RunParserTest(&Parser::NullBlockSeqEntry, "null block seq entry", passed, total);
|
||||
RunParserTest(&Parser::NullBlockMapKey, "null block map key", passed, total);
|
||||
RunParserTest(&Parser::NullBlockMapValue, "null block map value", passed, total);
|
||||
RunParserTest(&Parser::SimpleAlias, "simple alias", passed, total);
|
||||
RunParserTest(&Parser::AliasWithNull, "alias with null", passed, total);
|
||||
RunParserTest(&Parser::AnchorInSimpleKey, "anchor in simple key", passed, total);
|
||||
RunParserTest(&Parser::AliasAsSimpleKey, "alias as simple key", passed, total);
|
||||
RunParserTest(&Parser::ExplicitDoc, "explicit doc", passed, total);
|
||||
RunParserTest(&Parser::MultipleDocs, "multiple docs", passed, total);
|
||||
RunParserTest(&Parser::ExplicitEndDoc, "explicit end doc", passed, total);
|
||||
RunParserTest(&Parser::MultipleDocsWithSomeExplicitIndicators, "multiple docs with some explicit indicators", passed, total);
|
||||
|
||||
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf16LE, false, "UTF-16LE, no BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf16LE, true, "UTF-16LE with BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf16BE, false, "UTF-16BE, no BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf16BE, true, "UTF-16BE with BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf32LE, false, "UTF-32LE, no BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf32LE, true, "UTF-32LE with BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf32BE, false, "UTF-32BE, no BOM", passed, total);
|
||||
RunEncodingTest(&EncodeToUtf32BE, true, "UTF-32BE with BOM", passed, total);
|
||||
|
||||
std::cout << "Parser tests: " << passed << "/" << total << " passed\n";
|
||||
return passed == total;
|
||||
}
|
||||
}
|
||||
|
||||
|
11
yaml-reader/parsertests.h
Normal file
11
yaml-reader/parsertests.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
namespace Test {
|
||||
bool RunParserTests();
|
||||
}
|
||||
|
||||
#endif // PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,6 @@
|
||||
#include "tests.h"
|
||||
#include "emittertests.h"
|
||||
#include "parsertests.h"
|
||||
#include "spectests.h"
|
||||
#include "yaml.h"
|
||||
#include <fstream>
|
||||
@@ -23,367 +25,5 @@ namespace Test
|
||||
if(passed)
|
||||
std::cout << "All tests passed!\n";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Parser tests
|
||||
|
||||
namespace {
|
||||
void RunScalarParserTest(void (*test)(std::string&, std::string&), const std::string& name, bool& passed) {
|
||||
std::string error;
|
||||
std::string inputScalar, desiredOutput;
|
||||
std::string output;
|
||||
bool ok = true;
|
||||
try {
|
||||
test(inputScalar, desiredOutput);
|
||||
std::stringstream stream(inputScalar);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
doc >> output;
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok && output == desiredOutput) {
|
||||
std::cout << "Parser test passed: " << name << "\n";
|
||||
} else {
|
||||
passed = false;
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
else {
|
||||
std::cout << "Output:\n" << output << "<<<\n";
|
||||
std::cout << "Desired output:\n" << desiredOutput << "<<<\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RunParserTest(bool (*test)(), const std::string& name, bool& passed) {
|
||||
std::string error;
|
||||
bool ok = true;
|
||||
try {
|
||||
ok = test();
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok) {
|
||||
std::cout << "Parser test passed: " << name << "\n";
|
||||
} else {
|
||||
passed = false;
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
typedef void (*EncodingFn)(std::ostream&, int);
|
||||
|
||||
inline char Byte(int ch)
|
||||
{
|
||||
return static_cast<char>(static_cast<unsigned char>(static_cast<unsigned int>(ch)));
|
||||
}
|
||||
|
||||
void EncodeToUtf8(std::ostream& stream, int ch)
|
||||
{
|
||||
if (ch <= 0x7F)
|
||||
{
|
||||
stream << Byte(ch);
|
||||
}
|
||||
else if (ch <= 0x7FF)
|
||||
{
|
||||
stream << Byte(0xC0 | (ch >> 6));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
else if (ch <= 0xFFFF)
|
||||
{
|
||||
stream << Byte(0xE0 | (ch >> 12));
|
||||
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
else if (ch <= 0x1FFFFF)
|
||||
{
|
||||
stream << Byte(0xF0 | (ch >> 18));
|
||||
stream << Byte(0x80 | ((ch >> 12) & 0x3F));
|
||||
stream << Byte(0x80 | ((ch >> 6) & 0x3F));
|
||||
stream << Byte(0x80 | (ch & 0x3F));
|
||||
}
|
||||
}
|
||||
|
||||
bool SplitUtf16HighChar(std::ostream& stream, EncodingFn encoding, int ch)
|
||||
{
|
||||
int biasedValue = ch - 0x10000;
|
||||
if (biasedValue < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int high = 0xD800 | (biasedValue >> 10);
|
||||
int low = 0xDC00 | (biasedValue & 0x3FF);
|
||||
encoding(stream, high);
|
||||
encoding(stream, low);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EncodeToUtf16LE(std::ostream& stream, int ch)
|
||||
{
|
||||
if (!SplitUtf16HighChar(stream, &EncodeToUtf16LE, ch))
|
||||
{
|
||||
stream << Byte(ch & 0xFF) << Byte(ch >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
void EncodeToUtf16BE(std::ostream& stream, int ch)
|
||||
{
|
||||
if (!SplitUtf16HighChar(stream, &EncodeToUtf16BE, ch))
|
||||
{
|
||||
stream << Byte(ch >> 8) << Byte(ch & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
void EncodeToUtf32LE(std::ostream& stream, int ch)
|
||||
{
|
||||
stream << Byte(ch & 0xFF) << Byte((ch >> 8) & 0xFF)
|
||||
<< Byte((ch >> 16) & 0xFF) << Byte((ch >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
void EncodeToUtf32BE(std::ostream& stream, int ch)
|
||||
{
|
||||
stream << Byte((ch >> 24) & 0xFF) << Byte((ch >> 16) & 0xFF)
|
||||
<< Byte((ch >> 8) & 0xFF) << Byte(ch & 0xFF);
|
||||
}
|
||||
|
||||
class EncodingTester
|
||||
{
|
||||
public:
|
||||
EncodingTester(EncodingFn encoding, bool declareEncoding)
|
||||
{
|
||||
if (declareEncoding)
|
||||
{
|
||||
encoding(m_yaml, 0xFEFF);
|
||||
}
|
||||
|
||||
AddEntry(encoding, 0x0021, 0x007E); // Basic Latin
|
||||
AddEntry(encoding, 0x00A1, 0x00FF); // Latin-1 Supplement
|
||||
AddEntry(encoding, 0x0660, 0x06FF); // Arabic (largest contiguous block)
|
||||
|
||||
// CJK unified ideographs (multiple lines)
|
||||
AddEntry(encoding, 0x4E00, 0x4EFF);
|
||||
AddEntry(encoding, 0x4F00, 0x4FFF);
|
||||
AddEntry(encoding, 0x5000, 0x51FF); // 512 character line
|
||||
AddEntry(encoding, 0x5200, 0x54FF); // 768 character line
|
||||
AddEntry(encoding, 0x5500, 0x58FF); // 1024 character line
|
||||
|
||||
AddEntry(encoding, 0x103A0, 0x103C3); // Old Persian
|
||||
|
||||
m_yaml.seekg(0, std::ios::beg);
|
||||
}
|
||||
|
||||
std::istream& stream() {return m_yaml;}
|
||||
const std::vector<std::string>& entries() {return m_entries;}
|
||||
|
||||
private:
|
||||
std::stringstream m_yaml;
|
||||
std::vector<std::string> m_entries;
|
||||
|
||||
void AddEntry(EncodingFn encoding, int startCh, int endCh)
|
||||
{
|
||||
encoding(m_yaml, '-');
|
||||
encoding(m_yaml, ' ');
|
||||
encoding(m_yaml, '|');
|
||||
encoding(m_yaml, '\n');
|
||||
encoding(m_yaml, ' ');
|
||||
encoding(m_yaml, ' ');
|
||||
|
||||
std::stringstream entry;
|
||||
for (int ch = startCh; ch <= endCh; ++ch)
|
||||
{
|
||||
encoding(m_yaml, ch);
|
||||
EncodeToUtf8(entry, ch);
|
||||
}
|
||||
encoding(m_yaml, '\n');
|
||||
|
||||
m_entries.push_back(entry.str());
|
||||
}
|
||||
};
|
||||
|
||||
void RunEncodingTest(EncodingFn encoding, bool declareEncoding, const std::string& name, bool& passed)
|
||||
{
|
||||
EncodingTester tester(encoding, declareEncoding);
|
||||
std::string error;
|
||||
bool ok = true;
|
||||
try {
|
||||
YAML::Parser parser(tester.stream());
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
YAML::Iterator itNode = doc.begin();
|
||||
std::vector<std::string>::const_iterator itEntry = tester.entries().begin();
|
||||
for (; (itNode != doc.end()) && (itEntry != tester.entries().end()); ++itNode, ++itEntry)
|
||||
{
|
||||
std::string stScalarValue;
|
||||
if (!itNode->GetScalar(stScalarValue) && (stScalarValue == *itEntry))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((itNode != doc.end()) || (itEntry != tester.entries().end()))
|
||||
{
|
||||
ok = false;
|
||||
}
|
||||
} catch(const YAML::Exception& e) {
|
||||
ok = false;
|
||||
error = e.msg;
|
||||
}
|
||||
if(ok) {
|
||||
std::cout << "Parser test passed: " << name << "\n";
|
||||
} else {
|
||||
passed = false;
|
||||
std::cout << "Parser test failed: " << name << "\n";
|
||||
if(error != "")
|
||||
std::cout << "Caught exception: " << error << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RunParserTests()
|
||||
{
|
||||
bool passed = true;
|
||||
RunScalarParserTest(&Parser::SimpleScalar, "simple scalar", passed);
|
||||
RunScalarParserTest(&Parser::MultiLineScalar, "multi-line scalar", passed);
|
||||
RunScalarParserTest(&Parser::LiteralScalar, "literal scalar", passed);
|
||||
RunScalarParserTest(&Parser::FoldedScalar, "folded scalar", passed);
|
||||
RunScalarParserTest(&Parser::ChompedFoldedScalar, "chomped folded scalar", passed);
|
||||
RunScalarParserTest(&Parser::ChompedLiteralScalar, "chomped literal scalar", passed);
|
||||
RunScalarParserTest(&Parser::FoldedScalarWithIndent, "folded scalar with indent", passed);
|
||||
RunScalarParserTest(&Parser::ColonScalar, "colon scalar", passed);
|
||||
RunScalarParserTest(&Parser::QuotedScalar, "quoted scalar", passed);
|
||||
RunScalarParserTest(&Parser::CommaScalar, "comma scalar", passed);
|
||||
RunScalarParserTest(&Parser::DashScalar, "dash scalar", passed);
|
||||
RunScalarParserTest(&Parser::URLScalar, "url scalar", passed);
|
||||
|
||||
RunParserTest(&Parser::SimpleSeq, "simple seq", passed);
|
||||
RunParserTest(&Parser::SimpleMap, "simple map", passed);
|
||||
RunParserTest(&Parser::FlowSeq, "flow seq", passed);
|
||||
RunParserTest(&Parser::FlowMap, "flow map", passed);
|
||||
RunParserTest(&Parser::FlowMapWithOmittedKey, "flow map with omitted key", passed);
|
||||
RunParserTest(&Parser::FlowMapWithOmittedValue, "flow map with omitted value", passed);
|
||||
RunParserTest(&Parser::FlowMapWithSoloEntry, "flow map with solo entry", passed);
|
||||
RunParserTest(&Parser::FlowMapEndingWithSoloEntry, "flow map ending with solo entry", passed);
|
||||
RunParserTest(&Parser::QuotedSimpleKeys, "quoted simple keys", passed);
|
||||
RunParserTest(&Parser::CompressedMapAndSeq, "compressed map and seq", passed);
|
||||
RunParserTest(&Parser::NullBlockSeqEntry, "null block seq entry", passed);
|
||||
RunParserTest(&Parser::NullBlockMapKey, "null block map key", passed);
|
||||
RunParserTest(&Parser::NullBlockMapValue, "null block map value", passed);
|
||||
RunParserTest(&Parser::SimpleAlias, "simple alias", passed);
|
||||
RunParserTest(&Parser::AliasWithNull, "alias with null", passed);
|
||||
RunParserTest(&Parser::AnchorInSimpleKey, "anchor in simple key", passed);
|
||||
RunParserTest(&Parser::AliasAsSimpleKey, "alias as simple key", passed);
|
||||
RunParserTest(&Parser::ExplicitDoc, "explicit doc", passed);
|
||||
RunParserTest(&Parser::MultipleDocs, "multiple docs", passed);
|
||||
RunParserTest(&Parser::ExplicitEndDoc, "explicit end doc", passed);
|
||||
RunParserTest(&Parser::MultipleDocsWithSomeExplicitIndicators, "multiple docs with some explicit indicators", passed);
|
||||
|
||||
RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf16LE, false, "UTF-16LE, no BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf16LE, true, "UTF-16LE with BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf16BE, false, "UTF-16BE, no BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf16BE, true, "UTF-16BE with BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf32LE, false, "UTF-32LE, no BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf32LE, true, "UTF-32LE with BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf32BE, false, "UTF-32BE, no BOM", passed);
|
||||
RunEncodingTest(&EncodeToUtf32BE, true, "UTF-32BE with BOM", passed);
|
||||
return passed;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Emitter tests
|
||||
|
||||
namespace {
|
||||
void RunEmitterTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, bool& passed) {
|
||||
YAML::Emitter out;
|
||||
std::string desiredOutput;
|
||||
test(out, desiredOutput);
|
||||
std::string output = out.c_str();
|
||||
|
||||
if(output == desiredOutput) {
|
||||
std::cout << "Emitter test passed: " << name << "\n";
|
||||
} else {
|
||||
passed = false;
|
||||
std::cout << "Emitter test failed: " << name << "\n";
|
||||
std::cout << "Output:\n";
|
||||
std::cout << output << "<<<\n";
|
||||
std::cout << "Desired output:\n";
|
||||
std::cout << desiredOutput << "<<<\n";
|
||||
}
|
||||
}
|
||||
|
||||
void RunEmitterErrorTest(void (*test)(YAML::Emitter&, std::string&), const std::string& name, bool& passed) {
|
||||
YAML::Emitter out;
|
||||
std::string desiredError;
|
||||
test(out, desiredError);
|
||||
std::string lastError = out.GetLastError();
|
||||
if(!out.good() && lastError == desiredError) {
|
||||
std::cout << "Emitter test passed: " << name << "\n";
|
||||
} else {
|
||||
passed = false;
|
||||
std::cout << "Emitter test failed: " << name << "\n";
|
||||
if(out.good())
|
||||
std::cout << "No error detected\n";
|
||||
else
|
||||
std::cout << "Detected error: " << lastError << "\n";
|
||||
std::cout << "Expected error: " << desiredError << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RunEmitterTests()
|
||||
{
|
||||
bool passed = true;
|
||||
RunEmitterTest(&Emitter::SimpleScalar, "simple scalar", passed);
|
||||
RunEmitterTest(&Emitter::SimpleSeq, "simple seq", passed);
|
||||
RunEmitterTest(&Emitter::SimpleFlowSeq, "simple flow seq", passed);
|
||||
RunEmitterTest(&Emitter::EmptyFlowSeq, "empty flow seq", passed);
|
||||
RunEmitterTest(&Emitter::NestedBlockSeq, "nested block seq", passed);
|
||||
RunEmitterTest(&Emitter::NestedFlowSeq, "nested flow seq", passed);
|
||||
RunEmitterTest(&Emitter::SimpleMap, "simple map", passed);
|
||||
RunEmitterTest(&Emitter::SimpleFlowMap, "simple flow map", passed);
|
||||
RunEmitterTest(&Emitter::MapAndList, "map and list", passed);
|
||||
RunEmitterTest(&Emitter::ListAndMap, "list and map", passed);
|
||||
RunEmitterTest(&Emitter::NestedBlockMap, "nested block map", passed);
|
||||
RunEmitterTest(&Emitter::NestedFlowMap, "nested flow map", passed);
|
||||
RunEmitterTest(&Emitter::MapListMix, "map list mix", passed);
|
||||
RunEmitterTest(&Emitter::SimpleLongKey, "simple long key", passed);
|
||||
RunEmitterTest(&Emitter::SingleLongKey, "single long key", passed);
|
||||
RunEmitterTest(&Emitter::ComplexLongKey, "complex long key", passed);
|
||||
RunEmitterTest(&Emitter::AutoLongKey, "auto long key", passed);
|
||||
RunEmitterTest(&Emitter::ScalarFormat, "scalar format", passed);
|
||||
RunEmitterTest(&Emitter::AutoLongKeyScalar, "auto long key scalar", passed);
|
||||
RunEmitterTest(&Emitter::LongKeyFlowMap, "long key flow map", passed);
|
||||
RunEmitterTest(&Emitter::BlockMapAsKey, "block map as key", passed);
|
||||
RunEmitterTest(&Emitter::AliasAndAnchor, "alias and anchor", passed);
|
||||
RunEmitterTest(&Emitter::AliasAndAnchorWithNull, "alias and anchor with null", passed);
|
||||
RunEmitterTest(&Emitter::ComplexDoc, "complex doc", passed);
|
||||
RunEmitterTest(&Emitter::STLContainers, "STL containers", passed);
|
||||
RunEmitterTest(&Emitter::SimpleComment, "simple comment", passed);
|
||||
RunEmitterTest(&Emitter::MultiLineComment, "multi-line comment", passed);
|
||||
RunEmitterTest(&Emitter::ComplexComments, "complex comments", passed);
|
||||
RunEmitterTest(&Emitter::Indentation, "indentation", passed);
|
||||
RunEmitterTest(&Emitter::SimpleGlobalSettings, "simple global settings", passed);
|
||||
RunEmitterTest(&Emitter::ComplexGlobalSettings, "complex global settings", passed);
|
||||
RunEmitterTest(&Emitter::Null, "null", passed);
|
||||
|
||||
RunEmitterErrorTest(&Emitter::ExtraEndSeq, "extra EndSeq", passed);
|
||||
RunEmitterErrorTest(&Emitter::ExtraEndMap, "extra EndMap", passed);
|
||||
RunEmitterErrorTest(&Emitter::BadSingleQuoted, "bad single quoted string", passed);
|
||||
RunEmitterErrorTest(&Emitter::InvalidAnchor, "invalid anchor", passed);
|
||||
RunEmitterErrorTest(&Emitter::InvalidAlias, "invalid alias", passed);
|
||||
RunEmitterErrorTest(&Emitter::MissingKey, "missing key", passed);
|
||||
RunEmitterErrorTest(&Emitter::MissingValue, "missing value", passed);
|
||||
RunEmitterErrorTest(&Emitter::UnexpectedKey, "unexpected key", passed);
|
||||
RunEmitterErrorTest(&Emitter::UnexpectedValue, "unexpected value", passed);
|
||||
return passed;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -5,14 +5,9 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace YAML { class Emitter; }
|
||||
|
||||
namespace Test {
|
||||
void RunAll();
|
||||
|
||||
bool RunParserTests();
|
||||
bool RunEmitterTests();
|
||||
|
||||
namespace Parser {
|
||||
// scalar tests
|
||||
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
@@ -51,53 +46,6 @@ namespace Test {
|
||||
bool ExplicitEndDoc();
|
||||
bool MultipleDocsWithSomeExplicitIndicators();
|
||||
}
|
||||
|
||||
namespace Emitter {
|
||||
// correct emitting
|
||||
void SimpleScalar(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleSeq(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleFlowSeq(YAML::Emitter& ouptut, std::string& desiredOutput);
|
||||
void EmptyFlowSeq(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void NestedBlockSeq(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void NestedFlowSeq(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleFlowMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void MapAndList(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ListAndMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void NestedBlockMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void NestedFlowMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void MapListMix(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleLongKey(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SingleLongKey(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ComplexLongKey(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void AutoLongKey(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ScalarFormat(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void AutoLongKeyScalar(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void LongKeyFlowMap(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void BlockMapAsKey(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void AliasAndAnchor(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void AliasAndAnchorWithNull(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ComplexDoc(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void STLContainers(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleComment(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void MultiLineComment(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ComplexComments(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void Indentation(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void SimpleGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void ComplexGlobalSettings(YAML::Emitter& out, std::string& desiredOutput);
|
||||
void Null(YAML::Emitter& out, std::string& desiredOutput);
|
||||
|
||||
// incorrect emitting
|
||||
void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError);
|
||||
void ExtraEndMap(YAML::Emitter& out, std::string& desiredError);
|
||||
void BadSingleQuoted(YAML::Emitter& out, std::string& desiredError);
|
||||
void InvalidAnchor(YAML::Emitter& out, std::string& desiredError);
|
||||
void InvalidAlias(YAML::Emitter& out, std::string& desiredError);
|
||||
void MissingKey(YAML::Emitter& out, std::string& desiredError);
|
||||
void MissingValue(YAML::Emitter& out, std::string& desiredError);
|
||||
void UnexpectedKey(YAML::Emitter& out, std::string& desiredError);
|
||||
void UnexpectedValue(YAML::Emitter& out, std::string& desiredError);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
@@ -191,6 +191,10 @@
|
||||
RelativePath=".\src\node.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\null.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scalar.cpp"
|
||||
>
|
||||
|
Reference in New Issue
Block a user