This repository has been archived on 2024-03-07. You can view files and clone it, but cannot push or open issues/pull-requests.
wongola/lib/Lexer.hpp

61 lines
1.4 KiB
C++

#ifndef wg_LEXER_HPP
#define wg_LEXER_HPP
#include "commons.hpp"
#include "Node.hpp"
#include "Loc.hpp"
namespace wg
{
struct ScanInfo {
size_t cursor;
NodeType type;
std::string repr;
};
using scanner_t = std::function<std::optional<ScanInfo>()>;
class Lexer
{
public:
explicit Lexer();
virtual ~Lexer();
void scan(std::string const& source);
std::shared_ptr<Node> next();
std::vector<std::shared_ptr<Node>> all();
private:
std::string m_source;
size_t m_cursor = 0;
Loc m_loc;
std::vector<scanner_t> m_scanners;
std::vector<char> m_seps;
void add_text(std::string const& text,
NodeType node,
bool has_value=false);
void add_keyword(std::string const& text,
NodeType node,
bool has_value=false);
bool is_sep(size_t index) const;
void skip_spaces();
std::optional<ScanInfo> scan_text(std::string const& text,
NodeType type,
bool has_value) const;
std::optional<ScanInfo> scan_keyword(std::string const& text,
NodeType type,
bool has_value) const;
std::optional<ScanInfo> scan_ident() const;
std::optional<ScanInfo> scan_int() const;
};
}
#endif