bug fixes, tests passed, result modifier (!) added to function arguments and to types

This commit is contained in:
ProgramSnail 2023-07-24 13:01:34 +03:00
parent 4470454838
commit 3914ff7d8b
16 changed files with 418 additions and 62 deletions

View file

@ -15,10 +15,18 @@ public:
SymbolDocs(const std::string &description) : description_(description) {}
template <typename T>
bool add_annotation_info(const std::string &annotation, T &&info) {
bool add_annotation_info(const std::string &annotation, std::string &&info) {
if (annotations_info_.count(annotation) == 0) {
annotations_info_[annotation] = std::forward<T>(info);
annotations_info_[annotation] = std::move(info);
return true;
}
return false;
}
bool add_annotation_info(const std::string &annotation,
const std::string &info) {
if (annotations_info_.count(annotation) == 0) {
annotations_info_[annotation] = info;
return true;
}
return false;

View file

@ -2,6 +2,7 @@
#include "basic_nodes.hpp"
#include "tree_sitter_wrapper.hpp"
#include "utils.hpp"
#include <iostream>
@ -15,11 +16,6 @@ inline void print_position(std::ostream &out,
<< ']';
}
inline void handle_general_error(const std::string &message) {
std::cerr << "\x1b[1;31mGeneral Error:\x1b[0m " << message << ".\n";
exit(1);
}
inline void
handle_internal_error(const std::string &message, const std::string &place,
std::optional<nodes::Node> node = std::nullopt) {

View file

@ -363,15 +363,16 @@ private:
class NameExpression : public Node {
public:
template <typename T>
NameExpression(Node node, Identifier &&name)
: Node(node), name_(std::forward<T>(name)) {}
: Node(node), name_(std::move(name)) {}
NameExpression(Node node, const Identifier &name) : Node(node), name_(name) {}
NameExpression(
Node node, Identifier &&name,
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
&&arguments,
std::optional<const Type> &&prefix, bool is_point_call = false)
std::optional<TypeProxy> &&prefix, bool is_point_call = false)
: Node(node), name_(std::move(name)), arguments_(std::move(arguments)),
prefix_(std::move(prefix)), is_point_call_(is_point_call) {}
@ -379,16 +380,16 @@ public:
const std::string *get_name() const { return name_.get(); }
std::optional<const Type *> get_prefix() {
std::optional<TypeProxy> get_prefix() {
if (prefix_.has_value()) {
return &prefix_.value();
return prefix_.value();
}
return std::nullopt;
}
std::optional<const Type *> get_prefix() const {
std::optional<const TypeProxy> get_prefix() const {
if (prefix_.has_value()) {
return &prefix_.value();
return prefix_.value();
}
return std::nullopt;
}
@ -421,10 +422,10 @@ public:
private:
Identifier name_;
// universal function call syntax
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
arguments_;
// universal function call syntax
std::optional<const Type> prefix_;
std::optional<TypeProxy> prefix_;
// for static methods
bool is_point_call_ = false; // x.f ... or f x ...
};

View file

@ -61,12 +61,15 @@ public:
std::vector<Identifier> &&arguments,
std::vector<Modifier> &&reference_types,
std::vector<std::optional<TypeProxy>> &&types,
std::vector<bool> &&optional_arguments,
std::vector<bool> &&result_arguments,
std::optional<ExpressionProxy> expression)
: Node(node), docs_(std::move(docs)),
constraints_(std::move(constraints)), modifier_(modifier), name_(name),
annotations_(std::move(annotations)), arguments_(std::move(arguments)),
reference_types_(std::move(reference_types)), types_(std::move(types)),
expression_(expression) {}
optional_arguments_(optional_arguments),
result_arguments_(result_arguments), expression_(expression) {}
private:
SymbolDocs docs_;
@ -77,8 +80,9 @@ private:
std::vector<Identifier> arguments_;
std::vector<Modifier> reference_types_;
std::vector<std::optional<TypeProxy>> types_;
std::vector<bool> optional_arguments_;
std::vector<bool> result_arguments_;
std::optional<ExpressionProxy> expression_;
// std::vector<bool> optional_arguments_; // ??
}; // IN PROGRESS
class TypeDefinition : public Node {

View file

@ -2,7 +2,8 @@
#include <string>
// for clangd
#include "utils.hpp"
#include "tree_sitter/api.h"
extern "C" const TSLanguage *tree_sitter_lang();
@ -16,21 +17,47 @@ public:
Node(const TSNode &node, const std::string *source)
: node_(node), source_(source) {}
std::string get_type() const { return ts_node_type(node_); }
std::string get_type() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_type)");
}
return ts_node_type(node_);
}
std::pair<size_t, size_t> get_start_point() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_start_point)");
}
TSPoint point = ts_node_start_point(node_);
return {point.row, point.column};
}
std::pair<size_t, size_t> get_end_point() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_end_point)");
}
TSPoint point = ts_node_end_point(node_);
return {point.row, point.column};
}
std::string get_as_sexpression() const { return ts_node_string(node_); }
std::string get_as_sexpression() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_as_sexpression)");
}
return ts_node_string(node_);
}
std::string get_value() const { // from source
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_value)");
}
size_t start = ts_node_start_byte(node_);
size_t end = ts_node_end_byte(node_);
return source_->substr(start, end - start);
@ -38,47 +65,107 @@ public:
bool is_null() const { return ts_node_is_null(node_); }
bool is_named() const { return ts_node_is_named(node_); }
bool is_named() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (is_named)");
}
return ts_node_is_named(node_);
}
bool is_missing() const { return ts_node_is_missing(node_); }
bool is_missing() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (is_missing)");
}
return ts_node_is_missing(node_);
}
bool is_extra() const { // comments, etc.
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (is_extra)");
}
return ts_node_is_extra(node_);
}
bool has_error() const { return ts_node_has_error(node_); }
bool has_error() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (has_error)");
}
return ts_node_has_error(node_);
}
Node nth_child(size_t n) const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (nth_child)");
}
return Node(ts_node_child(node_, n), source_);
}
size_t child_count() const { return ts_node_child_count(node_); }
size_t child_count() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (child_count)");
}
return ts_node_child_count(node_);
}
Node nth_named_child(size_t n) const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (nth_named_child)");
}
return Node(ts_node_named_child(node_, n), source_);
}
size_t named_child_count() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (named_child_count)");
}
return ts_node_named_child_count(node_);
}
Node child_by_field_name(const std::string &name) const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (child_by_field_name)");
}
return Node(ts_node_child_by_field_name(node_, name.c_str(), name.size()),
source_);
}
Node previous_sibling() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (previous_sibling)");
}
return Node(ts_node_prev_sibling(node_), source_);
}
Node previous_named_sibling() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (previous_named_sibling)");
}
return Node(ts_node_prev_named_sibling(node_), source_);
}
Node next_sibling() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (next_sibling)");
}
return Node(ts_node_next_sibling(node_), source_);
}
Node next_named_sibling() const {
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (next_named_sibling)");
}
return Node(ts_node_next_named_sibling(node_), source_);
}

