This repository has been archived on 2023-09-10. You can view files and clone it, but cannot push or open issues/pull-requests.
joko/lib/Lexer.hpp

52 lines
1.0 KiB
C++
Raw Normal View History

2023-09-09 13:09:43 +00:00
#ifndef jk_LEXER_HPP
#define jk_LEXER_HPP
#include "commons.hpp"
#include "Logger.hpp"
#include "Node.hpp"
namespace jk
{
JK_ERROR(lexical_error);
struct ScanInfo {
size_t cursor;
NodeType type;
std::string repr;
};
using scanner_t = std::function<std::optional<ScanInfo>()>;
class Lexer
{
public:
explicit Lexer(Logger& logger, Loc const& loc);
virtual ~Lexer();
2023-09-09 22:03:28 +00:00
Loc loc() const { return m_loc; }
2023-09-09 13:09:43 +00:00
void scan(std::string const& source);
std::shared_ptr<Node> next();
private:
Logger& m_logger;
Loc m_loc;
size_t m_cursor;
std::string m_source;
std::vector<scanner_t> m_scanners;
bool more(size_t index) const;
2023-09-09 22:03:28 +00:00
char at(size_t index) const;
2023-09-09 13:09:43 +00:00
void skip_spaces();
std::optional<ScanInfo> scan_int() const;
2023-09-09 22:03:28 +00:00
std::optional<ScanInfo> scan_text(NodeType type,
std::string const& text,
bool has_value) const;
std::optional<ScanInfo> scan_ident() const;
2023-09-09 13:09:43 +00:00
};
}
#endif