mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-26 00:38:45 +00:00
printing fixes
This commit is contained in:
parent
0bb72e0b10
commit
469cb3581f
23 changed files with 318 additions and 151 deletions
|
|
@ -47,4 +47,8 @@ nodes::Identifier build_operator(parser::ParseTree::Node parser_node);
|
|||
|
||||
nodes::Identifier build_placeholder(parser::ParseTree::Node parser_node);
|
||||
|
||||
// --- empty lines
|
||||
|
||||
nodes::EmptyLines build_empty_lines(parser::ParseTree::Node parser_node);
|
||||
|
||||
} // namespace builders
|
||||
|
|
|
|||
|
|
@ -53,15 +53,9 @@ public:
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::variant<double, long long, std::string, char, bool, unit, null> *
|
||||
get_any() {
|
||||
return &value_;
|
||||
}
|
||||
auto get_any() { return &value_; }
|
||||
|
||||
const std::variant<double, long long, std::string, char, bool, unit, null> *
|
||||
get_any() const {
|
||||
return &value_;
|
||||
}
|
||||
auto get_any() const { return &value_; }
|
||||
|
||||
private:
|
||||
std::variant<double, long long, std::string, char, bool, unit, null> value_;
|
||||
|
|
@ -86,7 +80,7 @@ public:
|
|||
Identifier(Node node, IdentifierType type, const std::string &value)
|
||||
: Node(node), type_(type), value_(value) {}
|
||||
|
||||
IdentifierType get_type() { return type_; }
|
||||
IdentifierType get_type() const { return type_; }
|
||||
|
||||
std::string *get() { return &value_; }
|
||||
|
||||
|
|
@ -97,4 +91,14 @@ private:
|
|||
std::string value_;
|
||||
};
|
||||
|
||||
class EmptyLines : public Node {
|
||||
public:
|
||||
EmptyLines(Node node, size_t size) : Node(node), size_(size) {}
|
||||
|
||||
size_t size() const { return size_; }
|
||||
|
||||
private:
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
} // namespace nodes
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ public:
|
|||
bool print_words_instead_of_symbols)
|
||||
: output_(output), tab_width_(tab_width), width_limit_(width_limit),
|
||||
print_words_instead_of_symbols_(print_words_instead_of_symbols),
|
||||
current_position_(0), current_indentation_level_(0) {}
|
||||
current_position_(0), indentation_level_(0) {}
|
||||
|
||||
void print_converted(const std::string &value) {
|
||||
for (auto &ch : value) {
|
||||
|
|
@ -34,7 +34,7 @@ public:
|
|||
|
||||
void new_indent_line() {
|
||||
end_line();
|
||||
print_spaces(current_indentation_level_ * tab_width_);
|
||||
print_spaces(indentation_level_);
|
||||
}
|
||||
|
||||
void new_line(size_t indentation) {
|
||||
|
|
@ -42,9 +42,9 @@ public:
|
|||
print_spaces(indentation);
|
||||
}
|
||||
|
||||
void indent() { current_indentation_level_ += tab_width_; }
|
||||
void indent() { indentation_level_ += tab_width_; }
|
||||
|
||||
void deindent() { current_indentation_level_ -= tab_width_; }
|
||||
void deindent() { indentation_level_ -= tab_width_; }
|
||||
|
||||
void tab() { print_spaces(tab_width_); }
|
||||
|
||||
|
|
@ -66,12 +66,10 @@ public:
|
|||
|
||||
size_t get_current_position() const { return current_position_; }
|
||||
|
||||
size_t get_current_indentation_level() const {
|
||||
return current_indentation_level_;
|
||||
}
|
||||
size_t get_indentation_level() const { return indentation_level_; }
|
||||
|
||||
size_t set_current_indentation_level(size_t indentation_level) {
|
||||
current_indentation_level_ = indentation_level;
|
||||
void set_indentation_level(size_t indentation_level) {
|
||||
indentation_level_ = indentation_level;
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -117,7 +115,7 @@ private:
|
|||
bool print_words_instead_of_symbols_ = false;
|
||||
|
||||
size_t current_position_ = 0;
|
||||
size_t current_indentation_level_ = 0;
|
||||
size_t indentation_level_ = 0;
|
||||
};
|
||||
|
||||
void print_literal(const nodes::Literal &literal, Printer &printer);
|
||||
|
|
@ -126,4 +124,6 @@ void print_identifier(const nodes::Identifier &identifier, Printer &printer);
|
|||
|
||||
void print_annotation(const std::string &annotation, Printer &printer);
|
||||
|
||||
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer);
|
||||
|
||||
} // namespace printers
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace nodes {
|
||||
|
||||
|
|
@ -16,8 +17,9 @@ public:
|
|||
SymbolDocs(const std::string &description) : description_(description) {}
|
||||
|
||||
bool add_annotation_info(const std::string &annotation, std::string &&info) {
|
||||
if (annotations_info_.count(annotation) == 0) {
|
||||
annotations_info_[annotation] = std::move(info);
|
||||
if (annotations_info_ids_.count(annotation) == 0) {
|
||||
annotations_info_ids_[annotation] = annotations_info_.size();
|
||||
annotations_info_.emplace_back(annotation, std::move(info));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -25,13 +27,16 @@ public:
|
|||
|
||||
bool add_annotation_info(const std::string &annotation,
|
||||
const std::string &info) {
|
||||
if (annotations_info_.count(annotation) == 0) {
|
||||
annotations_info_[annotation] = info;
|
||||
if (annotations_info_ids_.count(annotation) == 0) {
|
||||
annotations_info_ids_[annotation] = annotations_info_.size();
|
||||
annotations_info_.emplace_back(annotation, std::move(info));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
std::optional<std::string *> get_description() {
|
||||
if (description_.has_value()) {
|
||||
return &description_.value();
|
||||
|
|
@ -46,27 +51,52 @@ public:
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
std::optional<std::string *>
|
||||
get_annotation_info(const std::string &annotation) {
|
||||
auto info_iterator = annotations_info_.find(annotation);
|
||||
if (info_iterator != annotations_info_.end()) {
|
||||
return &info_iterator->second;
|
||||
auto info_iterator = annotations_info_ids_.find(annotation);
|
||||
if (info_iterator != annotations_info_ids_.end()) {
|
||||
return &annotations_info_[info_iterator->second].second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<const std::string *>
|
||||
get_annotation_info(const std::string &annotation) const {
|
||||
auto info_iterator = annotations_info_.find(annotation);
|
||||
if (info_iterator != annotations_info_.end()) {
|
||||
return &info_iterator->second;
|
||||
auto info_iterator = annotations_info_ids_.find(annotation);
|
||||
if (info_iterator != annotations_info_ids_.end()) {
|
||||
return &annotations_info_[info_iterator->second].second;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
size_t get_annotations_info_size() const { return annotations_info_.size(); }
|
||||
|
||||
std::string *get_annotation(size_t id) {
|
||||
return &annotations_info_[id].first;
|
||||
}
|
||||
|
||||
const std::string *get_annotation(size_t id) const {
|
||||
return &annotations_info_[id].first;
|
||||
}
|
||||
|
||||
std::string *get_annotation_info(size_t id) {
|
||||
return &annotations_info_[id].second;
|
||||
}
|
||||
|
||||
const std::string *get_annotation_info(size_t id) const {
|
||||
return &annotations_info_[id].second;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private:
|
||||
std::optional<std::string> description_;
|
||||
std::unordered_map<std::string, std::string> annotations_info_;
|
||||
std::vector<std::pair<std::string, std::string>> annotations_info_;
|
||||
std::unordered_map<std::string, size_t> annotations_info_ids_;
|
||||
};
|
||||
|
||||
} // namespace nodes
|
||||
|
|
|
|||
|
|
@ -167,13 +167,13 @@ public:
|
|||
: Node(node), type_(LOOP), expression_(expression) {}
|
||||
|
||||
// WHILE
|
||||
Loop(Node node, ExpressionProxy expression, ExpressionProxy condition)
|
||||
Loop(Node node, ExpressionProxy condition, ExpressionProxy expression)
|
||||
: Node(node), type_(WHILE), expression_(expression),
|
||||
condition_(condition) {}
|
||||
|
||||
// FOR
|
||||
Loop(Node node, ExpressionProxy expression, ExpressionProxy variable,
|
||||
ExpressionProxy interval)
|
||||
Loop(Node node, ExpressionProxy variable, ExpressionProxy interval,
|
||||
ExpressionProxy expression)
|
||||
: Node(node), type_(FOR), expression_(expression), variable_(variable),
|
||||
interval_(interval) {}
|
||||
|
||||
|
|
@ -528,19 +528,9 @@ public:
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::variant<Match, Condition, Loop, Container, Return, NameDefinition,
|
||||
Access, LoopControl, ModifierExpression, NameExpression,
|
||||
Constructor, Lambda, Literal> *
|
||||
get_any() {
|
||||
return &expression_;
|
||||
}
|
||||
auto get_any() { return &expression_; }
|
||||
|
||||
const std::variant<Match, Condition, Loop, Container, Return, NameDefinition,
|
||||
Access, LoopControl, ModifierExpression, NameExpression,
|
||||
Constructor, Lambda, Literal> *
|
||||
get_any() const {
|
||||
return &expression_;
|
||||
}
|
||||
auto get_any() const { return &expression_; }
|
||||
|
||||
bool is_scoped() const { return is_scoped_; }
|
||||
|
||||
|
|
@ -570,7 +560,10 @@ private:
|
|||
NameExpression, Constructor, Lambda,
|
||||
|
||||
// --- literal
|
||||
Literal
|
||||
Literal,
|
||||
|
||||
// --- empty lines
|
||||
EmptyLines
|
||||
|
||||
>
|
||||
expression_;
|
||||
|
|
|
|||
|
|
@ -75,13 +75,16 @@ public:
|
|||
std::vector<TypeProxy> &&types,
|
||||
std::vector<bool> &&optional_arguments,
|
||||
std::vector<bool> &&result_arguments,
|
||||
bool is_annotations_same_to_names,
|
||||
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)),
|
||||
optional_arguments_(optional_arguments),
|
||||
result_arguments_(result_arguments), expression_(expression) {}
|
||||
result_arguments_(result_arguments),
|
||||
is_annotations_same_to_names_(is_annotations_same_to_names),
|
||||
expression_(expression) {}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -91,7 +94,7 @@ public:
|
|||
|
||||
//
|
||||
|
||||
size_t get_constraints_size() const { return arguments_.size(); }
|
||||
size_t get_constraints_size() const { return constraints_.size(); }
|
||||
|
||||
Constraint *get_constraint(size_t id) { return &constraints_.at(id); }
|
||||
|
||||
|
|
@ -159,6 +162,12 @@ public:
|
|||
|
||||
//
|
||||
|
||||
bool is_annotations_same_to_names() const {
|
||||
return is_annotations_same_to_names_;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
std::optional<Expression *> get_expression() {
|
||||
if (expression_.has_value()) {
|
||||
return expression_.value().get();
|
||||
|
|
@ -184,6 +193,7 @@ private:
|
|||
std::vector<TypeProxy> types_;
|
||||
std::vector<bool> optional_arguments_;
|
||||
std::vector<bool> result_arguments_;
|
||||
bool is_annotations_same_to_names_;
|
||||
std::optional<ExpressionProxy> expression_;
|
||||
}; // refactor ??
|
||||
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@
|
|||
|
||||
namespace printers {
|
||||
|
||||
void print_source_file(Printer &printer); // TODO
|
||||
// void print_source_file(Printer &printer); // TODO
|
||||
|
||||
void print_statement(Printer &printer); // TODO
|
||||
// void print_statement(Printer &printer); // TODO
|
||||
|
||||
void print_import(const nodes::Import &statement, Printer &printer);
|
||||
|
||||
|
|
|
|||
|
|
@ -62,6 +62,8 @@ enum class Type {
|
|||
DEFINITION_INFO,
|
||||
ANNOTATION_INFO,
|
||||
|
||||
EMPTY_LINES,
|
||||
|
||||
// --- tokens
|
||||
|
||||
PLACEHOLDER,
|
||||
|
|
@ -146,6 +148,8 @@ const static std::string TYPE = "type";
|
|||
const static std::string DEFINITION_INFO = "definition_info";
|
||||
const static std::string ANNOTATION_INFO = "annotation_info";
|
||||
|
||||
const static std::string EMPTY_LINES = "empty_lines";
|
||||
|
||||
// --- tokens
|
||||
|
||||
const static std::string PLACEHOLDER = "placeholder";
|
||||
|
|
@ -228,6 +232,8 @@ inline Type string_to_type(const std::string &str) {
|
|||
return Type::DEFINITION_INFO;
|
||||
} else if (str == ANNOTATION_INFO) {
|
||||
return Type::ANNOTATION_INFO;
|
||||
} else if (str == EMPTY_LINES) {
|
||||
return Type::EMPTY_LINES;
|
||||
} else if (str == PLACEHOLDER) {
|
||||
return Type::PLACEHOLDER;
|
||||
} else if (str == SIMPLE_NAME_IDENTIFIER) {
|
||||
|
|
|
|||
|
|
@ -63,6 +63,16 @@ public:
|
|||
return source_->substr(start, end - start);
|
||||
}
|
||||
|
||||
size_t get_value_length() const { // from source
|
||||
if (is_null()) {
|
||||
error_handling::handle_general_error(
|
||||
"Null parsing node method called (get_value_length)");
|
||||
}
|
||||
size_t start = ts_node_start_byte(node_);
|
||||
size_t end = ts_node_end_byte(node_);
|
||||
return end - start;
|
||||
}
|
||||
|
||||
bool is_null() const { return ts_node_is_null(node_); }
|
||||
|
||||
bool is_named() const {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue