Removed the document class (since it's really just a root node, and that's it).

This commit is contained in:
Jesse Beder 2008-07-04 22:56:43 +00:00
parent 2be40919de
commit ed488e5197
7 changed files with 27 additions and 103 deletions

View file

@ -1,63 +0,0 @@
#include "document.h"
#include "node.h"
#include "token.h"
#include "scanner.h"
namespace YAML
{
Document::Document(): m_pRoot(0)
{
}
Document::~Document()
{
Clear();
}
void Document::Clear()
{
delete m_pRoot;
m_pRoot = 0;
}
void Document::Parse(Scanner *pScanner, const ParserState& state)
{
Clear();
// we better have some tokens in the queue
if(!pScanner->PeekNextToken())
return;
// first eat doc start (optional)
if(pScanner->PeekNextToken()->type == TT_DOC_START)
pScanner->EatNextToken();
// now create our root node and parse it
m_pRoot = new Node;
m_pRoot->Parse(pScanner, state);
// and finally eat any doc ends we see
while(pScanner->PeekNextToken() && pScanner->PeekNextToken()->type == TT_DOC_END)
pScanner->EatNextToken();
}
const Node& Document::GetRoot() const
{
if(!m_pRoot)
throw;
return *m_pRoot;
}
std::ostream& operator << (std::ostream& out, const Document& doc)
{
out << "---\n";
if(!doc.m_pRoot) {
out << "{empty node}\n";
return out;
}
doc.m_pRoot->Write(out, 0);
return out;
}
}

View file

@ -1,26 +0,0 @@
#pragma once
#include <ios>
#include "parserstate.h"
namespace YAML
{
class Node;
class Scanner;
class Document
{
public:
Document();
~Document();
void Clear();
void Parse(Scanner *pScanner, const ParserState& state);
const Node& GetRoot() const;
friend std::ostream& operator << (std::ostream& out, const Document& doc);
private:
Node *m_pRoot;
};
}

View file

@ -72,11 +72,11 @@ int main()
if(!parser)
return 0;
YAML::Document doc;
YAML::Node doc;
parser.GetNextDocument(doc);
Level level;
doc.GetRoot() >> level;
doc >> level;
std::cout << level;
} catch(YAML::Exception&) {
std::cout << "Error parsing the yaml!\n";

1
node.h
View file

@ -48,6 +48,7 @@ namespace YAML
void Parse(Scanner *pScanner, const ParserState& state);
void Write(std::ostream& out, int indent);
// accessors
Iterator begin() const;
Iterator end() const;
unsigned size() const;

View file

@ -22,15 +22,35 @@ namespace YAML
return m_pScanner->PeekNextToken() != 0;
}
void Parser::GetNextDocument(Document& document)
// GetNextDocument
// . Reads the next document in the queue (of tokens).
// . Throws (ScannerException|ParserException)s on errors.
void Parser::GetNextDocument(Node& document)
{
// clear node
document.Clear();
// first read directives
ParseDirectives();
// then parse the document
// we better have some tokens in the queue
if(!m_pScanner->PeekNextToken())
return;
// first eat doc start (optional)
if(m_pScanner->PeekNextToken()->type == TT_DOC_START)
m_pScanner->EatNextToken();
// now parse our root node
document.Parse(m_pScanner, m_state);
// and finally eat any doc ends we see
while(m_pScanner->PeekNextToken() && m_pScanner->PeekNextToken()->type == TT_DOC_END)
m_pScanner->EatNextToken();
}
// ParseDirectives
// . Reads any directives that are next in the queue.
void Parser::ParseDirectives()
{
bool readDirective = false;

View file

@ -4,7 +4,7 @@
#include <string>
#include <vector>
#include <map>
#include "document.h"
#include "node.h"
#include "parserstate.h"
namespace YAML
@ -20,7 +20,7 @@ namespace YAML
operator bool() const;
void GetNextDocument(Document& document);
void GetNextDocument(Node& document);
void PrintTokens(std::ostream& out);
private:

View file

@ -216,10 +216,6 @@
RelativePath=".\content.cpp"
>
</File>
<File
RelativePath=".\document.cpp"
>
</File>
<File
RelativePath=".\iterator.cpp"
>
@ -298,10 +294,6 @@
RelativePath=".\content.h"
>
</File>
<File
RelativePath=".\document.h"
>
</File>
<File
RelativePath=".\map.h"
>