This repository has been archived on 2024-03-07. You can view files and clone it, but cannot push or open issues/pull-requests.
2023-09-27 18:18:05 +00:00
|
|
|
#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);
|
|
|
|
|
|
|
|
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_ident() const;
|
2023-09-27 21:05:04 +00:00
|
|
|
std::optional<ScanInfo> scan_int() const;
|
2023-09-27 18:18:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|