roza/lib/Lexer.hpp

55 lines
1.2 KiB
C++
Raw Normal View History

2023-08-30 18:06:26 +00:00
#ifndef roza_LEXER_HPP
#define roza_LEXER_HPP
#include "commons.hpp"
#include "Node.hpp"
#include "SrcLoc.hpp"
#include "StatusLog.hpp"
namespace roza
{
struct ScanInfo {
std::shared_ptr<Node> node = nullptr;
size_t cursor = 0;
};
using scanner = std::function<ScanInfo(void)>;
class Lexer
{
public:
explicit Lexer(StatusLog& log, SrcLoc loc);
virtual ~Lexer();
SrcLoc loc() const { return m_loc; }
size_t size() const { return m_nodes.size(); }
2023-08-30 22:31:19 +00:00
bool is_at_end() const;
2023-08-30 18:06:26 +00:00
void scan(std::string const& source);
void skip_blanks();
std::shared_ptr<Node> get_or_nullptr(size_t index) const;
private:
StatusLog& m_log;
SrcLoc m_loc;
std::string m_source;
size_t m_cursor = 0;
std::vector<std::shared_ptr<Node>> m_nodes;
std::vector<scanner> m_scanners;
2023-08-31 09:07:03 +00:00
bool is_sep(size_t index) const;
2023-08-30 18:06:26 +00:00
ScanInfo scan_int() const;
2023-08-30 22:31:19 +00:00
ScanInfo scan_text(std::string const& text,
NodeType type,
bool value=false) const;
2023-08-31 09:07:03 +00:00
ScanInfo scan_keyword(std::string const& keyword,
NodeType type,
bool value=false) const;
2023-08-30 18:06:26 +00:00
};
}
#endif