adding newest (0.2.3) version of yaml-cpp, which has greatly improved error messages, to fix #1911
This commit is contained in:
parent
b2e5889851
commit
3f494671c6
Binary file not shown.
|
@ -0,0 +1,40 @@
|
|||
cmake_minimum_required(VERSION 2.6)
|
||||
# this makefile slightly hacked up to fit inside the rosdep project
|
||||
# see http://code.google.com/p/yaml-cpp for the original distribution
|
||||
|
||||
project (YAML_CPP)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCC)
|
||||
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 "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)
|
||||
|
||||
set(INCLUDE_INSTALL_DIR include/yaml-cpp)
|
||||
set(LIB_INSTALL_DIR ${PROJECT_SOURCE_DIR}/../bin)
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin)
|
||||
|
||||
file(GLOB public_headers include/*.h)
|
||||
file(GLOB private_headers src/*.h)
|
||||
file(GLOB sources src/*.cpp)
|
||||
|
||||
include_directories(${YAML_CPP_SOURCE_DIR}/include)
|
||||
|
||||
add_library(yaml-cpp
|
||||
${LIB_TYPE}
|
||||
${public_headers}
|
||||
${private_headers}
|
||||
${sources}
|
||||
)
|
||||
set_target_properties(yaml-cpp PROPERTIES
|
||||
VERSION "${YAML_CPP_VERSION}"
|
||||
SOVERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}"
|
||||
)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
This version of yaml-cpp 0.2.3 is slightly hacked up from the original
|
||||
distribution; all I did was tweak the CMakeLists.txt file so it builds staticly
|
||||
with rosdep. Since this is the dependency bootstrapper of ROS, I wanted it to
|
||||
be as simple a build process as possible. The original distribution is
|
||||
available here:
|
||||
|
||||
http://code.google.com/p/yaml-cpp
|
||||
|
||||
Morgan Quigley
|
||||
23 Oct 2009
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "null.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
inline bool Convert(const std::string& input, std::string& output) {
|
||||
output = input;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Convert(const std::string& input, bool& output);
|
||||
bool Convert(const std::string& input, _Null& output);
|
||||
|
||||
#define YAML_MAKE_STREAM_CONVERT(type) \
|
||||
inline bool Convert(const std::string& input, type& output) { \
|
||||
std::stringstream stream(input); \
|
||||
stream >> output; \
|
||||
return !stream.fail(); \
|
||||
}
|
||||
|
||||
YAML_MAKE_STREAM_CONVERT(char)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned char)
|
||||
YAML_MAKE_STREAM_CONVERT(int)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned int)
|
||||
YAML_MAKE_STREAM_CONVERT(short)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned short)
|
||||
YAML_MAKE_STREAM_CONVERT(long)
|
||||
YAML_MAKE_STREAM_CONVERT(unsigned long)
|
||||
YAML_MAKE_STREAM_CONVERT(float)
|
||||
YAML_MAKE_STREAM_CONVERT(double)
|
||||
YAML_MAKE_STREAM_CONVERT(long double)
|
||||
|
||||
#undef YAML_MAKE_STREAM_CONVERT
|
||||
}
|
||||
|
||||
#endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef CRT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define CRT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
// for detecting memory leaks
|
||||
#ifdef _DEBUG
|
||||
|
||||
#define _CRTDBG_MAP_ALLOC
|
||||
#include <stdlib.h>
|
||||
#include <crtdbg.h>
|
||||
|
||||
#endif // _DEBUG
|
||||
|
||||
|
||||
#endif // CRT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,95 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "emittermanip.h"
|
||||
#include "ostream.h"
|
||||
#include "null.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class EmitterState;
|
||||
|
||||
class Emitter
|
||||
{
|
||||
public:
|
||||
Emitter();
|
||||
~Emitter();
|
||||
|
||||
// output
|
||||
const char *c_str() const;
|
||||
unsigned size() const;
|
||||
|
||||
// state checking
|
||||
bool good() const;
|
||||
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);
|
||||
bool SetSeqFormat(EMITTER_MANIP value);
|
||||
bool SetMapFormat(EMITTER_MANIP value);
|
||||
bool SetIndent(unsigned n);
|
||||
bool SetPreCommentIndent(unsigned n);
|
||||
bool SetPostCommentIndent(unsigned n);
|
||||
|
||||
// local setters
|
||||
Emitter& SetLocalValue(EMITTER_MANIP value);
|
||||
Emitter& SetLocalIndent(const _Indent& indent);
|
||||
|
||||
// overloads of write
|
||||
Emitter& Write(const std::string& str);
|
||||
Emitter& Write(const char *str);
|
||||
Emitter& Write(int i);
|
||||
Emitter& Write(bool b);
|
||||
Emitter& Write(float f);
|
||||
Emitter& Write(double d);
|
||||
Emitter& Write(const _Alias& alias);
|
||||
Emitter& Write(const _Anchor& anchor);
|
||||
Emitter& Write(const _Comment& comment);
|
||||
Emitter& Write(const _Null& null);
|
||||
|
||||
private:
|
||||
enum ATOMIC_TYPE { AT_SCALAR, AT_SEQ, AT_BLOCK_SEQ, AT_FLOW_SEQ, AT_MAP, AT_BLOCK_MAP, AT_FLOW_MAP };
|
||||
|
||||
void PreAtomicWrite();
|
||||
bool GotoNextPreAtomicState();
|
||||
void PostAtomicWrite();
|
||||
void EmitSeparationIfNecessary();
|
||||
|
||||
void EmitBeginSeq();
|
||||
void EmitEndSeq();
|
||||
void EmitBeginMap();
|
||||
void EmitEndMap();
|
||||
void EmitKey();
|
||||
void EmitValue();
|
||||
|
||||
private:
|
||||
ostream m_stream;
|
||||
std::auto_ptr <EmitterState> m_pState;
|
||||
};
|
||||
|
||||
// overloads of insertion
|
||||
template <typename T>
|
||||
inline Emitter& operator << (Emitter& emitter, T v) {
|
||||
return emitter.Write(v);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline Emitter& operator << (Emitter& emitter, EMITTER_MANIP value) {
|
||||
return emitter.SetLocalValue(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline Emitter& operator << (Emitter& emitter, _Indent indent) {
|
||||
return emitter.SetLocalIndent(indent);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,94 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
enum EMITTER_MANIP {
|
||||
// general manipulators
|
||||
Auto,
|
||||
|
||||
// output character set
|
||||
EmitNonAscii,
|
||||
EscapeNonAscii,
|
||||
|
||||
// string manipulators
|
||||
// Auto, // duplicate
|
||||
SingleQuoted,
|
||||
DoubleQuoted,
|
||||
Literal,
|
||||
|
||||
// bool manipulators
|
||||
YesNoBool, // yes, no
|
||||
TrueFalseBool, // true, false
|
||||
OnOffBool, // on, off
|
||||
UpperCase, // TRUE, N
|
||||
LowerCase, // f, yes
|
||||
CamelCase, // No, Off
|
||||
LongBool, // yes, On
|
||||
ShortBool, // y, t
|
||||
|
||||
// int manipulators
|
||||
Dec,
|
||||
Hex,
|
||||
Oct,
|
||||
|
||||
// sequence manipulators
|
||||
BeginSeq,
|
||||
EndSeq,
|
||||
Flow,
|
||||
Block,
|
||||
|
||||
// map manipulators
|
||||
BeginMap,
|
||||
EndMap,
|
||||
Key,
|
||||
Value,
|
||||
// Flow, // duplicate
|
||||
// Block, // duplicate
|
||||
// Auto, // duplicate
|
||||
LongKey
|
||||
};
|
||||
|
||||
struct _Indent {
|
||||
_Indent(int value_): value(value_) {}
|
||||
int value;
|
||||
};
|
||||
|
||||
inline _Indent Indent(int value) {
|
||||
return _Indent(value);
|
||||
}
|
||||
|
||||
struct _Alias {
|
||||
_Alias(const std::string& content_): content(content_) {}
|
||||
std::string content;
|
||||
};
|
||||
|
||||
inline _Alias Alias(const std::string content) {
|
||||
return _Alias(content);
|
||||
}
|
||||
|
||||
struct _Anchor {
|
||||
_Anchor(const std::string& content_): content(content_) {}
|
||||
std::string content;
|
||||
};
|
||||
|
||||
inline _Anchor Anchor(const std::string content) {
|
||||
return _Anchor(content);
|
||||
}
|
||||
|
||||
struct _Comment {
|
||||
_Comment(const std::string& content_): content(content_) {}
|
||||
std::string content;
|
||||
};
|
||||
|
||||
inline _Comment Comment(const std::string content) {
|
||||
return _Comment(content);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTERMANIP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,137 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "mark.h"
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// error messages
|
||||
namespace ErrorMsg
|
||||
{
|
||||
const std::string YAML_DIRECTIVE_ARGS = "YAML directives must have exactly one argument";
|
||||
const std::string YAML_VERSION = "bad YAML version: ";
|
||||
const std::string YAML_MAJOR_VERSION = "YAML major version too large";
|
||||
const std::string TAG_DIRECTIVE_ARGS = "TAG directives must have exactly two arguments";
|
||||
const std::string END_OF_MAP = "end of map not found";
|
||||
const std::string END_OF_MAP_FLOW = "end of map flow not found";
|
||||
const std::string END_OF_SEQ = "end of sequence not found";
|
||||
const std::string END_OF_SEQ_FLOW = "end of sequence flow not found";
|
||||
const std::string MULTIPLE_TAGS = "cannot assign multiple tags to the same node";
|
||||
const std::string MULTIPLE_ANCHORS = "cannot assign multiple anchors to the same node";
|
||||
const std::string MULTIPLE_ALIASES = "cannot assign multiple aliases to the same node";
|
||||
const std::string ALIAS_CONTENT = "aliases can't have any content, *including* tags";
|
||||
const std::string INVALID_HEX = "bad character found while scanning hex number";
|
||||
const std::string INVALID_UNICODE = "invalid unicode: ";
|
||||
const std::string INVALID_ESCAPE = "unknown escape character: ";
|
||||
const std::string UNKNOWN_TOKEN = "unknown token";
|
||||
const std::string DOC_IN_SCALAR = "illegal document indicator in scalar";
|
||||
const std::string EOF_IN_SCALAR = "illegal EOF in scalar";
|
||||
const std::string CHAR_IN_SCALAR = "illegal character in scalar";
|
||||
const std::string TAB_IN_INDENTATION = "illegal tab when looking for indentation";
|
||||
const std::string FLOW_END = "illegal flow end";
|
||||
const std::string BLOCK_ENTRY = "illegal block entry";
|
||||
const std::string MAP_KEY = "illegal map key";
|
||||
const std::string MAP_VALUE = "illegal map value";
|
||||
const std::string ALIAS_NOT_FOUND = "alias not found after *";
|
||||
const std::string ANCHOR_NOT_FOUND = "anchor not found after &";
|
||||
const std::string CHAR_IN_ALIAS = "illegal character found while scanning alias";
|
||||
const std::string CHAR_IN_ANCHOR = "illegal character found while scanning anchor";
|
||||
const std::string ZERO_INDENT_IN_BLOCK = "cannot set zero indentation for a block scalar";
|
||||
const std::string CHAR_IN_BLOCK = "unexpected character in block scalar";
|
||||
const std::string AMBIGUOUS_ANCHOR = "cannot assign the same alias to multiple nodes";
|
||||
const std::string UNKNOWN_ANCHOR = "the referenced anchor is not defined";
|
||||
|
||||
const std::string INVALID_SCALAR = "invalid scalar";
|
||||
const std::string KEY_NOT_FOUND = "key not found";
|
||||
const std::string BAD_DEREFERENCE = "bad dereference";
|
||||
|
||||
const std::string UNMATCHED_GROUP_TAG = "unmatched group tag";
|
||||
const std::string UNEXPECTED_END_SEQ = "unexpected end sequence token";
|
||||
const std::string UNEXPECTED_END_MAP = "unexpected end map token";
|
||||
const std::string SINGLE_QUOTED_CHAR = "invalid character in single-quoted string";
|
||||
const std::string INVALID_ANCHOR = "invalid anchor";
|
||||
const std::string INVALID_ALIAS = "invalid alias";
|
||||
const std::string EXPECTED_KEY_TOKEN = "expected key token";
|
||||
const std::string EXPECTED_VALUE_TOKEN = "expected value token";
|
||||
const std::string UNEXPECTED_KEY_TOKEN = "unexpected key token";
|
||||
const std::string UNEXPECTED_VALUE_TOKEN = "unexpected value token";
|
||||
}
|
||||
|
||||
class Exception: public std::exception {
|
||||
public:
|
||||
Exception(const Mark& mark_, const std::string& msg_)
|
||||
: mark(mark_), msg(msg_) {
|
||||
std::stringstream output;
|
||||
output << "yaml-cpp: error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg;
|
||||
what_ = output.str();
|
||||
}
|
||||
virtual ~Exception() throw() {}
|
||||
virtual const char *what() const throw() { return what_.c_str(); }
|
||||
|
||||
Mark mark;
|
||||
std::string msg;
|
||||
|
||||
private:
|
||||
std::string what_;
|
||||
};
|
||||
|
||||
class ParserException: public Exception {
|
||||
public:
|
||||
ParserException(const Mark& mark_, const std::string& msg_)
|
||||
: Exception(mark_, msg_) {}
|
||||
};
|
||||
|
||||
class RepresentationException: public Exception {
|
||||
public:
|
||||
RepresentationException(const Mark& mark_, const std::string& msg_)
|
||||
: Exception(mark_, msg_) {}
|
||||
};
|
||||
|
||||
// representation exceptions
|
||||
class InvalidScalar: public RepresentationException {
|
||||
public:
|
||||
InvalidScalar(const Mark& mark_)
|
||||
: RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {}
|
||||
};
|
||||
|
||||
class KeyNotFound: public RepresentationException {
|
||||
public:
|
||||
KeyNotFound(const Mark& mark_)
|
||||
: RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class TypedKeyNotFound: public KeyNotFound {
|
||||
public:
|
||||
TypedKeyNotFound(const Mark& mark_, const T& key_)
|
||||
: KeyNotFound(mark_), key(key_) {}
|
||||
~TypedKeyNotFound() throw() {}
|
||||
|
||||
T key;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
TypedKeyNotFound <T> MakeTypedKeyNotFound(const Mark& mark, const T& key) {
|
||||
return TypedKeyNotFound <T> (mark, key);
|
||||
}
|
||||
|
||||
class BadDereference: public RepresentationException {
|
||||
public:
|
||||
BadDereference()
|
||||
: RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {}
|
||||
};
|
||||
|
||||
class EmitterException: public Exception {
|
||||
public:
|
||||
EmitterException(const std::string& msg_)
|
||||
: Exception(Mark::null(), msg_) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
struct IterPriv;
|
||||
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
Iterator();
|
||||
Iterator(IterPriv *pData);
|
||||
Iterator(const Iterator& rhs);
|
||||
~Iterator();
|
||||
|
||||
Iterator& operator = (const Iterator& rhs);
|
||||
Iterator& operator ++ ();
|
||||
Iterator operator ++ (int);
|
||||
const Node& operator * () const;
|
||||
const Node *operator -> () const;
|
||||
const Node& first() const;
|
||||
const Node& second() const;
|
||||
|
||||
friend bool operator == (const Iterator& it, const Iterator& jt);
|
||||
friend bool operator != (const Iterator& it, const Iterator& jt);
|
||||
|
||||
private:
|
||||
IterPriv *m_pData;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ITERATOR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
struct Mark {
|
||||
Mark(): pos(0), line(0), column(0) {}
|
||||
|
||||
static const Mark null() { return Mark(-1, -1, -1); }
|
||||
|
||||
int pos;
|
||||
int line, column;
|
||||
|
||||
private:
|
||||
Mark(int pos_, int line_, int column_): pos(pos_), line(line_), column(column_) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MARK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,138 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "conversion.h"
|
||||
#include "exceptions.h"
|
||||
#include "iterator.h"
|
||||
#include "mark.h"
|
||||
#include "noncopyable.h"
|
||||
#include "parserstate.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Content;
|
||||
class Scanner;
|
||||
class Emitter;
|
||||
|
||||
enum CONTENT_TYPE { CT_NONE, CT_SCALAR, CT_SEQUENCE, CT_MAP };
|
||||
|
||||
class Node: private noncopyable
|
||||
{
|
||||
public:
|
||||
Node();
|
||||
~Node();
|
||||
|
||||
void Clear();
|
||||
std::auto_ptr<Node> Clone() const;
|
||||
void Parse(Scanner *pScanner, const ParserState& state);
|
||||
|
||||
CONTENT_TYPE GetType() const;
|
||||
|
||||
// file location of start of this node
|
||||
const Mark GetMark() const { return m_mark; }
|
||||
|
||||
// accessors
|
||||
Iterator begin() const;
|
||||
Iterator end() const;
|
||||
std::size_t size() const;
|
||||
|
||||
// extraction of scalars
|
||||
bool GetScalar(std::string& s) const;
|
||||
|
||||
// we can specialize this for other values
|
||||
template <typename T>
|
||||
bool Read(T& value) const;
|
||||
|
||||
template <typename T>
|
||||
const T Read() const;
|
||||
|
||||
template <typename T>
|
||||
operator T() const;
|
||||
|
||||
template <typename T>
|
||||
friend void operator >> (const Node& node, T& value);
|
||||
|
||||
// retrieval for maps and sequences
|
||||
template <typename T>
|
||||
const Node *FindValue(const T& key) const;
|
||||
|
||||
template <typename T>
|
||||
const Node& operator [] (const T& key) const;
|
||||
|
||||
// specific to maps
|
||||
const Node *FindValue(const char *key) const;
|
||||
const Node& operator [] (const char *key) const;
|
||||
|
||||
// for anchors/aliases
|
||||
const Node *Identity() const { return m_pIdentity; }
|
||||
bool IsAlias() const { return m_alias; }
|
||||
bool IsReferenced() const { return m_referenced; }
|
||||
|
||||
// emitting
|
||||
friend Emitter& operator << (Emitter& out, const Node& node);
|
||||
|
||||
// ordering
|
||||
int Compare(const Node& rhs) const;
|
||||
friend bool operator < (const Node& n1, const Node& n2);
|
||||
|
||||
private:
|
||||
// helper for sequences
|
||||
template <typename, bool> friend struct _FindFromNodeAtIndex;
|
||||
const Node *FindAtIndex(std::size_t i) const;
|
||||
|
||||
// helper for maps
|
||||
template <typename T>
|
||||
const Node& GetValue(const T& key) const;
|
||||
|
||||
template <typename T>
|
||||
const Node *FindValueForKey(const T& key) const;
|
||||
|
||||
// helper for cloning
|
||||
Node(const Mark& mark, const std::string& anchor, const std::string& tag, const Content *pContent);
|
||||
|
||||
// helpers for parsing
|
||||
void ParseHeader(Scanner *pScanner, const ParserState& state);
|
||||
void ParseTag(Scanner *pScanner, const ParserState& state);
|
||||
void ParseAnchor(Scanner *pScanner, const ParserState& state);
|
||||
void ParseAlias(Scanner *pScanner, const ParserState& state);
|
||||
|
||||
private:
|
||||
Mark m_mark;
|
||||
std::string m_anchor, m_tag;
|
||||
Content *m_pContent;
|
||||
bool m_alias;
|
||||
const Node *m_pIdentity;
|
||||
mutable bool m_referenced;
|
||||
};
|
||||
|
||||
// comparisons with auto-conversion
|
||||
template <typename T>
|
||||
bool operator == (const T& value, const Node& node);
|
||||
|
||||
template <typename T>
|
||||
bool operator == (const Node& node, const T& value);
|
||||
|
||||
template <typename T>
|
||||
bool operator != (const T& value, const Node& node);
|
||||
|
||||
template <typename T>
|
||||
bool operator != (const Node& node, const T& value);
|
||||
|
||||
bool operator == (const char *value, const Node& node);
|
||||
bool operator == (const Node& node, const char *value);
|
||||
bool operator != (const char *value, const Node& node);
|
||||
bool operator != (const Node& node, const char *value);
|
||||
}
|
||||
|
||||
#include "nodeimpl.h"
|
||||
#include "nodereadimpl.h"
|
||||
|
||||
#endif // NODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,118 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "nodeutil.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// implementation of templated things
|
||||
template <typename T>
|
||||
inline const T Node::Read() const {
|
||||
T value;
|
||||
*this >> value;
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Node::operator T() const {
|
||||
return Read<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void operator >> (const Node& node, T& value) {
|
||||
if(!ConvertScalar(node, value))
|
||||
throw InvalidScalar(node.m_mark);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node *Node::FindValue(const T& key) const {
|
||||
switch(GetType()) {
|
||||
case CT_MAP:
|
||||
return FindValueForKey(key);
|
||||
case CT_SEQUENCE:
|
||||
return FindFromNodeAtIndex(*this, key);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node *Node::FindValueForKey(const T& key) const {
|
||||
for(Iterator it=begin();it!=end();++it) {
|
||||
T t;
|
||||
if(it.first().Read(t)) {
|
||||
if(key == t)
|
||||
return &it.second();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node& Node::GetValue(const T& key) const {
|
||||
if(!m_pContent)
|
||||
throw BadDereference();
|
||||
|
||||
const Node *pValue = FindValue(key);
|
||||
if(!pValue)
|
||||
throw MakeTypedKeyNotFound(m_mark, key);
|
||||
|
||||
return *pValue;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const Node& Node::operator [] (const T& key) const {
|
||||
return GetValue(key);
|
||||
}
|
||||
|
||||
inline const Node *Node::FindValue(const char *key) const {
|
||||
return FindValue(std::string(key));
|
||||
}
|
||||
|
||||
inline const Node& Node::operator [] (const char *key) const {
|
||||
return GetValue(std::string(key));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool operator == (const T& value, const Node& node) {
|
||||
return value == node.operator T();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool operator == (const Node& node, const T& value) {
|
||||
return value == node.operator T();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool operator != (const T& value, const Node& node) {
|
||||
return value != node.operator T();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool operator != (const Node& node, const T& value) {
|
||||
return value != node.operator T();
|
||||
}
|
||||
|
||||
inline bool operator == (const char *value, const Node& node) {
|
||||
return std::string(value) == node;
|
||||
}
|
||||
|
||||
inline bool operator == (const Node& node, const char *value) {
|
||||
return std::string(value) == node;
|
||||
}
|
||||
|
||||
inline bool operator != (const char *value, const Node& node) {
|
||||
return std::string(value) != node;
|
||||
}
|
||||
|
||||
inline bool operator != (const Node& node, const char *value) {
|
||||
return std::string(value) != node;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,77 @@
|
|||
#pragma once
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// implementation for Node::Read
|
||||
// (the goal is to call ConvertScalar if we can, and fall back to operator >> if not)
|
||||
// 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;
|
||||
|
||||
// ConvertScalar available
|
||||
template<>
|
||||
struct read_impl<true> {
|
||||
template<typename T>
|
||||
static bool read(const Node& node, T& value) {
|
||||
return ConvertScalar(node, value);
|
||||
}
|
||||
};
|
||||
|
||||
// ConvertScalar not available
|
||||
template<>
|
||||
struct read_impl<false> {
|
||||
template<typename T>
|
||||
static bool read(const Node& node, T& value) {
|
||||
try {
|
||||
node >> value;
|
||||
} catch(const Exception&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
namespace fallback {
|
||||
// sizeof > 1
|
||||
struct flag { char c[2]; };
|
||||
flag Convert(...);
|
||||
|
||||
int operator,(flag, flag);
|
||||
|
||||
template<typename T>
|
||||
void operator,(flag, T const&);
|
||||
|
||||
char operator,(int, flag);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool Node::Read(T& value) const {
|
||||
using namespace fallback;
|
||||
|
||||
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>
|
||||
inline bool ConvertScalar(const Node& node, T& value) {
|
||||
std::string scalar;
|
||||
if(!node.GetScalar(scalar))
|
||||
return false;
|
||||
|
||||
return Convert(scalar, value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
template <typename T, typename U>
|
||||
struct is_same_type {
|
||||
enum { value = false };
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_same_type<T, T> {
|
||||
enum { value = true };
|
||||
};
|
||||
|
||||
template <typename T, bool check>
|
||||
struct is_index_type_with_check {
|
||||
enum { value = false };
|
||||
};
|
||||
|
||||
template <> struct is_index_type_with_check<std::size_t, false> { enum { value = true }; };
|
||||
|
||||
#define MAKE_INDEX_TYPE(Type) \
|
||||
template <> struct is_index_type_with_check<Type, is_same_type<Type, std::size_t>::value> { enum { value = true }; }
|
||||
|
||||
MAKE_INDEX_TYPE(int);
|
||||
MAKE_INDEX_TYPE(unsigned);
|
||||
MAKE_INDEX_TYPE(short);
|
||||
MAKE_INDEX_TYPE(unsigned short);
|
||||
MAKE_INDEX_TYPE(long);
|
||||
MAKE_INDEX_TYPE(unsigned long);
|
||||
|
||||
#undef MAKE_INDEX_TYPE
|
||||
|
||||
template <typename T>
|
||||
struct is_index_type: public is_index_type_with_check<T, false> {};
|
||||
|
||||
// messing around with template stuff to get the right overload for operator [] for a sequence
|
||||
template <typename T, bool b>
|
||||
struct _FindFromNodeAtIndex {
|
||||
const Node *pRet;
|
||||
_FindFromNodeAtIndex(const Node&, const T&): pRet(0) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct _FindFromNodeAtIndex<T, true> {
|
||||
const Node *pRet;
|
||||
_FindFromNodeAtIndex(const Node& node, const T& key): pRet(node.FindAtIndex(static_cast<std::size_t>(key))) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline const Node *FindFromNodeAtIndex(const Node& node, const T& key) {
|
||||
return _FindFromNodeAtIndex<T, is_index_type<T>::value>(node, key).pRet;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // NODEUTIL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// this is basically boost::noncopyable
|
||||
class noncopyable
|
||||
{
|
||||
protected:
|
||||
noncopyable() {}
|
||||
~noncopyable() {}
|
||||
|
||||
private:
|
||||
noncopyable(const noncopyable&);
|
||||
const noncopyable& operator = (const noncopyable&);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NONCOPYABLE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
struct _Null {};
|
||||
inline bool operator == (const _Null&, const _Null&) { return true; }
|
||||
inline bool operator != (const _Null&, const _Null&) { return false; }
|
||||
|
||||
bool IsNull(const Node& node);
|
||||
|
||||
extern _Null Null;
|
||||
}
|
||||
|
||||
#endif // NULL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef OSTREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define OSTREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class ostream
|
||||
{
|
||||
public:
|
||||
ostream();
|
||||
~ostream();
|
||||
|
||||
void reserve(unsigned size);
|
||||
void put(char ch);
|
||||
const char *str() const { return m_buffer; }
|
||||
|
||||
unsigned row() const { return m_row; }
|
||||
unsigned col() const { return m_col; }
|
||||
unsigned pos() const { return m_pos; }
|
||||
|
||||
private:
|
||||
char *m_buffer;
|
||||
unsigned m_pos;
|
||||
unsigned m_size;
|
||||
|
||||
unsigned m_row, m_col;
|
||||
};
|
||||
|
||||
ostream& operator << (ostream& out, const char *str);
|
||||
ostream& operator << (ostream& out, const std::string& str);
|
||||
ostream& operator << (ostream& out, char ch);
|
||||
}
|
||||
|
||||
#endif // OSTREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#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 <memory>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Scanner;
|
||||
struct Token;
|
||||
|
||||
class Parser: private noncopyable
|
||||
{
|
||||
public:
|
||||
Parser();
|
||||
Parser(std::istream& in);
|
||||
~Parser();
|
||||
|
||||
operator bool() const;
|
||||
|
||||
void Load(std::istream& in);
|
||||
bool GetNextDocument(Node& document);
|
||||
void PrintTokens(std::ostream& out);
|
||||
|
||||
private:
|
||||
void ParseDirectives();
|
||||
void HandleDirective(Token *pToken);
|
||||
void HandleYamlDirective(Token *pToken);
|
||||
void HandleTagDirective(Token *pToken);
|
||||
|
||||
private:
|
||||
std::auto_ptr<Scanner> m_pScanner;
|
||||
ParserState m_state;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // PARSER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef PARSERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define PARSERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
struct Version {
|
||||
int major, minor;
|
||||
};
|
||||
|
||||
struct ParserState
|
||||
{
|
||||
Version version;
|
||||
std::map <std::string, std::string> tags;
|
||||
|
||||
void Reset();
|
||||
std::string TranslateTag(const std::string& handle) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // PARSERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
template <typename T>
|
||||
inline Emitter& operator << (Emitter& emitter, const std::vector <T>& v) {
|
||||
typedef typename std::vector <T> vec;
|
||||
emitter << BeginSeq;
|
||||
for(typename vec::const_iterator it=v.begin();it!=v.end();++it)
|
||||
emitter << *it;
|
||||
emitter << EndSeq;
|
||||
return emitter;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Emitter& operator << (Emitter& emitter, const std::list <T>& v) {
|
||||
typedef typename std::list <T> list;
|
||||
emitter << BeginSeq;
|
||||
for(typename list::const_iterator it=v.begin();it!=v.end();++it)
|
||||
emitter << *it;
|
||||
emitter << EndSeq;
|
||||
return emitter;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
inline Emitter& operator << (Emitter& emitter, const std::map <K, V>& m) {
|
||||
typedef typename std::map <K, V> map;
|
||||
emitter << BeginMap;
|
||||
for(typename map::const_iterator it=m.begin();it!=m.end();++it)
|
||||
emitter << Key << it->first << Value << it->second;
|
||||
emitter << EndMap;
|
||||
return emitter;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // STLEMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "crt.h"
|
||||
#include "parser.h"
|
||||
#include "node.h"
|
||||
#include "iterator.h"
|
||||
#include "emitter.h"
|
||||
#include "stlemitter.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
#endif // YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,24 @@
|
|||
*** With CMake ***
|
||||
|
||||
yaml-cpp uses CMake to support cross-platform building. In a UNIX-like system, the basic steps to build are:
|
||||
|
||||
1. Download and install CMake (if you don't have root privileges, just install to a local directory, like ~/bin)
|
||||
|
||||
2. From the source directory, run:
|
||||
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
|
||||
and then the usual
|
||||
|
||||
make
|
||||
make install
|
||||
|
||||
3. To clean up, just remove the 'build' directory.
|
||||
|
||||
*** Without CMake ***
|
||||
|
||||
If you don't want to use CMake, just add all .cpp files to a makefile. yaml-cpp does not need any special build settings, so no 'configure' file is necessary.
|
||||
|
||||
(Note: this is pretty tedious. It's sooo much easier to use CMake.)
|
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2008 Jesse Beder.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
|
@ -0,0 +1,94 @@
|
|||
#include "crt.h"
|
||||
#include "aliascontent.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
AliasContent::AliasContent(Content* pNodeContent)
|
||||
: m_pRef(pNodeContent)
|
||||
{
|
||||
}
|
||||
|
||||
Content *AliasContent::Clone() const
|
||||
{
|
||||
return 0; // TODO: how to clone an alias?
|
||||
}
|
||||
|
||||
void AliasContent::Parse(Scanner * /*pScanner*/, const ParserState& /*state*/)
|
||||
{
|
||||
}
|
||||
|
||||
void AliasContent::Write(Emitter&) const
|
||||
{
|
||||
// no content (just an alias)
|
||||
}
|
||||
|
||||
bool AliasContent::GetBegin(std::vector <Node *>::const_iterator& i) const
|
||||
{
|
||||
return m_pRef->GetBegin(i);
|
||||
}
|
||||
|
||||
bool AliasContent::GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& i) const
|
||||
{
|
||||
return m_pRef->GetBegin(i);
|
||||
}
|
||||
|
||||
bool AliasContent::GetEnd(std::vector <Node *>::const_iterator& i) const
|
||||
{
|
||||
return m_pRef->GetEnd(i);
|
||||
}
|
||||
|
||||
bool AliasContent::GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& i) const
|
||||
{
|
||||
return m_pRef->GetEnd(i);
|
||||
}
|
||||
|
||||
Node* AliasContent::GetNode(std::size_t n) const
|
||||
{
|
||||
return m_pRef->GetNode(n);
|
||||
}
|
||||
|
||||
std::size_t AliasContent::GetSize() const
|
||||
{
|
||||
return m_pRef->GetSize();
|
||||
}
|
||||
|
||||
bool AliasContent::IsScalar() const
|
||||
{
|
||||
return m_pRef->IsScalar();
|
||||
}
|
||||
|
||||
bool AliasContent::IsMap() const
|
||||
{
|
||||
return m_pRef->IsMap();
|
||||
}
|
||||
|
||||
bool AliasContent::IsSequence() const
|
||||
{
|
||||
return m_pRef->IsSequence();
|
||||
}
|
||||
|
||||
bool AliasContent::GetScalar(std::string& scalar) const
|
||||
{
|
||||
return m_pRef->GetScalar(scalar);
|
||||
}
|
||||
|
||||
int AliasContent::Compare(Content *pContent)
|
||||
{
|
||||
return m_pRef->Compare(pContent);
|
||||
}
|
||||
|
||||
int AliasContent::Compare(Scalar *pScalar)
|
||||
{
|
||||
return m_pRef->Compare(pScalar);
|
||||
}
|
||||
|
||||
int AliasContent::Compare(Sequence *pSequence)
|
||||
{
|
||||
return m_pRef->Compare(pSequence);
|
||||
}
|
||||
|
||||
int AliasContent::Compare(Map *pMap)
|
||||
{
|
||||
return m_pRef->Compare(pMap);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "content.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class AliasContent : public Content
|
||||
{
|
||||
public:
|
||||
AliasContent(Content *pNodeContent);
|
||||
|
||||
virtual Content *Clone() const;
|
||||
|
||||
virtual void Parse(Scanner* pScanner, const ParserState& state);
|
||||
virtual void Write(Emitter&) const;
|
||||
|
||||
virtual bool GetBegin(std::vector <Node *>::const_iterator&) const;
|
||||
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator&) const;
|
||||
virtual bool GetEnd(std::vector <Node *>::const_iterator&) const;
|
||||
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator&) const;
|
||||
virtual Node* GetNode(std::size_t) const;
|
||||
virtual std::size_t GetSize() const;
|
||||
virtual bool IsScalar() const;
|
||||
virtual bool IsMap() const;
|
||||
virtual bool IsSequence() const;
|
||||
|
||||
virtual bool GetScalar(std::string& s) const;
|
||||
|
||||
virtual int Compare(Content *);
|
||||
virtual int Compare(Scalar *);
|
||||
virtual int Compare(Sequence *);
|
||||
virtual int Compare(Map *);
|
||||
|
||||
private:
|
||||
Content* m_pRef;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ALIASCONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,13 @@
|
|||
#include "crt.h"
|
||||
#include "content.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Content::Content()
|
||||
{
|
||||
}
|
||||
|
||||
Content::~Content()
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "parserstate.h"
|
||||
#include "exceptions.h"
|
||||
#include "ltnode.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Scanner;
|
||||
class Parser;
|
||||
class Node;
|
||||
class Scalar;
|
||||
class Sequence;
|
||||
class Map;
|
||||
class Emitter;
|
||||
|
||||
class Content
|
||||
{
|
||||
public:
|
||||
Content();
|
||||
virtual ~Content();
|
||||
|
||||
virtual Content *Clone() const = 0;
|
||||
|
||||
virtual void Parse(Scanner *pScanner, const ParserState& state) = 0;
|
||||
virtual void Write(Emitter& out) const = 0;
|
||||
|
||||
virtual bool GetBegin(std::vector <Node *>::const_iterator&) const { return false; }
|
||||
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator&) const { return false; }
|
||||
virtual bool GetEnd(std::vector <Node *>::const_iterator&) const { return false; }
|
||||
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator&) const { return false; }
|
||||
virtual Node *GetNode(std::size_t) const { return 0; }
|
||||
virtual std::size_t GetSize() const { return 0; }
|
||||
virtual bool IsScalar() const { return false; }
|
||||
virtual bool IsMap() const { return false; }
|
||||
virtual bool IsSequence() const { return false; }
|
||||
|
||||
// extraction
|
||||
virtual bool GetScalar(std::string&) const { return false; }
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *) { return 0; }
|
||||
virtual int Compare(Scalar *) { return 0; }
|
||||
virtual int Compare(Sequence *) { return 0; }
|
||||
virtual int Compare(Map *) { return 0; }
|
||||
|
||||
protected:
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CONTENT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,89 @@
|
|||
#include "conversion.h"
|
||||
#include <algorithm>
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
// Specializations for converting a string to specific types
|
||||
|
||||
namespace
|
||||
{
|
||||
// we're not gonna mess with the mess that is all the isupper/etc. functions
|
||||
bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
|
||||
bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
|
||||
char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }
|
||||
|
||||
std::string tolower(const std::string& str)
|
||||
{
|
||||
std::string s(str);
|
||||
std::transform(s.begin(), s.end(), s.begin(), ToLower);
|
||||
return s;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool IsEntirely(const std::string& str, T func)
|
||||
{
|
||||
for(std::size_t i=0;i<str.size();i++)
|
||||
if(!func(str[i]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// IsFlexibleCase
|
||||
// . Returns true if 'str' is:
|
||||
// . UPPERCASE
|
||||
// . lowercase
|
||||
// . Capitalized
|
||||
bool IsFlexibleCase(const std::string& str)
|
||||
{
|
||||
if(str.empty())
|
||||
return true;
|
||||
|
||||
if(IsEntirely(str, IsLower))
|
||||
return true;
|
||||
|
||||
bool firstcaps = IsUpper(str[0]);
|
||||
std::string rest = str.substr(1);
|
||||
return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
|
||||
}
|
||||
}
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
bool Convert(const std::string& input, bool& b)
|
||||
{
|
||||
// we can't use iostream bool extraction operators as they don't
|
||||
// recognize all possible values in the table below (taken from
|
||||
// http://yaml.org/type/bool.html)
|
||||
static const struct {
|
||||
std::string truename, falsename;
|
||||
} names[] = {
|
||||
{ "y", "n" },
|
||||
{ "yes", "no" },
|
||||
{ "true", "false" },
|
||||
{ "on", "off" },
|
||||
};
|
||||
|
||||
if(!IsFlexibleCase(input))
|
||||
return false;
|
||||
|
||||
for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
|
||||
if(names[i].truename == tolower(input)) {
|
||||
b = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(names[i].falsename == tolower(input)) {
|
||||
b = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Convert(const std::string& input, _Null& /*output*/)
|
||||
{
|
||||
return input.empty() || input == "~" || input == "null" || input == "Null" || input == "NULL";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,700 @@
|
|||
#include "emitter.h"
|
||||
#include "emitterstate.h"
|
||||
#include "emitterutils.h"
|
||||
#include "indentation.h"
|
||||
#include "exceptions.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Emitter::Emitter(): m_pState(new EmitterState)
|
||||
{
|
||||
}
|
||||
|
||||
Emitter::~Emitter()
|
||||
{
|
||||
}
|
||||
|
||||
const char *Emitter::c_str() const
|
||||
{
|
||||
return m_stream.str();
|
||||
}
|
||||
|
||||
unsigned Emitter::size() const
|
||||
{
|
||||
return m_stream.pos();
|
||||
}
|
||||
|
||||
// state checking
|
||||
bool Emitter::good() const
|
||||
{
|
||||
return m_pState->good();
|
||||
}
|
||||
|
||||
const std::string Emitter::GetLastError() const
|
||||
{
|
||||
return m_pState->GetLastError();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
bool Emitter::SetBoolFormat(EMITTER_MANIP value)
|
||||
{
|
||||
bool ok = false;
|
||||
if(m_pState->SetBoolFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetBoolCaseFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetBoolLengthFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool Emitter::SetIntBase(EMITTER_MANIP value)
|
||||
{
|
||||
return m_pState->SetIntFormat(value, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetSeqFormat(EMITTER_MANIP value)
|
||||
{
|
||||
return m_pState->SetFlowType(GT_SEQ, value, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetMapFormat(EMITTER_MANIP value)
|
||||
{
|
||||
bool ok = false;
|
||||
if(m_pState->SetFlowType(GT_MAP, value, GLOBAL))
|
||||
ok = true;
|
||||
if(m_pState->SetMapKeyFormat(value, GLOBAL))
|
||||
ok = true;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool Emitter::SetIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetPreCommentIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetPreCommentIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
bool Emitter::SetPostCommentIndent(unsigned n)
|
||||
{
|
||||
return m_pState->SetPostCommentIndent(n, GLOBAL);
|
||||
}
|
||||
|
||||
// SetLocalValue
|
||||
// . Either start/end a group, or set a modifier locally
|
||||
Emitter& Emitter::SetLocalValue(EMITTER_MANIP value)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
switch(value) {
|
||||
case BeginSeq:
|
||||
EmitBeginSeq();
|
||||
break;
|
||||
case EndSeq:
|
||||
EmitEndSeq();
|
||||
break;
|
||||
case BeginMap:
|
||||
EmitBeginMap();
|
||||
break;
|
||||
case EndMap:
|
||||
EmitEndMap();
|
||||
break;
|
||||
case Key:
|
||||
EmitKey();
|
||||
break;
|
||||
case Value:
|
||||
EmitValue();
|
||||
break;
|
||||
default:
|
||||
m_pState->SetLocalValue(value);
|
||||
break;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::SetLocalIndent(const _Indent& indent)
|
||||
{
|
||||
m_pState->SetIndent(indent.value, LOCAL);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// GotoNextPreAtomicState
|
||||
// . Runs the state machine, emitting if necessary, and returns 'true' if done (i.e., ready to emit an atom)
|
||||
bool Emitter::GotoNextPreAtomicState()
|
||||
{
|
||||
if(!good())
|
||||
return true;
|
||||
|
||||
unsigned curIndent = m_pState->GetCurIndent();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
switch(curState) {
|
||||
// document-level
|
||||
case ES_WAITING_FOR_DOC:
|
||||
m_pState->SwitchState(ES_WRITING_DOC);
|
||||
return true;
|
||||
case ES_WRITING_DOC:
|
||||
return true;
|
||||
|
||||
// block sequence
|
||||
case ES_WAITING_FOR_BLOCK_SEQ_ENTRY:
|
||||
m_stream << IndentTo(curIndent) << "-";
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_SEQ_ENTRY);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_SEQ_ENTRY:
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_SEQ_ENTRY:
|
||||
m_stream << '\n';
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
|
||||
return false;
|
||||
|
||||
// flow sequence
|
||||
case ES_WAITING_FOR_FLOW_SEQ_ENTRY:
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_SEQ_ENTRY);
|
||||
return true;
|
||||
case ES_WRITING_FLOW_SEQ_ENTRY:
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_SEQ_ENTRY:
|
||||
m_stream << ',';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
|
||||
return false;
|
||||
|
||||
// block map
|
||||
case ES_WAITING_FOR_BLOCK_MAP_ENTRY:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_BLOCK_MAP_KEY:
|
||||
if(m_pState->CurrentlyInLongKey()) {
|
||||
m_stream << IndentTo(curIndent) << '?';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_MAP_KEY);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_MAP_KEY:
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_MAP_KEY:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_BLOCK_MAP_VALUE:
|
||||
if(m_pState->CurrentlyInLongKey())
|
||||
m_stream << IndentTo(curIndent);
|
||||
m_stream << ':';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_BLOCK_MAP_VALUE);
|
||||
return true;
|
||||
case ES_WRITING_BLOCK_MAP_VALUE:
|
||||
return true;
|
||||
case ES_DONE_WITH_BLOCK_MAP_VALUE:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
|
||||
// flow map
|
||||
case ES_WAITING_FOR_FLOW_MAP_ENTRY:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_FLOW_MAP_KEY:
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_MAP_KEY);
|
||||
if(m_pState->CurrentlyInLongKey()) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << '?';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
return true;
|
||||
case ES_WRITING_FLOW_MAP_KEY:
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_MAP_KEY:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_VALUE_TOKEN);
|
||||
return true;
|
||||
case ES_WAITING_FOR_FLOW_MAP_VALUE:
|
||||
m_stream << ':';
|
||||
m_pState->RequireSeparation();
|
||||
m_pState->SwitchState(ES_WRITING_FLOW_MAP_VALUE);
|
||||
return true;
|
||||
case ES_WRITING_FLOW_MAP_VALUE:
|
||||
return true;
|
||||
case ES_DONE_WITH_FLOW_MAP_VALUE:
|
||||
m_pState->SetError(ErrorMsg::EXPECTED_KEY_TOKEN);
|
||||
return true;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
assert(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
// PreAtomicWrite
|
||||
// . Depending on the emitter state, write to the stream to get it
|
||||
// in position to do an atomic write (e.g., scalar, sequence, or map)
|
||||
void Emitter::PreAtomicWrite()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
while(!GotoNextPreAtomicState())
|
||||
;
|
||||
}
|
||||
|
||||
// PostAtomicWrite
|
||||
// . Clean up
|
||||
void Emitter::PostAtomicWrite()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
switch(curState) {
|
||||
// document-level
|
||||
case ES_WRITING_DOC:
|
||||
m_pState->SwitchState(ES_DONE_WITH_DOC);
|
||||
break;
|
||||
|
||||
// block seq
|
||||
case ES_WRITING_BLOCK_SEQ_ENTRY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_SEQ_ENTRY);
|
||||
break;
|
||||
|
||||
// flow seq
|
||||
case ES_WRITING_FLOW_SEQ_ENTRY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_SEQ_ENTRY);
|
||||
break;
|
||||
|
||||
// block map
|
||||
case ES_WRITING_BLOCK_MAP_KEY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_KEY);
|
||||
break;
|
||||
case ES_WRITING_BLOCK_MAP_VALUE:
|
||||
m_pState->SwitchState(ES_DONE_WITH_BLOCK_MAP_VALUE);
|
||||
break;
|
||||
|
||||
// flow map
|
||||
case ES_WRITING_FLOW_MAP_KEY:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_KEY);
|
||||
break;
|
||||
case ES_WRITING_FLOW_MAP_VALUE:
|
||||
m_pState->SwitchState(ES_DONE_WITH_FLOW_MAP_VALUE);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
};
|
||||
|
||||
m_pState->ClearModifiedSettings();
|
||||
}
|
||||
|
||||
// EmitSeparationIfNecessary
|
||||
void Emitter::EmitSeparationIfNecessary()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->RequiresSeparation())
|
||||
m_stream << ' ';
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
|
||||
// EmitBeginSeq
|
||||
void Emitter::EmitBeginSeq()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
// must have a long key if we're emitting a sequence
|
||||
m_pState->StartLongKey();
|
||||
|
||||
PreAtomicWrite();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
EMITTER_MANIP flowType = m_pState->GetFlowType(GT_SEQ);
|
||||
if(flowType == Block) {
|
||||
if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE) {
|
||||
m_stream << "\n";
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
m_pState->PushState(ES_WAITING_FOR_BLOCK_SEQ_ENTRY);
|
||||
} else if(flowType == Flow) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << "[";
|
||||
m_pState->PushState(ES_WAITING_FOR_FLOW_SEQ_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->BeginGroup(GT_SEQ);
|
||||
}
|
||||
|
||||
// EmitEndSeq
|
||||
void Emitter::EmitEndSeq()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->GetCurGroupType() != GT_SEQ)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_END_SEQ);
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
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);
|
||||
|
||||
m_pState->PopState();
|
||||
m_pState->EndGroup(GT_SEQ);
|
||||
|
||||
PostAtomicWrite();
|
||||
}
|
||||
|
||||
// EmitBeginMap
|
||||
void Emitter::EmitBeginMap()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
// must have a long key if we're emitting a map
|
||||
m_pState->StartLongKey();
|
||||
|
||||
PreAtomicWrite();
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
EMITTER_MANIP flowType = m_pState->GetFlowType(GT_MAP);
|
||||
if(flowType == Block) {
|
||||
if(curState == ES_WRITING_BLOCK_SEQ_ENTRY || curState == ES_WRITING_BLOCK_MAP_KEY || curState == ES_WRITING_BLOCK_MAP_VALUE) {
|
||||
m_stream << "\n";
|
||||
m_pState->UnsetSeparation();
|
||||
}
|
||||
m_pState->PushState(ES_WAITING_FOR_BLOCK_MAP_ENTRY);
|
||||
} else if(flowType == Flow) {
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << "{";
|
||||
m_pState->PushState(ES_WAITING_FOR_FLOW_MAP_ENTRY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
m_pState->BeginGroup(GT_MAP);
|
||||
}
|
||||
|
||||
// EmitEndMap
|
||||
void Emitter::EmitEndMap()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
if(m_pState->GetCurGroupType() != GT_MAP)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_END_MAP);
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
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);
|
||||
|
||||
m_pState->PopState();
|
||||
m_pState->EndGroup(GT_MAP);
|
||||
|
||||
PostAtomicWrite();
|
||||
}
|
||||
|
||||
// EmitKey
|
||||
void Emitter::EmitKey()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(curState != ES_WAITING_FOR_BLOCK_MAP_ENTRY && curState != ES_DONE_WITH_BLOCK_MAP_VALUE
|
||||
&& curState != ES_WAITING_FOR_FLOW_MAP_ENTRY && curState != ES_DONE_WITH_FLOW_MAP_VALUE)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_KEY_TOKEN);
|
||||
|
||||
if(flowType == FT_BLOCK) {
|
||||
if(curState == ES_DONE_WITH_BLOCK_MAP_VALUE)
|
||||
m_stream << '\n';
|
||||
unsigned curIndent = m_pState->GetCurIndent();
|
||||
m_stream << IndentTo(curIndent);
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_KEY);
|
||||
} else if(flowType == FT_FLOW) {
|
||||
if(curState == ES_DONE_WITH_FLOW_MAP_VALUE) {
|
||||
m_stream << ',';
|
||||
m_pState->RequireSeparation();
|
||||
}
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_KEY);
|
||||
} else
|
||||
assert(false);
|
||||
|
||||
if(m_pState->GetMapKeyFormat() == LongKey)
|
||||
m_pState->StartLongKey();
|
||||
else if(m_pState->GetMapKeyFormat() == Auto)
|
||||
m_pState->StartSimpleKey();
|
||||
else
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// EmitValue
|
||||
void Emitter::EmitValue()
|
||||
{
|
||||
if(!good())
|
||||
return;
|
||||
|
||||
EMITTER_STATE curState = m_pState->GetCurState();
|
||||
FLOW_TYPE flowType = m_pState->GetCurGroupFlowType();
|
||||
if(curState != ES_DONE_WITH_BLOCK_MAP_KEY && curState != ES_DONE_WITH_FLOW_MAP_KEY)
|
||||
return m_pState->SetError(ErrorMsg::UNEXPECTED_VALUE_TOKEN);
|
||||
|
||||
if(flowType == FT_BLOCK) {
|
||||
if(m_pState->CurrentlyInLongKey())
|
||||
m_stream << '\n';
|
||||
m_pState->SwitchState(ES_WAITING_FOR_BLOCK_MAP_VALUE);
|
||||
} else if(flowType == FT_FLOW) {
|
||||
m_pState->SwitchState(ES_WAITING_FOR_FLOW_MAP_VALUE);
|
||||
} else
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// *******************************************************************************************
|
||||
// overloads of Write
|
||||
|
||||
Emitter& Emitter::Write(const std::string& str)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
// literal scalars must use long keys
|
||||
if(m_pState->GetStringFormat() == Literal && m_pState->GetCurGroupFlowType() != FT_FLOW)
|
||||
m_pState->StartLongKey();
|
||||
|
||||
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, escapeNonAscii);
|
||||
break;
|
||||
case SingleQuoted:
|
||||
if(!Utils::WriteSingleQuotedString(m_stream, str)) {
|
||||
m_pState->SetError(ErrorMsg::SINGLE_QUOTED_CHAR);
|
||||
return *this;
|
||||
}
|
||||
break;
|
||||
case DoubleQuoted:
|
||||
Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii);
|
||||
break;
|
||||
case Literal:
|
||||
if(flowType == FT_FLOW)
|
||||
Utils::WriteString(m_stream, str, flowType == FT_FLOW, escapeNonAscii);
|
||||
else
|
||||
Utils::WriteLiteralString(m_stream, str, curIndent + m_pState->GetIndent());
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const char *str)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
return Write(std::string(str));
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(int i)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
EMITTER_MANIP intFmt = m_pState->GetIntFormat();
|
||||
std::stringstream str;
|
||||
switch(intFmt) {
|
||||
case Dec:
|
||||
str << std::dec;
|
||||
break;
|
||||
case Hex:
|
||||
str << std::hex;
|
||||
break;
|
||||
case Oct:
|
||||
str << std::oct;
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
str << i;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(bool b)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
// set up all possible bools to write
|
||||
struct BoolName { std::string trueName, falseName; };
|
||||
struct BoolFormatNames { BoolName upper, lower, camel; };
|
||||
struct BoolTypes { BoolFormatNames yesNo, trueFalse, onOff; };
|
||||
|
||||
static const BoolTypes boolTypes = {
|
||||
{ { "YES", "NO" }, { "yes", "no" }, { "Yes", "No" } },
|
||||
{ { "TRUE", "FALSE" }, { "true", "false" }, { "True", "False" } },
|
||||
{ { "ON", "OFF" }, { "on", "off" }, { "On", "Off" } }
|
||||
};
|
||||
|
||||
// select the right one
|
||||
EMITTER_MANIP boolFmt = m_pState->GetBoolFormat();
|
||||
EMITTER_MANIP boolLengthFmt = m_pState->GetBoolLengthFormat();
|
||||
EMITTER_MANIP boolCaseFmt = m_pState->GetBoolCaseFormat();
|
||||
|
||||
const BoolFormatNames& fmtNames = (boolFmt == YesNoBool ? boolTypes.yesNo : boolFmt == TrueFalseBool ? boolTypes.trueFalse : boolTypes.onOff);
|
||||
const BoolName& boolName = (boolCaseFmt == UpperCase ? fmtNames.upper : boolCaseFmt == LowerCase ? fmtNames.lower : fmtNames.camel);
|
||||
const std::string& name = (b ? boolName.trueName : boolName.falseName);
|
||||
|
||||
// and say it!
|
||||
// TODO: should we disallow writing OnOffBool with ShortBool? (it'll just print "o" for both, which is silly)
|
||||
if(boolLengthFmt == ShortBool)
|
||||
m_stream << name[0];
|
||||
else
|
||||
m_stream << name;
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(float f)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
std::stringstream str;
|
||||
str << f;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(double d)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
|
||||
std::stringstream str;
|
||||
str << d;
|
||||
m_stream << str.str();
|
||||
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Alias& alias)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
if(!Utils::WriteAlias(m_stream, alias.content)) {
|
||||
m_pState->SetError(ErrorMsg::INVALID_ALIAS);
|
||||
return *this;
|
||||
}
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Anchor& anchor)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
if(!Utils::WriteAnchor(m_stream, anchor.content)) {
|
||||
m_pState->SetError(ErrorMsg::INVALID_ANCHOR);
|
||||
return *this;
|
||||
}
|
||||
m_pState->RequireSeparation();
|
||||
// Note: no PostAtomicWrite() because we need another value for this node
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Comment& comment)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
m_stream << Indentation(m_pState->GetPreCommentIndent());
|
||||
Utils::WriteComment(m_stream, comment.content, m_pState->GetPostCommentIndent());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Null& /*null*/)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
m_stream << "~";
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,277 @@
|
|||
#include "emitterstate.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_requiresSeparation(false)
|
||||
{
|
||||
// start up
|
||||
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);
|
||||
m_boolCaseFmt.set(LowerCase);
|
||||
m_intFmt.set(Dec);
|
||||
m_indent.set(2);
|
||||
m_preCommentIndent.set(2);
|
||||
m_postCommentIndent.set(1);
|
||||
m_seqFmt.set(Block);
|
||||
m_mapFmt.set(Block);
|
||||
m_mapKeyFmt.set(Auto);
|
||||
}
|
||||
|
||||
EmitterState::~EmitterState()
|
||||
{
|
||||
while(!m_groups.empty())
|
||||
_PopGroup();
|
||||
}
|
||||
|
||||
std::auto_ptr <EmitterState::Group> EmitterState::_PopGroup()
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return std::auto_ptr <Group> (0);
|
||||
|
||||
std::auto_ptr <Group> pGroup(m_groups.top());
|
||||
m_groups.pop();
|
||||
return pGroup;
|
||||
}
|
||||
|
||||
// SetLocalValue
|
||||
// . We blindly tries to set all possible formatters to this value
|
||||
// . 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);
|
||||
SetBoolLengthFormat(value, LOCAL);
|
||||
SetIntFormat(value, LOCAL);
|
||||
SetFlowType(GT_SEQ, value, LOCAL);
|
||||
SetFlowType(GT_MAP, value, LOCAL);
|
||||
SetMapKeyFormat(value, LOCAL);
|
||||
}
|
||||
|
||||
void EmitterState::BeginGroup(GROUP_TYPE type)
|
||||
{
|
||||
unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top()->indent);
|
||||
m_curIndent += lastIndent;
|
||||
|
||||
std::auto_ptr <Group> pGroup(new Group(type));
|
||||
|
||||
// transfer settings (which last until this group is done)
|
||||
pGroup->modifiedSettings = m_modifiedSettings;
|
||||
|
||||
// set up group
|
||||
pGroup->flow = GetFlowType(type);
|
||||
pGroup->indent = GetIndent();
|
||||
pGroup->usingLongKey = (GetMapKeyFormat() == LongKey ? true : false);
|
||||
|
||||
m_groups.push(pGroup.release());
|
||||
}
|
||||
|
||||
void EmitterState::EndGroup(GROUP_TYPE type)
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
|
||||
|
||||
// get rid of the current group
|
||||
{
|
||||
std::auto_ptr <Group> pFinishedGroup = _PopGroup();
|
||||
if(pFinishedGroup->type != type)
|
||||
return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
|
||||
}
|
||||
|
||||
// reset old settings
|
||||
unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top()->indent);
|
||||
assert(m_curIndent >= lastIndent);
|
||||
m_curIndent -= lastIndent;
|
||||
|
||||
// some global settings that we changed may have been overridden
|
||||
// by a local setting we just popped, so we need to restore them
|
||||
m_globalModifiedSettings.restore();
|
||||
}
|
||||
|
||||
GROUP_TYPE EmitterState::GetCurGroupType() const
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return GT_NONE;
|
||||
|
||||
return m_groups.top()->type;
|
||||
}
|
||||
|
||||
FLOW_TYPE EmitterState::GetCurGroupFlowType() const
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return FT_NONE;
|
||||
|
||||
return (m_groups.top()->flow == Flow ? FT_FLOW : FT_BLOCK);
|
||||
}
|
||||
|
||||
bool EmitterState::CurrentlyInLongKey()
|
||||
{
|
||||
if(m_groups.empty())
|
||||
return false;
|
||||
return m_groups.top()->usingLongKey;
|
||||
}
|
||||
|
||||
void EmitterState::StartLongKey()
|
||||
{
|
||||
if(!m_groups.empty())
|
||||
m_groups.top()->usingLongKey = true;
|
||||
}
|
||||
|
||||
void EmitterState::StartSimpleKey()
|
||||
{
|
||||
if(!m_groups.empty())
|
||||
m_groups.top()->usingLongKey = false;
|
||||
}
|
||||
|
||||
void EmitterState::ClearModifiedSettings()
|
||||
{
|
||||
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) {
|
||||
case Auto:
|
||||
case SingleQuoted:
|
||||
case DoubleQuoted:
|
||||
case Literal:
|
||||
_Set(m_strFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case OnOffBool:
|
||||
case TrueFalseBool:
|
||||
case YesNoBool:
|
||||
_Set(m_boolFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case LongBool:
|
||||
case ShortBool:
|
||||
_Set(m_boolLengthFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case UpperCase:
|
||||
case LowerCase:
|
||||
case CamelCase:
|
||||
_Set(m_boolCaseFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Dec:
|
||||
case Hex:
|
||||
case Oct:
|
||||
_Set(m_intFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool EmitterState::SetIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_indent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetPreCommentIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_preCommentIndent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetPostCommentIndent(unsigned value, FMT_SCOPE scope)
|
||||
{
|
||||
if(value == 0)
|
||||
return false;
|
||||
|
||||
_Set(m_postCommentIndent, value, scope);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EmitterState::SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Block:
|
||||
case Flow:
|
||||
_Set(groupType == GT_SEQ ? m_seqFmt : m_mapFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
EMITTER_MANIP EmitterState::GetFlowType(GROUP_TYPE groupType) const
|
||||
{
|
||||
// force flow style if we're currently in a flow
|
||||
FLOW_TYPE flowType = GetCurGroupFlowType();
|
||||
if(flowType == FT_FLOW)
|
||||
return Flow;
|
||||
|
||||
// otherwise, go with what's asked of use
|
||||
return (groupType == GT_SEQ ? m_seqFmt.get() : m_mapFmt.get());
|
||||
}
|
||||
|
||||
bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope)
|
||||
{
|
||||
switch(value) {
|
||||
case Auto:
|
||||
case LongKey:
|
||||
_Set(m_mapKeyFmt, value, scope);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,205 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "setting.h"
|
||||
#include "emittermanip.h"
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
enum FMT_SCOPE {
|
||||
LOCAL,
|
||||
GLOBAL
|
||||
};
|
||||
|
||||
enum GROUP_TYPE {
|
||||
GT_NONE,
|
||||
GT_SEQ,
|
||||
GT_MAP
|
||||
};
|
||||
|
||||
enum FLOW_TYPE {
|
||||
FT_NONE,
|
||||
FT_FLOW,
|
||||
FT_BLOCK
|
||||
};
|
||||
|
||||
enum NODE_STATE {
|
||||
NS_START,
|
||||
NS_READY_FOR_ATOM,
|
||||
NS_END
|
||||
};
|
||||
|
||||
enum EMITTER_STATE {
|
||||
ES_WAITING_FOR_DOC,
|
||||
ES_WRITING_DOC,
|
||||
ES_DONE_WITH_DOC,
|
||||
|
||||
// block seq
|
||||
ES_WAITING_FOR_BLOCK_SEQ_ENTRY,
|
||||
ES_WRITING_BLOCK_SEQ_ENTRY,
|
||||
ES_DONE_WITH_BLOCK_SEQ_ENTRY,
|
||||
|
||||
// flow seq
|
||||
ES_WAITING_FOR_FLOW_SEQ_ENTRY,
|
||||
ES_WRITING_FLOW_SEQ_ENTRY,
|
||||
ES_DONE_WITH_FLOW_SEQ_ENTRY,
|
||||
|
||||
// block map
|
||||
ES_WAITING_FOR_BLOCK_MAP_ENTRY,
|
||||
ES_WAITING_FOR_BLOCK_MAP_KEY,
|
||||
ES_WRITING_BLOCK_MAP_KEY,
|
||||
ES_DONE_WITH_BLOCK_MAP_KEY,
|
||||
ES_WAITING_FOR_BLOCK_MAP_VALUE,
|
||||
ES_WRITING_BLOCK_MAP_VALUE,
|
||||
ES_DONE_WITH_BLOCK_MAP_VALUE,
|
||||
|
||||
// flow map
|
||||
ES_WAITING_FOR_FLOW_MAP_ENTRY,
|
||||
ES_WAITING_FOR_FLOW_MAP_KEY,
|
||||
ES_WRITING_FLOW_MAP_KEY,
|
||||
ES_DONE_WITH_FLOW_MAP_KEY,
|
||||
ES_WAITING_FOR_FLOW_MAP_VALUE,
|
||||
ES_WRITING_FLOW_MAP_VALUE,
|
||||
ES_DONE_WITH_FLOW_MAP_VALUE
|
||||
};
|
||||
|
||||
class EmitterState
|
||||
{
|
||||
public:
|
||||
EmitterState();
|
||||
~EmitterState();
|
||||
|
||||
// basic state checking
|
||||
bool good() const { return m_isGood; }
|
||||
const std::string GetLastError() const { return m_lastError; }
|
||||
void SetError(const std::string& error) { m_isGood = false; m_lastError = error; }
|
||||
|
||||
// main state of the machine
|
||||
EMITTER_STATE GetCurState() const { return m_stateStack.top(); }
|
||||
void SwitchState(EMITTER_STATE state) { PopState(); PushState(state); }
|
||||
void PushState(EMITTER_STATE state) { m_stateStack.push(state); }
|
||||
void PopState() { m_stateStack.pop(); }
|
||||
|
||||
void SetLocalValue(EMITTER_MANIP value);
|
||||
|
||||
// group handling
|
||||
void BeginGroup(GROUP_TYPE type);
|
||||
void EndGroup(GROUP_TYPE type);
|
||||
|
||||
GROUP_TYPE GetCurGroupType() const;
|
||||
FLOW_TYPE GetCurGroupFlowType() const;
|
||||
int GetCurIndent() const { return m_curIndent; }
|
||||
|
||||
bool CurrentlyInLongKey();
|
||||
void StartLongKey();
|
||||
void StartSimpleKey();
|
||||
|
||||
bool RequiresSeparation() const { return m_requiresSeparation; }
|
||||
void RequireSeparation() { m_requiresSeparation = true; }
|
||||
void UnsetSeparation() { m_requiresSeparation = false; }
|
||||
|
||||
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(); }
|
||||
|
||||
bool SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); }
|
||||
|
||||
bool SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); }
|
||||
|
||||
bool SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }
|
||||
|
||||
bool SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }
|
||||
|
||||
bool SetIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetIndent() const { return m_indent.get(); }
|
||||
|
||||
bool SetPreCommentIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetPreCommentIndent() const { return m_preCommentIndent.get(); }
|
||||
bool SetPostCommentIndent(unsigned value, FMT_SCOPE scope);
|
||||
int GetPostCommentIndent() const { return m_postCommentIndent.get(); }
|
||||
|
||||
bool SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetFlowType(GROUP_TYPE groupType) const;
|
||||
|
||||
bool SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope);
|
||||
EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void _Set(Setting<T>& fmt, T value, FMT_SCOPE scope);
|
||||
|
||||
private:
|
||||
// basic state ok?
|
||||
bool m_isGood;
|
||||
std::string m_lastError;
|
||||
|
||||
// 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;
|
||||
Setting <EMITTER_MANIP> m_boolCaseFmt;
|
||||
Setting <EMITTER_MANIP> m_intFmt;
|
||||
Setting <unsigned> m_indent;
|
||||
Setting <unsigned> m_preCommentIndent, m_postCommentIndent;
|
||||
Setting <EMITTER_MANIP> m_seqFmt;
|
||||
Setting <EMITTER_MANIP> m_mapFmt;
|
||||
Setting <EMITTER_MANIP> m_mapKeyFmt;
|
||||
|
||||
SettingChanges m_modifiedSettings;
|
||||
SettingChanges m_globalModifiedSettings;
|
||||
|
||||
struct Group {
|
||||
Group(GROUP_TYPE type_): type(type_), usingLongKey(false), indent(0) {}
|
||||
|
||||
GROUP_TYPE type;
|
||||
EMITTER_MANIP flow;
|
||||
bool usingLongKey;
|
||||
int indent;
|
||||
|
||||
SettingChanges modifiedSettings;
|
||||
};
|
||||
|
||||
std::auto_ptr <Group> _PopGroup();
|
||||
|
||||
std::stack <Group *> m_groups;
|
||||
unsigned m_curIndent;
|
||||
bool m_requiresSeparation;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void EmitterState::_Set(Setting<T>& fmt, T value, FMT_SCOPE scope) {
|
||||
switch(scope) {
|
||||
case LOCAL:
|
||||
m_modifiedSettings.push(fmt.set(value));
|
||||
break;
|
||||
case GLOBAL:
|
||||
fmt.set(value);
|
||||
m_globalModifiedSettings.push(fmt.set(value)); // this pushes an identity set, so when we restore,
|
||||
// it restores to the value here, and not the previous one
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,298 @@
|
|||
#include "emitterutils.h"
|
||||
#include "exp.h"
|
||||
#include "indentation.h"
|
||||
#include "exceptions.h"
|
||||
#include "stringsource.h"
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
namespace {
|
||||
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;
|
||||
}
|
||||
|
||||
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))
|
||||
return false;
|
||||
|
||||
// and check the end for plain whitespace (which can't be faithfully kept in a plain scalar)
|
||||
if(!str.empty() && *str.rbegin() == ' ')
|
||||
return false;
|
||||
|
||||
// then check until something is disallowed
|
||||
const RegEx& disallowed = (inFlow ? Exp::EndScalarInFlow : Exp::EndScalar)
|
||||
|| (Exp::BlankOrBreak + Exp::Comment)
|
||||
|| 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 escapeNonAscii)
|
||||
{
|
||||
if(IsValidPlainScalar(str, inFlow, escapeNonAscii)) {
|
||||
out << str;
|
||||
return true;
|
||||
} else
|
||||
return WriteDoubleQuotedString(out, str, escapeNonAscii);
|
||||
}
|
||||
|
||||
bool WriteSingleQuotedString(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "'";
|
||||
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 (codePoint == '\'')
|
||||
out << "''";
|
||||
else
|
||||
WriteCodePoint(out, codePoint);
|
||||
}
|
||||
out << "'";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteDoubleQuotedString(ostream& out, const std::string& str, bool escapeNonAscii)
|
||||
{
|
||||
out << "\"";
|
||||
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;
|
||||
}
|
||||
|
||||
bool WriteLiteralString(ostream& out, const std::string& str, int indent)
|
||||
{
|
||||
out << "|\n";
|
||||
out << 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
|
||||
WriteCodePoint(out, codePoint);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteComment(ostream& out, const std::string& str, int postCommentIndent)
|
||||
{
|
||||
unsigned curIndent = out.col();
|
||||
out << "#" << Indentation(postCommentIndent);
|
||||
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
|
||||
WriteCodePoint(out, codePoint);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteAlias(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "*";
|
||||
return WriteAliasName(out, str);
|
||||
}
|
||||
|
||||
bool WriteAnchor(ostream& out, const std::string& str)
|
||||
{
|
||||
out << "&";
|
||||
return WriteAliasName(out, str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "ostream.h"
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
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 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);
|
||||
bool WriteAnchor(ostream& out, const std::string& str);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EMITTERUTILS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,114 @@
|
|||
#include "crt.h"
|
||||
#include "exp.h"
|
||||
#include "exceptions.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
namespace Exp
|
||||
{
|
||||
unsigned ParseHex(const std::string& str, const Mark& mark)
|
||||
{
|
||||
unsigned value = 0;
|
||||
for(std::size_t i=0;i<str.size();i++) {
|
||||
char ch = str[i];
|
||||
int digit = 0;
|
||||
if('a' <= ch && ch <= 'f')
|
||||
digit = ch - 'a' + 10;
|
||||
else if('A' <= ch && ch <= 'F')
|
||||
digit = ch - 'A' + 10;
|
||||
else if('0' <= ch && ch <= '9')
|
||||
digit = ch - '0';
|
||||
else
|
||||
throw ParserException(mark, ErrorMsg::INVALID_HEX);
|
||||
|
||||
value = (value << 4) + digit;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string Str(unsigned ch)
|
||||
{
|
||||
return std::string(1, static_cast<char>(ch));
|
||||
}
|
||||
|
||||
// Escape
|
||||
// . Translates the next 'codeLength' characters into a hex number and returns the result.
|
||||
// . Throws if it's not actually hex.
|
||||
std::string Escape(Stream& in, int codeLength)
|
||||
{
|
||||
// grab string
|
||||
std::string str;
|
||||
for(int i=0;i<codeLength;i++)
|
||||
str += in.get();
|
||||
|
||||
// get the value
|
||||
unsigned value = ParseHex(str, in.mark());
|
||||
|
||||
// legal unicode?
|
||||
if((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
|
||||
std::stringstream msg;
|
||||
msg << ErrorMsg::INVALID_UNICODE << value;
|
||||
throw ParserException(in.mark(), msg.str());
|
||||
}
|
||||
|
||||
// now break it up into chars
|
||||
if(value <= 0x7F)
|
||||
return Str(value);
|
||||
else if(value <= 0x7FF)
|
||||
return Str(0xC0 + (value >> 6)) + Str(0x80 + (value & 0x3F));
|
||||
else if(value <= 0xFFFF)
|
||||
return Str(0xE0 + (value >> 12)) + Str(0x80 + ((value >> 6) & 0x3F)) + Str(0x80 + (value & 0x3F));
|
||||
else
|
||||
return Str(0xF0 + (value >> 18)) + Str(0x80 + ((value >> 12) & 0x3F)) +
|
||||
Str(0x80 + ((value >> 6) & 0x3F)) + Str(0x80 + (value & 0x3F));
|
||||
}
|
||||
|
||||
// Escape
|
||||
// . Escapes the sequence starting 'in' (it must begin with a '\' or single quote)
|
||||
// and returns the result.
|
||||
// . Throws if it's an unknown escape character.
|
||||
std::string Escape(Stream& in)
|
||||
{
|
||||
// eat slash
|
||||
char escape = in.get();
|
||||
|
||||
// switch on escape character
|
||||
char ch = in.get();
|
||||
|
||||
// first do single quote, since it's easier
|
||||
if(escape == '\'' && ch == '\'')
|
||||
return "\'";
|
||||
|
||||
// now do the slash (we're not gonna check if it's a slash - you better pass one!)
|
||||
switch(ch) {
|
||||
case '0': return std::string(1, '\x00');
|
||||
case 'a': return "\x07";
|
||||
case 'b': return "\x08";
|
||||
case 't':
|
||||
case '\t': return "\x09";
|
||||
case 'n': return "\x0A";
|
||||
case 'v': return "\x0B";
|
||||
case 'f': return "\x0C";
|
||||
case 'r': return "\x0D";
|
||||
case 'e': return "\x1B";
|
||||
case ' ': return "\x20";
|
||||
case '\"': return "\"";
|
||||
case '\'': return "\'";
|
||||
case '\\': return "\\";
|
||||
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);
|
||||
case 'u': return Escape(in, 4);
|
||||
case 'U': return Escape(in, 8);
|
||||
}
|
||||
|
||||
std::stringstream msg;
|
||||
throw ParserException(in.mark(), ErrorMsg::INVALID_ESCAPE + ch);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "regex.h"
|
||||
#include <string>
|
||||
#include <ios>
|
||||
#include "stream.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Here we store a bunch of expressions for matching different parts of the file.
|
||||
|
||||
namespace Exp
|
||||
{
|
||||
// misc
|
||||
const RegEx Space = RegEx(' ');
|
||||
const RegEx Tab = RegEx('\t');
|
||||
const RegEx Blank = Space || Tab;
|
||||
const RegEx Break = RegEx('\n') || RegEx("\r\n");
|
||||
const RegEx BlankOrBreak = Blank || Break;
|
||||
const RegEx Digit = RegEx('0', '9');
|
||||
const RegEx Alpha = RegEx('a', 'z') || RegEx('A', 'Z');
|
||||
const RegEx AlphaNumeric = Alpha || Digit;
|
||||
const RegEx Hex = Digit || RegEx('A', 'F') || RegEx('a', 'f');
|
||||
// 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
|
||||
|
||||
const RegEx DocStart = RegEx("---") + (BlankOrBreak || RegEx());
|
||||
const RegEx DocEnd = RegEx("...") + (BlankOrBreak || RegEx());
|
||||
const RegEx DocIndicator = DocStart || DocEnd;
|
||||
const RegEx BlockEntry = RegEx('-') + (BlankOrBreak || RegEx());
|
||||
const RegEx Key = RegEx('?'),
|
||||
KeyInFlow = RegEx('?') + BlankOrBreak;
|
||||
const RegEx Value = RegEx(':') + (BlankOrBreak || RegEx()),
|
||||
ValueInFlow = RegEx(':') + (BlankOrBreak || RegEx(",}", REGEX_OR));
|
||||
const RegEx Comment = RegEx('#');
|
||||
const RegEx AnchorEnd = RegEx("?:,]}%@`", REGEX_OR) || BlankOrBreak;
|
||||
|
||||
// Plain scalar rules:
|
||||
// . Cannot start with a blank.
|
||||
// . Can never start with any of , [ ] { } # & * ! | > \' \" % @ `
|
||||
// . In the block context - ? : must be not be followed with a space.
|
||||
// . In the flow context ? is illegal and : and - must not be followed with a space.
|
||||
const RegEx PlainScalar = !(BlankOrBreak || RegEx(",[]{}#&*!|>\'\"%@`", REGEX_OR) || (RegEx("-?:", REGEX_OR) + Blank)),
|
||||
PlainScalarInFlow = !(BlankOrBreak || RegEx("?,[]{}#&*!|>\'\"%@`", REGEX_OR) || (RegEx("-:", REGEX_OR) + Blank));
|
||||
const RegEx EndScalar = RegEx(':') + (BlankOrBreak || RegEx()),
|
||||
EndScalarInFlow = (RegEx(':') + (BlankOrBreak || RegEx(",]}", REGEX_OR))) || RegEx(",?[]{}", REGEX_OR);
|
||||
|
||||
const RegEx EscSingleQuote = RegEx("\'\'");
|
||||
const RegEx EscBreak = RegEx('\\') + Break;
|
||||
|
||||
const RegEx ChompIndicator = RegEx("+-", REGEX_OR);
|
||||
const RegEx Chomp = (ChompIndicator + Digit) || (Digit + ChompIndicator) || ChompIndicator || Digit;
|
||||
|
||||
// and some functions
|
||||
std::string Escape(Stream& in);
|
||||
}
|
||||
|
||||
namespace Keys
|
||||
{
|
||||
const char Directive = '%';
|
||||
const char FlowSeqStart = '[';
|
||||
const char FlowSeqEnd = ']';
|
||||
const char FlowMapStart = '{';
|
||||
const char FlowMapEnd = '}';
|
||||
const char FlowEntry = ',';
|
||||
const char Alias = '*';
|
||||
const char Anchor = '&';
|
||||
const char Tag = '!';
|
||||
const char LiteralScalar = '|';
|
||||
const char FoldedScalar = '>';
|
||||
}
|
||||
}
|
||||
|
||||
#endif // EXP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef INDENTATION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define INDENTATION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "ostream.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
struct Indentation {
|
||||
Indentation(unsigned n_): n(n_) {}
|
||||
unsigned n;
|
||||
};
|
||||
|
||||
inline ostream& operator << (ostream& out, const Indentation& indent) {
|
||||
for(unsigned i=0;i<indent.n;i++)
|
||||
out << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
struct IndentTo {
|
||||
IndentTo(unsigned n_): n(n_) {}
|
||||
unsigned n;
|
||||
};
|
||||
|
||||
inline ostream& operator << (ostream& out, const IndentTo& indent) {
|
||||
while(out.col() < indent.n)
|
||||
out << ' ';
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif // INDENTATION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,108 @@
|
|||
#include "crt.h"
|
||||
#include "node.h"
|
||||
#include "exceptions.h"
|
||||
#include "iterpriv.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Iterator::Iterator(): m_pData(0)
|
||||
{
|
||||
m_pData = new IterPriv;
|
||||
}
|
||||
|
||||
Iterator::Iterator(IterPriv *pData): m_pData(pData)
|
||||
{
|
||||
}
|
||||
|
||||
Iterator::Iterator(const Iterator& rhs): m_pData(0)
|
||||
{
|
||||
m_pData = new IterPriv(*rhs.m_pData);
|
||||
}
|
||||
|
||||
Iterator& Iterator::operator = (const Iterator& rhs)
|
||||
{
|
||||
if(this == &rhs)
|
||||
return *this;
|
||||
|
||||
delete m_pData;
|
||||
m_pData = new IterPriv(*rhs.m_pData);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Iterator::~Iterator()
|
||||
{
|
||||
delete m_pData;
|
||||
}
|
||||
|
||||
Iterator& Iterator::operator ++ ()
|
||||
{
|
||||
if(m_pData->type == IterPriv::IT_SEQ)
|
||||
++m_pData->seqIter;
|
||||
else if(m_pData->type == IterPriv::IT_MAP)
|
||||
++m_pData->mapIter;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Iterator Iterator::operator ++ (int)
|
||||
{
|
||||
Iterator temp = *this;
|
||||
|
||||
if(m_pData->type == IterPriv::IT_SEQ)
|
||||
++m_pData->seqIter;
|
||||
else if(m_pData->type == IterPriv::IT_MAP)
|
||||
++m_pData->mapIter;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
const Node& Iterator::operator * () const
|
||||
{
|
||||
if(m_pData->type == IterPriv::IT_SEQ)
|
||||
return **m_pData->seqIter;
|
||||
|
||||
throw BadDereference();
|
||||
}
|
||||
|
||||
const Node *Iterator::operator -> () const
|
||||
{
|
||||
if(m_pData->type == IterPriv::IT_SEQ)
|
||||
return *m_pData->seqIter;
|
||||
|
||||
throw BadDereference();
|
||||
}
|
||||
|
||||
const Node& Iterator::first() const
|
||||
{
|
||||
if(m_pData->type == IterPriv::IT_MAP)
|
||||
return *m_pData->mapIter->first;
|
||||
|
||||
throw BadDereference();
|
||||
}
|
||||
|
||||
const Node& Iterator::second() const
|
||||
{
|
||||
if(m_pData->type == IterPriv::IT_MAP)
|
||||
return *m_pData->mapIter->second;
|
||||
|
||||
throw BadDereference();
|
||||
}
|
||||
|
||||
bool operator == (const Iterator& it, const Iterator& jt)
|
||||
{
|
||||
if(it.m_pData->type != jt.m_pData->type)
|
||||
return false;
|
||||
|
||||
if(it.m_pData->type == IterPriv::IT_SEQ)
|
||||
return it.m_pData->seqIter == jt.m_pData->seqIter;
|
||||
else if(it.m_pData->type == IterPriv::IT_MAP)
|
||||
return it.m_pData->mapIter == jt.m_pData->mapIter;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator != (const Iterator& it, const Iterator& jt)
|
||||
{
|
||||
return !(it == jt);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef ITERPRIV_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define ITERPRIV_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "ltnode.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
// IterPriv
|
||||
// . The implementation for iterators - essentially a union of sequence and map iterators.
|
||||
struct IterPriv
|
||||
{
|
||||
IterPriv(): type(IT_NONE) {}
|
||||
IterPriv(std::vector <Node *>::const_iterator it): type(IT_SEQ), seqIter(it) {}
|
||||
IterPriv(std::map <Node *, Node *, ltnode>::const_iterator it): type(IT_MAP), mapIter(it) {}
|
||||
|
||||
enum ITER_TYPE { IT_NONE, IT_SEQ, IT_MAP };
|
||||
ITER_TYPE type;
|
||||
|
||||
std::vector <Node *>::const_iterator seqIter;
|
||||
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ITERPRIV_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef LTNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define LTNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
struct ltnode {
|
||||
bool operator()(const Node *pNode1, const Node *pNode2) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LTNODE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,189 @@
|
|||
#include "crt.h"
|
||||
#include "map.h"
|
||||
#include "node.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include "emitter.h"
|
||||
#include <memory>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Map::Map()
|
||||
{
|
||||
}
|
||||
|
||||
Map::Map(const node_map& data)
|
||||
{
|
||||
for(node_map::const_iterator it=data.begin();it!=data.end();++it) {
|
||||
std::auto_ptr<Node> pKey = it->first->Clone();
|
||||
std::auto_ptr<Node> pValue = it->second->Clone();
|
||||
m_data[pKey.release()] = pValue.release();
|
||||
}
|
||||
}
|
||||
|
||||
Map::~Map()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void Map::Clear()
|
||||
{
|
||||
for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
|
||||
delete it->first;
|
||||
delete it->second;
|
||||
}
|
||||
m_data.clear();
|
||||
}
|
||||
|
||||
Content *Map::Clone() const
|
||||
{
|
||||
return new Map(m_data);
|
||||
}
|
||||
|
||||
bool Map::GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const
|
||||
{
|
||||
it = m_data.begin();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Map::GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const
|
||||
{
|
||||
it = m_data.end();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t Map::GetSize() const
|
||||
{
|
||||
return m_data.size();
|
||||
}
|
||||
|
||||
void Map::Parse(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
Clear();
|
||||
|
||||
// split based on start token
|
||||
switch(pScanner->peek().type) {
|
||||
case Token::BLOCK_MAP_START: ParseBlock(pScanner, state); break;
|
||||
case Token::FLOW_MAP_START: ParseFlow(pScanner, state); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void Map::ParseBlock(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
// eat start token
|
||||
pScanner->pop();
|
||||
|
||||
while(1) {
|
||||
if(pScanner->empty())
|
||||
throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP);
|
||||
|
||||
Token token = pScanner->peek();
|
||||
if(token.type != Token::KEY && token.type != Token::VALUE && token.type != Token::BLOCK_MAP_END)
|
||||
throw ParserException(token.mark, ErrorMsg::END_OF_MAP);
|
||||
|
||||
if(token.type == Token::BLOCK_MAP_END) {
|
||||
pScanner->pop();
|
||||
break;
|
||||
}
|
||||
|
||||
std::auto_ptr <Node> pKey(new Node), pValue(new Node);
|
||||
|
||||
// grab key (if non-null)
|
||||
if(token.type == Token::KEY) {
|
||||
pScanner->pop();
|
||||
pKey->Parse(pScanner, state);
|
||||
}
|
||||
|
||||
// now grab value (optional)
|
||||
if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
|
||||
pScanner->pop();
|
||||
pValue->Parse(pScanner, state);
|
||||
}
|
||||
|
||||
// assign the map with the actual pointers
|
||||
m_data[pKey.release()] = pValue.release();
|
||||
}
|
||||
}
|
||||
|
||||
void Map::ParseFlow(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
// eat start token
|
||||
pScanner->pop();
|
||||
|
||||
while(1) {
|
||||
if(pScanner->empty())
|
||||
throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP_FLOW);
|
||||
|
||||
Token& token = pScanner->peek();
|
||||
// first check for end
|
||||
if(token.type == Token::FLOW_MAP_END) {
|
||||
pScanner->pop();
|
||||
break;
|
||||
}
|
||||
|
||||
std::auto_ptr <Node> pKey(new Node), pValue(new Node);
|
||||
|
||||
// grab key (if non-null)
|
||||
if(token.type == Token::KEY) {
|
||||
pScanner->pop();
|
||||
pKey->Parse(pScanner, state);
|
||||
}
|
||||
|
||||
// now grab value (optional)
|
||||
if(!pScanner->empty() && pScanner->peek().type == Token::VALUE) {
|
||||
pScanner->pop();
|
||||
pValue->Parse(pScanner, state);
|
||||
}
|
||||
|
||||
// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
|
||||
Token& nextToken = pScanner->peek();
|
||||
if(nextToken.type == Token::FLOW_ENTRY)
|
||||
pScanner->pop();
|
||||
else if(nextToken.type != Token::FLOW_MAP_END)
|
||||
throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW);
|
||||
|
||||
// assign the map with the actual pointers
|
||||
m_data[pKey.release()] = pValue.release();
|
||||
}
|
||||
}
|
||||
|
||||
void Map::Write(Emitter& out) const
|
||||
{
|
||||
out << BeginMap;
|
||||
for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it)
|
||||
out << Key << *it->first << Value << *it->second;
|
||||
out << EndMap;
|
||||
}
|
||||
|
||||
int Map::Compare(Content *pContent)
|
||||
{
|
||||
return -pContent->Compare(this);
|
||||
}
|
||||
|
||||
int Map::Compare(Map *pMap)
|
||||
{
|
||||
node_map::const_iterator it = m_data.begin(), jt = pMap->m_data.begin();
|
||||
while(1) {
|
||||
if(it == m_data.end()) {
|
||||
if(jt == pMap->m_data.end())
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
if(jt == pMap->m_data.end())
|
||||
return 1;
|
||||
|
||||
int cmp = it->first->Compare(*jt->first);
|
||||
if(cmp != 0)
|
||||
return cmp;
|
||||
|
||||
cmp = it->second->Compare(*jt->second);
|
||||
if(cmp != 0)
|
||||
return cmp;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "content.h"
|
||||
#include <map>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
class Map: public Content
|
||||
{
|
||||
private:
|
||||
typedef std::map <Node *, Node *, ltnode> node_map;
|
||||
|
||||
public:
|
||||
Map();
|
||||
Map(const node_map& data);
|
||||
virtual ~Map();
|
||||
|
||||
void Clear();
|
||||
virtual Content *Clone() const;
|
||||
|
||||
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
||||
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
||||
virtual std::size_t GetSize() const;
|
||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||
virtual void Write(Emitter& out) const;
|
||||
|
||||
virtual bool IsMap() const { return true; }
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *pContent);
|
||||
virtual int Compare(Scalar *) { return 1; }
|
||||
virtual int Compare(Sequence *) { return 1; }
|
||||
virtual int Compare(Map *pMap);
|
||||
|
||||
private:
|
||||
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
||||
void ParseFlow(Scanner *pScanner, const ParserState& state);
|
||||
|
||||
private:
|
||||
node_map m_data;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MAP_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,292 @@
|
|||
#include "crt.h"
|
||||
#include "node.h"
|
||||
#include "token.h"
|
||||
#include "scanner.h"
|
||||
#include "content.h"
|
||||
#include "parser.h"
|
||||
#include "scalar.h"
|
||||
#include "sequence.h"
|
||||
#include "map.h"
|
||||
#include "aliascontent.h"
|
||||
#include "iterpriv.h"
|
||||
#include "emitter.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// the ordering!
|
||||
bool ltnode::operator ()(const Node *pNode1, const Node *pNode2) const
|
||||
{
|
||||
return *pNode1 < *pNode2;
|
||||
}
|
||||
|
||||
Node::Node(): m_pContent(0), m_alias(false), m_pIdentity(this), m_referenced(true)
|
||||
{
|
||||
}
|
||||
|
||||
Node::Node(const Mark& mark, const std::string& anchor, const std::string& tag, const Content *pContent)
|
||||
: m_mark(mark), m_anchor(anchor), m_tag(tag), m_pContent(0), m_alias(false), m_pIdentity(this), m_referenced(false)
|
||||
{
|
||||
if(m_pContent)
|
||||
m_pContent = pContent->Clone();
|
||||
}
|
||||
|
||||
Node::~Node()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void Node::Clear()
|
||||
{
|
||||
delete m_pContent;
|
||||
m_pContent = 0;
|
||||
m_alias = false;
|
||||
m_referenced = false;
|
||||
m_anchor.clear();
|
||||
m_tag.clear();
|
||||
}
|
||||
|
||||
std::auto_ptr<Node> Node::Clone() const
|
||||
{
|
||||
if(m_alias)
|
||||
throw std::runtime_error("yaml-cpp: Can't clone alias"); // TODO: what to do about aliases?
|
||||
|
||||
return std::auto_ptr<Node> (new Node(m_mark, m_anchor, m_tag, m_pContent));
|
||||
}
|
||||
|
||||
void Node::Parse(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
Clear();
|
||||
|
||||
// an empty node *is* a possibility
|
||||
if(pScanner->empty())
|
||||
return;
|
||||
|
||||
// save location
|
||||
m_mark = pScanner->peek().mark;
|
||||
|
||||
ParseHeader(pScanner, state);
|
||||
|
||||
// is this an alias? if so, its contents are an alias to
|
||||
// a previously defined anchor
|
||||
if(m_alias) {
|
||||
// the scanner throws an exception if it doesn't know this anchor name
|
||||
const Node *pReferencedNode = pScanner->Retrieve(m_anchor);
|
||||
m_pIdentity = pReferencedNode;
|
||||
|
||||
// mark the referenced node for the sake of the client code
|
||||
pReferencedNode->m_referenced = true;
|
||||
|
||||
// use of an Alias object keeps the referenced content from
|
||||
// being deleted twice
|
||||
Content *pAliasedContent = pReferencedNode->m_pContent;
|
||||
if(pAliasedContent)
|
||||
m_pContent = new AliasContent(pAliasedContent);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// now split based on what kind of node we should be
|
||||
switch(pScanner->peek().type) {
|
||||
case Token::SCALAR:
|
||||
m_pContent = new Scalar;
|
||||
break;
|
||||
case Token::FLOW_SEQ_START:
|
||||
case Token::BLOCK_SEQ_START:
|
||||
m_pContent = new Sequence;
|
||||
break;
|
||||
case Token::FLOW_MAP_START:
|
||||
case Token::BLOCK_MAP_START:
|
||||
m_pContent = new Map;
|
||||
break;
|
||||
default:
|
||||
// std::stringstream str;
|
||||
// str << TokenNames[pScanner->peek().type];
|
||||
// throw std::runtime_error(str.str());
|
||||
break;
|
||||
}
|
||||
|
||||
// Have to save anchor before parsing to allow for aliases as
|
||||
// contained node (recursive structure)
|
||||
if(!m_anchor.empty())
|
||||
pScanner->Save(m_anchor, this);
|
||||
|
||||
if(m_pContent)
|
||||
m_pContent->Parse(pScanner, state);
|
||||
}
|
||||
|
||||
// ParseHeader
|
||||
// . Grabs any tag, alias, or anchor tokens and deals with them.
|
||||
void Node::ParseHeader(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
while(1) {
|
||||
if(pScanner->empty())
|
||||
return;
|
||||
|
||||
switch(pScanner->peek().type) {
|
||||
case Token::TAG: ParseTag(pScanner, state); break;
|
||||
case Token::ANCHOR: ParseAnchor(pScanner, state); break;
|
||||
case Token::ALIAS: ParseAlias(pScanner, state); break;
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Node::ParseTag(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
Token& token = pScanner->peek();
|
||||
if(m_tag != "")
|
||||
throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS);
|
||||
|
||||
m_tag = state.TranslateTag(token.value);
|
||||
|
||||
for(std::size_t i=0;i<token.params.size();i++)
|
||||
m_tag += token.params[i];
|
||||
pScanner->pop();
|
||||
}
|
||||
|
||||
void Node::ParseAnchor(Scanner *pScanner, const ParserState& /*state*/)
|
||||
{
|
||||
Token& token = pScanner->peek();
|
||||
if(m_anchor != "")
|
||||
throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS);
|
||||
|
||||
m_anchor = token.value;
|
||||
m_alias = false;
|
||||
pScanner->pop();
|
||||
}
|
||||
|
||||
void Node::ParseAlias(Scanner *pScanner, const ParserState& /*state*/)
|
||||
{
|
||||
Token& token = pScanner->peek();
|
||||
if(m_anchor != "")
|
||||
throw ParserException(token.mark, ErrorMsg::MULTIPLE_ALIASES);
|
||||
if(m_tag != "")
|
||||
throw ParserException(token.mark, ErrorMsg::ALIAS_CONTENT);
|
||||
|
||||
m_anchor = token.value;
|
||||
m_alias = true;
|
||||
pScanner->pop();
|
||||
}
|
||||
|
||||
CONTENT_TYPE Node::GetType() const
|
||||
{
|
||||
if(!m_pContent)
|
||||
return CT_NONE;
|
||||
|
||||
if(m_pContent->IsScalar())
|
||||
return CT_SCALAR;
|
||||
else if(m_pContent->IsSequence())
|
||||
return CT_SEQUENCE;
|
||||
else if(m_pContent->IsMap())
|
||||
return CT_MAP;
|
||||
|
||||
return CT_NONE;
|
||||
}
|
||||
|
||||
// begin
|
||||
// Returns an iterator to the beginning of this (sequence or map).
|
||||
Iterator Node::begin() const
|
||||
{
|
||||
if(!m_pContent)
|
||||
return Iterator();
|
||||
|
||||
std::vector <Node *>::const_iterator seqIter;
|
||||
if(m_pContent->GetBegin(seqIter))
|
||||
return Iterator(new IterPriv(seqIter));
|
||||
|
||||
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
|
||||
if(m_pContent->GetBegin(mapIter))
|
||||
return Iterator(new IterPriv(mapIter));
|
||||
|
||||
return Iterator();
|
||||
}
|
||||
|
||||
// end
|
||||
// . Returns an iterator to the end of this (sequence or map).
|
||||
Iterator Node::end() const
|
||||
{
|
||||
if(!m_pContent)
|
||||
return Iterator();
|
||||
|
||||
std::vector <Node *>::const_iterator seqIter;
|
||||
if(m_pContent->GetEnd(seqIter))
|
||||
return Iterator(new IterPriv(seqIter));
|
||||
|
||||
std::map <Node *, Node *, ltnode>::const_iterator mapIter;
|
||||
if(m_pContent->GetEnd(mapIter))
|
||||
return Iterator(new IterPriv(mapIter));
|
||||
|
||||
return Iterator();
|
||||
}
|
||||
|
||||
// size
|
||||
// . Returns the size of this node, if it's a sequence node.
|
||||
// . Otherwise, returns zero.
|
||||
std::size_t Node::size() const
|
||||
{
|
||||
if(!m_pContent)
|
||||
return 0;
|
||||
|
||||
return m_pContent->GetSize();
|
||||
}
|
||||
|
||||
const Node *Node::FindAtIndex(std::size_t i) const
|
||||
{
|
||||
if(!m_pContent)
|
||||
return 0;
|
||||
|
||||
return m_pContent->GetNode(i);
|
||||
}
|
||||
|
||||
bool Node::GetScalar(std::string& s) const
|
||||
{
|
||||
if(!m_pContent) {
|
||||
s = "~";
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_pContent->GetScalar(s);
|
||||
}
|
||||
|
||||
Emitter& operator << (Emitter& out, const Node& node)
|
||||
{
|
||||
// write anchor/alias
|
||||
if(node.m_anchor != "") {
|
||||
if(node.m_alias)
|
||||
out << Alias(node.m_anchor);
|
||||
else
|
||||
out << Anchor(node.m_anchor);
|
||||
}
|
||||
|
||||
// TODO: write tag
|
||||
|
||||
// write content
|
||||
if(node.m_pContent)
|
||||
node.m_pContent->Write(out);
|
||||
else if(!node.m_alias)
|
||||
out << Null;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
int Node::Compare(const Node& rhs) const
|
||||
{
|
||||
// Step 1: no content is the smallest
|
||||
if(!m_pContent) {
|
||||
if(rhs.m_pContent)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
if(!rhs.m_pContent)
|
||||
return 1;
|
||||
|
||||
return m_pContent->Compare(rhs.m_pContent);
|
||||
}
|
||||
|
||||
bool operator < (const Node& n1, const Node& n2)
|
||||
{
|
||||
return n1.Compare(n2) < 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include "null.h"
|
||||
#include "node.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
_Null Null;
|
||||
|
||||
bool IsNull(const Node& node)
|
||||
{
|
||||
return node.Read(Null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
#include "ostream.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
ostream::ostream(): m_buffer(0), m_pos(0), m_size(0), m_row(0), m_col(0)
|
||||
{
|
||||
reserve(1024);
|
||||
}
|
||||
|
||||
ostream::~ostream()
|
||||
{
|
||||
delete [] m_buffer;
|
||||
}
|
||||
|
||||
void ostream::reserve(unsigned size)
|
||||
{
|
||||
if(size <= m_size)
|
||||
return;
|
||||
|
||||
char *newBuffer = new char[size];
|
||||
std::memset(newBuffer, 0, size * sizeof(char));
|
||||
std::memcpy(newBuffer, m_buffer, m_size * sizeof(char));
|
||||
delete [] m_buffer;
|
||||
m_buffer = newBuffer;
|
||||
m_size = size;
|
||||
}
|
||||
|
||||
void ostream::put(char ch)
|
||||
{
|
||||
if(m_pos >= m_size - 1) // an extra space for the NULL terminator
|
||||
reserve(m_size * 2);
|
||||
|
||||
m_buffer[m_pos] = ch;
|
||||
m_pos++;
|
||||
|
||||
if(ch == '\n') {
|
||||
m_row++;
|
||||
m_col = 0;
|
||||
} else
|
||||
m_col++;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, const char *str)
|
||||
{
|
||||
std::size_t length = std::strlen(str);
|
||||
for(std::size_t i=0;i<length;i++)
|
||||
out.put(str[i]);
|
||||
return out;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, const std::string& str)
|
||||
{
|
||||
out << str.c_str();
|
||||
return out;
|
||||
}
|
||||
|
||||
ostream& operator << (ostream& out, char ch)
|
||||
{
|
||||
out.put(ch);
|
||||
return out;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,147 @@
|
|||
#include "crt.h"
|
||||
#include "parser.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Parser::Parser()
|
||||
{
|
||||
}
|
||||
|
||||
Parser::Parser(std::istream& in)
|
||||
{
|
||||
Load(in);
|
||||
}
|
||||
|
||||
Parser::~Parser()
|
||||
{
|
||||
}
|
||||
|
||||
Parser::operator bool() const
|
||||
{
|
||||
return m_pScanner.get() && !m_pScanner->empty();
|
||||
}
|
||||
|
||||
void Parser::Load(std::istream& 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.
|
||||
bool Parser::GetNextDocument(Node& document)
|
||||
{
|
||||
if(!m_pScanner.get())
|
||||
return false;
|
||||
|
||||
// clear node
|
||||
document.Clear();
|
||||
|
||||
// first read directives
|
||||
ParseDirectives();
|
||||
|
||||
// we better have some tokens in the queue
|
||||
if(m_pScanner->empty())
|
||||
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.get(), m_state);
|
||||
|
||||
// and finally eat any doc ends we see
|
||||
while(!m_pScanner->empty() && m_pScanner->peek().type == Token::DOC_END)
|
||||
m_pScanner->pop();
|
||||
|
||||
// clear anchors from the scanner, which are no longer relevant
|
||||
m_pScanner->ClearAnchors();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ParseDirectives
|
||||
// . Reads any directives that are next in the queue.
|
||||
void Parser::ParseDirectives()
|
||||
{
|
||||
bool readDirective = false;
|
||||
|
||||
while(1) {
|
||||
if(m_pScanner->empty())
|
||||
break;
|
||||
|
||||
Token& token = m_pScanner->peek();
|
||||
if(token.type != Token::DIRECTIVE)
|
||||
break;
|
||||
|
||||
// we keep the directives from the last document if none are specified;
|
||||
// but if any directives are specific, then we reset them
|
||||
if(!readDirective)
|
||||
m_state.Reset();
|
||||
|
||||
readDirective = true;
|
||||
HandleDirective(&token);
|
||||
m_pScanner->pop();
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::HandleDirective(Token *pToken)
|
||||
{
|
||||
if(pToken->value == "YAML")
|
||||
HandleYamlDirective(pToken);
|
||||
else if(pToken->value == "TAG")
|
||||
HandleTagDirective(pToken);
|
||||
}
|
||||
|
||||
// HandleYamlDirective
|
||||
// . Should be of the form 'major.minor' (like a version number)
|
||||
void Parser::HandleYamlDirective(Token *pToken)
|
||||
{
|
||||
if(pToken->params.size() != 1)
|
||||
throw ParserException(pToken->mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
|
||||
|
||||
std::stringstream str(pToken->params[0]);
|
||||
str >> m_state.version.major;
|
||||
str.get();
|
||||
str >> m_state.version.minor;
|
||||
if(!str || str.peek() != EOF)
|
||||
throw ParserException(pToken->mark, ErrorMsg::YAML_VERSION + pToken->params[0]);
|
||||
|
||||
if(m_state.version.major > 1)
|
||||
throw ParserException(pToken->mark, ErrorMsg::YAML_MAJOR_VERSION);
|
||||
|
||||
// TODO: warning on major == 1, minor > 2?
|
||||
}
|
||||
|
||||
// HandleTagDirective
|
||||
// . Should be of the form 'handle prefix', where 'handle' is converted to 'prefix' in the file.
|
||||
void Parser::HandleTagDirective(Token *pToken)
|
||||
{
|
||||
if(pToken->params.size() != 2)
|
||||
throw ParserException(pToken->mark, ErrorMsg::TAG_DIRECTIVE_ARGS);
|
||||
|
||||
std::string handle = pToken->params[0], prefix = pToken->params[1];
|
||||
m_state.tags[handle] = prefix;
|
||||
}
|
||||
|
||||
void Parser::PrintTokens(std::ostream& out)
|
||||
{
|
||||
if(!m_pScanner.get())
|
||||
return;
|
||||
|
||||
while(1) {
|
||||
if(m_pScanner->empty())
|
||||
break;
|
||||
|
||||
out << m_pScanner->peek() << "\n";
|
||||
m_pScanner->pop();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include "crt.h"
|
||||
#include "parserstate.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
void ParserState::Reset()
|
||||
{
|
||||
// version
|
||||
version.major = 1;
|
||||
version.minor = 2;
|
||||
|
||||
// and tags
|
||||
tags.clear();
|
||||
tags["!"] = "!";
|
||||
tags["!!"] = "tag:yaml.org,2002:";
|
||||
}
|
||||
|
||||
std::string ParserState::TranslateTag(const std::string& handle) const
|
||||
{
|
||||
std::map <std::string, std::string>::const_iterator it = tags.find(handle);
|
||||
if(it == tags.end())
|
||||
return handle;
|
||||
|
||||
return it->second;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#include "crt.h"
|
||||
#include "regex.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// constructors
|
||||
RegEx::RegEx(): m_op(REGEX_EMPTY)
|
||||
{
|
||||
}
|
||||
|
||||
RegEx::RegEx(REGEX_OP op): m_op(op)
|
||||
{
|
||||
}
|
||||
|
||||
RegEx::RegEx(char ch): m_op(REGEX_MATCH), m_a(ch)
|
||||
{
|
||||
}
|
||||
|
||||
RegEx::RegEx(char a, char z): m_op(REGEX_RANGE), m_a(a), m_z(z)
|
||||
{
|
||||
}
|
||||
|
||||
RegEx::RegEx(const std::string& str, REGEX_OP op): m_op(op)
|
||||
{
|
||||
for(std::size_t i=0;i<str.size();i++)
|
||||
m_params.push_back(RegEx(str[i]));
|
||||
}
|
||||
|
||||
// combination constructors
|
||||
RegEx operator ! (const RegEx& ex)
|
||||
{
|
||||
RegEx ret(REGEX_NOT);
|
||||
ret.m_params.push_back(ex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
RegEx operator || (const RegEx& ex1, const RegEx& ex2)
|
||||
{
|
||||
RegEx ret(REGEX_OR);
|
||||
ret.m_params.push_back(ex1);
|
||||
ret.m_params.push_back(ex2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
RegEx operator && (const RegEx& ex1, const RegEx& ex2)
|
||||
{
|
||||
RegEx ret(REGEX_AND);
|
||||
ret.m_params.push_back(ex1);
|
||||
ret.m_params.push_back(ex2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
RegEx operator + (const RegEx& ex1, const RegEx& ex2)
|
||||
{
|
||||
RegEx ret(REGEX_SEQ);
|
||||
ret.m_params.push_back(ex1);
|
||||
ret.m_params.push_back(ex2);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef REGEX_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define REGEX_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Stream;
|
||||
|
||||
enum REGEX_OP { REGEX_EMPTY, REGEX_MATCH, REGEX_RANGE, REGEX_OR, REGEX_AND, REGEX_NOT, REGEX_SEQ };
|
||||
|
||||
// simplified regular expressions
|
||||
// . Only straightforward matches (no repeated characters)
|
||||
// . Only matches from start of string
|
||||
class RegEx
|
||||
{
|
||||
public:
|
||||
RegEx();
|
||||
RegEx(char ch);
|
||||
RegEx(char a, char z);
|
||||
RegEx(const std::string& str, REGEX_OP op = REGEX_SEQ);
|
||||
~RegEx() {}
|
||||
|
||||
friend RegEx operator ! (const RegEx& ex);
|
||||
friend RegEx operator || (const RegEx& ex1, const RegEx& ex2);
|
||||
friend RegEx operator && (const RegEx& ex1, const RegEx& ex2);
|
||||
friend RegEx operator + (const RegEx& ex1, const RegEx& ex2);
|
||||
|
||||
bool Matches(char ch) const;
|
||||
bool Matches(const std::string& str) const;
|
||||
bool Matches(const Stream& in) const;
|
||||
template <typename Source> bool Matches(const Source& source) const;
|
||||
|
||||
int Match(const std::string& str) const;
|
||||
int Match(const Stream& in) const;
|
||||
|
||||
private:
|
||||
RegEx(REGEX_OP op);
|
||||
|
||||
template <typename Source> bool IsValidSource(const Source& source) const;
|
||||
template <typename Source> int Match(const Source& source) const;
|
||||
template <typename Source> int MatchUnchecked(const Source& source) const;
|
||||
|
||||
template <typename Source> int MatchOpEmpty(const Source& source) const;
|
||||
template <typename Source> int MatchOpMatch(const Source& source) const;
|
||||
template <typename Source> int MatchOpRange(const Source& source) const;
|
||||
template <typename Source> int MatchOpOr(const Source& source) const;
|
||||
template <typename Source> int MatchOpAnd(const Source& source) const;
|
||||
template <typename Source> int MatchOpNot(const Source& source) const;
|
||||
template <typename Source> int MatchOpSeq(const Source& source) const;
|
||||
|
||||
private:
|
||||
REGEX_OP m_op;
|
||||
char m_a, m_z;
|
||||
std::vector <RegEx> m_params;
|
||||
};
|
||||
}
|
||||
|
||||
#include "regeximpl.h"
|
||||
|
||||
#endif // REGEX_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,178 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "stream.h"
|
||||
#include "stringsource.h"
|
||||
#include "streamcharsource.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// query matches
|
||||
inline bool RegEx::Matches(char ch) const {
|
||||
std::string str;
|
||||
str += ch;
|
||||
return Matches(str);
|
||||
}
|
||||
|
||||
inline bool RegEx::Matches(const std::string& str) const {
|
||||
return Match(str) >= 0;
|
||||
}
|
||||
|
||||
inline bool RegEx::Matches(const Stream& in) const {
|
||||
return Match(in) >= 0;
|
||||
}
|
||||
|
||||
template <typename Source>
|
||||
inline bool RegEx::Matches(const Source& source) const {
|
||||
return Match(source) >= 0;
|
||||
}
|
||||
|
||||
// Match
|
||||
// . Matches the given string against this regular expression.
|
||||
// . Returns the number of characters matched.
|
||||
// . Returns -1 if no characters were matched (the reason for
|
||||
// not returning zero is that we may have an empty regex
|
||||
// which is ALWAYS successful at matching zero characters).
|
||||
// . REMEMBER that we only match from the start of the buffer!
|
||||
inline int RegEx::Match(const std::string& str) const
|
||||
{
|
||||
StringCharSource source(str.c_str(), str.size());
|
||||
return Match(source);
|
||||
}
|
||||
|
||||
inline int RegEx::Match(const Stream& in) const
|
||||
{
|
||||
StreamCharSource source(in);
|
||||
return Match(source);
|
||||
}
|
||||
|
||||
template <typename Source>
|
||||
inline bool RegEx::IsValidSource(const Source& source) const
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool RegEx::IsValidSource<StringCharSource>(const StringCharSource&source) const
|
||||
{
|
||||
return source || m_op == REGEX_EMPTY;
|
||||
}
|
||||
|
||||
template <typename Source>
|
||||
inline int RegEx::Match(const Source& source) const
|
||||
{
|
||||
return IsValidSource(source) ? MatchUnchecked(source) : -1;
|
||||
}
|
||||
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchUnchecked(const Source& source) const
|
||||
{
|
||||
switch(m_op) {
|
||||
case REGEX_EMPTY:
|
||||
return MatchOpEmpty(source);
|
||||
case REGEX_MATCH:
|
||||
return MatchOpMatch(source);
|
||||
case REGEX_RANGE:
|
||||
return MatchOpRange(source);
|
||||
case REGEX_OR:
|
||||
return MatchOpOr(source);
|
||||
case REGEX_AND:
|
||||
return MatchOpAnd(source);
|
||||
case REGEX_NOT:
|
||||
return MatchOpNot(source);
|
||||
case REGEX_SEQ:
|
||||
return MatchOpSeq(source);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Operators
|
||||
// Note: the convention MatchOp*<Source> is that we can assume IsSourceValid(source).
|
||||
// So we do all our checks *before* we call these functions
|
||||
|
||||
// EmptyOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpEmpty(const Source& source) const {
|
||||
return source[0] == Stream::eof() ? 0 : -1;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline int RegEx::MatchOpEmpty<StringCharSource>(const StringCharSource& source) const {
|
||||
return !source ? 0 : -1; // the empty regex only is successful on the empty string
|
||||
}
|
||||
|
||||
// MatchOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpMatch(const Source& source) const {
|
||||
if(source[0] != m_a)
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// RangeOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpRange(const Source& source) const {
|
||||
if(m_a > source[0] || m_z < source[0])
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// OrOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpOr(const Source& source) const {
|
||||
for(std::size_t i=0;i<m_params.size();i++) {
|
||||
int n = m_params[i].MatchUnchecked(source);
|
||||
if(n >= 0)
|
||||
return n;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// AndOperator
|
||||
// Note: 'AND' is a little funny, since we may be required to match things
|
||||
// of different lengths. If we find a match, we return the length of
|
||||
// the FIRST entry on the list.
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpAnd(const Source& source) const {
|
||||
int first = -1;
|
||||
for(std::size_t i=0;i<m_params.size();i++) {
|
||||
int n = m_params[i].MatchUnchecked(source);
|
||||
if(n == -1)
|
||||
return -1;
|
||||
if(i == 0)
|
||||
first = n;
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
// NotOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpNot(const Source& source) const {
|
||||
if(m_params.empty())
|
||||
return -1;
|
||||
if(m_params[0].MatchUnchecked(source) >= 0)
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// SeqOperator
|
||||
template <typename Source>
|
||||
inline int RegEx::MatchOpSeq(const Source& source) const {
|
||||
int offset = 0;
|
||||
for(std::size_t i=0;i<m_params.size();i++) {
|
||||
int n = m_params[i].Match(source + offset); // note Match, not MatchUnchecked because we need to check validity after the offset
|
||||
if(n == -1)
|
||||
return -1;
|
||||
offset += n;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,55 @@
|
|||
#include "crt.h"
|
||||
#include "scalar.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include "node.h"
|
||||
#include "emitter.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Scalar::Scalar()
|
||||
{
|
||||
}
|
||||
|
||||
Scalar::Scalar(const std::string& data): m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
Scalar::~Scalar()
|
||||
{
|
||||
}
|
||||
|
||||
Content *Scalar::Clone() const
|
||||
{
|
||||
return new Scalar(m_data);
|
||||
}
|
||||
|
||||
void Scalar::Parse(Scanner *pScanner, const ParserState& /*state*/)
|
||||
{
|
||||
Token& token = pScanner->peek();
|
||||
m_data = token.value;
|
||||
pScanner->pop();
|
||||
}
|
||||
|
||||
void Scalar::Write(Emitter& out) const
|
||||
{
|
||||
out << m_data;
|
||||
}
|
||||
|
||||
int Scalar::Compare(Content *pContent)
|
||||
{
|
||||
return -pContent->Compare(this);
|
||||
}
|
||||
|
||||
int Scalar::Compare(Scalar *pScalar)
|
||||
{
|
||||
if(m_data < pScalar->m_data)
|
||||
return -1;
|
||||
else if(m_data > pScalar->m_data)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "content.h"
|
||||
#include <string>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Scalar: public Content
|
||||
{
|
||||
public:
|
||||
Scalar();
|
||||
Scalar(const std::string& data);
|
||||
virtual ~Scalar();
|
||||
|
||||
virtual Content *Clone() const;
|
||||
|
||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||
virtual void Write(Emitter& out) const;
|
||||
|
||||
virtual bool IsScalar() const { return true; }
|
||||
|
||||
// extraction
|
||||
virtual bool GetScalar(std::string& scalar) const {
|
||||
scalar = m_data;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *pContent);
|
||||
virtual int Compare(Scalar *pScalar);
|
||||
virtual int Compare(Sequence *) { return -1; }
|
||||
virtual int Compare(Map *) { return -1; }
|
||||
|
||||
protected:
|
||||
std::string m_data;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
|
@ -0,0 +1,391 @@
|
|||
#include "crt.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include "exp.h"
|
||||
#include <cassert>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Scanner::Scanner(std::istream& in)
|
||||
: INPUT(in), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false)
|
||||
{
|
||||
}
|
||||
|
||||
Scanner::~Scanner()
|
||||
{
|
||||
}
|
||||
|
||||
// empty
|
||||
// . Returns true if there are no more tokens to be read
|
||||
bool Scanner::empty()
|
||||
{
|
||||
EnsureTokensInQueue();
|
||||
return m_tokens.empty();
|
||||
}
|
||||
|
||||
// pop
|
||||
// . Simply removes the next token on the queue.
|
||||
void Scanner::pop()
|
||||
{
|
||||
EnsureTokensInQueue();
|
||||
if(!m_tokens.empty()) {
|
||||
// Saved anchors shouldn't survive popping the document end marker
|
||||
if (m_tokens.front().type == Token::DOC_END) {
|
||||
ClearAnchors();
|
||||
}
|
||||
m_tokens.pop();
|
||||
}
|
||||
}
|
||||
|
||||
// peek
|
||||
// . Returns (but does not remove) the next token on the queue.
|
||||
Token& Scanner::peek()
|
||||
{
|
||||
EnsureTokensInQueue();
|
||||
assert(!m_tokens.empty()); // should we be asserting here? I mean, we really just be checking
|
||||
// if it's empty before peeking.
|
||||
|
||||
#if 0
|
||||
static Token *pLast = 0;
|
||||
if(pLast != &m_tokens.front())
|
||||
std::cerr << "peek: " << m_tokens.front() << "\n";
|
||||
pLast = &m_tokens.front();
|
||||
#endif
|
||||
|
||||
return m_tokens.front();
|
||||
}
|
||||
|
||||
// EnsureTokensInQueue
|
||||
// . Scan until there's a valid token at the front of the queue,
|
||||
// or we're sure the queue is empty.
|
||||
void Scanner::EnsureTokensInQueue()
|
||||
{
|
||||
while(1) {
|
||||
if(!m_tokens.empty()) {
|
||||
Token& token = m_tokens.front();
|
||||
|
||||
// if this guy's valid, then we're done
|
||||
if(token.status == Token::VALID)
|
||||
return;
|
||||
|
||||
// here's where we clean up the impossible tokens
|
||||
if(token.status == Token::INVALID) {
|
||||
m_tokens.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
// note: what's left are the unverified tokens
|
||||
}
|
||||
|
||||
// no token? maybe we've actually finished
|
||||
if(m_endedStream)
|
||||
return;
|
||||
|
||||
// no? then scan...
|
||||
ScanNextToken();
|
||||
}
|
||||
}
|
||||
|
||||
// ScanNextToken
|
||||
// . The main scanning function; here we branch out and
|
||||
// scan whatever the next token should be.
|
||||
void Scanner::ScanNextToken()
|
||||
{
|
||||
if(m_endedStream)
|
||||
return;
|
||||
|
||||
if(!m_startedStream)
|
||||
return StartStream();
|
||||
|
||||
// get rid of whitespace, etc. (in between tokens it should be irrelevent)
|
||||
ScanToNextToken();
|
||||
|
||||
// maybe need to end some blocks
|
||||
PopIndentToHere();
|
||||
|
||||
// *****
|
||||
// And now branch based on the next few characters!
|
||||
// *****
|
||||
|
||||
// end of stream
|
||||
if(!INPUT)
|
||||
return EndStream();
|
||||
|
||||
if(INPUT.column() == 0 && INPUT.peek() == Keys::Directive)
|
||||
return ScanDirective();
|
||||
|
||||
// document token
|
||||
if(INPUT.column() == 0 && Exp::DocStart.Matches(INPUT))
|
||||
return ScanDocStart();
|
||||
|
||||
if(INPUT.column() == 0 && Exp::DocEnd.Matches(INPUT))
|
||||
return ScanDocEnd();
|
||||
|
||||
// flow start/end/entry
|
||||
if(INPUT.peek() == Keys::FlowSeqStart || INPUT.peek() == Keys::FlowMapStart)
|
||||
return ScanFlowStart();
|
||||
|
||||
if(INPUT.peek() == Keys::FlowSeqEnd || INPUT.peek() == Keys::FlowMapEnd)
|
||||
return ScanFlowEnd();
|
||||
|
||||
if(INPUT.peek() == Keys::FlowEntry)
|
||||
return ScanFlowEntry();
|
||||
|
||||
// block/map stuff
|
||||
if(Exp::BlockEntry.Matches(INPUT))
|
||||
return ScanBlockEntry();
|
||||
|
||||
if((InBlockContext() ? Exp::Key : Exp::KeyInFlow).Matches(INPUT))
|
||||
return ScanKey();
|
||||
|
||||
if((InBlockContext() ? Exp::Value : Exp::ValueInFlow).Matches(INPUT))
|
||||
return ScanValue();
|
||||
|
||||
// alias/anchor
|
||||
if(INPUT.peek() == Keys::Alias || INPUT.peek() == Keys::Anchor)
|
||||
return ScanAnchorOrAlias();
|
||||
|
||||
// tag
|
||||
if(INPUT.peek() == Keys::Tag)
|
||||
return ScanTag();
|
||||
|
||||
// special scalars
|
||||
if(InBlockContext() && (INPUT.peek() == Keys::LiteralScalar || INPUT.peek() == Keys::FoldedScalar))
|
||||
return ScanBlockScalar();
|
||||
|
||||
if(INPUT.peek() == '\'' || INPUT.peek() == '\"')
|
||||
return ScanQuotedScalar();
|
||||
|
||||
// plain scalars
|
||||
if((InBlockContext() ? Exp::PlainScalar : Exp::PlainScalarInFlow).Matches(INPUT))
|
||||
return ScanPlainScalar();
|
||||
|
||||
// don't know what it is!
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN);
|
||||
}
|
||||
|
||||
// ScanToNextToken
|
||||
// . Eats input until we reach the next token-like thing.
|
||||
void Scanner::ScanToNextToken()
|
||||
{
|
||||
while(1) {
|
||||
// first eat whitespace
|
||||
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)) {
|
||||
// eat until line break
|
||||
while(INPUT && !Exp::Break.Matches(INPUT))
|
||||
INPUT.eat(1);
|
||||
}
|
||||
|
||||
// if it's NOT a line break, then we're done!
|
||||
if(!Exp::Break.Matches(INPUT))
|
||||
break;
|
||||
|
||||
// otherwise, let's eat the line break and keep going
|
||||
int n = Exp::Break.Match(INPUT);
|
||||
INPUT.eat(n);
|
||||
|
||||
// oh yeah, and let's get rid of that simple key
|
||||
InvalidateSimpleKey();
|
||||
|
||||
// new line - we may be able to accept a simple key now
|
||||
if(InBlockContext())
|
||||
m_simpleKeyAllowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Misc. helpers
|
||||
|
||||
// IsWhitespaceToBeEaten
|
||||
// . 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')
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// StartStream
|
||||
// . Set the initial conditions for starting a stream.
|
||||
void Scanner::StartStream()
|
||||
{
|
||||
m_startedStream = true;
|
||||
m_simpleKeyAllowed = true;
|
||||
m_indents.push(IndentMarker(-1, IndentMarker::NONE));
|
||||
m_anchors.clear();
|
||||
}
|
||||
|
||||
// EndStream
|
||||
// . Close out the stream, finish up, etc.
|
||||
void Scanner::EndStream()
|
||||
{
|
||||
// force newline
|
||||
if(INPUT.column() > 0)
|
||||
INPUT.ResetColumn();
|
||||
|
||||
PopAllIndents();
|
||||
PopAllSimpleKeys();
|
||||
|
||||
m_simpleKeyAllowed = false;
|
||||
m_endedStream = true;
|
||||
}
|
||||
|
||||
// PushIndentTo
|
||||
// . Pushes an indentation onto the stack, and enqueues the
|
||||
// proper token (sequence start or mapping start).
|
||||
// . Returns the indent marker it generates (if any).
|
||||
Scanner::IndentMarker *Scanner::PushIndentTo(int column, IndentMarker::INDENT_TYPE type)
|
||||
{
|
||||
// are we in flow?
|
||||
if(InFlowContext())
|
||||
return 0;
|
||||
|
||||
IndentMarker indent(column, type);
|
||||
const IndentMarker& lastIndent = m_indents.top();
|
||||
|
||||
// is this actually an indentation?
|
||||
if(indent.column < lastIndent.column)
|
||||
return 0;
|
||||
if(indent.column == lastIndent.column && !(indent.type == IndentMarker::SEQ && lastIndent.type == IndentMarker::MAP))
|
||||
return 0;
|
||||
|
||||
// push a start token
|
||||
if(type == IndentMarker::SEQ)
|
||||
m_tokens.push(Token(Token::BLOCK_SEQ_START, INPUT.mark()));
|
||||
else if(type == IndentMarker::MAP)
|
||||
m_tokens.push(Token(Token::BLOCK_MAP_START, INPUT.mark()));
|
||||
else
|
||||
assert(false);
|
||||
indent.pStartToken = &m_tokens.back();
|
||||
|
||||
// and then the indent
|
||||
m_indents.push(indent);
|
||||
return &m_indents.top();
|
||||
}
|
||||
|
||||
// PopIndentToHere
|
||||
// . Pops indentations off the stack until we reach the current indentation level,
|
||||
// and enqueues the proper token each time.
|
||||
void Scanner::PopIndentToHere()
|
||||
{
|
||||
// are we in flow?
|
||||
if(InFlowContext())
|
||||
return;
|
||||
|
||||
// now pop away
|
||||
while(!m_indents.empty()) {
|
||||
const IndentMarker& indent = m_indents.top();
|
||||
if(indent.column < INPUT.column())
|
||||
break;
|
||||
if(indent.column == INPUT.column() && !(indent.type == IndentMarker::SEQ && !Exp::BlockEntry.Matches(INPUT)))
|
||||
break;
|
||||
|
||||
PopIndent();
|
||||
}
|
||||
}
|
||||
|
||||
// PopAllIndents
|
||||
// . Pops all indentations (except for the base empty one) off the stack,
|
||||
// and enqueues the proper token each time.
|
||||
void Scanner::PopAllIndents()
|
||||
{
|
||||
// are we in flow?
|
||||
if(InFlowContext())
|
||||
return;
|
||||
|
||||
// now pop away
|
||||
while(!m_indents.empty()) {
|
||||
const IndentMarker& indent = m_indents.top();
|
||||
if(indent.type == IndentMarker::NONE)
|
||||
break;
|
||||
|
||||
PopIndent();
|
||||
}
|
||||
}
|
||||
|
||||
// PopIndent
|
||||
// . Pops a single indent, pushing the proper token
|
||||
void Scanner::PopIndent()
|
||||
{
|
||||
IndentMarker indent = m_indents.top();
|
||||
IndentMarker::INDENT_TYPE type = indent.type;
|
||||
m_indents.pop();
|
||||
if(!indent.isValid) {
|
||||
InvalidateSimpleKey();
|
||||
return;
|
||||
}
|
||||
|
||||
if(type == IndentMarker::SEQ)
|
||||
m_tokens.push(Token(Token::BLOCK_SEQ_END, INPUT.mark()));
|
||||
else if(type == IndentMarker::MAP)
|
||||
m_tokens.push(Token(Token::BLOCK_MAP_END, INPUT.mark()));
|
||||
}
|
||||
|
||||
// GetTopIndent
|
||||
int Scanner::GetTopIndent() const
|
||||
{
|
||||
if(m_indents.empty())
|
||||
return 0;
|
||||
return m_indents.top().column;
|
||||
}
|
||||
|
||||
// Save
|
||||
// . Saves a pointer to the Node object referenced by a particular anchor
|
||||
// name.
|
||||
void Scanner::Save(const std::string& anchor, Node* value)
|
||||
{
|
||||
m_anchors[anchor] = value;
|
||||
}
|
||||
|
||||
// Retrieve
|
||||
// . Retrieves a pointer previously saved for an anchor name.
|
||||
// . Throws an exception if the anchor has not been defined.
|
||||
const Node *Scanner::Retrieve(const std::string& anchor) const
|
||||
{
|
||||
typedef std::map<std::string, const Node *> map;
|
||||
|
||||
map::const_iterator itNode = m_anchors.find(anchor);
|
||||
|
||||
if(m_anchors.end() == itNode)
|
||||
ThrowParserException(ErrorMsg::UNKNOWN_ANCHOR);
|
||||
|
||||
return itNode->second;
|
||||
}
|
||||
|
||||
// ThrowParserException
|
||||
// . Throws a ParserException with the current token location
|
||||
// (if available).
|
||||
// . Does not parse any more tokens.
|
||||
void Scanner::ThrowParserException(const std::string& msg) const
|
||||
{
|
||||
Mark mark = Mark::null();
|
||||
if(!m_tokens.empty()) {
|
||||
const Token& token = m_tokens.front();
|
||||
mark = token.mark;
|
||||
}
|
||||
throw ParserException(mark, msg);
|
||||
}
|
||||
|
||||
void Scanner::ClearAnchors()
|
||||
{
|
||||
m_anchors.clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <ios>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include "stream.h"
|
||||
#include "token.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
class Scanner
|
||||
{
|
||||
public:
|
||||
Scanner(std::istream& in);
|
||||
~Scanner();
|
||||
|
||||
// token queue management (hopefully this looks kinda stl-ish)
|
||||
bool empty();
|
||||
void pop();
|
||||
Token& peek();
|
||||
|
||||
// anchor management
|
||||
void Save(const std::string& anchor, Node* value);
|
||||
const Node *Retrieve(const std::string& anchor) const;
|
||||
void ClearAnchors();
|
||||
|
||||
private:
|
||||
struct IndentMarker {
|
||||
enum INDENT_TYPE { MAP, SEQ, NONE };
|
||||
IndentMarker(int column_, INDENT_TYPE type_): column(column_), type(type_), isValid(true), pStartToken(0) {}
|
||||
|
||||
int column;
|
||||
INDENT_TYPE type;
|
||||
bool isValid;
|
||||
Token *pStartToken;
|
||||
};
|
||||
|
||||
enum FLOW_MARKER { FLOW_MAP, FLOW_SEQ };
|
||||
|
||||
private:
|
||||
// scanning
|
||||
void EnsureTokensInQueue();
|
||||
void ScanNextToken();
|
||||
void ScanToNextToken();
|
||||
void StartStream();
|
||||
void EndStream();
|
||||
|
||||
bool InFlowContext() const { return !m_flows.empty(); }
|
||||
bool InBlockContext() const { return m_flows.empty(); }
|
||||
int GetFlowLevel() const { return m_flows.size(); }
|
||||
|
||||
IndentMarker *PushIndentTo(int column, IndentMarker::INDENT_TYPE type);
|
||||
void PopIndentToHere();
|
||||
void PopAllIndents();
|
||||
void PopIndent();
|
||||
int GetTopIndent() const;
|
||||
|
||||
// checking input
|
||||
bool CanInsertPotentialSimpleKey() const;
|
||||
bool ExistsActiveSimpleKey() const;
|
||||
void InsertPotentialSimpleKey();
|
||||
void InvalidateSimpleKey();
|
||||
bool VerifySimpleKey();
|
||||
void PopAllSimpleKeys();
|
||||
|
||||
void ThrowParserException(const std::string& msg) const;
|
||||
|
||||
bool IsWhitespaceToBeEaten(char ch);
|
||||
|
||||
struct SimpleKey {
|
||||
SimpleKey(const Mark& mark_, int flowLevel_);
|
||||
|
||||
void Validate();
|
||||
void Invalidate();
|
||||
|
||||
Mark mark;
|
||||
int flowLevel;
|
||||
IndentMarker *pIndent;
|
||||
Token *pMapStart, *pKey;
|
||||
};
|
||||
|
||||
// and the tokens
|
||||
void ScanDirective();
|
||||
void ScanDocStart();
|
||||
void ScanDocEnd();
|
||||
void ScanBlockSeqStart();
|
||||
void ScanBlockMapSTart();
|
||||
void ScanBlockEnd();
|
||||
void ScanBlockEntry();
|
||||
void ScanFlowStart();
|
||||
void ScanFlowEnd();
|
||||
void ScanFlowEntry();
|
||||
void ScanKey();
|
||||
void ScanValue();
|
||||
void ScanAnchorOrAlias();
|
||||
void ScanTag();
|
||||
void ScanPlainScalar();
|
||||
void ScanQuotedScalar();
|
||||
void ScanBlockScalar();
|
||||
|
||||
private:
|
||||
// the stream
|
||||
Stream INPUT;
|
||||
|
||||
// the output (tokens)
|
||||
std::queue <Token> m_tokens;
|
||||
|
||||
// state info
|
||||
bool m_startedStream, m_endedStream;
|
||||
bool m_simpleKeyAllowed;
|
||||
std::stack <SimpleKey> m_simpleKeys;
|
||||
std::stack <IndentMarker> m_indents;
|
||||
std::stack <FLOW_MARKER> m_flows;
|
||||
std::map <std::string, const Node *> m_anchors;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SCANNER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,187 @@
|
|||
#include "crt.h"
|
||||
#include "scanscalar.h"
|
||||
#include "scanner.h"
|
||||
#include "exp.h"
|
||||
#include "exceptions.h"
|
||||
#include "token.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
// ScanScalar
|
||||
// . This is where the scalar magic happens.
|
||||
//
|
||||
// . We do the scanning in three phases:
|
||||
// 1. Scan until newline
|
||||
// 2. Eat newline
|
||||
// 3. Scan leading blanks.
|
||||
//
|
||||
// . Depending on the parameters given, we store or stop
|
||||
// and different places in the above flow.
|
||||
std::string ScanScalar(Stream& INPUT, ScanScalarParams& params)
|
||||
{
|
||||
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;
|
||||
|
||||
// document indicator?
|
||||
if(INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT)) {
|
||||
if(params.onDocIndicator == BREAK)
|
||||
break;
|
||||
else if(params.onDocIndicator == THROW)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR);
|
||||
}
|
||||
|
||||
foundNonEmptyLine = true;
|
||||
pastOpeningBreak = true;
|
||||
|
||||
// escaped newline? (only if we're escaping on slash)
|
||||
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
|
||||
char ch = INPUT.get();
|
||||
scalar += ch;
|
||||
if(ch != ' ' && ch != '\t')
|
||||
lastNonWhitespaceChar = scalar.size();
|
||||
}
|
||||
|
||||
// eof? if we're looking to eat something, then we throw
|
||||
if(!INPUT) {
|
||||
if(params.eatEnd)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR);
|
||||
break;
|
||||
}
|
||||
|
||||
// doc indicator?
|
||||
if(params.onDocIndicator == BREAK && INPUT.column() == 0 && Exp::DocIndicator.Matches(INPUT))
|
||||
break;
|
||||
|
||||
// are we done via character match?
|
||||
int n = params.end.Match(INPUT);
|
||||
if(n >= 0) {
|
||||
if(params.eatEnd)
|
||||
INPUT.eat(n);
|
||||
break;
|
||||
}
|
||||
|
||||
// do we remove trailing whitespace?
|
||||
if(params.fold == FOLD_FLOW)
|
||||
scalar.erase(lastNonWhitespaceChar);
|
||||
|
||||
// ********************************
|
||||
// Phase #2: eat line ending
|
||||
n = Exp::Break.Match(INPUT);
|
||||
INPUT.eat(n);
|
||||
|
||||
// ********************************
|
||||
// Phase #3: scan initial spaces
|
||||
|
||||
// first the required indentation
|
||||
while(INPUT.peek() == ' ' && (INPUT.column() < params.indent || (params.detectIndent && !foundNonEmptyLine)))
|
||||
INPUT.eat(1);
|
||||
|
||||
// update indent if we're auto-detecting
|
||||
if(params.detectIndent && !foundNonEmptyLine)
|
||||
params.indent = std::max(params.indent, INPUT.column());
|
||||
|
||||
// and then the rest of the whitespace
|
||||
while(Exp::Blank.Matches(INPUT)) {
|
||||
// we check for tabs that masquerade as indentation
|
||||
if(INPUT.peek() == '\t'&& INPUT.column() < params.indent && params.onTabInIndentation == THROW)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION);
|
||||
|
||||
if(!params.eatLeadingWhitespace)
|
||||
break;
|
||||
|
||||
INPUT.eat(1);
|
||||
}
|
||||
|
||||
// was this an empty line?
|
||||
bool nextEmptyLine = Exp::Break.Matches(INPUT);
|
||||
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)
|
||||
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;
|
||||
moreIndented = nextMoreIndented;
|
||||
pastOpeningBreak = true;
|
||||
|
||||
// are we done via indentation?
|
||||
if(!emptyLine && INPUT.column() < params.indent) {
|
||||
params.leadingSpaces = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// post-processing
|
||||
if(params.trimTrailingSpaces) {
|
||||
std::size_t pos = scalar.find_last_not_of(' ');
|
||||
if(pos < scalar.size())
|
||||
scalar.erase(pos + 1);
|
||||
}
|
||||
|
||||
if(params.chomp == STRIP || params.chomp == CLIP) {
|
||||
std::size_t pos = scalar.find_last_not_of('\n');
|
||||
if(params.chomp == CLIP && pos + 1 < scalar.size())
|
||||
scalar.erase(pos + 2);
|
||||
else if(params.chomp == STRIP && pos < scalar.size())
|
||||
scalar.erase(pos + 1);
|
||||
}
|
||||
|
||||
return scalar;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "regex.h"
|
||||
#include "stream.h"
|
||||
|
||||
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(DONT_FOLD),
|
||||
trimTrailingSpaces(0), chomp(CLIP), onDocIndicator(NONE), onTabInIndentation(NONE), leadingSpaces(false) {}
|
||||
|
||||
// input:
|
||||
RegEx end; // what condition ends this scalar?
|
||||
bool eatEnd; // should we eat that condition when we see it?
|
||||
int indent; // what level of indentation should be eaten and ignored?
|
||||
bool detectIndent; // should we try to autodetect the indent?
|
||||
bool eatLeadingWhitespace; // should we continue eating this delicious indentation after 'indent' spaces?
|
||||
char escape; // what character do we escape on (i.e., slash or single quote) (0 for none)
|
||||
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
|
||||
ACTION onDocIndicator; // what do we do if we see a document indicator?
|
||||
ACTION onTabInIndentation; // what do we do if we see a tab where we should be seeing indentation spaces
|
||||
|
||||
// output:
|
||||
bool leadingSpaces;
|
||||
};
|
||||
|
||||
std::string ScanScalar(Stream& INPUT, ScanScalarParams& info);
|
||||
}
|
||||
|
||||
#endif // SCANSCALAR_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,418 @@
|
|||
#include "crt.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include "exp.h"
|
||||
#include "scanscalar.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Specialization for scanning specific tokens
|
||||
|
||||
// Directive
|
||||
// . Note: no semantic checking is done here (that's for the parser to do)
|
||||
void Scanner::ScanDirective()
|
||||
{
|
||||
std::string name;
|
||||
std::vector <std::string> params;
|
||||
|
||||
// pop indents and simple keys
|
||||
PopAllIndents();
|
||||
PopAllSimpleKeys();
|
||||
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// store pos and eat indicator
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(1);
|
||||
|
||||
// read name
|
||||
while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
|
||||
name += INPUT.get();
|
||||
|
||||
// read parameters
|
||||
while(1) {
|
||||
// first get rid of whitespace
|
||||
while(Exp::Blank.Matches(INPUT))
|
||||
INPUT.eat(1);
|
||||
|
||||
// break on newline or comment
|
||||
if(!INPUT || Exp::Break.Matches(INPUT) || Exp::Comment.Matches(INPUT))
|
||||
break;
|
||||
|
||||
// now read parameter
|
||||
std::string param;
|
||||
while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
|
||||
param += INPUT.get();
|
||||
|
||||
params.push_back(param);
|
||||
}
|
||||
|
||||
Token token(Token::DIRECTIVE, mark);
|
||||
token.value = name;
|
||||
token.params = params;
|
||||
m_tokens.push(token);
|
||||
}
|
||||
|
||||
// DocStart
|
||||
void Scanner::ScanDocStart()
|
||||
{
|
||||
PopAllIndents();
|
||||
PopAllSimpleKeys();
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(3);
|
||||
m_tokens.push(Token(Token::DOC_START, mark));
|
||||
}
|
||||
|
||||
// DocEnd
|
||||
void Scanner::ScanDocEnd()
|
||||
{
|
||||
PopAllIndents();
|
||||
PopAllSimpleKeys();
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(3);
|
||||
m_tokens.push(Token(Token::DOC_END, mark));
|
||||
}
|
||||
|
||||
// FlowStart
|
||||
void Scanner::ScanFlowStart()
|
||||
{
|
||||
// flows can be simple keys
|
||||
InsertPotentialSimpleKey();
|
||||
m_simpleKeyAllowed = true;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
char ch = INPUT.get();
|
||||
FLOW_MARKER flowType = (ch == Keys::FlowSeqStart ? FLOW_SEQ : FLOW_MAP);
|
||||
m_flows.push(flowType);
|
||||
Token::TYPE type = (flowType == FLOW_SEQ ? Token::FLOW_SEQ_START : Token::FLOW_MAP_START);
|
||||
m_tokens.push(Token(type, mark));
|
||||
}
|
||||
|
||||
// FlowEnd
|
||||
void Scanner::ScanFlowEnd()
|
||||
{
|
||||
if(InBlockContext())
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::FLOW_END);
|
||||
|
||||
// we might have a solo entry in the flow context
|
||||
if(VerifySimpleKey())
|
||||
m_tokens.push(Token(Token::VALUE, INPUT.mark()));
|
||||
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
char ch = INPUT.get();
|
||||
|
||||
// check that it matches the start
|
||||
FLOW_MARKER flowType = (ch == Keys::FlowSeqEnd ? FLOW_SEQ : FLOW_MAP);
|
||||
if(m_flows.top() != flowType)
|
||||
throw ParserException(mark, ErrorMsg::FLOW_END);
|
||||
m_flows.pop();
|
||||
|
||||
Token::TYPE type = (flowType ? Token::FLOW_SEQ_END : Token::FLOW_MAP_END);
|
||||
m_tokens.push(Token(type, mark));
|
||||
}
|
||||
|
||||
// FlowEntry
|
||||
void Scanner::ScanFlowEntry()
|
||||
{
|
||||
// we might have a solo entry in the flow context
|
||||
if(VerifySimpleKey())
|
||||
m_tokens.push(Token(Token::VALUE, INPUT.mark()));
|
||||
|
||||
m_simpleKeyAllowed = true;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(1);
|
||||
m_tokens.push(Token(Token::FLOW_ENTRY, mark));
|
||||
}
|
||||
|
||||
// BlockEntry
|
||||
void Scanner::ScanBlockEntry()
|
||||
{
|
||||
// we better be in the block context!
|
||||
if(InFlowContext())
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY);
|
||||
|
||||
// can we put it here?
|
||||
if(!m_simpleKeyAllowed)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY);
|
||||
|
||||
PushIndentTo(INPUT.column(), IndentMarker::SEQ);
|
||||
m_simpleKeyAllowed = true;
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(1);
|
||||
m_tokens.push(Token(Token::BLOCK_ENTRY, mark));
|
||||
}
|
||||
|
||||
// Key
|
||||
void Scanner::ScanKey()
|
||||
{
|
||||
// handle keys diffently in the block context (and manage indents)
|
||||
if(InBlockContext()) {
|
||||
if(!m_simpleKeyAllowed)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::MAP_KEY);
|
||||
|
||||
PushIndentTo(INPUT.column(), IndentMarker::MAP);
|
||||
}
|
||||
|
||||
// can only put a simple key here if we're in block context
|
||||
m_simpleKeyAllowed = InBlockContext();
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(1);
|
||||
m_tokens.push(Token(Token::KEY, mark));
|
||||
}
|
||||
|
||||
// Value
|
||||
void Scanner::ScanValue()
|
||||
{
|
||||
// and check that simple key
|
||||
bool isSimpleKey = VerifySimpleKey();
|
||||
|
||||
if(isSimpleKey) {
|
||||
// can't follow a simple key with another simple key (dunno why, though - it seems fine)
|
||||
m_simpleKeyAllowed = false;
|
||||
} else {
|
||||
// handle values diffently in the block context (and manage indents)
|
||||
if(InBlockContext()) {
|
||||
if(!m_simpleKeyAllowed)
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::MAP_VALUE);
|
||||
|
||||
PushIndentTo(INPUT.column(), IndentMarker::MAP);
|
||||
}
|
||||
|
||||
// can only put a simple key here if we're in block context
|
||||
m_simpleKeyAllowed = InBlockContext();
|
||||
}
|
||||
|
||||
// eat
|
||||
Mark mark = INPUT.mark();
|
||||
INPUT.eat(1);
|
||||
m_tokens.push(Token(Token::VALUE, mark));
|
||||
}
|
||||
|
||||
// AnchorOrAlias
|
||||
void Scanner::ScanAnchorOrAlias()
|
||||
{
|
||||
bool alias;
|
||||
std::string name;
|
||||
|
||||
// insert a potential simple key
|
||||
InsertPotentialSimpleKey();
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// eat the indicator
|
||||
Mark mark = INPUT.mark();
|
||||
char indicator = INPUT.get();
|
||||
alias = (indicator == Keys::Alias);
|
||||
|
||||
// now eat the content
|
||||
while(Exp::AlphaNumeric.Matches(INPUT))
|
||||
name += INPUT.get();
|
||||
|
||||
// we need to have read SOMETHING!
|
||||
if(name.empty())
|
||||
throw ParserException(INPUT.mark(), alias ? ErrorMsg::ALIAS_NOT_FOUND : ErrorMsg::ANCHOR_NOT_FOUND);
|
||||
|
||||
// and needs to end correctly
|
||||
if(INPUT && !Exp::AnchorEnd.Matches(INPUT))
|
||||
throw ParserException(INPUT.mark(), alias ? ErrorMsg::CHAR_IN_ALIAS : ErrorMsg::CHAR_IN_ANCHOR);
|
||||
|
||||
// and we're done
|
||||
Token token(alias ? Token::ALIAS : Token::ANCHOR, mark);
|
||||
token.value = name;
|
||||
m_tokens.push(token);
|
||||
}
|
||||
|
||||
// Tag
|
||||
void Scanner::ScanTag()
|
||||
{
|
||||
std::string handle, suffix;
|
||||
|
||||
// insert a potential simple key
|
||||
InsertPotentialSimpleKey();
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
// eat the indicator
|
||||
Mark mark = INPUT.mark();
|
||||
handle += INPUT.get();
|
||||
|
||||
// read the handle
|
||||
while(INPUT && INPUT.peek() != Keys::Tag && !Exp::BlankOrBreak.Matches(INPUT))
|
||||
handle += INPUT.get();
|
||||
|
||||
// is there a suffix?
|
||||
if(INPUT.peek() == Keys::Tag) {
|
||||
// eat the indicator
|
||||
handle += INPUT.get();
|
||||
|
||||
// then read it
|
||||
while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
|
||||
suffix += INPUT.get();
|
||||
} else {
|
||||
// this is a bit weird: we keep just the '!' as the handle and move the rest to the suffix
|
||||
suffix = handle.substr(1);
|
||||
handle = "!";
|
||||
}
|
||||
|
||||
Token token(Token::TAG, mark);
|
||||
token.value = handle;
|
||||
token.params.push_back(suffix);
|
||||
m_tokens.push(token);
|
||||
}
|
||||
|
||||
// PlainScalar
|
||||
void Scanner::ScanPlainScalar()
|
||||
{
|
||||
std::string scalar;
|
||||
|
||||
// set up the scanning parameters
|
||||
ScanScalarParams params;
|
||||
params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
|
||||
params.eatEnd = false;
|
||||
params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
|
||||
params.fold = FOLD_FLOW;
|
||||
params.eatLeadingWhitespace = true;
|
||||
params.trimTrailingSpaces = true;
|
||||
params.chomp = STRIP;
|
||||
params.onDocIndicator = BREAK;
|
||||
params.onTabInIndentation = THROW;
|
||||
|
||||
// insert a potential simple key
|
||||
InsertPotentialSimpleKey();
|
||||
|
||||
Mark mark = INPUT.mark();
|
||||
scalar = ScanScalar(INPUT, params);
|
||||
|
||||
// can have a simple key only if we ended the scalar by starting a new line
|
||||
m_simpleKeyAllowed = params.leadingSpaces;
|
||||
|
||||
// finally, check and see if we ended on an illegal character
|
||||
//if(Exp::IllegalCharInScalar.Matches(INPUT))
|
||||
// throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR);
|
||||
|
||||
Token token(Token::SCALAR, mark);
|
||||
token.value = scalar;
|
||||
m_tokens.push(token);
|
||||
}
|
||||
|
||||
// QuotedScalar
|
||||
void Scanner::ScanQuotedScalar()
|
||||
{
|
||||
std::string scalar;
|
||||
|
||||
// peek at single or double quote (don't eat because we need to preserve (for the time being) the input position)
|
||||
char quote = INPUT.peek();
|
||||
bool single = (quote == '\'');
|
||||
|
||||
// setup the scanning parameters
|
||||
ScanScalarParams params;
|
||||
params.end = (single ? RegEx(quote) && !Exp::EscSingleQuote : RegEx(quote));
|
||||
params.eatEnd = true;
|
||||
params.escape = (single ? '\'' : '\\');
|
||||
params.indent = 0;
|
||||
params.fold = FOLD_FLOW;
|
||||
params.eatLeadingWhitespace = true;
|
||||
params.trimTrailingSpaces = false;
|
||||
params.chomp = CLIP;
|
||||
params.onDocIndicator = THROW;
|
||||
|
||||
// insert a potential simple key
|
||||
InsertPotentialSimpleKey();
|
||||
|
||||
Mark mark = INPUT.mark();
|
||||
|
||||
// now eat that opening quote
|
||||
INPUT.get();
|
||||
|
||||
// and scan
|
||||
scalar = ScanScalar(INPUT, params);
|
||||
m_simpleKeyAllowed = false;
|
||||
|
||||
Token token(Token::SCALAR, mark);
|
||||
token.value = scalar;
|
||||
m_tokens.push(token);
|
||||
}
|
||||
|
||||
// BlockScalarToken
|
||||
// . These need a little extra processing beforehand.
|
||||
// . We need to scan the line where the indicator is (this doesn't count as part of the scalar),
|
||||
// and then we need to figure out what level of indentation we'll be using.
|
||||
void Scanner::ScanBlockScalar()
|
||||
{
|
||||
std::string scalar;
|
||||
|
||||
ScanScalarParams params;
|
||||
params.indent = 1;
|
||||
params.detectIndent = true;
|
||||
|
||||
// eat block indicator ('|' or '>')
|
||||
Mark mark = INPUT.mark();
|
||||
char indicator = INPUT.get();
|
||||
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();
|
||||
if(ch == '+')
|
||||
params.chomp = KEEP;
|
||||
else if(ch == '-')
|
||||
params.chomp = STRIP;
|
||||
else if(Exp::Digit.Matches(ch)) {
|
||||
if(ch == '0')
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::ZERO_INDENT_IN_BLOCK);
|
||||
|
||||
params.indent = ch - '0';
|
||||
params.detectIndent = false;
|
||||
}
|
||||
}
|
||||
|
||||
// now eat whitespace
|
||||
while(Exp::Blank.Matches(INPUT))
|
||||
INPUT.eat(1);
|
||||
|
||||
// and comments to the end of the line
|
||||
if(Exp::Comment.Matches(INPUT))
|
||||
while(INPUT && !Exp::Break.Matches(INPUT))
|
||||
INPUT.eat(1);
|
||||
|
||||
// if it's not a line break, then we ran into a bad character inline
|
||||
if(INPUT && !Exp::Break.Matches(INPUT))
|
||||
throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_BLOCK);
|
||||
|
||||
// set the initial indentation
|
||||
if(GetTopIndent() >= 0)
|
||||
params.indent += GetTopIndent();
|
||||
|
||||
params.eatLeadingWhitespace = false;
|
||||
params.trimTrailingSpaces = false;
|
||||
params.onTabInIndentation = THROW;
|
||||
|
||||
scalar = ScanScalar(INPUT, params);
|
||||
|
||||
// simple keys always ok after block scalars (since we're gonna start a new line anyways)
|
||||
m_simpleKeyAllowed = true;
|
||||
|
||||
Token token(Token::SCALAR, mark);
|
||||
token.value = scalar;
|
||||
m_tokens.push(token);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
#include "crt.h"
|
||||
#include "sequence.h"
|
||||
#include "node.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "emitter.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Sequence::Sequence()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Sequence::Sequence(const std::vector<Node *>& data)
|
||||
{
|
||||
for(std::size_t i=0;i<data.size();i++)
|
||||
m_data.push_back(data[i]->Clone().release());
|
||||
}
|
||||
|
||||
Sequence::~Sequence()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
void Sequence::Clear()
|
||||
{
|
||||
for(std::size_t i=0;i<m_data.size();i++)
|
||||
delete m_data[i];
|
||||
m_data.clear();
|
||||
}
|
||||
|
||||
Content *Sequence::Clone() const
|
||||
{
|
||||
return new Sequence(m_data);
|
||||
}
|
||||
|
||||
bool Sequence::GetBegin(std::vector <Node *>::const_iterator& it) const
|
||||
{
|
||||
it = m_data.begin();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Sequence::GetEnd(std::vector <Node *>::const_iterator& it) const
|
||||
{
|
||||
it = m_data.end();
|
||||
return true;
|
||||
}
|
||||
|
||||
Node *Sequence::GetNode(std::size_t i) const
|
||||
{
|
||||
if(i < m_data.size())
|
||||
return m_data[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::size_t Sequence::GetSize() const
|
||||
{
|
||||
return m_data.size();
|
||||
}
|
||||
|
||||
void Sequence::Parse(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
Clear();
|
||||
|
||||
// split based on start token
|
||||
switch(pScanner->peek().type) {
|
||||
case Token::BLOCK_SEQ_START: ParseBlock(pScanner, state); break;
|
||||
case Token::FLOW_SEQ_START: ParseFlow(pScanner, state); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void Sequence::ParseBlock(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
// eat start token
|
||||
pScanner->pop();
|
||||
|
||||
while(1) {
|
||||
if(pScanner->empty())
|
||||
throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ);
|
||||
|
||||
Token token = pScanner->peek();
|
||||
if(token.type != Token::BLOCK_ENTRY && token.type != Token::BLOCK_SEQ_END)
|
||||
throw ParserException(token.mark, ErrorMsg::END_OF_SEQ);
|
||||
|
||||
pScanner->pop();
|
||||
if(token.type == Token::BLOCK_SEQ_END)
|
||||
break;
|
||||
|
||||
Node *pNode = new Node;
|
||||
m_data.push_back(pNode);
|
||||
|
||||
// check for null
|
||||
if(!pScanner->empty()) {
|
||||
const Token& token = pScanner->peek();
|
||||
if(token.type == Token::BLOCK_ENTRY || token.type == Token::BLOCK_SEQ_END)
|
||||
continue;
|
||||
}
|
||||
|
||||
pNode->Parse(pScanner, state);
|
||||
}
|
||||
}
|
||||
|
||||
void Sequence::ParseFlow(Scanner *pScanner, const ParserState& state)
|
||||
{
|
||||
// eat start token
|
||||
pScanner->pop();
|
||||
|
||||
while(1) {
|
||||
if(pScanner->empty())
|
||||
throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ_FLOW);
|
||||
|
||||
// first check for end
|
||||
if(pScanner->peek().type == Token::FLOW_SEQ_END) {
|
||||
pScanner->pop();
|
||||
break;
|
||||
}
|
||||
|
||||
// then read the node
|
||||
Node *pNode = new Node;
|
||||
m_data.push_back(pNode);
|
||||
pNode->Parse(pScanner, state);
|
||||
|
||||
// now eat the separator (or could be a sequence end, which we ignore - but if it's neither, then it's a bad node)
|
||||
Token& token = pScanner->peek();
|
||||
if(token.type == Token::FLOW_ENTRY)
|
||||
pScanner->pop();
|
||||
else if(token.type != Token::FLOW_SEQ_END)
|
||||
throw ParserException(token.mark, ErrorMsg::END_OF_SEQ_FLOW);
|
||||
}
|
||||
}
|
||||
|
||||
void Sequence::Write(Emitter& out) const
|
||||
{
|
||||
out << BeginSeq;
|
||||
for(std::size_t i=0;i<m_data.size();i++)
|
||||
out << *m_data[i];
|
||||
out << EndSeq;
|
||||
}
|
||||
|
||||
int Sequence::Compare(Content *pContent)
|
||||
{
|
||||
return -pContent->Compare(this);
|
||||
}
|
||||
|
||||
int Sequence::Compare(Sequence *pSeq)
|
||||
{
|
||||
std::size_t n = m_data.size(), m = pSeq->m_data.size();
|
||||
if(n < m)
|
||||
return -1;
|
||||
else if(n > m)
|
||||
return 1;
|
||||
|
||||
for(std::size_t i=0;i<n;i++) {
|
||||
int cmp = m_data[i]->Compare(*pSeq->m_data[i]);
|
||||
if(cmp != 0)
|
||||
return cmp;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "content.h"
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class Node;
|
||||
|
||||
class Sequence: public Content
|
||||
{
|
||||
public:
|
||||
Sequence();
|
||||
Sequence(const std::vector<Node *>& data);
|
||||
virtual ~Sequence();
|
||||
|
||||
void Clear();
|
||||
virtual Content *Clone() const;
|
||||
|
||||
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) const;
|
||||
virtual bool GetEnd(std::vector <Node *>::const_iterator& it) const;
|
||||
virtual Node *GetNode(std::size_t i) const;
|
||||
virtual std::size_t GetSize() const;
|
||||
|
||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||
virtual void Write(Emitter& out) const;
|
||||
|
||||
virtual bool IsSequence() const { return true; }
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *pContent);
|
||||
virtual int Compare(Scalar *) { return 1; }
|
||||
virtual int Compare(Sequence *pSeq);
|
||||
virtual int Compare(Map *) { return -1; }
|
||||
|
||||
private:
|
||||
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
||||
void ParseFlow(Scanner *pScanner, const ParserState& state);
|
||||
|
||||
protected:
|
||||
std::vector <Node *> m_data;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SEQUENCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,103 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "noncopyable.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class SettingChangeBase;
|
||||
|
||||
template <typename T>
|
||||
class Setting
|
||||
{
|
||||
public:
|
||||
Setting(): m_value() {}
|
||||
|
||||
const T get() const { return m_value; }
|
||||
std::auto_ptr <SettingChangeBase> set(const T& value);
|
||||
void restore(const Setting<T>& oldSetting) {
|
||||
m_value = oldSetting.get();
|
||||
}
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
};
|
||||
|
||||
class SettingChangeBase
|
||||
{
|
||||
public:
|
||||
virtual ~SettingChangeBase() {}
|
||||
virtual void pop() = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class SettingChange: public SettingChangeBase
|
||||
{
|
||||
public:
|
||||
SettingChange(Setting<T> *pSetting): m_pCurSetting(pSetting) {
|
||||
// copy old setting to save its state
|
||||
m_oldSetting = *pSetting;
|
||||
}
|
||||
|
||||
virtual void pop() {
|
||||
m_pCurSetting->restore(m_oldSetting);
|
||||
}
|
||||
|
||||
private:
|
||||
Setting<T> *m_pCurSetting;
|
||||
Setting<T> m_oldSetting;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::auto_ptr <SettingChangeBase> Setting<T>::set(const T& value) {
|
||||
std::auto_ptr <SettingChangeBase> pChange(new SettingChange<T> (this));
|
||||
m_value = value;
|
||||
return pChange;
|
||||
}
|
||||
|
||||
class SettingChanges: private noncopyable
|
||||
{
|
||||
public:
|
||||
SettingChanges() {}
|
||||
~SettingChanges() { clear(); }
|
||||
|
||||
void clear() {
|
||||
restore();
|
||||
|
||||
for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
|
||||
delete *it;
|
||||
m_settingChanges.clear();
|
||||
}
|
||||
|
||||
void restore() {
|
||||
for(setting_changes::const_iterator it=m_settingChanges.begin();it!=m_settingChanges.end();++it)
|
||||
(*it)->pop();
|
||||
}
|
||||
|
||||
void push(std::auto_ptr <SettingChangeBase> pSettingChange) {
|
||||
m_settingChanges.push_back(pSettingChange.release());
|
||||
}
|
||||
|
||||
// like std::auto_ptr - assignment is transfer of ownership
|
||||
SettingChanges& operator = (SettingChanges& rhs) {
|
||||
if(this == &rhs)
|
||||
return *this;
|
||||
|
||||
clear();
|
||||
m_settingChanges = rhs.m_settingChanges;
|
||||
rhs.m_settingChanges.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::vector <SettingChangeBase *> setting_changes;
|
||||
setting_changes m_settingChanges;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SETTING_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,138 @@
|
|||
#include "crt.h"
|
||||
#include "scanner.h"
|
||||
#include "token.h"
|
||||
#include "exceptions.h"
|
||||
#include "exp.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Scanner::SimpleKey::SimpleKey(const Mark& mark_, int flowLevel_)
|
||||
: mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0)
|
||||
{
|
||||
}
|
||||
|
||||
void Scanner::SimpleKey::Validate()
|
||||
{
|
||||
// Note: pIndent will *not* be garbage here; see below
|
||||
if(pIndent)
|
||||
pIndent->isValid = true;
|
||||
if(pMapStart)
|
||||
pMapStart->status = Token::VALID;
|
||||
if(pKey)
|
||||
pKey->status = Token::VALID;
|
||||
}
|
||||
|
||||
void Scanner::SimpleKey::Invalidate()
|
||||
{
|
||||
// Note: pIndent might be a garbage pointer here, but that's ok
|
||||
// An indent will only be popped if the simple key is invalid
|
||||
if(pMapStart)
|
||||
pMapStart->status = Token::INVALID;
|
||||
if(pKey)
|
||||
pKey->status = Token::INVALID;
|
||||
}
|
||||
|
||||
// CanInsertPotentialSimpleKey
|
||||
bool Scanner::CanInsertPotentialSimpleKey() const
|
||||
{
|
||||
if(!m_simpleKeyAllowed)
|
||||
return false;
|
||||
|
||||
if(InFlowContext() && m_flows.top() != FLOW_MAP)
|
||||
return false;
|
||||
|
||||
return !ExistsActiveSimpleKey();
|
||||
}
|
||||
|
||||
// ExistsActiveSimpleKey
|
||||
// . Returns true if there's a potential simple key at our flow level
|
||||
// (there's allowed at most one per flow level, i.e., at the start of the flow start token)
|
||||
bool Scanner::ExistsActiveSimpleKey() const
|
||||
{
|
||||
if(m_simpleKeys.empty())
|
||||
return false;
|
||||
|
||||
const SimpleKey& key = m_simpleKeys.top();
|
||||
return key.flowLevel == GetFlowLevel();
|
||||
}
|
||||
|
||||
// InsertPotentialSimpleKey
|
||||
// . If we can, add a potential simple key to the queue,
|
||||
// and save it on a stack.
|
||||
void Scanner::InsertPotentialSimpleKey()
|
||||
{
|
||||
if(!CanInsertPotentialSimpleKey())
|
||||
return;
|
||||
|
||||
SimpleKey key(INPUT.mark(), GetFlowLevel());
|
||||
|
||||
// first add a map start, if necessary
|
||||
key.pIndent = PushIndentTo(INPUT.column(), IndentMarker::MAP);
|
||||
if(key.pIndent) {
|
||||
key.pIndent->isValid = false;
|
||||
key.pMapStart = key.pIndent->pStartToken;
|
||||
key.pMapStart->status = Token::UNVERIFIED;
|
||||
}
|
||||
|
||||
// then add the (now unverified) key
|
||||
m_tokens.push(Token(Token::KEY, INPUT.mark()));
|
||||
key.pKey = &m_tokens.back();
|
||||
key.pKey->status = Token::UNVERIFIED;
|
||||
|
||||
m_simpleKeys.push(key);
|
||||
}
|
||||
|
||||
// InvalidateSimpleKey
|
||||
// . Automatically invalidate the simple key in our flow level
|
||||
void Scanner::InvalidateSimpleKey()
|
||||
{
|
||||
if(m_simpleKeys.empty())
|
||||
return;
|
||||
|
||||
// grab top key
|
||||
SimpleKey& key = m_simpleKeys.top();
|
||||
if(key.flowLevel != GetFlowLevel())
|
||||
return;
|
||||
|
||||
key.Invalidate();
|
||||
m_simpleKeys.pop();
|
||||
}
|
||||
|
||||
// VerifySimpleKey
|
||||
// . Determines whether the latest simple key to be added is valid,
|
||||
// and if so, makes it valid.
|
||||
bool Scanner::VerifySimpleKey()
|
||||
{
|
||||
if(m_simpleKeys.empty())
|
||||
return false;
|
||||
|
||||
// grab top key
|
||||
SimpleKey key = m_simpleKeys.top();
|
||||
|
||||
// only validate if we're in the correct flow level
|
||||
if(key.flowLevel != GetFlowLevel())
|
||||
return false;
|
||||
|
||||
m_simpleKeys.pop();
|
||||
|
||||
bool isValid = true;
|
||||
|
||||
// needs to be less than 1024 characters and inline
|
||||
if(INPUT.line() != key.mark.line || INPUT.pos() - key.mark.pos > 1024)
|
||||
isValid = false;
|
||||
|
||||
// invalidate key
|
||||
if(isValid)
|
||||
key.Validate();
|
||||
else
|
||||
key.Invalidate();
|
||||
|
||||
return isValid;
|
||||
}
|
||||
|
||||
void Scanner::PopAllSimpleKeys()
|
||||
{
|
||||
while(!m_simpleKeys.empty())
|
||||
m_simpleKeys.pop();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,455 @@
|
|||
#include "crt.h"
|
||||
#include "stream.h"
|
||||
#include <iostream>
|
||||
#include "exp.h"
|
||||
|
||||
#ifndef YAML_PREFETCH_SIZE
|
||||
#define YAML_PREFETCH_SIZE 2048
|
||||
#endif
|
||||
|
||||
#define S_ARRAY_SIZE( A ) (sizeof(A)/sizeof(*(A)))
|
||||
#define S_ARRAY_END( A ) ((A) + S_ARRAY_SIZE(A))
|
||||
|
||||
#define CP_REPLACEMENT_CHARACTER (0xFFFD)
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
enum UtfIntroState {
|
||||
uis_start,
|
||||
uis_utfbe_b1,
|
||||
uis_utf32be_b2,
|
||||
uis_utf32be_bom3,
|
||||
uis_utf32be,
|
||||
uis_utf16be,
|
||||
uis_utf16be_bom1,
|
||||
uis_utfle_bom1,
|
||||
uis_utf16le_bom2,
|
||||
uis_utf32le_bom3,
|
||||
uis_utf16le,
|
||||
uis_utf32le,
|
||||
uis_utf8_imp,
|
||||
uis_utf16le_imp,
|
||||
uis_utf32le_imp3,
|
||||
uis_utf8_bom1,
|
||||
uis_utf8_bom2,
|
||||
uis_utf8,
|
||||
uis_error
|
||||
};
|
||||
|
||||
enum UtfIntroCharType {
|
||||
uict00,
|
||||
uictBB,
|
||||
uictBF,
|
||||
uictEF,
|
||||
uictFE,
|
||||
uictFF,
|
||||
uictAscii,
|
||||
uictOther,
|
||||
uictMax
|
||||
};
|
||||
|
||||
static bool s_introFinalState[] = {
|
||||
false, //uis_start
|
||||
false, //uis_utfbe_b1
|
||||
false, //uis_utf32be_b2
|
||||
false, //uis_utf32be_bom3
|
||||
true, //uis_utf32be
|
||||
true, //uis_utf16be
|
||||
false, //uis_utf16be_bom1
|
||||
false, //uis_utfle_bom1
|
||||
false, //uis_utf16le_bom2
|
||||
false, //uis_utf32le_bom3
|
||||
true, //uis_utf16le
|
||||
true, //uis_utf32le
|
||||
false, //uis_utf8_imp
|
||||
false, //uis_utf16le_imp
|
||||
false, //uis_utf32le_imp3
|
||||
false, //uis_utf8_bom1
|
||||
false, //uis_utf8_bom2
|
||||
true, //uis_utf8
|
||||
true, //uis_error
|
||||
};
|
||||
|
||||
static UtfIntroState s_introTransitions[][uictMax] = {
|
||||
// uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther
|
||||
{uis_utfbe_b1, uis_utf8, uis_utf8, uis_utf8_bom1, uis_utf16be_bom1, uis_utfle_bom1, uis_utf8_imp, uis_utf8},
|
||||
{uis_utf32be_b2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16be, uis_utf8},
|
||||
{uis_utf32be, uis_utf8, uis_utf8, uis_utf8, uis_utf32be_bom3, uis_utf8, uis_utf8, uis_utf8},
|
||||
{uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf32be, uis_utf8, uis_utf8},
|
||||
{uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be, uis_utf32be},
|
||||
{uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be, uis_utf16be},
|
||||
{uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16be, uis_utf8, uis_utf8},
|
||||
{uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf16le_bom2, uis_utf8, uis_utf8, uis_utf8},
|
||||
{uis_utf32le_bom3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le},
|
||||
{uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le},
|
||||
{uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le},
|
||||
{uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le, uis_utf32le},
|
||||
{uis_utf16le_imp, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8},
|
||||
{uis_utf32le_imp3, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le},
|
||||
{uis_utf32le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le, uis_utf16le},
|
||||
{uis_utf8, uis_utf8_bom2, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8},
|
||||
{uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8},
|
||||
{uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8, uis_utf8},
|
||||
};
|
||||
|
||||
static char s_introUngetCount[][uictMax] = {
|
||||
// uict00, uictBB, uictBF, uictEF, uictFE, uictFF, uictAscii, uictOther
|
||||
{0, 1, 1, 0, 0, 0, 0, 1},
|
||||
{0, 2, 2, 2, 2, 2, 2, 2},
|
||||
{3, 3, 3, 3, 0, 3, 3, 3},
|
||||
{4, 4, 4, 4, 4, 0, 4, 4},
|
||||
{1, 1, 1, 1, 1, 1, 1, 1},
|
||||
{1, 1, 1, 1, 1, 1, 1, 1},
|
||||
{2, 2, 2, 2, 2, 0, 2, 2},
|
||||
{2, 2, 2, 2, 0, 2, 2, 2},
|
||||
{0, 1, 1, 1, 1, 1, 1, 1},
|
||||
{0, 2, 2, 2, 2, 2, 2, 2},
|
||||
{1, 1, 1, 1, 1, 1, 1, 1},
|
||||
{1, 1, 1, 1, 1, 1, 1, 1},
|
||||
{0, 2, 2, 2, 2, 2, 2, 2},
|
||||
{0, 3, 3, 3, 3, 3, 3, 3},
|
||||
{4, 4, 4, 4, 4, 4, 4, 4},
|
||||
{2, 0, 2, 2, 2, 2, 2, 2},
|
||||
{3, 3, 0, 3, 3, 3, 3, 3},
|
||||
{1, 1, 1, 1, 1, 1, 1, 1},
|
||||
};
|
||||
|
||||
inline UtfIntroCharType IntroCharTypeOf(std::istream::int_type ch)
|
||||
{
|
||||
if (std::istream::traits_type::eof() == ch) {
|
||||
return uictOther;
|
||||
}
|
||||
|
||||
switch (ch) {
|
||||
case 0: return uict00;
|
||||
case 0xBB: return uictBB;
|
||||
case 0xBF: return uictBF;
|
||||
case 0xEF: return uictEF;
|
||||
case 0xFE: return uictFE;
|
||||
case 0xFF: return uictFF;
|
||||
}
|
||||
|
||||
if ((ch > 0) && (ch < 0xFF)) {
|
||||
return uictAscii;
|
||||
}
|
||||
|
||||
return uictOther;
|
||||
}
|
||||
|
||||
inline char Utf8Adjust(unsigned long ch, unsigned char lead_bits, unsigned char rshift)
|
||||
{
|
||||
const unsigned char header = ((1 << lead_bits) - 1) << (8 - lead_bits);
|
||||
const unsigned char mask = (0xFF >> (lead_bits + 1));
|
||||
return static_cast<char>(static_cast<unsigned char>(
|
||||
header | ((ch >> rshift) & mask)
|
||||
));
|
||||
}
|
||||
|
||||
inline void QueueUnicodeCodepoint(std::deque<char>& q, unsigned long ch)
|
||||
{
|
||||
// We are not allowed to queue the Stream::eof() codepoint, so
|
||||
// replace it with CP_REPLACEMENT_CHARACTER
|
||||
if (static_cast<unsigned long>(Stream::eof()) == ch)
|
||||
{
|
||||
ch = CP_REPLACEMENT_CHARACTER;
|
||||
}
|
||||
|
||||
if (ch < 0x80)
|
||||
{
|
||||
q.push_back(Utf8Adjust(ch, 0, 0));
|
||||
}
|
||||
else if (ch < 0x800)
|
||||
{
|
||||
q.push_back(Utf8Adjust(ch, 2, 6));
|
||||
q.push_back(Utf8Adjust(ch, 1, 0));
|
||||
}
|
||||
else if (ch < 0x10000)
|
||||
{
|
||||
q.push_back(Utf8Adjust(ch, 3, 12));
|
||||
q.push_back(Utf8Adjust(ch, 1, 6));
|
||||
q.push_back(Utf8Adjust(ch, 1, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
q.push_back(Utf8Adjust(ch, 4, 18));
|
||||
q.push_back(Utf8Adjust(ch, 1, 12));
|
||||
q.push_back(Utf8Adjust(ch, 1, 6));
|
||||
q.push_back(Utf8Adjust(ch, 1, 0));
|
||||
}
|
||||
}
|
||||
|
||||
Stream::Stream(std::istream& input)
|
||||
: m_input(input), m_nPushedBack(0),
|
||||
m_pPrefetched(new unsigned char[YAML_PREFETCH_SIZE]),
|
||||
m_nPrefetchedAvailable(0), m_nPrefetchedUsed(0)
|
||||
{
|
||||
typedef std::istream::traits_type char_traits;
|
||||
|
||||
if(!input)
|
||||
return;
|
||||
|
||||
// Determine (or guess) the character-set by reading the BOM, if any. See
|
||||
// the YAML specification for the determination algorithm.
|
||||
char_traits::int_type intro[4];
|
||||
int nIntroUsed = 0;
|
||||
UtfIntroState state = uis_start;
|
||||
for (; !s_introFinalState[state]; ) {
|
||||
std::istream::int_type ch = input.get();
|
||||
intro[nIntroUsed++] = ch;
|
||||
UtfIntroCharType charType = IntroCharTypeOf(ch);
|
||||
UtfIntroState newState = s_introTransitions[state][charType];
|
||||
int nUngets = s_introUngetCount[state][charType];
|
||||
if (nUngets > 0) {
|
||||
for (; nUngets > 0; --nUngets) {
|
||||
if (char_traits::eof() != intro[--nIntroUsed]) {
|
||||
m_bufPushback[m_nPushedBack++] =
|
||||
char_traits::to_char_type(intro[nIntroUsed]);
|
||||
}
|
||||
}
|
||||
}
|
||||
state = newState;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case uis_utf8: m_charSet = utf8; break;
|
||||
case uis_utf16le: m_charSet = utf16le; break;
|
||||
case uis_utf16be: m_charSet = utf16be; break;
|
||||
case uis_utf32le: m_charSet = utf32le; break;
|
||||
case uis_utf32be: m_charSet = utf32be; break;
|
||||
default: m_charSet = utf8; break;
|
||||
}
|
||||
|
||||
ReadAheadTo(0);
|
||||
}
|
||||
|
||||
Stream::~Stream()
|
||||
{
|
||||
delete[] m_pPrefetched;
|
||||
}
|
||||
|
||||
char Stream::peek() const
|
||||
{
|
||||
if (m_readahead.empty())
|
||||
{
|
||||
return Stream::eof();
|
||||
}
|
||||
|
||||
return m_readahead[0];
|
||||
}
|
||||
|
||||
Stream::operator bool() const
|
||||
{
|
||||
return m_input.good() || (!m_readahead.empty() && m_readahead[0] != Stream::eof());
|
||||
}
|
||||
|
||||
// get
|
||||
// . Extracts a character from the stream and updates our position
|
||||
char Stream::get()
|
||||
{
|
||||
char ch = peek();
|
||||
AdvanceCurrent();
|
||||
m_mark.column++;
|
||||
|
||||
if(ch == '\n') {
|
||||
m_mark.column = 0;
|
||||
m_mark.line++;
|
||||
}
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
// get
|
||||
// . Extracts 'n' characters from the stream and updates our position
|
||||
std::string Stream::get(int n)
|
||||
{
|
||||
std::string ret;
|
||||
ret.reserve(n);
|
||||
for(int i=0;i<n;i++)
|
||||
ret += get();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// eat
|
||||
// . Eats 'n' characters and updates our position.
|
||||
void Stream::eat(int n)
|
||||
{
|
||||
for(int i=0;i<n;i++)
|
||||
get();
|
||||
}
|
||||
|
||||
void Stream::AdvanceCurrent()
|
||||
{
|
||||
if (!m_readahead.empty())
|
||||
{
|
||||
m_readahead.pop_front();
|
||||
m_mark.pos++;
|
||||
}
|
||||
|
||||
ReadAheadTo(0);
|
||||
}
|
||||
|
||||
bool Stream::_ReadAheadTo(size_t i) const
|
||||
{
|
||||
while (m_input.good() && (m_readahead.size() <= i))
|
||||
{
|
||||
switch (m_charSet)
|
||||
{
|
||||
case utf8: StreamInUtf8(); break;
|
||||
case utf16le: StreamInUtf16(); break;
|
||||
case utf16be: StreamInUtf16(); break;
|
||||
case utf32le: StreamInUtf32(); break;
|
||||
case utf32be: StreamInUtf32(); break;
|
||||
}
|
||||
}
|
||||
|
||||
// signal end of stream
|
||||
if(!m_input.good())
|
||||
m_readahead.push_back(Stream::eof());
|
||||
|
||||
return m_readahead.size() > i;
|
||||
}
|
||||
|
||||
void Stream::StreamInUtf8() const
|
||||
{
|
||||
unsigned char b = GetNextByte();
|
||||
if (m_input.good())
|
||||
{
|
||||
m_readahead.push_back(b);
|
||||
}
|
||||
}
|
||||
|
||||
void Stream::StreamInUtf16() const
|
||||
{
|
||||
unsigned long ch = 0;
|
||||
unsigned char bytes[2];
|
||||
int nBigEnd = (m_charSet == utf16be) ? 0 : 1;
|
||||
|
||||
bytes[0] = GetNextByte();
|
||||
bytes[1] = GetNextByte();
|
||||
if (!m_input.good())
|
||||
{
|
||||
return;
|
||||
}
|
||||
ch = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) |
|
||||
static_cast<unsigned long>(bytes[1 ^ nBigEnd]);
|
||||
|
||||
if (ch >= 0xDC00 && ch < 0xE000)
|
||||
{
|
||||
// Trailing (low) surrogate...ugh, wrong order
|
||||
QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);
|
||||
return;
|
||||
}
|
||||
else if (ch >= 0xD800 && ch < 0xDC00)
|
||||
{
|
||||
// ch is a leading (high) surrogate
|
||||
|
||||
// Four byte UTF-8 code point
|
||||
|
||||
// Read the trailing (low) surrogate
|
||||
for (;;)
|
||||
{
|
||||
bytes[0] = GetNextByte();
|
||||
bytes[1] = GetNextByte();
|
||||
if (!m_input.good())
|
||||
{
|
||||
QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);
|
||||
return;
|
||||
}
|
||||
unsigned long chLow = (static_cast<unsigned long>(bytes[nBigEnd]) << 8) |
|
||||
static_cast<unsigned long>(bytes[1 ^ nBigEnd]);
|
||||
if (chLow < 0xDC00 || ch >= 0xE000)
|
||||
{
|
||||
// Trouble...not a low surrogate. Dump a REPLACEMENT CHARACTER into the stream.
|
||||
QueueUnicodeCodepoint(m_readahead, CP_REPLACEMENT_CHARACTER);
|
||||
|
||||
// Deal with the next UTF-16 unit
|
||||
if (chLow < 0xD800 || ch >= 0xE000)
|
||||
{
|
||||
// Easiest case: queue the codepoint and return
|
||||
QueueUnicodeCodepoint(m_readahead, ch);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start the loop over with the new high surrogate
|
||||
ch = chLow;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Select the payload bits from the high surrogate
|
||||
ch &= 0x3FF;
|
||||
ch <<= 10;
|
||||
|
||||
// Include bits from low surrogate
|
||||
ch |= (chLow & 0x3FF);
|
||||
|
||||
// Add the surrogacy offset
|
||||
ch += 0x10000;
|
||||
}
|
||||
}
|
||||
|
||||
QueueUnicodeCodepoint(m_readahead, ch);
|
||||
}
|
||||
|
||||
inline char* ReadBuffer(unsigned char* pBuffer)
|
||||
{
|
||||
return reinterpret_cast<char*>(pBuffer);
|
||||
}
|
||||
|
||||
unsigned char Stream::GetNextByte() const
|
||||
{
|
||||
if (m_nPushedBack)
|
||||
{
|
||||
return m_bufPushback[--m_nPushedBack];
|
||||
}
|
||||
|
||||
if (m_nPrefetchedUsed >= m_nPrefetchedAvailable)
|
||||
{
|
||||
std::streambuf *pBuf = m_input.rdbuf();
|
||||
m_nPrefetchedAvailable = pBuf->sgetn(ReadBuffer(m_pPrefetched),
|
||||
YAML_PREFETCH_SIZE);
|
||||
m_nPrefetchedUsed = 0;
|
||||
if (!m_nPrefetchedAvailable)
|
||||
{
|
||||
m_input.setstate(std::ios_base::eofbit);
|
||||
}
|
||||
|
||||
if (0 == m_nPrefetchedAvailable)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return m_pPrefetched[m_nPrefetchedUsed++];
|
||||
}
|
||||
|
||||
void Stream::StreamInUtf32() const
|
||||
{
|
||||
static int indexes[2][4] = {
|
||||
{3, 2, 1, 0},
|
||||
{0, 1, 2, 3}
|
||||
};
|
||||
|
||||
unsigned long ch = 0;
|
||||
unsigned char bytes[4];
|
||||
int* pIndexes = (m_charSet == utf32be) ? indexes[1] : indexes[0];
|
||||
|
||||
bytes[0] = GetNextByte();
|
||||
bytes[1] = GetNextByte();
|
||||
bytes[2] = GetNextByte();
|
||||
bytes[3] = GetNextByte();
|
||||
if (!m_input.good())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
ch <<= 8;
|
||||
ch |= bytes[pIndexes[i]];
|
||||
}
|
||||
|
||||
QueueUnicodeCodepoint(m_readahead, ch);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "noncopyable.h"
|
||||
#include "mark.h"
|
||||
#include <deque>
|
||||
#include <ios>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
static const size_t MAX_PARSER_PUSHBACK = 8;
|
||||
|
||||
class Stream: private noncopyable
|
||||
{
|
||||
public:
|
||||
friend class StreamCharSource;
|
||||
|
||||
Stream(std::istream& input);
|
||||
~Stream();
|
||||
|
||||
operator bool() const;
|
||||
bool operator !() const { return !static_cast <bool>(*this); }
|
||||
|
||||
char peek() const;
|
||||
char get();
|
||||
std::string get(int n);
|
||||
void eat(int n = 1);
|
||||
|
||||
static char eof() { return 0x04; }
|
||||
|
||||
const Mark mark() const { return m_mark; }
|
||||
int pos() const { return m_mark.pos; }
|
||||
int line() const { return m_mark.line; }
|
||||
int column() const { return m_mark.column; }
|
||||
void ResetColumn() { m_mark.column = 0; }
|
||||
|
||||
private:
|
||||
enum CharacterSet {utf8, utf16le, utf16be, utf32le, utf32be};
|
||||
|
||||
std::istream& m_input;
|
||||
Mark m_mark;
|
||||
|
||||
CharacterSet m_charSet;
|
||||
unsigned char m_bufPushback[MAX_PARSER_PUSHBACK];
|
||||
mutable size_t m_nPushedBack;
|
||||
mutable std::deque<char> m_readahead;
|
||||
unsigned char* const m_pPrefetched;
|
||||
mutable size_t m_nPrefetchedAvailable;
|
||||
mutable size_t m_nPrefetchedUsed;
|
||||
|
||||
void AdvanceCurrent();
|
||||
char CharAt(size_t i) const;
|
||||
bool ReadAheadTo(size_t i) const;
|
||||
bool _ReadAheadTo(size_t i) const;
|
||||
void StreamInUtf8() const;
|
||||
void StreamInUtf16() const;
|
||||
void StreamInUtf32() const;
|
||||
unsigned char GetNextByte() const;
|
||||
};
|
||||
|
||||
// CharAt
|
||||
// . Unchecked access
|
||||
inline char Stream::CharAt(size_t i) const {
|
||||
return m_readahead[i];
|
||||
}
|
||||
|
||||
inline bool Stream::ReadAheadTo(size_t i) const {
|
||||
if(m_readahead.size() > i)
|
||||
return true;
|
||||
return _ReadAheadTo(i);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // STREAM_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "noncopyable.h"
|
||||
#include <cstddef>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class StreamCharSource
|
||||
{
|
||||
public:
|
||||
StreamCharSource(const Stream& stream): m_offset(0), m_stream(stream) {}
|
||||
StreamCharSource(const StreamCharSource& source): m_offset(source.m_offset), m_stream(source.m_stream) {}
|
||||
~StreamCharSource() {}
|
||||
|
||||
operator bool() const;
|
||||
char operator [] (std::size_t i) const { return m_stream.CharAt(m_offset + i); }
|
||||
bool operator !() const { return !static_cast<bool>(*this); }
|
||||
|
||||
const StreamCharSource operator + (int i) const;
|
||||
|
||||
private:
|
||||
std::size_t m_offset;
|
||||
const Stream& m_stream;
|
||||
|
||||
StreamCharSource& operator = (const StreamCharSource&); // non-assignable
|
||||
};
|
||||
|
||||
inline StreamCharSource::operator bool() const {
|
||||
return m_stream.ReadAheadTo(m_offset);
|
||||
}
|
||||
|
||||
inline const StreamCharSource StreamCharSource::operator + (int i) const {
|
||||
StreamCharSource source(*this);
|
||||
if(static_cast<int> (source.m_offset) + i >= 0)
|
||||
source.m_offset += i;
|
||||
else
|
||||
source.m_offset = 0;
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // STREAMCHARSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
class StringCharSource
|
||||
{
|
||||
public:
|
||||
StringCharSource(const char *str, std::size_t size): m_str(str), m_size(size), m_offset(0) {}
|
||||
|
||||
operator bool() const { return m_offset < m_size; }
|
||||
char operator [] (std::size_t i) const { return m_str[m_offset + i]; }
|
||||
bool operator !() const { return !static_cast<bool>(*this); }
|
||||
|
||||
const StringCharSource operator + (int i) const {
|
||||
StringCharSource source(*this);
|
||||
if(static_cast<int> (source.m_offset) + i >= 0)
|
||||
source.m_offset += i;
|
||||
else
|
||||
source.m_offset = 0;
|
||||
return source;
|
||||
}
|
||||
|
||||
StringCharSource& operator ++ () {
|
||||
++m_offset;
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
const char *m_str;
|
||||
std::size_t m_size;
|
||||
std::size_t m_offset;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // STRINGSOURCE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,79 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
|
||||
#include "mark.h"
|
||||
#include <ios>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
const std::string TokenNames[] = {
|
||||
"DIRECTIVE",
|
||||
"DOC_START",
|
||||
"DOC_END",
|
||||
"BLOCK_SEQ_START",
|
||||
"BLOCK_MAP_START",
|
||||
"BLOCK_SEQ_END",
|
||||
"BLOCK_MAP_END",
|
||||
"BLOCK_ENTRY",
|
||||
"FLOW_SEQ_START",
|
||||
"FLOW_MAP_START",
|
||||
"FLOW_SEQ_END",
|
||||
"FLOW_MAP_END",
|
||||
"FLOW_ENTRY",
|
||||
"KEY",
|
||||
"VALUE",
|
||||
"ANCHOR",
|
||||
"ALIAS",
|
||||
"TAG",
|
||||
"SCALAR"
|
||||
};
|
||||
|
||||
struct Token {
|
||||
// enums
|
||||
enum STATUS { VALID, INVALID, UNVERIFIED };
|
||||
enum TYPE {
|
||||
DIRECTIVE,
|
||||
DOC_START,
|
||||
DOC_END,
|
||||
BLOCK_SEQ_START,
|
||||
BLOCK_MAP_START,
|
||||
BLOCK_SEQ_END,
|
||||
BLOCK_MAP_END,
|
||||
BLOCK_ENTRY,
|
||||
FLOW_SEQ_START,
|
||||
FLOW_MAP_START,
|
||||
FLOW_SEQ_END,
|
||||
FLOW_MAP_END,
|
||||
FLOW_ENTRY,
|
||||
KEY,
|
||||
VALUE,
|
||||
ANCHOR,
|
||||
ALIAS,
|
||||
TAG,
|
||||
SCALAR
|
||||
};
|
||||
|
||||
// data
|
||||
Token(TYPE type_, const Mark& mark_): status(VALID), type(type_), mark(mark_) {}
|
||||
|
||||
friend std::ostream& operator << (std::ostream& out, const Token& token) {
|
||||
out << TokenNames[token.type] << std::string(": ") << token.value;
|
||||
for(std::size_t i=0;i<token.params.size();i++)
|
||||
out << std::string(" ") << token.params[i];
|
||||
return out;
|
||||
}
|
||||
|
||||
STATUS status;
|
||||
TYPE type;
|
||||
Mark mark;
|
||||
std::string value;
|
||||
std::vector <std::string> params;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TOKEN_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,2 @@
|
|||
add_executable(parse parse.cpp)
|
||||
target_link_libraries(parse yaml-cpp)
|
|
@ -0,0 +1,24 @@
|
|||
#include "yaml.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
std::ifstream fin;
|
||||
if(argc > 1)
|
||||
fin.open(argv[1]);
|
||||
|
||||
std::istream& input = (argc > 1 ? fin : std::cin);
|
||||
try {
|
||||
YAML::Parser parser(input);
|
||||
YAML::Node doc;
|
||||
while(parser.GetNextDocument(doc)) {
|
||||
YAML::Emitter emitter;
|
||||
emitter << doc;
|
||||
std::cout << emitter.c_str() << "\n";
|
||||
}
|
||||
} catch(const YAML::Exception& e) {
|
||||
std::cerr << e.what() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
prefix=@CMAKE_INSTALL_PREFIX@
|
||||
exec_prefix=@CMAKE_INSTALL_PREFIX@
|
||||
libdir=${prefix}/@LIB_INSTALL_DIR@
|
||||
includedir=${prefix}/@INCLUDE_INSTALL_DIR@
|
||||
|
||||
Name: Yaml-cpp
|
||||
Description: A YAML parser and emitter for C++
|
||||
Version: @YAML_CPP_VERSION@
|
||||
Requires:
|
||||
Libs: -L${libdir} -lyaml-cpp
|
||||
Cflags: -I${includedir}
|
|
@ -0,0 +1,216 @@
|
|||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9.00"
|
||||
Name="yaml-reader"
|
||||
ProjectGUID="{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}"
|
||||
RootNamespace="yamlreader"
|
||||
TargetFrameworkVersion="196613"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="2"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="include"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
WarningLevel="3"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="yamlcppd.lib"
|
||||
AdditionalLibraryDirectories="lib"
|
||||
GenerateDebugInformation="true"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="2"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
AdditionalIncludeDirectories="include"
|
||||
RuntimeLibrary="2"
|
||||
EnableFunctionLevelLinking="true"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="yamlcpp.lib"
|
||||
AdditionalLibraryDirectories="lib"
|
||||
GenerateDebugInformation="true"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\emittertests.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\main.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\parsertests.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\spectests.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\yaml-reader\tests.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
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"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,10 @@
|
|||
file(GLOB yaml-reader_headers *.h)
|
||||
file(GLOB yaml-reader_sources *.cpp)
|
||||
|
||||
add_executable(yaml-reader
|
||||
${yaml-reader_sources}
|
||||
${yaml-reader_headers}
|
||||
)
|
||||
target_link_libraries(yaml-reader yaml-cpp)
|
||||
|
||||
add_test(yaml-reader-test yaml-reader)
|
|
@ -0,0 +1,650 @@
|
|||
#include "tests.h"
|
||||
#include "yaml.h"
|
||||
|
||||
namespace Test
|
||||
{
|
||||
namespace Emitter {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// correct emitting
|
||||
|
||||
void SimpleScalar(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << "Hello, World!";
|
||||
desiredOutput = "Hello, World!";
|
||||
}
|
||||
|
||||
void SimpleSeq(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginSeq;
|
||||
out << "eggs";
|
||||
out << "bread";
|
||||
out << "milk";
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- eggs\n- bread\n- milk";
|
||||
}
|
||||
|
||||
void SimpleFlowSeq(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::Flow;
|
||||
out << YAML::BeginSeq;
|
||||
out << "Larry";
|
||||
out << "Curly";
|
||||
out << "Moe";
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "[Larry, Curly, Moe]";
|
||||
}
|
||||
|
||||
void EmptyFlowSeq(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::Flow;
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "[]";
|
||||
}
|
||||
|
||||
void NestedBlockSeq(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginSeq;
|
||||
out << "item 1";
|
||||
out << YAML::BeginSeq << "subitem 1" << "subitem 2" << YAML::EndSeq;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- item 1\n-\n - subitem 1\n - subitem 2";
|
||||
}
|
||||
|
||||
void NestedFlowSeq(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginSeq;
|
||||
out << "one";
|
||||
out << YAML::Flow << YAML::BeginSeq << "two" << "three" << YAML::EndSeq;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- one\n- [two, three]";
|
||||
}
|
||||
|
||||
void SimpleMap(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name";
|
||||
out << YAML::Value << "Ryan Braun";
|
||||
out << YAML::Key << "position";
|
||||
out << YAML::Value << "3B";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "name: Ryan Braun\nposition: 3B";
|
||||
}
|
||||
|
||||
void SimpleFlowMap(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::Flow;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "shape";
|
||||
out << YAML::Value << "square";
|
||||
out << YAML::Key << "color";
|
||||
out << YAML::Value << "blue";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "{shape: square, color: blue}";
|
||||
}
|
||||
|
||||
void MapAndList(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name";
|
||||
out << YAML::Value << "Barack Obama";
|
||||
out << YAML::Key << "children";
|
||||
out << YAML::Value << YAML::BeginSeq << "Sasha" << "Malia" << YAML::EndSeq;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "name: Barack Obama\nchildren:\n - Sasha\n - Malia";
|
||||
}
|
||||
|
||||
void ListAndMap(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginSeq;
|
||||
out << "item 1";
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "pens" << YAML::Value << 8;
|
||||
out << YAML::Key << "pencils" << YAML::Value << 14;
|
||||
out << YAML::EndMap;
|
||||
out << "item 2";
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- item 1\n-\n pens: 8\n pencils: 14\n- item 2";
|
||||
}
|
||||
|
||||
void NestedBlockMap(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name";
|
||||
out << YAML::Value << "Fred";
|
||||
out << YAML::Key << "grades";
|
||||
out << YAML::Value;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "algebra" << YAML::Value << "A";
|
||||
out << YAML::Key << "physics" << YAML::Value << "C+";
|
||||
out << YAML::Key << "literature" << YAML::Value << "B";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "name: Fred\ngrades:\n algebra: A\n physics: C+\n literature: B";
|
||||
}
|
||||
|
||||
void NestedFlowMap(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::Flow;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name";
|
||||
out << YAML::Value << "Fred";
|
||||
out << YAML::Key << "grades";
|
||||
out << YAML::Value;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "algebra" << YAML::Value << "A";
|
||||
out << YAML::Key << "physics" << YAML::Value << "C+";
|
||||
out << YAML::Key << "literature" << YAML::Value << "B";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "{name: Fred, grades: {algebra: A, physics: C+, literature: B}}";
|
||||
}
|
||||
|
||||
void MapListMix(YAML::Emitter& out, std::string& desiredOutput) {
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name";
|
||||
out << YAML::Value << "Bob";
|
||||
out << YAML::Key << "position";
|
||||
out << YAML::Value;
|
||||
out << YAML::Flow << YAML::BeginSeq << 2 << 4 << YAML::EndSeq;
|
||||
out << YAML::Key << "invincible" << YAML::Value << YAML::OnOffBool << false;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "name: Bob\nposition: [2, 4]\ninvincible: off";
|
||||
}
|
||||
|
||||
void SimpleLongKey(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::LongKey;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "height";
|
||||
out << YAML::Value << "5'9\"";
|
||||
out << YAML::Key << "weight";
|
||||
out << YAML::Value << 145;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "? height\n: 5'9\"\n? weight\n: 145";
|
||||
}
|
||||
|
||||
void SingleLongKey(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "age";
|
||||
out << YAML::Value << "24";
|
||||
out << YAML::LongKey << YAML::Key << "height";
|
||||
out << YAML::Value << "5'9\"";
|
||||
out << YAML::Key << "weight";
|
||||
out << YAML::Value << 145;
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "age: 24\n? height\n: 5'9\"\nweight: 145";
|
||||
}
|
||||
|
||||
void ComplexLongKey(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::LongKey;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << YAML::BeginSeq << 1 << 3 << YAML::EndSeq;
|
||||
out << YAML::Value << "monster";
|
||||
out << YAML::Key << YAML::Flow << YAML::BeginSeq << 2 << 0 << YAML::EndSeq;
|
||||
out << YAML::Value << "demon";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "?\n - 1\n - 3\n: monster\n? [2, 0]\n: demon";
|
||||
}
|
||||
|
||||
void AutoLongKey(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << YAML::BeginSeq << 1 << 3 << YAML::EndSeq;
|
||||
out << YAML::Value << "monster";
|
||||
out << YAML::Key << YAML::Flow << YAML::BeginSeq << 2 << 0 << YAML::EndSeq;
|
||||
out << YAML::Value << "demon";
|
||||
out << YAML::Key << "the origin";
|
||||
out << YAML::Value << "angel";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "?\n - 1\n - 3\n: monster\n? [2, 0]\n: demon\nthe origin: angel";
|
||||
}
|
||||
|
||||
void ScalarFormat(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << "simple scalar";
|
||||
out << YAML::SingleQuoted << "explicit single-quoted scalar";
|
||||
out << YAML::DoubleQuoted << "explicit double-quoted scalar";
|
||||
out << "auto-detected\ndouble-quoted scalar";
|
||||
out << "a non-\"auto-detected\" double-quoted scalar";
|
||||
out << YAML::Literal << "literal scalar\nthat may span\nmany, many\nlines and have \"whatever\" crazy\tsymbols that we like";
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- simple scalar\n- 'explicit single-quoted scalar'\n- \"explicit double-quoted scalar\"\n- \"auto-detected\\x0adouble-quoted scalar\"\n- a non-\"auto-detected\" double-quoted scalar\n- |\n literal scalar\n that may span\n many, many\n lines and have \"whatever\" crazy\tsymbols that we like";
|
||||
}
|
||||
|
||||
void AutoLongKeyScalar(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << YAML::Literal << "multi-line\nscalar";
|
||||
out << YAML::Value << "and its value";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "? |\n multi-line\n scalar\n: and its value";
|
||||
}
|
||||
|
||||
void LongKeyFlowMap(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::Flow;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "simple key";
|
||||
out << YAML::Value << "and value";
|
||||
out << YAML::LongKey << YAML::Key << "long key";
|
||||
out << YAML::Value << "and its value";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "{simple key: and value, ? long key: and its value}";
|
||||
}
|
||||
|
||||
void BlockMapAsKey(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key" << YAML::Value << "value";
|
||||
out << YAML::Key << "next key" << YAML::Value << "next value";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::Value;
|
||||
out << "total value";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "?\n key: value\n next key: next value\n: total value";
|
||||
}
|
||||
|
||||
void AliasAndAnchor(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Anchor("fred");
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "name" << YAML::Value << "Fred";
|
||||
out << YAML::Key << "age" << YAML::Value << 42;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::Alias("fred");
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- &fred\n name: Fred\n age: 42\n- *fred";
|
||||
}
|
||||
|
||||
void AliasAndAnchorWithNull(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Anchor("fred") << YAML::Null;
|
||||
out << YAML::Alias("fred");
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- &fred ~\n- *fred";
|
||||
}
|
||||
|
||||
void ComplexDoc(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "receipt";
|
||||
out << YAML::Value << "Oz-Ware Purchase Invoice";
|
||||
out << YAML::Key << "date";
|
||||
out << YAML::Value << "2007-08-06";
|
||||
out << YAML::Key << "customer";
|
||||
out << YAML::Value;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "given";
|
||||
out << YAML::Value << "Dorothy";
|
||||
out << YAML::Key << "family";
|
||||
out << YAML::Value << "Gale";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::Key << "items";
|
||||
out << YAML::Value;
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "part_no";
|
||||
out << YAML::Value << "A4786";
|
||||
out << YAML::Key << "descrip";
|
||||
out << YAML::Value << "Water Bucket (Filled)";
|
||||
out << YAML::Key << "price";
|
||||
out << YAML::Value << 1.47;
|
||||
out << YAML::Key << "quantity";
|
||||
out << YAML::Value << 4;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "part_no";
|
||||
out << YAML::Value << "E1628";
|
||||
out << YAML::Key << "descrip";
|
||||
out << YAML::Value << "High Heeled \"Ruby\" Slippers";
|
||||
out << YAML::Key << "price";
|
||||
out << YAML::Value << 100.27;
|
||||
out << YAML::Key << "quantity";
|
||||
out << YAML::Value << 1;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndSeq;
|
||||
out << YAML::Key << "bill-to";
|
||||
out << YAML::Value << YAML::Anchor("id001");
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "street";
|
||||
out << YAML::Value << YAML::Literal << "123 Tornado Alley\nSuite 16";
|
||||
out << YAML::Key << "city";
|
||||
out << YAML::Value << "East Westville";
|
||||
out << YAML::Key << "state";
|
||||
out << YAML::Value << "KS";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::Key << "ship-to";
|
||||
out << YAML::Value << YAML::Alias("id001");
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "receipt: Oz-Ware Purchase Invoice\ndate: 2007-08-06\ncustomer:\n given: Dorothy\n family: Gale\nitems:\n -\n part_no: A4786\n descrip: Water Bucket (Filled)\n price: 1.47\n quantity: 4\n -\n part_no: E1628\n descrip: High Heeled \"Ruby\" Slippers\n price: 100.27\n quantity: 1\nbill-to: &id001\n street: |\n 123 Tornado Alley\n Suite 16\n city: East Westville\n state: KS\nship-to: *id001";
|
||||
}
|
||||
|
||||
void STLContainers(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
std::vector <int> primes;
|
||||
primes.push_back(2);
|
||||
primes.push_back(3);
|
||||
primes.push_back(5);
|
||||
primes.push_back(7);
|
||||
primes.push_back(11);
|
||||
primes.push_back(13);
|
||||
out << YAML::Flow << primes;
|
||||
std::map <std::string, int> ages;
|
||||
ages["Daniel"] = 26;
|
||||
ages["Jesse"] = 24;
|
||||
out << ages;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- [2, 3, 5, 7, 11, 13]\n-\n Daniel: 26\n Jesse: 24";
|
||||
}
|
||||
|
||||
void SimpleComment(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "method";
|
||||
out << YAML::Value << "least squares" << YAML::Comment("should we change this method?");
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "method: least squares # should we change this method?";
|
||||
}
|
||||
|
||||
void MultiLineComment(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << "item 1" << YAML::Comment("really really long\ncomment that couldn't possibly\nfit on one line");
|
||||
out << "item 2";
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "- item 1 # really really long\n # comment that couldn't possibly\n # fit on one line\n- item 2";
|
||||
}
|
||||
|
||||
void ComplexComments(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::LongKey << YAML::Key << "long key" << YAML::Comment("long key");
|
||||
out << YAML::Value << "value";
|
||||
out << YAML::EndMap;
|
||||
|
||||
desiredOutput = "? long key # long key\n: value";
|
||||
}
|
||||
|
||||
void Indentation(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::Indent(4);
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key 1" << YAML::Value << "value 1";
|
||||
out << YAML::Key << "key 2" << YAML::Value << YAML::BeginSeq << "a" << "b" << "c" << YAML::EndSeq;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "-\n key 1: value 1\n key 2:\n - a\n - b\n - c";
|
||||
}
|
||||
|
||||
void SimpleGlobalSettings(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out.SetIndent(4);
|
||||
out.SetMapFormat(YAML::LongKey);
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key 1" << YAML::Value << "value 1";
|
||||
out << YAML::Key << "key 2" << YAML::Value << YAML::Flow << YAML::BeginSeq << "a" << "b" << "c" << YAML::EndSeq;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "-\n ? key 1\n : value 1\n ? key 2\n : [a, b, c]";
|
||||
}
|
||||
|
||||
void ComplexGlobalSettings(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Block;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key 1" << YAML::Value << "value 1";
|
||||
out << YAML::Key << "key 2" << YAML::Value;
|
||||
out.SetSeqFormat(YAML::Flow);
|
||||
out << YAML::BeginSeq << "a" << "b" << "c" << YAML::EndSeq;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << YAML::BeginSeq << 1 << 2 << YAML::EndSeq;
|
||||
out << YAML::Value << YAML::BeginMap << YAML::Key << "a" << YAML::Value << "b" << YAML::EndMap;
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
desiredOutput = "-\n key 1: value 1\n key 2: [a, b, c]\n-\n ? [1, 2]\n :\n a: b";
|
||||
}
|
||||
|
||||
void Null(YAML::Emitter& out, std::string& desiredOutput)
|
||||
{
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Null;
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "null value" << YAML::Value << YAML::Null;
|
||||
out << YAML::Key << YAML::Null << YAML::Value << "null key";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndSeq;
|
||||
|
||||
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
|
||||
|
||||
void ExtraEndSeq(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::UNEXPECTED_END_SEQ;
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << "Hello";
|
||||
out << "World";
|
||||
out << YAML::EndSeq;
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void ExtraEndMap(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::UNEXPECTED_END_MAP;
|
||||
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "Hello" << YAML::Value << "World";
|
||||
out << YAML::EndMap;
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
|
||||
void BadSingleQuoted(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::SINGLE_QUOTED_CHAR;
|
||||
|
||||
out << YAML::SingleQuoted << "Hello\nWorld";
|
||||
}
|
||||
|
||||
void InvalidAnchor(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::INVALID_ANCHOR;
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Anchor("new\nline") << "Test";
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void InvalidAlias(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::INVALID_ALIAS;
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Alias("new\nline");
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void MissingKey(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::EXPECTED_KEY_TOKEN;
|
||||
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key" << YAML::Value << "value";
|
||||
out << "missing key" << YAML::Value << "value";
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
|
||||
void MissingValue(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::EXPECTED_VALUE_TOKEN;
|
||||
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "key" << "value";
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
|
||||
void UnexpectedKey(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::UNEXPECTED_KEY_TOKEN;
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Key << "hi";
|
||||
out << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void UnexpectedValue(YAML::Emitter& out, std::string& desiredError)
|
||||
{
|
||||
desiredError = YAML::ErrorMsg::UNEXPECTED_VALUE_TOKEN;
|
||||
|
||||
out << YAML::BeginSeq;
|
||||
out << YAML::Value << "hi";
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#include "tests.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef WINDOWS
|
||||
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
||||
#endif // WINDOWS
|
||||
Test::RunAll();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,888 @@
|
|||
#include "tests.h"
|
||||
#include "yaml.h"
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Test
|
||||
{
|
||||
namespace Parser {
|
||||
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "Hello, World!";
|
||||
desiredOutput = "Hello, World!";
|
||||
}
|
||||
|
||||
void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
"normal scalar, but\n"
|
||||
"over several lines";
|
||||
desiredOutput = "normal scalar, but over several lines";
|
||||
}
|
||||
|
||||
void LiteralScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
"|\n"
|
||||
" literal scalar - so we can draw ASCII:\n"
|
||||
" \n"
|
||||
" - -\n"
|
||||
" | - |\n"
|
||||
" -----\n";
|
||||
desiredOutput =
|
||||
"literal scalar - so we can draw ASCII:\n"
|
||||
"\n"
|
||||
" - -\n"
|
||||
" | - |\n"
|
||||
" -----\n";
|
||||
}
|
||||
|
||||
void FoldedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
">\n"
|
||||
" and a folded scalar... so we\n"
|
||||
" can just keep writing various\n"
|
||||
" things. And if we want to keep indentation:\n"
|
||||
" \n"
|
||||
" we just indent a little\n"
|
||||
" see, this stays indented";
|
||||
desiredOutput =
|
||||
"and a folded scalar... so we"
|
||||
" can just keep writing various"
|
||||
" things. And if we want to keep indentation:\n"
|
||||
"\n"
|
||||
" we just indent a little\n"
|
||||
" see, this stays indented";
|
||||
}
|
||||
|
||||
void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
">-\n"
|
||||
" Here's a folded scalar\n"
|
||||
" that gets chomped.";
|
||||
desiredOutput =
|
||||
"Here's a folded scalar"
|
||||
" that gets chomped.";
|
||||
}
|
||||
|
||||
void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
"|-\n"
|
||||
" Here's a literal scalar\n"
|
||||
" that gets chomped.";
|
||||
desiredOutput =
|
||||
"Here's a literal scalar\n"
|
||||
"that gets chomped.";
|
||||
}
|
||||
|
||||
void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar =
|
||||
">2\n"
|
||||
" Here's a folded scalar\n"
|
||||
" that starts with some indentation.";
|
||||
desiredOutput =
|
||||
" Here's a folded scalar\n"
|
||||
"that starts with some indentation.";
|
||||
}
|
||||
|
||||
void ColonScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "::vector";
|
||||
desiredOutput = "::vector";
|
||||
}
|
||||
|
||||
void QuotedScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "\": - ()\"";
|
||||
desiredOutput = ": - ()";
|
||||
}
|
||||
|
||||
void CommaScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "Up, up, and away!";
|
||||
desiredOutput = "Up, up, and away!";
|
||||
}
|
||||
|
||||
void DashScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "-123";
|
||||
desiredOutput = "-123";
|
||||
}
|
||||
|
||||
void URLScalar(std::string& inputScalar, std::string& desiredOutput)
|
||||
{
|
||||
inputScalar = "http://example.com/foo#bar";
|
||||
desiredOutput = "http://example.com/foo#bar";
|
||||
}
|
||||
|
||||
bool SimpleSeq()
|
||||
{
|
||||
std::string input =
|
||||
"- eggs\n"
|
||||
"- bread\n"
|
||||
"- milk";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "eggs")
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != "bread")
|
||||
return false;
|
||||
doc[2] >> output;
|
||||
if(output != "milk")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SimpleMap()
|
||||
{
|
||||
std::string input =
|
||||
"name: Prince Fielder\n"
|
||||
"position: 1B\n"
|
||||
"bats: L";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["name"] >> output;
|
||||
if(output != "Prince Fielder")
|
||||
return false;
|
||||
doc["position"] >> output;
|
||||
if(output != "1B")
|
||||
return false;
|
||||
doc["bats"] >> output;
|
||||
if(output != "L")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowSeq()
|
||||
{
|
||||
std::string input = "[ 2 , 3, 5 , 7, 11]";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
int output;
|
||||
doc[0] >> output;
|
||||
if(output != 2)
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != 3)
|
||||
return false;
|
||||
doc[2] >> output;
|
||||
if(output != 5)
|
||||
return false;
|
||||
doc[3] >> output;
|
||||
if(output != 7)
|
||||
return false;
|
||||
doc[4] >> output;
|
||||
if(output != 11)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowMap()
|
||||
{
|
||||
std::string input = "{hr: 65, avg: 0.278}";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["hr"] >> output;
|
||||
if(output != "65")
|
||||
return false;
|
||||
doc["avg"] >> output;
|
||||
if(output != "0.278")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowMapWithOmittedKey()
|
||||
{
|
||||
std::string input = "{: omitted key}";
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc[YAML::Null] >> output;
|
||||
if(output != "omitted key")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowMapWithOmittedValue()
|
||||
{
|
||||
std::string input = "{a: b, c:, d:}";
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["a"] >> output;
|
||||
if(output != "b")
|
||||
return false;
|
||||
if(!IsNull(doc["c"]))
|
||||
return false;
|
||||
if(!IsNull(doc["d"]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowMapWithSoloEntry()
|
||||
{
|
||||
std::string input = "{a: b, c, d: e}";
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["a"] >> output;
|
||||
if(output != "b")
|
||||
return false;
|
||||
if(!IsNull(doc["c"]))
|
||||
return false;
|
||||
doc["d"] >> output;
|
||||
if(output != "e")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowMapEndingWithSoloEntry()
|
||||
{
|
||||
std::string input = "{a: b, c}";
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["a"] >> output;
|
||||
if(output != "b")
|
||||
return false;
|
||||
if(!IsNull(doc["c"]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QuotedSimpleKeys()
|
||||
{
|
||||
std::string KeyValue[3] = { "\"double\": double\n", "'single': single\n", "plain: plain\n" };
|
||||
|
||||
int perm[3] = { 0, 1, 2 };
|
||||
do {
|
||||
std::string input = KeyValue[perm[0]] + KeyValue[perm[1]] + KeyValue[perm[2]];
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["double"] >> output;
|
||||
if(output != "double")
|
||||
return false;
|
||||
doc["single"] >> output;
|
||||
if(output != "single")
|
||||
return false;
|
||||
doc["plain"] >> output;
|
||||
if(output != "plain")
|
||||
return false;
|
||||
} while(std::next_permutation(perm, perm + 3));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CompressedMapAndSeq()
|
||||
{
|
||||
std::string input = "key:\n- one\n- two";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
const YAML::Node& seq = doc["key"];
|
||||
if(seq.size() != 2)
|
||||
return false;
|
||||
|
||||
std::string output;
|
||||
seq[0] >> output;
|
||||
if(output != "one")
|
||||
return false;
|
||||
seq[1] >> output;
|
||||
if(output != "two")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NullBlockSeqEntry()
|
||||
{
|
||||
std::string input = "- hello\n-\n- world";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "hello")
|
||||
return false;
|
||||
if(!IsNull(doc[1]))
|
||||
return false;
|
||||
doc[2] >> output;
|
||||
if(output != "world")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NullBlockMapKey()
|
||||
{
|
||||
std::string input = ": empty key";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc[YAML::Null] >> output;
|
||||
if(output != "empty key")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NullBlockMapValue()
|
||||
{
|
||||
std::string input = "empty value:";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(!IsNull(doc["empty value"]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SimpleAlias()
|
||||
{
|
||||
std::string input = "- &alias test\n- *alias";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "test")
|
||||
return false;
|
||||
|
||||
doc[1] >> output;
|
||||
if(output != "test")
|
||||
return false;
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AliasWithNull()
|
||||
{
|
||||
std::string input = "- &alias\n- *alias";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(!IsNull(doc[0]))
|
||||
return false;
|
||||
|
||||
if(!IsNull(doc[1]))
|
||||
return false;
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AnchorInSimpleKey()
|
||||
{
|
||||
std::string input = "- &a b: c\n- *a";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
std::string output;
|
||||
doc[0]["b"] >> output;
|
||||
if(output != "c")
|
||||
return false;
|
||||
|
||||
doc[1] >> output;
|
||||
if(output != "b")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AliasAsSimpleKey()
|
||||
{
|
||||
std::string input = "- &a b\n- *a: c";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "b")
|
||||
return false;
|
||||
|
||||
doc[1]["b"] >> output;
|
||||
if(output != "c")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExplicitDoc()
|
||||
{
|
||||
std::string input = "---\n- one\n- two";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "one")
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != "two")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleDocs()
|
||||
{
|
||||
std::string input = "---\nname: doc1\n---\nname: doc2";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string output;
|
||||
doc["name"] >> output;
|
||||
if(output != "doc1")
|
||||
return false;
|
||||
|
||||
if(!parser)
|
||||
return false;
|
||||
|
||||
parser.GetNextDocument(doc);
|
||||
doc["name"] >> output;
|
||||
if(output != "doc2")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ExplicitEndDoc()
|
||||
{
|
||||
std::string input = "- one\n- two\n...\n...";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
|
||||
std::string output;
|
||||
doc[0] >> output;
|
||||
if(output != "one")
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != "two")
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultipleDocsWithSomeExplicitIndicators()
|
||||
{
|
||||
std::string input =
|
||||
"- one\n- two\n...\n"
|
||||
"---\nkey: value\n...\n...\n"
|
||||
"- three\n- four\n"
|
||||
"---\nkey: value";
|
||||
|
||||
std::stringstream stream(input);
|
||||
YAML::Parser parser(stream);
|
||||
YAML::Node doc;
|
||||
std::string output;
|
||||
|
||||
parser.GetNextDocument(doc);
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
doc[0] >> output;
|
||||
if(output != "one")
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != "two")
|
||||
return false;
|
||||
|
||||
parser.GetNextDocument(doc);
|
||||
doc["key"] >> output;
|
||||
if(output != "value")
|
||||
return false;
|
||||
|
||||
parser.GetNextDocument(doc);
|
||||
if(doc.size() != 2)
|
||||
return false;
|
||||
doc[0] >> output;
|
||||
if(output != "three")
|
||||
return false;
|
||||
doc[1] >> output;
|
||||
if(output != "four")
|
||||
return false;
|
||||
|
||||
parser.GetNextDocument(doc);
|
||||
doc["key"] >> output;
|
||||
if(output != "value")
|
||||
return false;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
namespace Test {
|
||||
bool RunSpecTests();
|
||||
}
|
||||
|
||||
#endif // SPECTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include "tests.h"
|
||||
#include "emittertests.h"
|
||||
#include "parsertests.h"
|
||||
#include "spectests.h"
|
||||
#include "yaml.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
namespace Test
|
||||
{
|
||||
void RunAll()
|
||||
{
|
||||
bool passed = true;
|
||||
if(!RunParserTests())
|
||||
passed = false;
|
||||
|
||||
if(!RunEmitterTests())
|
||||
passed = false;
|
||||
|
||||
if(!RunSpecTests())
|
||||
passed = false;
|
||||
|
||||
if(passed)
|
||||
std::cout << "All tests passed!\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
#define TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Test {
|
||||
void RunAll();
|
||||
|
||||
namespace Parser {
|
||||
// scalar tests
|
||||
void SimpleScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void MultiLineScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void LiteralScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void FoldedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void ChompedFoldedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void ChompedLiteralScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void FoldedScalarWithIndent(std::string& inputScalar, std::string& desiredOutput);
|
||||
void ColonScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void QuotedScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void CommaScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void DashScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
void URLScalar(std::string& inputScalar, std::string& desiredOutput);
|
||||
|
||||
// misc tests
|
||||
bool SimpleSeq();
|
||||
bool SimpleMap();
|
||||
bool FlowSeq();
|
||||
bool FlowMap();
|
||||
bool FlowMapWithOmittedKey();
|
||||
bool FlowMapWithOmittedValue();
|
||||
bool FlowMapWithSoloEntry();
|
||||
bool FlowMapEndingWithSoloEntry();
|
||||
bool QuotedSimpleKeys();
|
||||
bool CompressedMapAndSeq();
|
||||
bool NullBlockSeqEntry();
|
||||
bool NullBlockMapKey();
|
||||
bool NullBlockMapValue();
|
||||
bool SimpleAlias();
|
||||
bool AliasWithNull();
|
||||
bool AnchorInSimpleKey();
|
||||
bool AliasAsSimpleKey();
|
||||
bool ExplicitDoc();
|
||||
bool MultipleDocs();
|
||||
bool ExplicitEndDoc();
|
||||
bool MultipleDocsWithSomeExplicitIndicators();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
|
@ -0,0 +1,4 @@
|
|||
--- &list
|
||||
- This document contains a recursive list.
|
||||
- *list
|
||||
...
|
|
@ -0,0 +1,29 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 10.00
|
||||
# Visual Studio 2008
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "yaml-reader", "yaml-reader.vcproj", "{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{3104AB4E-CD31-4F47-95E9-0E8D9374E15D} = {3104AB4E-CD31-4F47-95E9-0E8D9374E15D}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "yamlcpp", "yamlcpp.vcproj", "{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{E8CC0D8A-D784-4A6B-B78B-ACEA13F9FB0B}.Release|Win32.Build.0 = Release|Win32
|
||||
{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,419 @@
|
|||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="9.00"
|
||||
Name="yamlcpp"
|
||||
ProjectGUID="{3104AB4E-CD31-4F47-95E9-0E8D9374E15D}"
|
||||
RootNamespace="yamlcpp"
|
||||
Keyword="Win32Proj"
|
||||
TargetFrameworkVersion="196613"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="include"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_LIB"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="lib\$(ProjectName)d.lib"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
EnableIntrinsicFunctions="true"
|
||||
AdditionalIncludeDirectories="include"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
|
||||
RuntimeLibrary="2"
|
||||
EnableFunctionLevelLinking="true"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="4"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLibrarianTool"
|
||||
OutputFile="lib\$(ProjectName).lib"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
|
||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||
>
|
||||
<Filter
|
||||
Name="Parser"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\parser.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\parserstate.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Representation"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\aliascontent.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\content.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\conversion.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\iterator.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\map.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\node.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\null.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scalar.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\sequence.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Scanner"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\exp.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\regex.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scanner.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scanscalar.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scantoken.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\simplekey.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stream.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Emitter"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\emitter.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\emitterstate.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\emitterutils.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\ostream.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl;inc;xsd"
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\crt.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\exceptions.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\indentation.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\noncopyable.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\yaml.h"
|
||||
>
|
||||
</File>
|
||||
<Filter
|
||||
Name="Parser"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\parser.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\parserstate.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Representation"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\aliascontent.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\content.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\conversion.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\iterator.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\iterpriv.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\ltnode.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\map.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\node.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scalar.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\sequence.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Scanner"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\exp.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\regex.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\regeximpl.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scanner.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\scanscalar.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stream.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\streamcharsource.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\stringsource.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\token.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Emitter"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\include\emitter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\emittermanip.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\emitterstate.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\emitterutils.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\ostream.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\stlemitter.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
|
||||
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
|
||||
>
|
||||
</Filter>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
Loading…
Reference in New Issue