53 lines
1.0 KiB
C++
53 lines
1.0 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);
|
||
|
|
||
|
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;
|
||
|
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif
|