Add ast::node and handle<T*, Error> comparison
And parser test for literals fingerscrossed This commit is good, innit? But there will also be second one, and dessert Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -23,11 +23,19 @@ public:
|
||||
statement_node_t statement_type() const override { return statement_node_t::Declaration; }
|
||||
|
||||
virtual declaration_node_t declaration_type() const = 0;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
struct function_body {
|
||||
location begin, end;
|
||||
std::vector<node_handle<statement_node>> statements;
|
||||
|
||||
bool operator==(const function_body& rhs) const {
|
||||
return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
|
||||
}
|
||||
|
||||
bool operator!=(const function_body& rhs) const { return !this->operator==(rhs); }
|
||||
};
|
||||
|
||||
using function_body_handle = handle<ast::function_body>;
|
||||
@@ -42,6 +50,8 @@ public:
|
||||
front::token name() const { return m_name; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
protected:
|
||||
front::token m_name;
|
||||
};
|
||||
@@ -56,6 +66,8 @@ public:
|
||||
const function_body_handle& body() const { return m_body; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
function_body_handle m_body;
|
||||
};
|
||||
|
||||
@@ -18,6 +18,8 @@ public:
|
||||
statement_node_t statement_type() const override { return statement_node_t::Expression; }
|
||||
|
||||
virtual expression_node_t expression_type() const = 0;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
expression_node_t expression_type() const override { return expression_node_t::Literal; }
|
||||
|
||||
virtual literal_node_t literal_type() const = 0;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
class string_literal_node : public literal_node {
|
||||
@@ -31,10 +33,9 @@ public:
|
||||
|
||||
const handle<std::string_view>& value() const { return m_value; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override {
|
||||
if (m_value.has_error()) return os << m_value.error();
|
||||
return os << "string literal (" << *m_value << ")";
|
||||
}
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
handle<std::string_view> m_value;
|
||||
};
|
||||
@@ -49,6 +50,8 @@ public:
|
||||
const handle<front::integer_token>& value() const { return m_value; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
handle<front::integer_token> m_value;
|
||||
};
|
||||
|
||||
@@ -37,10 +37,15 @@ public:
|
||||
node& operator=(const node&) = delete;
|
||||
public:
|
||||
virtual node_t category() const = 0;
|
||||
|
||||
public:
|
||||
bool operator==(const node& rhs) const { return category() == rhs.category() && equal(rhs); }
|
||||
bool operator!=(const node& rhs) const { return !this->operator==(rhs); }
|
||||
public:
|
||||
virtual std::ostream& print(std::ostream& os) const = 0;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const node& node) { return node.print(os); }
|
||||
protected:
|
||||
virtual bool equal(const node& rhs) const = 0;
|
||||
};
|
||||
|
||||
template <typename T, typename Error = std::string>
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
const std::vector<node_handle<declaration_node>>& declarations() const { return m_declarations; }
|
||||
public:
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
std::vector<node_handle<declaration_node>> m_declarations;
|
||||
};
|
||||
|
||||
@@ -17,6 +17,8 @@ public:
|
||||
node_t category() const override { return node_t::Statement; }
|
||||
|
||||
virtual statement_node_t statement_type() const = 0;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
};
|
||||
|
||||
class expression_node;
|
||||
@@ -26,10 +28,14 @@ public:
|
||||
|
||||
return_statement_node(node_handle<expression_node>&& value)
|
||||
: m_value(std::move(value)) {}
|
||||
public:
|
||||
node_handle<expression_node> value() const { return m_value; }
|
||||
public:
|
||||
statement_node_t statement_type() const override { return statement_node_t::Return; }
|
||||
|
||||
std::ostream& print(std::ostream& os) const override;
|
||||
protected:
|
||||
bool equal(const node& rhs) const override;
|
||||
private:
|
||||
node_handle<expression_node> m_value;
|
||||
};
|
||||
|
||||
@@ -10,6 +10,12 @@ struct location {
|
||||
std::string_view filename;
|
||||
std::size_t line = 0;
|
||||
std::size_t column = 0;
|
||||
|
||||
bool operator==(const location& rhs) const {
|
||||
return filename == rhs.filename && line == rhs.line && column == rhs.column;
|
||||
}
|
||||
|
||||
bool operator!=(const location& rhs) const { return !this->operator==(rhs); }
|
||||
};
|
||||
|
||||
static inline std::ostream& operator<<(std::ostream& os, const location& location) {
|
||||
|
||||
@@ -80,7 +80,16 @@ public:
|
||||
pointer operator->() { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
||||
const_pointer operator->() const { return &m_value.value(); } // NOLINT(bugprone-unchecked-optional-access)
|
||||
public:
|
||||
bool operator==(const T& rhs) const { return m_value.has_value() && *m_value == rhs; }
|
||||
bool operator==(const handle<T, Error>& rhs) const {
|
||||
if (present() != rhs.present() || error() != rhs.error()) return false;
|
||||
if (m_value.has_value() && m_value.value() != rhs.m_value.value()) // NOLINT(bugprone-unchecked-optional-access)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator==(const T& rhs) const {
|
||||
return m_value.has_value() && *m_value == rhs; // NOLINT(bugprone-unchecked-optional-access)
|
||||
}
|
||||
public:
|
||||
friend std::ostream& operator<<(std::ostream& os, const handle& result) {
|
||||
os << result.m_location << ": ";
|
||||
@@ -167,13 +176,23 @@ public:
|
||||
bool present() const { return m_data.has_value() && m_data->value != nullptr; }
|
||||
bool has_error() const { return m_data.has_value() && m_data->value == nullptr; }
|
||||
|
||||
std::shared_ptr<value_type> shared() const { return m_data->value; }
|
||||
Error error() const { return m_data->error; }
|
||||
std::shared_ptr<value_type> shared() const { return m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
|
||||
Error error() const { return m_data->error; } // NOLINT(bugprone-unchecked-optional-access)
|
||||
|
||||
reference operator*() { return *m_data->value; }
|
||||
const_reference operator*() const { return *m_data->value; }
|
||||
pointer operator->() { return m_data->value.get(); }
|
||||
const_pointer operator->() const { return m_data->value.get(); }
|
||||
reference operator*() { return *m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
|
||||
const_reference operator*() const { return *m_data->value; } // NOLINT(bugprone-unchecked-optional-access)
|
||||
pointer operator->() { return m_data->value.get(); } // NOLINT(bugprone-unchecked-optional-access)
|
||||
const_pointer operator->() const { return m_data->value.get(); } // NOLINT(bugprone-unchecked-optional-access)
|
||||
public:
|
||||
bool operator==(const handle<T*, Error>& rhs) const {
|
||||
if (present() != rhs.present() || error() != rhs.error()) return false;
|
||||
if (present() && m_data->value != rhs.m_data->value) return false; // NOLINT(bugprone-unchecked-optional-access)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator==(const value_type& rhs) const {
|
||||
return present() && *m_data->value == rhs; // NOLINT(bugprone-unchecked-optional-access)
|
||||
}
|
||||
public:
|
||||
friend std::ostream& operator<<(std::ostream& os, const handle& result) {
|
||||
if (!result.m_data.has_value()) return os << "handle empty";
|
||||
@@ -211,11 +230,13 @@ private:
|
||||
template <typename U,
|
||||
typename = std::enable_if_t<std::is_base_of_v<value_type, U> || std::is_base_of_v<U, value_type>>>
|
||||
data(const handle<U*, Error>& other)
|
||||
: location(other.m_data->location) {
|
||||
: location(other.m_data->location) { // NOLINT(bugprone-unchecked-optional-access)
|
||||
if constexpr (std::is_base_of_v<value_type, U>) {
|
||||
value = std::static_pointer_cast<value_type>(std::move(other.m_data->value));
|
||||
value = std::static_pointer_cast<value_type>(
|
||||
std::move(other.m_data->value)); // NOLINT(bugprone-unchecked-optional-access)
|
||||
} else {
|
||||
value = std::dynamic_pointer_cast<value_type>(std::move(other.m_data->value));
|
||||
value = std::dynamic_pointer_cast<value_type>(
|
||||
std::move(other.m_data->value)); // NOLINT(bugprone-unchecked-optional-access)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user