#include "token.h" void gen_tokens(std::string filename, std::vector & tokens){ std::ifstream input_file(filename.c_str()); if (!input_file) { std::cerr << "I can't read " << filename << "." << std::endl; abort(); } std::string line; for (int line_no = 1; std::getline(input_file, line); ++line_no) { for (size_t i = 0; i < line.size();) { // comments if (line[i] == '/') { ++i; if ((i == line.size()) || (line[i] != '/')) { std::cerr << "LINE " << line_no << ": a single / is not allowed" << std::endl; abort(); } break; // skip the rest of the line by exiting the loop } // spaces if ((line[i] == ' ') || (line[i] == '\t') || (line[i] == '\r') || (line[i] == '\n')) { ++i; // skip this space character continue; // skip the rest of the iteration } // SINGLE if ((line[i] == '(') || (line[i] == ')') || (line[i] == '[') || (line[i] == ']') || (line[i] == ':') || (line[i] == ';') || (line[i] == ',')) { tokens.push_back(Token(Token::SINGLE, std::string(&(line[i]),1))); ++i; // we consumed this character continue; // skip the rest of the iteration } // NAME if (((line[i] >= 'a') && (line[i] <= 'z')) // a to z || ((line[i] >= 'A') && (line[i] <= 'Z')) // A to Z || (line[i] == '_')) { size_t name_begin = i; for (++i; i < line.size(); ++i) { if (!(((line[i] >= 'a') && (line[i] <= 'z')) || ((line[i] >= 'A') && (line[i] <= 'Z')) || ((line[i] >= '0') && (line[i] <= '9')) || (line[i] == '_') || (line[i] == '$'))) { break; // [name_begin, i) is the range for the token } } tokens.push_back(Token(Token::NAME, line.substr(name_begin, i - name_begin))); } else { // NUMBER if ((line[i] >= '0') && (line[i] <= '9')) // 0-9 { char * begin = &(line[i]); char * end = NULL; //used to find the //start and end of the number; strtoll(begin, &end, 10); i+= end - begin; tokens.push_back(Token(Token::NUMBER, std::string(begin, end - begin))); continue; } else { std::cerr << "LINE " << line_no << ": invalid character" << std::endl; abort(); } } } } }