59 lines
1.3 KiB
C++
59 lines
1.3 KiB
C++
#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();
|
|
|
|
Loc loc() const { return m_loc; }
|
|
|
|
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;
|
|
std::vector<std::string> m_texts;
|
|
|
|
bool more(size_t index) const;
|
|
char at(size_t index) const;
|
|
bool is_sep(size_t index) const;
|
|
|
|
void skip_spaces();
|
|
|
|
std::optional<ScanInfo> scan_int() const;
|
|
std::optional<ScanInfo> scan_text(NodeType type,
|
|
std::string const& text,
|
|
bool has_value) const;
|
|
|
|
std::optional<ScanInfo> scan_keyword(NodeType type,
|
|
std::string const& text,
|
|
bool has_value) const;
|
|
|
|
std::optional<ScanInfo> scan_ident() const;
|
|
};
|
|
}
|
|
|
|
#endif
|