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
+74
View File
@@ -0,0 +1,74 @@
#ifndef FURC_AST_DECLARATION_HPP
#define FURC_AST_DECLARATION_HPP
#include "furc/ast/node.hpp"
#include "furc/ast/statement.hpp"
#include "furc/front/token.hpp"
#include <ostream>
#include <vector>
namespace furc {
namespace ast {
enum class declaration_node_t {
Function,
Variable,
};
static inline std::ostream& operator<<(std::ostream& os, declaration_node_t type) {
switch (type) {
case declaration_node_t::Function: return os << "function";
case declaration_node_t::Variable: return os << "variable";
}
}
class declaration_node : public abstract_node<node_t::Declaration> {
public:
virtual declaration_node_t type() const = 0;
public:
virtual std::ostream& print(std::ostream& os) const = 0;
friend std::ostream& operator<<(std::ostream& os, const declaration_node& node) {
os << node.type() << " declaration";
return node.print(os);
}
};
struct function_body {
location begin, end;
std::vector<node_handle<statement_node>> statements;
};
using function_body_handle = handle<ast::function_body>;
class function_declarartion_node : public declaration_node {
public:
function_declarartion_node(front::token name)
: m_name(name) {}
function_declarartion_node(front::token name, function_body&& body)
: m_name(name), m_body(std::move(body)) {}
public:
declaration_node_t type() const override { return declaration_node_t::Function; }
public:
std::ostream& print(std::ostream& os) const override {
os << ": " << m_name;
if (m_body.has_value()) {
os << '\n' << m_body->begin << ": begin:";
for (const auto& entry : m_body->statements) {
os << '\n' << entry;
}
os << '\n' << m_body->end << ": end";
}
return os;
}
private:
front::token m_name;
std::optional<function_body> m_body;
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_DECLARATION_HPP
+44
View File
@@ -0,0 +1,44 @@
#ifndef FURC_AST_NODE_HPP
#define FURC_AST_NODE_HPP
#include "furc/handle.hpp"
#include <string>
namespace furc {
namespace ast {
enum class node_t {
Literal,
Expression,
Statement,
Declaration,
Program,
};
class node {
public:
node() = default;
virtual ~node() = default;
node(node&&) = default;
node(const node&) = delete;
node& operator=(node&&) = default;
node& operator=(const node&) = delete;
public:
virtual node_t category() const = 0;
};
template <node_t Category>
class abstract_node : public node {
public:
node_t category() const override { return Category; }
};
template <typename T, typename Error = std::string>
using node_handle = handle<T*, Error>;
} // namespace ast
} // namespace furc
#endif // FURC_AST_NODE_HPP
+35
View File
@@ -0,0 +1,35 @@
#ifndef FURC_AST_PROGRAM_HPP
#define FURC_AST_PROGRAM_HPP
#include "furc/ast/declaration.hpp"
#include "furc/ast/node.hpp"
#include <ostream>
#include <vector>
namespace furc {
namespace ast {
class program_node : public abstract_node<node_t::Program> {
public:
program_node() {}
public:
void push(node_handle<declaration_node>&& declaration) { m_declarations.push_back(std::move(declaration)); }
const std::vector<node_handle<declaration_node>>& declarations() const { return m_declarations; }
public:
friend std::ostream& operator<<(std::ostream& os, const program_node& node) {
os << "program";
for (const auto& handle : node.m_declarations) {
os << '\n' << handle;
}
return os;
}
private:
std::vector<node_handle<declaration_node>> m_declarations;
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_PROGRAM_HPP
+60
View File
@@ -0,0 +1,60 @@
#ifndef FURC_AST_STATEMENT_HPP
#define FURC_AST_STATEMENT_HPP
#include "furc/ast/node.hpp"
#include <ostream>
namespace furc {
namespace ast {
enum class statement_node_t {
Declaration,
Return,
};
static inline std::ostream& operator<<(std::ostream& os, statement_node_t type) {
switch (type) {
case statement_node_t::Declaration: return os << "declaration";
case statement_node_t::Return: return os << "return";
}
}
class statement_node : public abstract_node<node_t::Statement> {
public:
virtual statement_node_t type() const = 0;
public:
virtual std::ostream& print(std::ostream& os) const = 0;
friend std::ostream& operator<<(std::ostream& os, const statement_node& node) {
os << node.type() << " statement";
return node.print(os);
}
};
class declaration_node;
class declaration_statement_node : public statement_node {
public:
declaration_statement_node(declaration_node* declaration)
: m_declaration(declaration) {}
public:
statement_node_t type() const override { return statement_node_t::Declaration; }
std::ostream& print(std::ostream& os) const override { return os; }
private:
declaration_node* m_declaration = nullptr;
};
class return_statement_node : public statement_node {
public:
return_statement_node() = default;
public:
statement_node_t type() const override { return statement_node_t::Return; }
std::ostream& print(std::ostream& os) const override { return os; }
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_STATEMENT_HPP
+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
+193
View File
@@ -0,0 +1,193 @@
#ifndef FURC_HANDLE_HPP
#define FURC_HANDLE_HPP
#include "furc/diag.hpp"
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <type_traits>
namespace furc {
template <typename T, typename Error = std::string, typename = void>
class handle;
template <typename T, typename Error>
class handle<T, Error> {
template <typename, typename, typename>
friend class handle;
friend std::ostream& operator<<(std::ostream& os, const handle& result);
public:
using value_type = std::remove_reference_t<T>;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = T&;
using const_reference = const T&;
public:
handle(location location, value_type&& value)
: m_location(location), m_value(std::move(value)) {}
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_type, Args...>>>
handle(location location, Args&&... args)
: m_location(location), m_value(value_type(std::forward<Args>(args)...)) {}
handle(location location, Error&& error)
: m_location(location), m_error(std::move(error)) {}
template <typename U>
handle(const handle<U, Error>& error)
: m_location(error.location()), m_error(error) {}
~handle() = default;
handle(handle&& other) noexcept
: m_location(other.m_location), m_value(std::move(other.m_value)), m_error(std::move(other.m_error)) {
other.m_value.reset();
}
handle(const handle&) = delete;
handle& operator=(handle&& other) noexcept {
if (this == &other) return *this;
m_location = other.m_location;
m_value = other.m_value;
m_error = std::move(other.m_error);
other.m_value.reset();
return *this;
}
handle& operator=(const handle&) = delete;
public:
location location() const { return m_location; }
bool present() const { return m_value.has_value(); }
bool error() const { return !m_value.has_value(); }
value_type move() {
value_type value = *m_value;
m_value.reset();
return value;
}
operator reference() { return *m_value; }
operator const_reference() const { return *m_value; }
operator Error() const { return m_error; }
reference operator*() { return m_value; }
const_reference operator*() const { return m_value; }
pointer operator->() { return &*m_value; }
const_pointer operator->() const { return &*m_value; }
public:
friend std::ostream& operator<<(std::ostream& os, const handle& result) {
os << result.m_location << ": ";
if (result.m_value.has_value()) {
os << *result.m_value;
} else {
os << "ERROR: " << result.m_error;
}
return os;
}
private:
struct location m_location;
std::optional<value_type> m_value = {};
Error m_error;
};
template <typename T, typename Error>
class handle<T*, Error> {
template <typename, typename, typename>
friend class handle;
friend std::ostream& operator<<(std::ostream& os, const handle& result);
public:
using value_type = T;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = T&;
using const_reference = const T&;
public:
template <typename U = value_type, typename = std::enable_if_t<std::is_base_of_v<value_type, U>>>
handle(location location, std::shared_ptr<U> value)
: m_location(location), m_value(value) {}
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<value_type, Args...>>>
handle(location location, Args&&... args)
: m_location(location), m_value(std::make_shared<value_type>(std::forward<Args>(args)...)) {}
handle(location location, Error&& error)
: m_location(location), m_error(std::move(error)) {}
template <typename U>
handle(const handle<U, Error>& error)
: m_location(error.location()), m_error(error) {}
~handle() = default;
handle(handle&& other) noexcept
: m_location(other.m_location), m_value(std::move(other.m_value)), m_error(std::move(other.m_error)) {
other.m_value = nullptr;
}
template <typename U = value_type, typename = std::enable_if_t<std::is_base_of_v<value_type, U>>>
handle(handle<U*, Error>&& other) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
: m_location(other.m_location), m_value(std::move(other.m_value)), m_error(std::move(other.m_error)) {
other.m_value = nullptr;
}
handle(const handle& other)
: m_location(other.m_location), m_value(other.m_value), m_error(other.m_error) {}
handle& operator=(handle&& other) noexcept {
if (this == &other) return *this;
m_location = other.m_location;
m_value = std::move(other.m_value);
m_error = std::move(other.m_error);
other.m_value = nullptr;
return *this;
}
handle& operator=(const handle& other) {
if (this == &other) return *this;
m_location = other.m_location;
m_value = other.m_value;
m_error = other.m_error;
return *this;
}
public:
location location() const { return m_location; }
bool present() const { return m_value != nullptr; }
bool error() const { return m_value == nullptr; }
std::shared_ptr<value_type> shared() { return m_value; }
operator reference() { return *m_value; }
operator const_reference() const { return *m_value; }
operator Error() const { return m_error; }
reference operator*() { return *m_value; }
const_reference operator*() const { return *m_value; }
pointer operator->() { return m_value; }
const_pointer operator->() const { return m_value; }
public:
friend std::ostream& operator<<(std::ostream& os, const handle& result) {
os << result.m_location << ": ";
if (result.m_value != nullptr) {
os << *result.m_value;
} else {
os << "ERROR: " << result.m_error;
}
return os;
}
private:
struct location m_location;
std::shared_ptr<value_type> m_value = nullptr;
Error m_error;
};
} // namespace furc
#endif // FURC_HANDLE_HPP