Files
BlitzLLVM/code_compiler/source/lexer.hpp
T

78 lines
1.8 KiB
C++
Raw Normal View History

2024-06-25 18:59:15 +02:00
/// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2017-2025 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
2024-06-25 18:59:15 +02:00
// AUTOGENERATED COPYRIGHT HEADER END
2017-11-13 02:14:57 +01:00
#pragma once
2024-06-26 00:31:06 +02:00
#include <cinttypes>
2024-06-25 18:59:15 +02:00
#include <filesystem>
#include <fstream>
#include <iostream>
2017-11-13 02:14:57 +01:00
#include <istream>
2024-06-06 13:37:13 +02:00
#include <memory>
2017-11-13 02:14:57 +01:00
#include <string>
2024-06-06 13:37:13 +02:00
#include <utility>
2017-11-13 02:14:57 +01:00
2024-06-26 00:31:06 +02:00
#include "error.hpp"
2024-06-25 18:59:15 +02:00
// ToDo:
// - Figure out a way to let the lexer output line and character information?
namespace blitz {
struct token {
2024-06-26 00:31:06 +02:00
std::pair<uint64_t, uint64_t> location;
std::string text;
2024-06-25 18:59:15 +02:00
enum class variant : uint64_t {
NONE, // There is no token here.
2024-06-25 18:59:15 +02:00
UNKNOWN, // We have absolutely no fucking clue.
2024-06-26 00:31:06 +02:00
ENDOFFILE, // End of the file.
NEWLINE, // New Line.
2025-02-12 00:03:19 +01:00
//SEPARATOR, // Command Separator.
2024-06-26 00:31:06 +02:00
CONTROL, // All kinds of control signals
2024-06-25 18:59:15 +02:00
SYMBOL, // All kinds of symbols.
2024-06-26 00:31:06 +02:00
COMMENT, // ; Whatever
2024-06-25 18:59:15 +02:00
TEXT, // HelloWorld
STRING, // "HelloWorld"
2024-06-26 00:31:06 +02:00
INTEGER, // 1, 1% (without the %)
REAL, // 1.0, 1# (without the #)
2024-06-25 18:59:15 +02:00
} type;
2024-06-26 00:31:06 +02:00
std::string to_string();
2025-01-25 16:27:50 +01:00
bool operator==(blitz::token::variant rhs);
bool operator==(std::string const& rhs);
2024-06-25 18:59:15 +02:00
};
2017-11-13 02:14:57 +01:00
2024-06-25 18:59:15 +02:00
class lexer {
std::filesystem::path _file;
std::ifstream _stream;
2017-11-13 02:14:57 +01:00
2024-06-25 18:59:15 +02:00
// Current location in the file.
2024-06-26 00:31:06 +02:00
std::pair<uint64_t, uint64_t> _location;
2017-11-13 02:14:57 +01:00
2024-06-25 18:59:15 +02:00
blitz::token _current;
blitz::token _next;
2017-11-13 02:14:57 +01:00
public:
2024-06-25 18:59:15 +02:00
~lexer();
lexer(std::filesystem::path file);
/** Retrieve the current token information.
*/
blitz::token current();
/** Retrieve the next token in the given stream.
*
* This will replace the current token.
*/
blitz::token next();
2025-01-25 16:27:50 +01:00
/** Peek at the next token in the given stream.
*
* The current token will remain in-tact.
*/
blitz::token peek();
public:
std::filesystem::path file();
2017-11-13 02:14:57 +01:00
};
2024-06-25 18:59:15 +02:00
} // namespace blitz