From ec2ecad197a54e56eee6f2a58fc35353afea3f8a Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Thu, 7 Aug 2008 03:30:56 +0000 Subject: [PATCH] Added CMake scripts for other platforms\nFixed some bugs that gcc complained about\nFixed CR/LF vs LF bug --- CMakeLists.txt | 4 ++++ include/crt.h | 3 ++- include/exceptions.h | 1 + include/yaml.h | 3 ++- src/CMakeLists.txt | 7 +++++++ src/content.h | 6 +++--- src/exp.h | 2 +- src/map.cpp | 12 ++++------- src/map.h | 2 +- src/node.cpp | 21 ++++++++++++------- src/parser.cpp | 4 ++-- src/regex.cpp | 15 ++++++++++++- src/regex.h | 10 ++++++--- src/scalar.cpp | 5 ----- src/scalar.h | 2 +- src/sequence.cpp | 12 ++++------- src/sequence.h | 2 +- src/stream.cpp | 21 +++++++++++++++++-- src/stream.h | 10 ++++----- src/token.h | 4 ++-- yaml-reader/CMakeLists.txt | 6 ++++++ yaml-reader/main.cpp | 32 +++++++++++++--------------- yaml-reader/tests.cpp | 31 +++++++++++++++++---------- yaml-reader/tests.h | 4 ++-- yaml-reader/tests/directives.yaml | 5 +++++ yaml-reader/tests/mixed.yaml | 32 ++++++++++++++++++++++++++++ yaml-reader/tests/out.yaml | 8 +++++++ yaml-reader/tests/scalars.yaml | 35 +++++++++++++++++++++++++++++++ yaml-reader/tests/simple.yaml | 13 ++++++++++++ yaml-reader/tests/test.yaml | 3 +++ 30 files changed, 233 insertions(+), 82 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 yaml-reader/CMakeLists.txt create mode 100644 yaml-reader/tests/directives.yaml create mode 100644 yaml-reader/tests/mixed.yaml create mode 100644 yaml-reader/tests/out.yaml create mode 100644 yaml-reader/tests/scalars.yaml create mode 100644 yaml-reader/tests/simple.yaml create mode 100644 yaml-reader/tests/test.yaml diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..789f731 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +project (YAML_CPP) +set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) +add_subdirectory (src) +add_subdirectory (yaml-reader) diff --git a/include/crt.h b/include/crt.h index 82f13ec..500ef01 100644 --- a/include/crt.h +++ b/include/crt.h @@ -7,4 +7,5 @@ #include #include -#endif // _DEBUG \ No newline at end of file +#endif // _DEBUG + diff --git a/include/exceptions.h b/include/exceptions.h index 0e6031e..3185b13 100644 --- a/include/exceptions.h +++ b/include/exceptions.h @@ -10,6 +10,7 @@ namespace YAML public: ParserException(int line_, int column_, const std::string& msg_) : line(line_), column(column_), msg(msg_) {} + virtual ~ParserException() throw () {} int line, column; std::string msg; diff --git a/include/yaml.h b/include/yaml.h index de7f222..f64e07a 100644 --- a/include/yaml.h +++ b/include/yaml.h @@ -4,4 +4,5 @@ #include "parser.h" #include "node.h" #include "iterator.h" -#include "exceptions.h" \ No newline at end of file +#include "exceptions.h" + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..d2cd7bf --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +set(FILES content.cpp iterator.cpp node.cpp parserstate.cpp + scalar.cpp scanscalar.cpp sequence.cpp stream.cpp + exp.cpp map.cpp parser.cpp regex.cpp scanner.cpp + scantoken.cpp simplekey.cpp) + +include_directories(${YAML_CPP_SOURCE_DIR}/include) +add_library(yaml-cpp ${FILES}) diff --git a/src/content.h b/src/content.h index 9ea6a95..166932e 100644 --- a/src/content.h +++ b/src/content.h @@ -16,8 +16,6 @@ namespace YAML class Sequence; class Map; - enum CONTENT_TYPE; - class Content { public: @@ -33,7 +31,9 @@ namespace YAML virtual bool GetEnd(std::map ::const_iterator& it) const { return false; } virtual Node *GetNode(unsigned i) const { return 0; } virtual unsigned GetSize() const { return 0; } - virtual CONTENT_TYPE GetType() const = 0; + virtual bool IsScalar() const { return false; } + virtual bool IsMap() const { return false; } + virtual bool IsSequence() const { return false; } // extraction virtual void Read(std::string& s) { throw InvalidScalar(); } diff --git a/src/exp.h b/src/exp.h index 530973e..d2bd469 100644 --- a/src/exp.h +++ b/src/exp.h @@ -14,7 +14,7 @@ namespace YAML { // misc const RegEx Blank = RegEx(' ') || RegEx('\t'); - const RegEx Break = RegEx('\n'); + 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'); diff --git a/src/map.cpp b/src/map.cpp index 02c2da0..42c381e 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -3,7 +3,8 @@ #include "node.h" #include "scanner.h" #include "token.h" -#include "exceptions.h" +#include "exceptions.h" +#include namespace YAML { @@ -142,7 +143,7 @@ namespace YAML void Map::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) { if(startedLine && !onlyOneCharOnLine) - out << std::endl; + out << "\n"; for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) { if((startedLine && !onlyOneCharOnLine) || it != m_data.begin()) { @@ -160,12 +161,7 @@ namespace YAML } if(m_data.empty()) - out << std::endl; - } - - CONTENT_TYPE Map::GetType() const - { - return CT_MAP; + out << "\n"; } int Map::Compare(Content *pContent) diff --git a/src/map.h b/src/map.h index 5f34072..8e58887 100644 --- a/src/map.h +++ b/src/map.h @@ -19,7 +19,7 @@ namespace YAML virtual void Parse(Scanner *pScanner, const ParserState& state); virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine); - virtual CONTENT_TYPE GetType() const; + virtual bool IsMap() const { return true; } // ordering virtual int Compare(Content *pContent); diff --git a/src/node.cpp b/src/node.cpp index e1f323d..ac3cbec 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -122,23 +122,23 @@ namespace YAML // write anchor/alias if(m_anchor != "") { if(m_alias) - out << "*"; + out << std::string("*"); else - out << "&"; - out << m_anchor << " "; + out << std::string("&"); + out << m_anchor << std::string(" "); startedLine = true; onlyOneCharOnLine = false; } // write tag if(m_tag != "") { - out << "!<" << m_tag << "> "; + out << std::string("!<") << m_tag << std::string("> "); startedLine = true; onlyOneCharOnLine = false; } if(!m_pContent) { - out << std::endl; + out << std::string("\n"); } else { m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine); } @@ -148,8 +148,15 @@ namespace YAML { if(!m_pContent) return CT_NONE; - - return m_pContent->GetType(); + + 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 diff --git a/src/parser.cpp b/src/parser.cpp index 7d4169d..7566526 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -121,12 +121,12 @@ namespace YAML } void Parser::PrintTokens(std::ostream& out) - { + { while(1) { if(m_pScanner->empty()) break; - out << m_pScanner->peek() << std::endl; + out << m_pScanner->peek() << "\n"; m_pScanner->pop(); } } diff --git a/src/regex.cpp b/src/regex.cpp index cbd1eb0..45ce684 100644 --- a/src/regex.cpp +++ b/src/regex.cpp @@ -1,5 +1,7 @@ #include "crt.h" -#include "regex.h" +#include "regex.h" +#include "stream.h" +#include namespace YAML { @@ -88,6 +90,11 @@ namespace YAML } bool RegEx::Matches(std::istream& in) const + { + return Match(in) >= 0; + } + + bool RegEx::Matches(Stream& in) const { return Match(in) >= 0; } @@ -106,6 +113,12 @@ namespace YAML return m_pOp->Match(str, *this); } + // Match + int RegEx::Match(Stream& in) const + { + return Match(in.stream()); + } + // Match // . The stream version does the same thing as the string version; // REMEMBER that we only match from the start of the stream! diff --git a/src/regex.h b/src/regex.h index b5429c7..6212983 100644 --- a/src/regex.h +++ b/src/regex.h @@ -5,7 +5,9 @@ #include namespace YAML -{ +{ + class Stream; + enum REGEX_OP { REGEX_EMPTY, REGEX_MATCH, REGEX_RANGE, REGEX_OR, REGEX_AND, REGEX_NOT, REGEX_SEQ }; // simplified regular expressions @@ -64,9 +66,11 @@ namespace YAML bool Matches(char ch) const; bool Matches(const std::string& str) const; - bool Matches(std::istream& in) const; + bool Matches(std::istream& in) const; + bool Matches(Stream& in) const; int Match(const std::string& str) const; - int Match(std::istream& in) const; + int Match(std::istream& in) const; + int Match(Stream& in) const; friend RegEx operator ! (const RegEx& ex); friend RegEx operator || (const RegEx& ex1, const RegEx& ex2); diff --git a/src/scalar.cpp b/src/scalar.cpp index 622813e..56edb4a 100644 --- a/src/scalar.cpp +++ b/src/scalar.cpp @@ -38,11 +38,6 @@ namespace YAML out << "\"\n"; } - CONTENT_TYPE Scalar::GetType() const - { - return CT_SCALAR; - } - void Scalar::Read(std::string& s) { s = m_data; diff --git a/src/scalar.h b/src/scalar.h index 8367705..f33f8a5 100644 --- a/src/scalar.h +++ b/src/scalar.h @@ -14,7 +14,7 @@ namespace YAML virtual void Parse(Scanner *pScanner, const ParserState& state); virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine); - virtual CONTENT_TYPE GetType() const; + virtual bool IsScalar() const { return true; } // extraction virtual void Read(std::string& s); diff --git a/src/sequence.cpp b/src/sequence.cpp index 0c81ba4..985f979 100644 --- a/src/sequence.cpp +++ b/src/sequence.cpp @@ -2,7 +2,8 @@ #include "sequence.h" #include "node.h" #include "scanner.h" -#include "token.h" +#include "token.h" +#include namespace YAML { @@ -134,7 +135,7 @@ namespace YAML void Sequence::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) { if(startedLine && !onlyOneCharOnLine) - out << std::endl; + out << "\n"; for(unsigned i=0;i 0) { @@ -147,12 +148,7 @@ namespace YAML } if(m_data.empty()) - out << std::endl; - } - - CONTENT_TYPE Sequence::GetType() const - { - return CT_SEQUENCE; + out << "\n"; } int Sequence::Compare(Content *pContent) diff --git a/src/sequence.h b/src/sequence.h index 8fe612c..89dba85 100644 --- a/src/sequence.h +++ b/src/sequence.h @@ -22,7 +22,7 @@ namespace YAML virtual void Parse(Scanner *pScanner, const ParserState& state); virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine); - virtual CONTENT_TYPE GetType() const; + virtual bool IsSequence() const { return true; } // ordering virtual int Compare(Content *pContent); diff --git a/src/stream.cpp b/src/stream.cpp index 38f4c8e..07e0c54 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -1,8 +1,24 @@ #include "crt.h" -#include "stream.h" +#include "stream.h" +#include namespace YAML { + int Stream::pos() const + { + return input.tellg(); + } + + char Stream::peek() + { + return input.peek(); + } + + Stream::operator bool() + { + return input.good(); + } + // get // . Extracts a character from the stream and updates our position char Stream::get() @@ -32,5 +48,6 @@ namespace YAML { for(int i=0;i> item; - std::cout << item << "\n"; - } + YAML::Parser parser(fin); + parser.PrintTokens(std::cout); } catch(YAML::Exception&) { std::cout << "Error parsing the yaml!\n"; } } -int main() -{ - _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); - Test::RunAll(); +int main(int argc, char **argv) +{ + bool verbose = false; + for(int i=1;i files; - files.push_back("yaml-reader/tests/simple.yaml"); - files.push_back("yaml-reader/tests/mixed.yaml"); - files.push_back("yaml-reader/tests/scalars.yaml"); - files.push_back("yaml-reader/tests/directives.yaml"); + files.push_back("tests/simple.yaml"); + files.push_back("tests/mixed.yaml"); + files.push_back("tests/scalars.yaml"); + files.push_back("tests/directives.yaml"); bool passed = true; for(unsigned i=0;i namespace Test { - void RunAll(); - bool Inout(const std::string& file); + void RunAll(bool verbose); + bool Inout(const std::string& file, bool verbose); } diff --git a/yaml-reader/tests/directives.yaml b/yaml-reader/tests/directives.yaml new file mode 100644 index 0000000..ff09846 --- /dev/null +++ b/yaml-reader/tests/directives.yaml @@ -0,0 +1,5 @@ +%YAML 1.2 +%TAG ! !howdy +--- +- basic node +- ! yeah baby \ No newline at end of file diff --git a/yaml-reader/tests/mixed.yaml b/yaml-reader/tests/mixed.yaml new file mode 100644 index 0000000..88da1e7 --- /dev/null +++ b/yaml-reader/tests/mixed.yaml @@ -0,0 +1,32 @@ +- the main thing is a sequence +- here's a key: value + and another: value +- let's inline: [1, 2, 3] + and an inline map: {key: value, 243: 101} +- and multiple indents: + - here's + - a + - list + and another: + - list + - of + - things +- maybe now: + let's: get + pretty: + deep: here + in: + the: nesting + just: to + confuse: + the: heck + out: + - of + - the: parser + if: + - we + - can + - do: that + what: do + you: think? + \ No newline at end of file diff --git a/yaml-reader/tests/out.yaml b/yaml-reader/tests/out.yaml new file mode 100644 index 0000000..c02918e --- /dev/null +++ b/yaml-reader/tests/out.yaml @@ -0,0 +1,8 @@ +--- +- "basic node" +- ! "yeah baby" + +--- +- "basic node" +- !> "yeah baby" + diff --git a/yaml-reader/tests/scalars.yaml b/yaml-reader/tests/scalars.yaml new file mode 100644 index 0000000..ae0059a --- /dev/null +++ b/yaml-reader/tests/scalars.yaml @@ -0,0 +1,35 @@ +- normal scalar, but + over several lines +- | + literal scalar - so we can draw ASCII: + + - - + | - | + ------ +- > + and a folded scalar... so we + can just keep writing various + things. And if we want to keep indentation: + + we just indent a little + see, this stays indented +- >- + Here's a folded scalar + that gets chomped. +- |- + And here's a literal scalar + that gets chomped. +- >2 + Here's a folded scalar + that starts with some indentation. +- ::vector +- ": - ()" +- Up, up, and away! +- -123 +- http://example.com/foo#bar +# Inside flow collection: +- [ ::vector, + ": - ()", + "Up, up and away!", + -123, + http://example.com/foo#bar ] \ No newline at end of file diff --git a/yaml-reader/tests/simple.yaml b/yaml-reader/tests/simple.yaml new file mode 100644 index 0000000..55b2d21 --- /dev/null +++ b/yaml-reader/tests/simple.yaml @@ -0,0 +1,13 @@ +--- +just a scalar +--- +and another scalar +--- +now an end document +... +--- +and now two +... +... +--- +and that's it \ No newline at end of file diff --git a/yaml-reader/tests/test.yaml b/yaml-reader/tests/test.yaml new file mode 100644 index 0000000..64db5ef --- /dev/null +++ b/yaml-reader/tests/test.yaml @@ -0,0 +1,3 @@ +- it's just +- one thing +- after another