Commit a2a3bd3c authored by Šmelko Adam Mgr.'s avatar Šmelko Adam Mgr.
Browse files

add test

parent d400c62f
#include "parser.hpp"
std::vector<parser::line_t> parser::process_input(std::istream& input)
{
std::vector<line_t> lines;
while (input.good())
{
std::string line;
getline(input, line);
lines.emplace_back(process_line(line));
}
return lines;
}
parser::line_t parser::process_line(const std::string& line)
{
auto splits = split(line);
line_t tokens;
for (size_t i = 0; i < splits.size(); i++)
{
if (splits[i].back() == ':')
{
splits[i].pop_back();
tokens.emplace_back(std::make_unique<label>(std::move(splits[i])));
}
else if (splits[i].front() == '#')
{
std::string comment_text;
for (size_t j = i; j < splits.size(); j++)
comment_text += splits[j];
tokens.emplace_back(std::make_unique<comment>(std::move(comment_text)));
break;
}
else
tokens.emplace_back(std::make_unique<word>(std::move(splits[i])));
}
return tokens;
}
std::vector<std::string> parser::split(const std::string& line)
{
std::vector<std::string> tokens;
tokens.emplace_back();
for (auto&& c : line)
{
if (c == ' ' || c == '\t')
{
if (!tokens.back().empty())
{
tokens.emplace_back();
}
}
else
{
tokens.back() += c;
}
}
if (tokens.back().empty())
tokens.pop_back();
return tokens;
}
#include <iostream>
#include <string>
#include <vector>
#include <memory>
enum class token_t
{
LABEL, COMMENT, WORD
};
struct token;
using token_ptr = std::unique_ptr<token>;
struct token
{
virtual token_t get_type() const = 0;
virtual ~token() = default;
};
struct string_token : token
{
std::string text;
string_token(std::string text) : text(std::move(text)) {}
};
struct label : string_token
{
label(std::string label_text) : string_token(std::move(label_text)) {}
virtual token_t get_type() const { return token_t::LABEL; }
};
struct comment : string_token
{
comment(std::string comment_text) : string_token(std::move(text)) {}
virtual token_t get_type() const { return token_t::COMMENT; }
};
struct word : string_token
{
word(std::string word_text) : string_token(std::move(word_text)) {}
virtual token_t get_type() const { return token_t::WORD; }
};
class parser
{
public:
using line_t = std::vector<token_ptr>;
// Retrieves all lines in a vector.
// A line consists of vector of tokens.
std::vector<line_t> process_input(std::istream& input);
private:
std::vector<std::string> split(const std::string& line);
line_t process_line(const std::string& line);
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment