1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
* ========================================================================== *
* *
* This file is part of the Openterface Mini KVM App QT version *
* *
* Copyright (C) 2024 <info@openterface.com> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation version 3. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* ========================================================================== *
*/
#include "Lexer.h"
#include <cctype>
#include <stdexcept>
Lexer::Lexer() : currentIndex(0) {}
void Lexer::setSource(const std::string& source) {
this->source = source;
currentIndex = 0;
}
char Lexer::currentChar() {
return currentIndex < source.size() ? source[currentIndex] : '\0';
}
void Lexer::advance() {
if (currentIndex < source.size()) {
currentIndex++;
}
}
std::vector<Token> Lexer::tokenize() {
if (source.empty()) {
throw std::runtime_error("Source is not set.");
}
std::vector<Token> tokens;
Token token;
do {
token = nextToken();
tokens.push_back(token);
} while (token.type != AHKTokenType::ENDOFFILE);
return tokens;
}
Token Lexer::nextToken() {
while (std::isspace(currentChar())) {
if (currentChar() == '\n') {
advance();
return {AHKTokenType::NEWLINE, "\\n"};
}
advance();
return {AHKTokenType::WHITESPACE, " "};
}
if (std::isalpha(currentChar())) {
return identifier();
}
if (std::isdigit(currentChar())) {
return number();
}
for (const auto& op : operators) {
if (source.substr(currentIndex, op.length()) == op) {
advance();
return {AHKTokenType::OPERATOR, op};
}
}
if (currentChar() == '\0') {
return {AHKTokenType::ENDOFFILE, ""};
}
return symbol();
}
Token Lexer::identifier() {
std::string result;
while (std::isalnum(currentChar())) {
result += currentChar();
advance();
}
if (keywords.find(result) != keywords.end()) {
return {AHKTokenType::KEYWORD, result};
}
if (mouse_keyboard.find(result) != mouse_keyboard.end()){
return {AHKTokenType::COMMAND, result};
}
return {AHKTokenType::IDENTIFIER, result};
}
Token Lexer::number() {
std::string result;
bool hasDecimalPoint = false;
while (std::isdigit(currentChar()) || (currentChar() == '.' && !hasDecimalPoint)) {
if (currentChar() == '.') {
hasDecimalPoint = true;
}
result += currentChar();
advance();
}
return {hasDecimalPoint ? AHKTokenType::FLOAT : AHKTokenType::INTEGER, result};
}
Token Lexer::symbol() {
char current = currentChar();
advance();
return {AHKTokenType::SYMBOL, std::string(1, current)};
}
|