Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
27 KB
Referenced Files
None
Subscribers
None
diff --git a/util/token.cpp b/util/token.cpp
index 8021d4a1..7e7d70a5 100644
--- a/util/token.cpp
+++ b/util/token.cpp
@@ -1,695 +1,711 @@
#include "token.h"
#include "token_exception.h"
#include <string>
#include <vector>
#include <ostream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string.h>
#include "debug.h"
using namespace std;
Token::Token():
num_token(1),
parent( NULL ),
own(true){
name = "HEAD";
}
Token::Token( string tok, bool parse ):
num_token( 1 ),
parent( NULL ),
own(true){
/* legacy code, not used much */
if ( !parse ){
name = tok;
while ( name.find(' ' ) == 0 )
name.erase( 0, 1 );
// lowerCase( name );
return;
}
}
Token::Token(Token const & copy):
num_token(1),
parent(copy.parent),
own(false){
this->tokens = copy.tokens;
this->name = copy.name;
this->filename = copy.filename;
}
/* Dump token to the screen */
void Token::print( const string space ) const {
Global::debug(0) <<space<<"Token: "<< getName() << endl;
for ( signed int i = 0; i < numTokens(); i++ ){
Token * x = getToken( i );
x->print( space + " |--" );
}
}
void Token::toStringCompact(ostream & stream) const {
if (numTokens() == -1){
stream << getName();
} else {
stream << "(" << getName();
for (signed int i = 0; i < numTokens(); i++){
Token * x = getToken(i);
stream << " ";
x->toStringCompact(stream);
}
stream << ")";
}
}
void Token::toString(ostream & stream, const string & space) const {
if (numTokens() == -1){
stream << getName();
} else {
stream << endl;
stream << space << "(" << getName();
for ( signed int i = 0; i < numTokens(); i++ ){
Token * x = getToken(i);
stream << " ";
x->toString( stream, space + " " );
}
stream << ")";
}
}
std::string Token::toString() const {
std::ostringstream out;
toString(out, "");
return out.str();
}
+
+std::string Token::toStringCompact() const {
+ std::ostringstream out;
+ toStringCompact(out);
+ return out.str();
+}
/* helper function */
string Token::lowerCase( const string & s ) const {
string ret = s;
for ( unsigned int q = 0; q < s.length(); q++ ){
if ( s[q] >= 'A' && s[q] <= 'Z' ){
ret[q] = s[q] - 'A' + 'a';
} else {
// ret[q] = s[q];
}
}
return ret;
}
/* Return next token and increment the internal position
* of the current token
*/
Token * Token::readToken(){
if ( num_token < tokens.size() ){
return tokens[ num_token++ ];
}
return NULL;
}
bool Token::hasTokens() const {
return num_token < tokens.size();
}
/*
vector<Token*> Token::findTokens(const string & path){
vector<Token*> whoba;
cout << "whoba has " << whoba.size() << endl;
whoba.push_back(this);
if (whoba[0] != this){
cout << "complete failure!!!" << endl;
}
int x = 2;
x = x + 12;
return whoba;
}
*/
vector<const Token *> Token::findTokens(const string & path) const {
vector<const Token *> found;
if (path == ""){
return found;
}
size_t find = path.find('/');
string self;
if (find == string::npos){
self = path;
} else {
self = path.substr(0, find);
}
/* a name of `_' means succeed with the current token no matter
* what its called.
* `*' means test all children. this is useful if you dont know where in the
* tree a given node lives.
* (... (... (... (foo ...))))
* *\foo would find the foo token (I used backslash because forward slash will
* kill the current c++ comment)
*/
if (self == "*"){
/* `*' and 'blah/stuff', test the current token for 'blah/stuff' */
string rest = path.substr(find+1);
vector<const Token*> more = findTokens(rest);
found.insert(found.end(), more.begin(), more.end());
/* then test all children for the original path */
for (int i = 0; i < numTokens(); i++){
Token * next = getToken(i);
if (next != NULL){
vector<const Token *> more = next->findTokens(path);
found.insert(found.end(), more.begin(), more.end());
}
}
} else if (self == "_" || *this == self){
if (find == string::npos){
found.push_back(this);
if (found[0] != this){
Global::debug(0) << "internal consistency error!!!!" << endl;
throw exception();
}
} else {
string rest = path.substr(find+1);
for (int i = 0; i < numTokens(); i++){
Token * next = getToken(i);
if (next != NULL){
vector<const Token *> more = next->findTokens(rest);
found.insert(found.end(), more.begin(), more.end());
}
}
}
}
return found;
}
TokenMatcher Token::getMatcher(const std::string & subject) const {
TokenMatcher matcher(findTokens(subject));
return matcher;
}
Token * Token::findToken(const string & path){
vector<const Token *> all = findTokens(path);
if (all.size() == 0){
return NULL;
}
return (Token*) all[0];
}
const Token * Token::findToken(const string & path) const {
vector<const Token *> all = findTokens(path);
if (all.size() == 0){
return NULL;
}
return all[0];
}
/*
Token * Token::findToken(const string & path){
if (path == ""){
return NULL;
}
size_t find = path.find('/');
string self;
if (find == string::npos){
self = path;
} else {
self = path.substr(0, find);
}
if (*this == self){
if (find == string::npos){
return this;
} else {
for (int i = 0; i < numTokens(); i++){
Token * next = getToken(i);
if (next != NULL){
Token * ok = next->findToken(path.substr(find+1));
if (ok != NULL){
return ok;
}
}
}
}
}
return NULL;
}
*/
Token * Token::getToken( unsigned int n ) const {
int q = numTokens();
if ( q == -1 ) return NULL;
if ( (signed int)n < q )
return tokens[n+1];
return NULL;
}
/* If the token has children then the name of this token
* is the name of the first child token.
* Otherwise, the name is this token's name
*/
const string & Token::getName() const {
if ( numTokens() != -1 ){
return tokens[0]->_getName();
}
// cout<<"No tokens!!"<<endl;
return name;
}
const Token * Token::getParent() const {
return this->parent;
}
Token * Token::getParent(){
return parent;
}
const Token * Token::getRootParent() const {
if (getParent() != NULL){
return getParent()->getRootParent();
}
return this;
}
const string Token::getLineage() const {
if (getParent() != NULL){
return getParent()->getLineage() + " -> " + getName();
}
return getName();
}
/* A token's identity is its name
*/
bool Token::operator== (const string & rhs) const {
return lowerCase(getName()) == lowerCase(rhs);
}
bool Token::operator!=(const string & rhs) const {
return !(*this == rhs);
}
void Token::setFile(const string & s){
filename = s;
}
void Token::removeToken(Token * token){
for (vector<Token*>::iterator it = tokens.begin(); it != tokens.end(); it++){
Token * what = *it;
if (token == what){
/* Found the token. If we own it then delete it, otherwise just
* remove it from the list and return.
*/
if (own){
delete what;
}
it = tokens.erase(it);
return;
}
}
}
const string Token::getFileName() const {
if (parent){
return parent->getFileName();
} else {
return filename;
}
}
/*
Token & Token::operator>>( Token * & rhs ) throw( TokenException ){
Token * x = readToken();
if ( x == NULL ){
throw TokenException(__FILE__, __LINE__, getFileName() + ": " + string("Tried to read a token from ") + this->getName() + string(" but there are no more elements") );
}
rhs = x;
return *this;
}
Token & Token::operator>>( string & rhs ) throw( TokenException ){
Token * token = readToken();
if (token == NULL){
throw TokenException(__FILE__, __LINE__, getFileName() + ":" + string("Tried to read a string from '") + this->getLineage() + string("' but there no more elements") );
}
if (!token->isData()){
ostringstream out;
out << getFileName() << ": Element is not a string '";
token->toString(out, "");
// throw TokenException(__FILE__, __LINE__, getFileName() + ":" + string(" Element is not a string: "));
throw TokenException(__FILE__, __LINE__, out.str());
}
rhs = token->getName();
// rhs = getName();
return *this;
}
Token & Token::operator>>( int & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
throw TokenException(__FILE__, __LINE__, getFileName() + ": " + string("Tried to read an int from ") + this->getLineage() + string(" but there are no more elements") );
}
if (!l->isData()){
throw TokenException(__FILE__, __LINE__, getFileName() + ":" + string(" Element is not a string"));
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
Token & Token::operator>>( double & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
throw TokenException(__FILE__, __LINE__, getFileName() + ": " + string("Tried to read a double from ") + this->getLineage() + string(" but there no more elements") );
}
if (!l->isData()){
throw TokenException(__FILE__, __LINE__, getFileName() + ":" + string(" Element is not a string"));
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
Token & Token::operator>>( bool & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
throw TokenException(__FILE__, __LINE__, getFileName() + ": " + string("Tried to read a bool from ") + this->getLineage() + string(" but there no more elements") );
}
if (!l->isData()){
throw TokenException(__FILE__, __LINE__, getFileName() + ":" + string(" Element is not a string"));
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
*/
void Token::addToken(Token * t){
/*
if (!own){
throw TokenException(__FILE__, __LINE__, "This token does not own its own tokens, so you cannot add tokens to it");
}
*/
t->setParent(this);
tokens.push_back(t);
}
Token * Token::newToken(){
Token * token = new Token();
addToken(token);
return token;
}
TokenView Token::view() const {
vector<const Token*> out;
out.insert(out.end(), tokens.begin(), tokens.end());
return TokenView(out);
}
static bool needQuotes(const std::string & what){
/* ripped from tokenreader.cpp, maybe use a variable for nice sharing.. */
const char * alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./-_!:";
for (unsigned int position = 0; position < what.size(); position++){
if (strchr(alpha, what[position]) == NULL){
return true;
}
/*
if (what[position] == '"' ||
what[position] == ' ' ||
(unsigned char) what[position] > 127){
return true;
}
*/
}
return false;
}
/* put quotes around a string if there are spaces in it */
static string quoteify(const string & rhs){
if (needQuotes(rhs)){
return "\"" + rhs + "\"";
}
return rhs;
}
Token & Token::operator<<(Token * token){
if (!own){
throw TokenException(__FILE__, __LINE__, "Cannot add tokens to a token you don't own");
}
this->addToken(token);
return *this;
}
Token & Token::operator<<( const string rhs ){
if (!own){
throw TokenException(__FILE__, __LINE__, "Cannot add raw strings to a token you don't own");
}
Token * n = new Token(quoteify(rhs), false );
this->addToken(n);
return *this;
}
Token & Token::operator<<( const int rhs ){
if (!own){
throw TokenException(__FILE__, __LINE__, "Cannot add raw integers to a token you don't own");
}
ostringstream o;
o << rhs;
return *this << o.str();
}
+Token & Token::operator<<(const unsigned int rhs){
+ if (!own){
+ throw TokenException(__FILE__, __LINE__, "Cannot add raw integers to a token you don't own");
+ }
+
+ ostringstream o;
+ o << rhs;
+ return *this << o.str();
+}
+
Token & Token::operator<<( const double rhs ){
if (!own){
throw TokenException(__FILE__, __LINE__, "Cannot add raw doubles to a token you don't own");
}
ostringstream o;
o << setprecision(6) << fixed << rhs;
return *this << o.str();
}
Token * Token::copy() const {
Token * token = new Token();
token->filename = this->filename;
token->name = this->name;
for (vector<Token *>::const_iterator it = this->tokens.begin(); it != this->tokens.end(); it++){
Token * him = (*it)->copy();
him->setParent(token);
token->addToken(him);
}
return token;
}
/* Delete tokens that are commented.
* A commented token has a '!' character as the first
* character in the name, e.g:
* (!a_token (child_token 2))
*/
/*
void Token::finalize(){
for ( vector< Token * >::iterator it = tokens.begin(); it != tokens.end(); ){
Token * t = *it;
if ( t->getName().find('!') == 0 ){
delete t;
it = tokens.erase( it );
} else {
t->finalize();
it++;
}
}
}
*/
Token::~Token(){
if (own){
for (vector<Token * >::iterator it = tokens.begin(); it != tokens.end(); it++){
delete *it;
}
}
}
TokenMatcher::TokenMatcher(){
throw std::exception();
}
TokenMatcher & TokenMatcher::operator=(const TokenMatcher & matcher){
tokens = matcher.tokens;
current = tokens.begin();
return *this;
}
TokenMatcher::TokenMatcher(std::vector<const Token*> tokens):
tokens(tokens){
current = this->tokens.begin();
}
TokenView::TokenView(std::vector<const Token *> tokens):
tokens(tokens){
current = this->tokens.begin();
if (current != this->tokens.end()){
current++;
}
}
TokenView::TokenView(const TokenView & view):
tokens(view.tokens){
current = this->tokens.begin();
if (current != this->tokens.end()){
current++;
}
}
bool TokenView::hasMore() const {
return current != tokens.end();
}
TokenView & TokenView::operator=(const TokenView & view){
tokens = view.tokens;
current = tokens.begin();
if (current != tokens.end()){
current++;
}
return *this;
}
TokenView & TokenView::operator>>(string & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
item = child->getName();
current++;
return *this;
}
TokenView & TokenView::operator>>(int & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
istringstream out(child->getName());
out >> item;
current++;
return *this;
}
TokenView & TokenView::operator>>(unsigned int & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
istringstream out(child->getName());
out >> item;
current++;
return *this;
}
TokenView & TokenView::operator>>(double & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
istringstream out(child->getName());
out >> item;
current++;
return *this;
}
static bool isTrue(const string & name){
return name == "true" ||
name == "1" ||
name == "on" ||
name == "enable";
}
static bool isFalse(const string & name){
return name == "false" ||
name == "0" ||
name == "off" ||
name == "disable";
}
TokenView & TokenView::operator>>(bool & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
string what = child->getName();
if (isTrue(what)){
item = true;
} else if (isFalse(what)){
item = false;
} else {
ostringstream fail;
fail << "Not a true/false value: " << what << ". True values are 'true', '1', 'on', 'enable'. False values are 'false', 0', 'off', 'disable'";
throw TokenException(__FILE__, __LINE__, fail.str());
}
current++;
return *this;
}
TokenView & TokenView::operator>>(const Token* & item){
if (current == tokens.end()){
throw TokenException(__FILE__, __LINE__, "No more elements");
}
const Token * child = *current;
/*
if (!child->isData()){
throw TokenException(__FILE__, __LINE__, "Token is not a datum");
}
*/
item = child;
current++;
return *this;
}
TokenException::TokenException(const std::string & file, int line, const std::string reason):
Exception::Base(file, line),
reason(reason){
}
TokenException::TokenException(const TokenException & copy):
Exception::Base(copy),
reason(copy.reason){
}
TokenException::TokenException(const Exception::Base & copy):
Exception::Base(copy),
reason("unknown"){
}
Exception::Base * TokenException::copy() const {
return new TokenException(*this);
}
TokenException::~TokenException() throw() {
}
diff --git a/util/token.h b/util/token.h
index 5a6ba601..f92f5e01 100644
--- a/util/token.h
+++ b/util/token.h
@@ -1,308 +1,310 @@
#ifndef _paintown_token_h
#define _paintown_token_h
#include <string>
#include <vector>
#include <ostream>
#include "token_exception.h"
class TokenReader;
class Configuration;
class Token;
class TokenView{
public:
TokenView(std::vector<const Token *> tokens);
TokenView(const TokenView & view);
bool hasMore() const;
TokenView & operator=(const TokenView & view);
TokenView & operator>>(const Token* & item);
TokenView & operator>>(std::string & item);
TokenView & operator>>(int & item);
TokenView & operator>>(unsigned int & item);
TokenView & operator>>(double & item);
TokenView & operator>>(bool & item);
protected:
std::vector<const Token*> tokens;
std::vector<const Token*>::iterator current;
};
class TokenMatcher{
public:
template <typename X1>
bool match(X1 & obj1);
template <typename X1, typename X2>
bool match(X1 & obj1, X2 & obj2);
template <typename X1, typename X2, typename X3>
bool match(X1 & obj1, X2 & obj2, X3 & obj3);
template <typename X1, typename X2, typename X3, typename X4>
bool match(X1 & obj1, X2 & obj2, X3 & obj3, X4 & obj4);
TokenMatcher & operator=(const TokenMatcher & matcher);
protected:
TokenMatcher(std::vector<const Token*> tokens);
explicit TokenMatcher();
friend class Token;
std::vector<const Token*> tokens;
std::vector<const Token*>::iterator current;
};
/* Token:
* Basically a tree where each node stores a value in a string
* and can have 0 or more children
*/
class Token{
public:
typedef TokenMatcher Matcher;
Token();
Token(Token const & copy);
Token(std::string tok, bool parse = true);
virtual ~Token();
/* add an existing token to the tree */
void addToken(Token * t);
/* creates a new empty token and returns it */
Token * newToken();
/*
inline const string & getName(){
return name;
}
*/
const std::string & getName() const;
const Token * getParent() const;
Token * getParent();
/* get the original parent, the parent with no parents */
const Token * getRootParent() const;
void setFile( const std::string & s );
const std::string getFileName() const;
/* gets the parents of the token.
*
*
* The lineage of (baz) is 'foo -> bar -> baz'
*/
const std::string getLineage() const;
void print( const std::string space ) const;
std::string toString() const;
/* a pretty printed s-expression */
void toString(std::ostream & stream, const std::string & space) const;
/* no extra whitespace */
void toStringCompact(std::ostream & stream) const;
+ std::string toStringCompact() const;
/*
bool match(const std::string & subject) const {
TokenMatcher matcher = getMatcher(subject);
return false;
}
*/
template <typename X>
bool match(const std::string & subject, X & obj) const {
TokenMatcher matcher = getMatcher(subject);
return matcher.match(obj);
}
template <typename X1, typename X2>
bool match(const std::string & subject, X1 & obj1, X2 & obj2) const {
TokenMatcher matcher = getMatcher(subject);
return matcher.match(obj1, obj2);
/*
return matcher.match(obj1) &&
matcher.match(obj2);
*/
}
template <typename X1, typename X2, typename X3>
bool match(const std::string & subject, X1 & obj1, X2 & obj2, X3 & obj3) const {
TokenMatcher matcher = getMatcher(subject);
return matcher.match(obj1, obj2, obj3);
}
template <typename X1, typename X2, typename X3, typename X4>
bool match(const std::string & subject, X1 & obj1, X2 & obj2, X3 & obj3, X4 & obj4) const {
TokenMatcher matcher = getMatcher(subject);
return matcher.match(obj1, obj2, obj3, obj4);
}
/*
template <typename X1, typename X2, typename X3, typename X4, typename X5>
bool match(const std::string & subject, X1 & obj1, X2 & obj2, X3 & obj3, X4 & obj4, X5 & obj5) const {
TokenMatcher matcher = getMatcher(subject);
return matcher.match(obj1) &&
matcher.match(obj2) &&
matcher.match(obj3) &&
matcher.match(obj4) &&
matcher.match(obj5);
}
*/
TokenView view() const;
TokenMatcher getMatcher(const std::string & subject) const;
Token * getToken(unsigned int n) const;
/* xpath-esque searching for tokens
* '/' delimits tokens
* <literal> matches a token
*/
const Token * findToken(const std::string & path) const;
Token * findToken(const std::string & path);
/* find all tokens. special characters _ and * */
std::vector<const Token *> findTokens(const std::string & path) const;
/* Removes the given token if the current one contains it */
void removeToken(Token * token);
inline signed int numTokens() const {
return tokens.size() - 1;
}
inline bool isData() const {
return numTokens() == -1;
}
inline const std::vector< Token * > * getTokens() const {
return &tokens;
}
inline void resetToken(){
num_token = 1;
}
/* returns a deep copy of this token. the parent field is set to null */
Token * copy() const;
Token * readToken();
bool hasTokens() const;
bool operator== ( const std::string & rhs ) const;
bool operator!= ( const std::string & rhs ) const;
/*
Token & operator>>( std::string & rhs ) throw( TokenException );
Token & operator>>( int & rhs ) throw( TokenException );
Token & operator>>( double & rhs ) throw( TokenException );
Token & operator>>( Token * & rhs ) throw( TokenException );
Token & operator>>( bool & rhs ) throw( TokenException );
*/
- Token & operator<<( const std::string rhs );
- Token & operator<<( const int rhs );
+ Token & operator<<(const std::string rhs);
+ Token & operator<<(const int rhs);
+ Token & operator<<(const unsigned int rhs);
Token & operator<<(Token * token);
- Token & operator<<( const double rhs );
+ Token & operator<<(const double rhs);
protected:
friend class TokenReader;
virtual inline const std::string & _getName(){
return name;
}
virtual inline void setParent(Token * parent){
this->parent = parent;
}
std::string lowerCase(const std::string & s) const;
unsigned int num_token;
std::vector< Token * > tokens;
std::string filename;
Token * parent;
std::string name;
bool own;
};
template <typename X1>
bool TokenMatcher::match(X1 & obj1){
if (current == tokens.end()){
return false;
}
const Token * token = *current;
current++;
try{
token->view() >> obj1;
return true;
} catch (const TokenException & t){
}
return false;
}
template <typename X1, typename X2>
bool TokenMatcher::match(X1 & obj1, X2 & obj2){
if (current == tokens.end()){
return false;
}
const Token * token = *current;
current++;
try{
token->view() >> obj1 >> obj2;
return true;
} catch (const TokenException & t){
}
return false;
}
template <typename X1, typename X2, typename X3>
bool TokenMatcher::match(X1 & obj1, X2 & obj2, X3 & obj3){
if (current == tokens.end()){
return false;
}
const Token * token = *current;
current++;
try{
token->view() >> obj1 >> obj2 >> obj3;
return true;
} catch (const TokenException & t){
}
return false;
}
template <typename X1, typename X2, typename X3, typename X4>
bool TokenMatcher::match(X1 & obj1, X2 & obj2, X3 & obj3, X4 & obj4){
if (current == tokens.end()){
return false;
}
const Token * token = *current;
current++;
try{
token->view() >> obj1 >> obj2 >> obj3 >> obj4;
return true;
} catch (const TokenException & t){
}
return false;
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Mon, Jun 22, 8:11 PM (6 d, 11 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72939
Default Alt Text
(27 KB)

Event Timeline