Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/util/token.cpp b/util/token.cpp
index 884478b2..3665135a 100644
--- a/util/token.cpp
+++ b/util/token.cpp
@@ -1,169 +1,182 @@
#include "token.h"
#include "token_exception.h"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <sstream>
using namespace std;
-Token::Token(){
+Token::Token():
+parent( NULL ){
name = "HEAD";
num_token = 1;
}
Token::Token( string tok, bool parse ):
-num_token( 1 ){
+num_token( 1 ),
+parent( NULL ){
/* legacy code, not used much */
if ( !parse ){
name = tok;
while ( name.find(' ' ) == 0 )
name.erase( 0, 1 );
lowerCase( name );
return;
}
}
/* Dump token to the screen */
void Token::print( const string & space ){
cout<<space<<"Token: "<< getName() << endl;
for ( signed int i = 0; i < numTokens(); i++ ){
Token * x = getToken( i );
x->print( space + " |--" );
}
-
}
/* helper function */
void Token::lowerCase( string & s ){
for ( unsigned int q = 0; q < s.length(); q++ ){
if ( s[q] >= 'A' && s[q] <= 'Z' )
s[q] = s[q] - 'A' + 'a';
}
}
/* 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(){
return num_token < tokens.size();
}
Token * Token::getToken( unsigned int n ){
int q = numTokens();
if ( q == -1 ) return NULL;
if ( n >= 0 && (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 string & Token::getName() const {
if ( numTokens() != -1 ){
return tokens[0]->_getName();
}
// cout<<"No tokens!!"<<endl;
return name;
}
+const Token * const Token::getParent() const {
+ return this->parent;
+}
+
+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 ){
return getName() == rhs;
}
bool Token::operator!= ( const string & rhs ){
return !( *this == rhs );
}
/* extracting operators */
Token & Token::operator>>( Token * & rhs ) throw( TokenException ){
Token * x = readToken();
if ( x == NULL ){
- throw TokenException("No more elements");
+ throw TokenException( 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 * l = readToken();
if ( l == NULL ){
- throw TokenException("No more elements");
+ throw TokenException( string("Tried to read a string from ") + this->getLineage() + string(" but there no more elements") );
}
rhs = l->getName();
// rhs = getName();
return *this;
}
Token & Token::operator>>( int & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
- throw TokenException("No more elements");
+ throw TokenException(string("Tried to read an int from ") + this->getLineage() + string(" but there are no more elements") );
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
Token & Token::operator>>( double & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
- throw TokenException("No more elements");
+ throw TokenException( string("Tried to read a double from ") + this->getLineage() + string(" but there no more elements") );
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
Token & Token::operator>>( bool & rhs ) throw( TokenException ){
Token * l = readToken();
if ( l == NULL ){
- throw TokenException("No more elements");
+ throw TokenException( string("Tried to read a bool from ") + this->getLineage() + string(" but there no more elements") );
}
istringstream is ( l->getName() );
is >> rhs;
return *this;
}
void Token::addToken( Token * t ){
tokens.push_back( t );
}
/* 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(){
for ( vector< Token * >::iterator it = tokens.begin(); it != tokens.end(); it++ )
delete *it;
}
diff --git a/util/token.h b/util/token.h
index bba081e6..40798d01 100644
--- a/util/token.h
+++ b/util/token.h
@@ -1,76 +1,84 @@
#ifndef _token_h
#define _token_h
#include <string>
#include <vector>
#include "token_exception.h"
class TokenReader;
using namespace std;
/* Token:
* Basically a tree where each node stores a value in a string
* and can have 0 or more children
*/
class Token{
public:
void addToken( Token * t );
/*
inline const string & getName(){
return name;
}
*/
- const string & getName();
+ const string & getName() const;
+ const Token * const getParent() const;
+
+ const string getLineage() const;
void print( const string & space );
Token * getToken( unsigned int n );
inline signed int numTokens() const{
return tokens.size() - 1;
}
inline const vector< Token * > * getTokens() const{
return &tokens;
}
inline void resetToken(){
num_token = 1;
}
Token * readToken();
bool hasTokens();
bool operator== ( const string & rhs );
bool operator!= ( const string & rhs );
Token & operator>>( 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 );
protected:
/* Only TokenReader can create and destroy a Token */
Token();
Token( string tok, bool parse = true );
virtual ~Token();
friend class TokenReader;
virtual inline const string & _getName(){
return name;
}
+ virtual inline void setParent( const Token * const parent ){
+ this->parent = parent;
+ }
+
void lowerCase( string & s );
void finalize();
unsigned int num_token;
vector< Token * > tokens;
+ Token const * parent;
string name;
};
#endif
diff --git a/util/token_exception.h b/util/token_exception.h
index 34ad7178..e641c5ee 100644
--- a/util/token_exception.h
+++ b/util/token_exception.h
@@ -1,25 +1,25 @@
#ifndef _token_exception_h
#define _token_exception_h
#include <exception>
#include <string>
using namespace std;
-class TokenException : public exception{
+class TokenException : public exception {
public:
TokenException();
TokenException( const string & reason );
~TokenException() throw();
inline const string & getReason() const{
return reason;
}
protected:
string reason;
};
#endif
diff --git a/util/tokenreader.cpp b/util/tokenreader.cpp
index 00437720..b27be2dc 100644
--- a/util/tokenreader.cpp
+++ b/util/tokenreader.cpp
@@ -1,140 +1,143 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "token.h"
#include "token_exception.h"
#include "tokenreader.h"
using namespace std;
TokenReader::TokenReader( const char * file ){
ifile.open( file );
myfile = file;
ifile >> noskipws;
cout<<"Opened "<<file<<endl;
}
TokenReader::~TokenReader(){
ifile.close();
/* tokenreader giveth, and tokenreader taketh */
for ( vector< Token * >::iterator it = my_tokens.begin(); it != my_tokens.end(); it++ ){
delete *it;
}
}
Token * TokenReader::readToken() throw( TokenException ){
if ( !ifile ) throw TokenException("Could not load file");
// Token * t;
// string token_string;
char open_paren = 'x';
int parens = 1;
while ( ifile.good() && open_paren != '(' ){
ifile >> open_paren;
}
// token_string += '(';
Token * cur_token = new Token();
my_tokens.push_back( cur_token );
Token * first = cur_token;
vector< Token * > token_stack;
token_stack.push_back( cur_token );
char n;
string cur_string = "";
// while ( parens != 0 ){
/* in_quote is true if a " is read and before another " is read */
bool in_quote = false;
/* escaped unconditionally adds the next character to the string */
bool escaped = false;
while ( !token_stack.empty() ){
if ( !ifile ){
cout<<__FILE__<<": "<<myfile<<" is bad. Open parens "<<parens<<endl;
// cout<<"Dump: "<< token_string << "Last token = [" << n << "]" << (int)n << endl;
first->print( " " );
throw TokenException("Wrong number of parentheses");
}
// char n;
// slow as we go
ifile >> n;
const char * alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./-_!";
const char * nonalpha = " ()#\"";
// cout<<"Alpha char: "<<n<<endl;
if ( escaped ){
cur_string += n;
escaped = false;
continue;
}
if ( n == '\\' ){
escaped = true;
continue;
}
if ( in_quote ){
if ( n == '"' ){
in_quote = false;
Token * sub = new Token( cur_string, false );
+ sub->setParent( cur_token );
cur_token->addToken( sub );
cur_string = "";
} else
cur_string += n;
} else {
if ( n == '"' )
in_quote = true;
if ( strchr( alpha, n ) != NULL ){
cur_string += n;
} else if ( cur_string != "" && strchr( nonalpha, n ) != NULL ){
// cout<<"Made new token "<<cur_string<<endl;
Token * sub = new Token( cur_string, false );
+ sub->setParent( cur_token );
cur_token->addToken( sub );
cur_string = "";
}
}
if ( n == '#' ){
while ( n != '\n' && !ifile.eof() ){
ifile >> n;
}
continue;
} else if ( n == '(' ){
Token * another = new Token();
+ another->setParent( cur_token );
cur_token->addToken( another );
cur_token = another;
token_stack.push_back( cur_token );
/*
parens++;
cout<<"Inc Parens is "<<parens<<endl;
*/
} else if ( n == ')' ){
if ( token_stack.empty() ){
cout<<"Stack is empty"<<endl;
throw TokenException("Stack is empty");
}
token_stack.pop_back();
cur_token = token_stack.back();
}
}
// first->print("");
first->finalize();
return first;
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 2:19 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69431
Default Alt Text
(10 KB)

Event Timeline