#include "Lexer.h" Lexer::Lexer() { string keys[] = {"function", "class", "for", "while", "if", "else", "true", "false", "and", "or"}; keywords.assign(keys, keys+10); char punct[] = {'.', ',', ';', '{', '}', '[', ']', '(', ')'}; punctuation.assign(punct, punct+9); string oper[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!="}; operators.assign(oper, oper +17); } Lexer::~Lexer() { //dtor } std::string Lexer::unescape_string(string text) { std::string result = text; unsigned int offset = 0; for(unsigned int i = 0; i < text.size(); i++) { char replacement = 0; if(text[i] == '\\') { switch (text[i+1]) { case 'n': replacement = '\n'; break; case 'r': replacement = '\r'; break; case 't': replacement = '\t'; break; case '[': replacement = 27; break; } } if(replacement != 0) { char replacement_chars[2]; replacement_chars[0] = replacement; replacement_chars[1] = 0; result.replace(i + offset, 2, replacement_chars); offset--; } } return result; } void Lexer::add_keyword(string word) { if(!is_keyword(word)) { keywords.push_back(word); } } void Lexer::add_punctuation_char(char c) { if(!is_punctuation(c)) { punctuation.push_back(c); } } void Lexer::add_operator(string oper) { if(!is_operator(oper)) { operators.push_back(oper); } } vector Lexer::parse_line(string line) { vector tokens; while(line != "") { pair result_of_parsing = parse_token(line); line = result_of_parsing.first; Token token = result_of_parsing.second; if(token.get_value() != "") { tokens.push_back(token); } } return tokens; } pair Lexer::parse_token(string line) { string token_value = ""; unsigned int i; for(i=0; i< line.size(); i++) { if(token_value == "" && isspace(line[i])) continue; if(isdigit(line[i])) { token_value += line[i++]; for(; i < line.size(); i++) { if(isdigit(line[i]) || line[i] == '.') { token_value += line[i]; } else { break; } } } if(line[i] == '\"') { token_value += line[i++]; for(; i < line.size() ; i++) { token_value += line[i]; if (line[i] == '\"') { i++; break; } } } if(isalnum(line[i]) || line[i]== '_') { token_value += line[i]; } else if(ispunct(line[i])) { if(token_value=="") { token_value=line[i]; i++; if(i