Switch Token to smart pointer

This commit is contained in:
Jonathan Turner
2009-05-27 15:07:29 +00:00
parent fbb9534601
commit 332fdf0c8e
5 changed files with 43 additions and 40 deletions

View File

@@ -13,11 +13,11 @@
class TokenType { public: enum Type { Whitespace, Identifier, Number, Operator, Parens_Open, Parens_Close,
Square_Open, Square_Close, Curly_Open, Curly_Close, Comma, Quoted_String, Single_Quoted_String, Carriage_Return, Semicolon }; };
void debug_print(Token &token) {
std::cout << "Token: " << token.text << "(" << token.identifier << ") @ " << token.filename << ": (" << token.start.column
<< ", " << token.start.line << ") to (" << token.end.column << ", " << token.end.line << ") " << std::endl;
void debug_print(TokenPtr token) {
std::cout << "Token: " << token->text << "(" << token->identifier << ") @ " << token->filename << ": (" << token->start.column
<< ", " << token->start.line << ") to (" << token->end.column << ", " << token->end.line << ") " << std::endl;
}
void debug_print(std::vector<Token> &tokens) {
void debug_print(std::vector<TokenPtr> &tokens) {
for (unsigned int i = 0; i < tokens.size(); ++i) {
debug_print(tokens[i]);
}
@@ -42,14 +42,13 @@ std::string load_file(const char *filename) {
return ret_val;
}
void parse(std::vector<Token> &tokens) {
void parse(std::vector<TokenPtr> &tokens) {
Rule rule;
rule.rule = boost::bind(String_Rule, _1, _2, "def");
Token_Iterator iter = tokens.begin(), end = tokens.end();
std::pair<Token_Iterator, Token> results = rule(iter, end);
std::pair<Token_Iterator, TokenPtr> results = rule(iter, end);
if (results.first != iter) {
std::cout << "Parse successful: " << std::endl;
@@ -84,7 +83,7 @@ int main(int argc, char *argv[]) {
std::cout << "Expression> ";
std::getline(std::cin, input);
while (input != "quit") {
std::vector<Token> tokens = lexer.lex(input, "INPUT");
std::vector<TokenPtr> tokens = lexer.lex(input, "INPUT");
debug_print(tokens);
parse(tokens);
@@ -93,7 +92,7 @@ int main(int argc, char *argv[]) {
}
}
else {
std::vector<Token> tokens = lexer.lex(load_file(argv[1]), argv[1]);
std::vector<TokenPtr> tokens = lexer.lex(load_file(argv[1]), argv[1]);
debug_print(tokens);
}
}