Basic parser

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-25 07:45:24 +02:00
committed by CHatingPython
parent 28f6deaba5
commit 7df35fc075
11 changed files with 687 additions and 36 deletions
+7 -11
View File
@@ -8,6 +8,7 @@ namespace front {
class lexer {
public:
lexer() = default;
lexer(std::string_view filename, std::string_view content);
~lexer() = default;
@@ -16,19 +17,14 @@ public:
lexer& operator=(lexer&&) = default;
lexer& operator=(const lexer&) = delete;
public:
token next_token();
token_handle<> next_token();
bool empty() const { return m_cursor >= m_content.size(); }
private:
void next();
char get(std::size_t offset = 0) const;
void skip_spaces();
void next();
char get(std::size_t offset = 0) const;
void skip_spaces();
location current_location();
private:
template <typename... Args>
token create_token(location location, Args&&... args) {
token tok(std::forward<Args>(args)...);
tok.location = location;
return tok;
}
private:
std::string_view m_filename;
std::string_view m_content;
+45
View File
@@ -0,0 +1,45 @@
#ifndef FURC_FRONT_PARSER_HPP
#define FURC_FRONT_PARSER_HPP
#include "furc/ast/declaration.hpp"
#include "furc/ast/node.hpp"
#include "furc/ast/program.hpp"
#include "furc/front/lexer.hpp"
#include <vector>
namespace furc {
namespace front {
class parser {
public:
parser(std::string_view filename, std::string_view content);
parser(std::string_view filename);
~parser() = default;
parser(parser&&) = default;
parser(const parser&) = delete;
parser& operator=(parser&&) = default;
parser& operator=(const parser&) = delete;
public:
ast::node_handle<ast::program_node> parse();
private:
ast::node_handle<ast::declaration_node> parse_declaration();
ast::node_handle<ast::statement_node> parse_statement();
ast::function_body_handle parse_body();
private:
token_handle<> next_token();
const token_handle<>& peek_token();
token_handle<> eat_token(token_t type);
private:
std::string m_filename;
std::string m_content;
lexer m_lexer;
std::vector<token_handle<>> m_peekBuffer;
};
} // namespace front
} // namespace furc
#endif // FURC_FRONT_PARSER_HPP
+45 -3
View File
@@ -1,7 +1,7 @@
#ifndef FURC_FRONT_TOKEN_HPP
#define FURC_FRONT_TOKEN_HPP
#include "furc/diag.hpp"
#include "furc/handle.hpp"
#include <cassert>
#include <ostream>
@@ -24,6 +24,8 @@ enum class token_t {
Rbracket,
Semicolon,
Colon,
Comma,
Dot,
};
static inline bool is_token_type_empty(token_t type) {
@@ -45,6 +47,28 @@ static inline std::ostream& operator<<(std::ostream& os, token_t type) {
case token_t::Rbracket: return os << "']'";
case token_t::Semicolon: return os << "';'";
case token_t::Colon: return os << "':'";
case token_t::Comma: return os << "','";
case token_t::Dot: return os << "'.'";
}
}
static inline std::string operator+(const std::string& str, token_t type) {
switch (type) {
case token_t::None: return str + "none";
case token_t::Error: return str + "error";
case token_t::Identifier: return str + "identifier";
case token_t::Keyword: return str + "keyword";
case token_t::Integer: return str + "integer";
case token_t::Lparen: return str + "'('";
case token_t::Rparen: return str + "')'";
case token_t::Lbrace: return str + "'{'";
case token_t::Rbrace: return str + "'}'";
case token_t::Lbracket: return str + "'['";
case token_t::Rbracket: return str + "']'";
case token_t::Semicolon: return str + "';'";
case token_t::Colon: return str + "':'";
case token_t::Comma: return str + "','";
case token_t::Dot: return str + "'.'";
}
}
@@ -66,12 +90,27 @@ enum class keyword_token {
Return,
};
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
switch (keyword) {
case keyword_token::None: return os << "none";
case keyword_token::Func: return os << "func";
case keyword_token::Return: return os << "return";
}
}
static inline std::string operator+(const std::string& str, keyword_token keyword) {
switch (keyword) {
case keyword_token::None: return str + "none";
case keyword_token::Func: return str + "func";
case keyword_token::Return: return str + "return";
}
}
struct token {
token_t type = token_t::None;
error_token error = error_token::None;
keyword_token keyword = keyword_token::None;
std::string_view value;
location location;
token() = default;
@@ -86,7 +125,7 @@ struct token {
};
static inline std::ostream& operator<<(std::ostream& os, const token& token) {
os << token.location << ": " << token.type;
os << token.type;
switch (token.type) {
case token_t::Error: {
os << ": " << token.error;
@@ -100,6 +139,9 @@ static inline std::ostream& operator<<(std::ostream& os, const token& token) {
}
}
template <typename Error = std::string>
using token_handle = handle<token, Error>;
} // namespace front
} // namespace furc