View file

@ -34,26 +34,28 @@ private:
class Type : public Node {
public:
Type(Node node, Identifier &&identifier, bool is_on_heap = false,
bool is_optional = false)
bool is_optional = false, bool is_result = false)
: Node(node), name_(std::move(identifier)), is_on_heap_(is_on_heap),
is_optional_(is_optional) {}
is_optional_(is_optional), is_result_(is_result) {}
Type(Node node, const Identifier &identifier, bool is_on_heap = false,
bool is_optional = false)
bool is_optional = false, bool is_result = false)
: Node(node), name_(identifier), is_on_heap_(is_on_heap),
is_optional_(is_optional) {}
is_optional_(is_optional), is_result_(is_result) {}
Type(Node node, Identifier &&identifier, std::vector<TypeProxy> &&parameters,
bool is_on_heap = false, bool is_optional = false)
bool is_on_heap = false, bool is_optional = false,
bool is_result = false)
: Node(node), name_(std::move(identifier)),
parameters_(std::move(parameters)), is_on_heap_(is_on_heap),
is_optional_(is_optional) {}
is_optional_(is_optional), is_result_(is_result) {}
Type(Node node, const Identifier &identifier,
std::vector<TypeProxy> &&parameters, bool is_on_heap = false,
bool is_optional = false)
bool is_optional = false, bool is_result = false)
: Node(node), name_(identifier), parameters_(std::move(parameters)),
is_on_heap_(is_on_heap), is_optional_(is_optional) {}
is_on_heap_(is_on_heap), is_optional_(is_optional),
is_result_(is_result) {}
std::string *get_name() { return name_.get(); }
@ -71,12 +73,15 @@ public:
bool is_optional() { return is_optional_; }
bool is_result() { return is_result_; }
private:
Identifier name_;
std::vector<TypeProxy> parameters_;
// or use allocator ??
bool is_on_heap_ = false;
bool is_optional_ = false;
bool is_result_ = false;
};
class TypeStorage {

View file

@ -1 +1,13 @@
#pragma once
#include <iostream>
#include <string>
namespace error_handling {
inline void handle_general_error(const std::string &message) {
std::cerr << "\x1b[1;31mGeneral Error:\x1b[0m " << message << ".\n";
exit(1);
}
} // namespace error_handling