mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-03 21:41:26 +12:00
Started the parser.
This commit is contained in:
parent
ed6c294749
commit
b6a0ef207b
12 changed files with 143 additions and 276 deletions
33
document.cpp
33
document.cpp
|
@ -1,12 +1,5 @@
|
||||||
#include "document.h"
|
#include "document.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "parser.h"
|
|
||||||
#include "scanner.h"
|
|
||||||
#include "exceptions.h"
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include "token.h"
|
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
|
@ -14,11 +7,6 @@ namespace YAML
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Document::Document(const std::string& fileName): m_pRoot(0)
|
|
||||||
{
|
|
||||||
Load(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
Document::~Document()
|
Document::~Document()
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
|
@ -29,25 +17,4 @@ namespace YAML
|
||||||
delete m_pRoot;
|
delete m_pRoot;
|
||||||
m_pRoot = 0;
|
m_pRoot = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Document::Load(const std::string& fileName)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
|
|
||||||
std::ifstream fin(fileName.c_str());
|
|
||||||
Scanner scanner(fin);
|
|
||||||
|
|
||||||
// scan and output, for now
|
|
||||||
while(1) {
|
|
||||||
Token *pToken = scanner.GetNextToken();
|
|
||||||
if(!pToken)
|
|
||||||
break;
|
|
||||||
|
|
||||||
std::cout << typeid(*pToken).name() << ": " << *pToken << std::endl;
|
|
||||||
delete pToken;
|
|
||||||
}
|
|
||||||
getchar();
|
|
||||||
|
|
||||||
// m_pRoot = parser.ReadNextNode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
class Node;
|
class Node;
|
||||||
|
@ -10,11 +8,9 @@ namespace YAML
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Document();
|
Document();
|
||||||
Document(const std::string& fileName);
|
|
||||||
~Document();
|
~Document();
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
void Load(const std::string& fileName);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node *m_pRoot;
|
Node *m_pRoot;
|
||||||
|
|
9
main.cpp
9
main.cpp
|
@ -1,9 +1,12 @@
|
||||||
#include "document.h"
|
#include "reader.h"
|
||||||
#include "regex.h"
|
#include <fstream>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
YAML::Document doc("test.yaml");
|
std::ifstream fin("test.yaml");
|
||||||
|
YAML::Reader reader(fin);
|
||||||
|
YAML::Document doc;
|
||||||
|
reader.GetNextDocument(doc);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
11
node.cpp
11
node.cpp
|
@ -20,15 +20,4 @@ namespace YAML
|
||||||
delete m_pContent;
|
delete m_pContent;
|
||||||
m_pContent = 0;
|
m_pContent = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::Read(Parser *pParser, const std::string& token)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
|
|
||||||
if(token == std::string("") + SeqToken) {
|
|
||||||
m_pContent = new Sequence(pParser);
|
|
||||||
} else {
|
|
||||||
m_pContent = new Scalar(token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
2
node.h
2
node.h
|
@ -10,7 +10,6 @@ namespace YAML
|
||||||
const std::string MapTag = "!!map";
|
const std::string MapTag = "!!map";
|
||||||
|
|
||||||
class Content;
|
class Content;
|
||||||
class Parser;
|
|
||||||
|
|
||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
|
@ -19,7 +18,6 @@ namespace YAML
|
||||||
~Node();
|
~Node();
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
void Read(Parser *pParser, const std::string& token);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_tag;
|
std::string m_tag;
|
||||||
|
|
140
parser.cpp
140
parser.cpp
|
@ -1,141 +1,33 @@
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
Parser::Parser(std::istream& in): INPUT(in), m_ok(true)
|
Parser::Parser(std::istream& in): m_scanner(in)
|
||||||
{
|
{
|
||||||
m_state.push(State(C_BLOCK, -1, true));
|
// eat the stream start token
|
||||||
|
// TODO: check?
|
||||||
// read header
|
Token *pToken = m_scanner.GetNextToken();
|
||||||
std::string token = ReadNextToken();
|
|
||||||
if(token != DocStart)
|
|
||||||
m_ok = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser::~Parser()
|
Parser::~Parser()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser::operator bool() const
|
void Parser::GetNextDocument(Document& document)
|
||||||
{
|
{
|
||||||
return m_ok;
|
// scan and output, for now
|
||||||
}
|
while(1) {
|
||||||
|
Token *pToken = m_scanner.GetNextToken();
|
||||||
|
if(!pToken)
|
||||||
|
break;
|
||||||
|
|
||||||
bool Parser::operator !() const
|
std::cout << typeid(*pToken).name() << ": " << *pToken << std::endl;
|
||||||
{
|
delete pToken;
|
||||||
return !m_ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Parser::IsWhitespace(char ch)
|
|
||||||
{
|
|
||||||
return (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t');
|
|
||||||
}
|
|
||||||
|
|
||||||
void Parser::Putback(const std::string& str)
|
|
||||||
{
|
|
||||||
for(int i=str.size()-1;i>=0;i--)
|
|
||||||
INPUT.putback(str[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringWhitespace
|
|
||||||
// . Strips up to n whitespace characters (or as many
|
|
||||||
// as there are, if n is -1)
|
|
||||||
void Parser::StripWhitespace(int n)
|
|
||||||
{
|
|
||||||
while(--n >= 0 && IsWhitespace(INPUT.peek()))
|
|
||||||
INPUT.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
int Parser::GetNumOfSpaces()
|
|
||||||
{
|
|
||||||
// get 'em out
|
|
||||||
int n = 0;
|
|
||||||
while(INPUT.peek() == ' ') {
|
|
||||||
INPUT.get();
|
|
||||||
n++;
|
|
||||||
}
|
}
|
||||||
|
getchar();
|
||||||
// put 'em back
|
|
||||||
for(int i=0;i<n;i++)
|
|
||||||
INPUT.putback(' ');
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
// SeqContinues
|
|
||||||
// . Returns true if the next item to be read is a continuation of the current sequence.
|
|
||||||
bool Parser::SeqContinues()
|
|
||||||
{
|
|
||||||
const State& state = m_state.top();
|
|
||||||
if(state.context != C_BLOCK)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int n = GetNumOfSpaces();
|
|
||||||
return (n == state.indent);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadNextNode
|
|
||||||
// . Reads and returns the next node from the current input (to be used recursively).
|
|
||||||
// . The caller is responsible for cleanup!
|
|
||||||
Node *Parser::ReadNextNode()
|
|
||||||
{
|
|
||||||
if(!INPUT)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
std::string token = ReadNextToken();
|
|
||||||
if(token == DocStart || token == DocEnd)
|
|
||||||
return 0; // TODO: putback DocStart?
|
|
||||||
|
|
||||||
Node *pNode = new Node;
|
|
||||||
pNode->Read(this, token);
|
|
||||||
return pNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadNextToken
|
|
||||||
// . Reads:
|
|
||||||
// . If the first character is non-whitespace, non-special token, then until
|
|
||||||
// the end of this scalar.
|
|
||||||
std::string Parser::ReadNextToken()
|
|
||||||
{
|
|
||||||
const State& state = m_state.top();
|
|
||||||
|
|
||||||
if(state.startingNewLine) {
|
|
||||||
int n = GetNumOfSpaces();
|
|
||||||
StripWhitespace(n);
|
|
||||||
if(n > state.indent) {
|
|
||||||
m_state.push(State(C_BLOCK, n, true));
|
|
||||||
};
|
|
||||||
|
|
||||||
while(m_state.top().startingNewLine && n < m_state.top().indent)
|
|
||||||
m_state.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
char ch = INPUT.peek();
|
|
||||||
if(IsWhitespace(ch))
|
|
||||||
return ""; // TODO
|
|
||||||
|
|
||||||
if(ch == SeqToken) {
|
|
||||||
// grab token
|
|
||||||
INPUT.get();
|
|
||||||
|
|
||||||
// is next token whitespace?
|
|
||||||
if(!IsWhitespace(INPUT.peek())) {
|
|
||||||
// read entire line
|
|
||||||
std::string line;
|
|
||||||
std::getline(INPUT, line);
|
|
||||||
return ch + line;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if so, strip whitespace and go
|
|
||||||
StripWhitespace();
|
|
||||||
|
|
||||||
return std::string("") + SeqToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
// read until end-of-line
|
|
||||||
std::string line;
|
|
||||||
std::getline(INPUT, line);
|
|
||||||
return line;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
40
parser.h
40
parser.h
|
@ -2,54 +2,22 @@
|
||||||
|
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stack>
|
#include "scanner.h"
|
||||||
|
#include "document.h"
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
class Node;
|
class Node;
|
||||||
|
|
||||||
const std::string DocStart = "---";
|
|
||||||
const std::string DocEnd = "...";
|
|
||||||
|
|
||||||
const char SeqToken = '-';
|
|
||||||
|
|
||||||
enum CONTEXT { C_BLOCK, C_FLOW };
|
|
||||||
|
|
||||||
class Parser
|
class Parser
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
struct State
|
|
||||||
{
|
|
||||||
State(CONTEXT context_, int indent_, bool startingNewLine_)
|
|
||||||
: context(context_), indent(indent_), startingNewLine(startingNewLine_) {}
|
|
||||||
|
|
||||||
CONTEXT context;
|
|
||||||
int indent;
|
|
||||||
bool startingNewLine;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Parser(std::istream& in);
|
Parser(std::istream& in);
|
||||||
~Parser();
|
~Parser();
|
||||||
|
|
||||||
operator bool () const;
|
void GetNextDocument(Document& document);
|
||||||
bool operator !() const;
|
|
||||||
|
|
||||||
// parse helpers
|
|
||||||
static bool IsWhitespace(char ch);
|
|
||||||
void Putback(const std::string& str);
|
|
||||||
void StripWhitespace(int n = -1);
|
|
||||||
int GetNumOfSpaces();
|
|
||||||
bool SeqContinues();
|
|
||||||
|
|
||||||
// readers
|
|
||||||
Node *ReadNextNode();
|
|
||||||
std::string ReadNextToken();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_ok;
|
Scanner m_scanner;
|
||||||
|
|
||||||
std::istream& INPUT;
|
|
||||||
std::stack <State> m_state;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
21
reader.cpp
Normal file
21
reader.cpp
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#include "reader.h"
|
||||||
|
#include "scanner.h"
|
||||||
|
#include "parser.h"
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
Reader::Reader(std::istream& in): m_pParser(0)
|
||||||
|
{
|
||||||
|
m_pParser = new Parser(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
Reader::~Reader()
|
||||||
|
{
|
||||||
|
delete m_pParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reader::GetNextDocument(Document& document)
|
||||||
|
{
|
||||||
|
m_pParser->GetNextDocument(document);
|
||||||
|
}
|
||||||
|
}
|
21
reader.h
Normal file
21
reader.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ios>
|
||||||
|
#include "document.h"
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
class Parser;
|
||||||
|
|
||||||
|
class Reader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Reader(std::istream& in);
|
||||||
|
~Reader();
|
||||||
|
|
||||||
|
void GetNextDocument(Document& document);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Parser *m_pParser;
|
||||||
|
};
|
||||||
|
}
|
13
sequence.cpp
13
sequence.cpp
|
@ -1,12 +1,11 @@
|
||||||
#include "sequence.h"
|
#include "sequence.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "parser.h"
|
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
Sequence::Sequence(Parser *pParser)
|
Sequence::Sequence()
|
||||||
{
|
{
|
||||||
Read(pParser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Sequence::~Sequence()
|
Sequence::~Sequence()
|
||||||
|
@ -14,12 +13,4 @@ namespace YAML
|
||||||
for(unsigned i=0;i<m_data.size();i++)
|
for(unsigned i=0;i<m_data.size();i++)
|
||||||
delete m_data[i];
|
delete m_data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sequence::Read(Parser *pParser)
|
|
||||||
{
|
|
||||||
do {
|
|
||||||
Node *pNode = pParser->ReadNextNode();
|
|
||||||
m_data.push_back(pNode);
|
|
||||||
} while(pParser->SeqContinues());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,13 @@
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
class Node;
|
class Node;
|
||||||
class Parser;
|
|
||||||
|
|
||||||
class Sequence: public Content
|
class Sequence: public Content
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Sequence(Parser *pParser);
|
Sequence();
|
||||||
virtual ~Sequence();
|
virtual ~Sequence();
|
||||||
|
|
||||||
void Read(Parser *pParser);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector <Node *> m_data;
|
std::vector <Node *> m_data;
|
||||||
};
|
};
|
||||||
|
|
|
@ -169,10 +169,6 @@
|
||||||
RelativePath=".\document.cpp"
|
RelativePath=".\document.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\exp.cpp"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\main.cpp"
|
RelativePath=".\main.cpp"
|
||||||
>
|
>
|
||||||
|
@ -186,41 +182,57 @@
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\parser.cpp"
|
RelativePath=".\reader.cpp"
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\regex.cpp"
|
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\scalar.cpp"
|
RelativePath=".\scalar.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\scanner.cpp"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\scanscalar.cpp"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\scantoken.cpp"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\sequence.cpp"
|
RelativePath=".\sequence.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<Filter
|
||||||
RelativePath=".\simplekey.cpp"
|
Name="Scanner"
|
||||||
>
|
>
|
||||||
</File>
|
<File
|
||||||
<File
|
RelativePath=".\exp.cpp"
|
||||||
RelativePath=".\stream.cpp"
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\regex.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\scanner.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\scanscalar.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\scantoken.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\simplekey.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\stream.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
|
<Filter
|
||||||
|
Name="Parser"
|
||||||
>
|
>
|
||||||
</File>
|
<File
|
||||||
|
RelativePath=".\parser.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
</Filter>
|
</Filter>
|
||||||
<Filter
|
<Filter
|
||||||
Name="Header Files"
|
Name="Header Files"
|
||||||
|
@ -239,10 +251,6 @@
|
||||||
RelativePath=".\exceptions.h"
|
RelativePath=".\exceptions.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\exp.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\map.h"
|
RelativePath=".\map.h"
|
||||||
>
|
>
|
||||||
|
@ -252,37 +260,53 @@
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\parser.h"
|
RelativePath=".\reader.h"
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\regex.h"
|
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\scalar.h"
|
RelativePath=".\scalar.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\scanner.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath=".\scanscalar.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\sequence.h"
|
RelativePath=".\sequence.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath=".\stream.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath=".\token.h"
|
RelativePath=".\token.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<Filter
|
||||||
|
Name="Scanner"
|
||||||
|
>
|
||||||
|
<File
|
||||||
|
RelativePath=".\exp.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\regex.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\scanner.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\scanscalar.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\stream.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
|
<Filter
|
||||||
|
Name="Parser"
|
||||||
|
>
|
||||||
|
<File
|
||||||
|
RelativePath=".\parser.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
</Filter>
|
||||||
</Filter>
|
</Filter>
|
||||||
<Filter
|
<Filter
|
||||||
Name="Resource Files"
|
Name="Resource Files"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue