Initial commit
This commit is contained in:
70
src/lexer/lexer.cpp
Normal file
70
src/lexer/lexer.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "lexer.h"
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
std::optional<char> Lexer::consume() {
|
||||
incrementor ++;
|
||||
if (incrementor < file.size()) {
|
||||
return file[incrementor];
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<char> Lexer::peek(int ahead) {
|
||||
if (incrementor + ahead < file.size()) {
|
||||
return file[incrementor + ahead];
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool Lexer::isDelimiter(char c) {
|
||||
if (std::find(delimiters.begin(), delimiters.end(), c) != delimiters.end()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructs a Lexer object and tokenizes the provided input string.
|
||||
*
|
||||
* This constructor initializes the Lexer with the given input string and processes it
|
||||
* to generate a list of tokens. It supports handling strings encapsulated by double quotes
|
||||
* and uses specified delimiters to separate tokens.
|
||||
*
|
||||
* @param in The input string to be tokenized.
|
||||
* @return A constructed Lexer instance with tokenized content stored in the `content` member.
|
||||
*/
|
||||
Lexer::Lexer(std::string in) : file(std::move(in)) {
|
||||
std::string buf;
|
||||
bool instring = false;
|
||||
while (true) {
|
||||
std::optional<char> c = consume();
|
||||
if (c.has_value()) {
|
||||
if (c.value() == '"') {
|
||||
instring = !instring;
|
||||
if (!instring) {
|
||||
content.push_back(buf + '"');
|
||||
buf.clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!instring && isDelimiter(c.value())) {
|
||||
if (!buf.empty()) content.push_back(buf);
|
||||
if (c.value() != ' ') content.emplace_back(1, c.value());
|
||||
buf.clear();
|
||||
} else {
|
||||
buf += c.value();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/lexer/lexer.h
Normal file
30
src/lexer/lexer.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
/**
|
||||
* @class Lexer
|
||||
* @brief The Lexer class processes input strings to tokenize and parse contents.
|
||||
*
|
||||
* This class is designed to take a given string input, tokenize it based on
|
||||
* specific delimiters, and store the resulting tokens. It facilitates basic
|
||||
* operations like consuming, peeking at characters, and identifying delimiters.
|
||||
*/
|
||||
class Lexer {
|
||||
private:
|
||||
std::vector<char> delimiters = {
|
||||
'(', ')', '{', '}', '.', '\n', ' '
|
||||
};
|
||||
std::string file;
|
||||
size_t incrementor = -1;
|
||||
std::optional<char> consume();
|
||||
std::optional<char> peek(int ahead = 1);
|
||||
bool isDelimiter(char c);
|
||||
|
||||
public:
|
||||
std::vector<std::string> content;
|
||||
explicit Lexer(std::string in);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user