changes for new grammar, fixes

This commit is contained in:
ProgramSnail 2023-06-03 19:01:03 +03:00
parent 91f9affadc
commit 3106a64949
35 changed files with 605 additions and 550 deletions

View file

@ -62,7 +62,9 @@ void BuildVisitor::Visit(Namespace* node) {
node->modifier = utils::ClassInternalsModifier::Static;
}
node->type = parse_node.ChildByFieldName("type").GetValue();
current_node_ = parse_node.ChildByFieldName("type");
node->type = current_node_.GetValue();
node->is_type_namespace = (current_node_.PreviousSibling().GetValue() == "\\");
current_node_ = parse_node.ChildByFieldName("scope");
Visit(&node->scope);
@ -130,7 +132,7 @@ void BuildVisitor::Visit(AliasDefinitionStatement* node) {
}
current_node_ = parse_node.ChildByFieldName("value");
node->value = std::make_unique<TypeExpression>();
node->value = std::make_unique<WrappedTypeExpression>();
Visit(node->value.get());
current_node_ = parse_node;
@ -961,9 +963,9 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
node->prefix = std::make_unique<SubExpressionToken>();
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
++excluded_child_count;
} else if (current_node_type == parser::tokens::TypeExpression) {
node->prefix = std::make_unique<TypeExpression>();
Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get());
} else if (current_node_type == parser::tokens::WrappedTypeExpression) {
node->prefix = std::make_unique<WrappedTypeExpression>();
Visit(std::get<std::unique_ptr<WrappedTypeExpression>>(node->prefix.value()).get());
++excluded_child_count;
} else {
// no prefix
@ -1282,7 +1284,8 @@ void BuildVisitor::Visit(FunctionType* node) {
for (size_t i = 0; i < types_count; ++i) {
current_node_ = parse_node.NthNamedChild(i);
Visit(node->types[i]);
node->types[i] = std::make_unique<ExtendedScopedAnyType>();
Visit(node->types[i].get());
}
current_node_ = parse_node;
@ -1340,19 +1343,28 @@ void BuildVisitor::Visit(VariantType* node) {
}
node->constructors.resize(child_count - excluded_child_count);
for (size_t i = 0; i < node->constructors.size(); ++i) {
current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
if (child_count > excluded_child_count) {
node->constructors.reserve(child_count - excluded_child_count);
for (size_t i = 0; i < child_count - excluded_child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
std::string current_node_type = current_node_.GetType();
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::Constructor) {
node->constructors[i] = current_node_.GetValue();
} else if (current_node_type == parser::tokens::TupleType) {
node->constructors[i] = std::make_unique<TupleType>();
Visit(std::get<std::unique_ptr<TupleType>>(node->constructors[i]).get());
} else {
// error
if (current_node_type == parser::tokens::Constructor) {
std::string constructor_name = current_node_.GetValue();
constructor_name = constructor_name.substr(1, constructor_name.size() - 1);
node->constructors.push_back({constructor_name, std::nullopt});
} else if (current_node_type == parser::tokens::TupleType) {
if (!node->constructors.empty() && !node->constructors.back().second.has_value()) {
node->constructors.back().second = std::make_unique<TupleType>();
Visit(node->constructors.back().second.value().get());
} else {
node->constructors.push_back({std::nullopt, std::make_unique<TupleType>()});
Visit(node->constructors.back().second.value().get());
}
} else {
// error
}
}
}
@ -1389,11 +1401,11 @@ void BuildVisitor::Visit(TypeExpression* node) {
node->is_optional = true;
} else {
if (suffix_node1.GetValue() == "_" && suffix_node2.GetValue() == "!") {
if (suffix_node1.GetValue() == "`" && suffix_node2.GetValue() == "?") {
node->array_size = 0;
node->is_optional = true;
} else if (suffix_node1.GetValue() == "_") {
} else if (suffix_node1.GetValue() == "`") {
current_node_ = suffix_node2;
NumberLiteral literal;
@ -1407,9 +1419,9 @@ void BuildVisitor::Visit(TypeExpression* node) {
}
}
} else {
if (suffix_node1.GetValue() == "_") {
if (suffix_node1.GetValue() == "`") {
node->array_size = 0;
} else if (suffix_node1.GetValue() == "!") {
} else if (suffix_node1.GetValue() == "?") {
node->is_optional = true;
} else {
error_handling::HandleInternalError("Undefined suffix (1 element)",
@ -1438,9 +1450,9 @@ void BuildVisitor::Visit(AnyType& node) {
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::TypeExpression) {
node = std::make_unique<TypeExpression>();
Visit(std::get<std::unique_ptr<TypeExpression>>(node).get());
if (current_node_type == parser::tokens::WrappedTypeExpression) {
node = std::make_unique<WrappedTypeExpression>();
Visit(std::get<std::unique_ptr<WrappedTypeExpression>>(node).get());
} else if (current_node_type == parser::tokens::TupleType) {
node = std::make_unique<TupleType>();
Visit(std::get<std::unique_ptr<TupleType>>(node).get());
@ -1550,18 +1562,47 @@ void BuildVisitor::Visit(NumberLiteral* node) {
node->value = std::stoll(current_node_.GetValue());
}
void BuildVisitor::Visit(StringLiteral* node) { // TODO: special symbols, etc.
void BuildVisitor::Visit(StringLiteral* node) { // TODO: other special symbols, etc.
SetPosition(node->base, current_node_);
std::string literal = current_node_.GetValue();
node->value = literal.substr(1, literal.size() - 2);
literal = literal.substr(1, literal.size() - 2);
node->value.reserve(literal.size());
bool is_escape_symbol = false;
for (size_t i = 0; i < literal.size(); ++i) {
if (literal[i] == '\\' && !is_escape_symbol) {
is_escape_symbol = true;
} else {
if (is_escape_symbol) {
auto maybe_escape_symbol = utils::ToEscapeSymbol(literal[i]);
if (maybe_escape_symbol.has_value()) {
node->value += maybe_escape_symbol.value();
} else {
node->value += literal[i];
}
} else {
node->value += literal[i];
}
}
}
}
void BuildVisitor::Visit(CharLiteral* node) {
SetPosition(node->base, current_node_);
std::string literal = current_node_.GetValue();
node->value = literal.substr(1, literal.size() - 2).back(); // TODO: special symbols, etc.
literal = literal.substr(2, literal.size() - 2); // TODO: other special symbols, etc.
if (literal[0] == '\\') {
auto maybe_escape_symbol = utils::ToEscapeSymbol(literal.back());
if (maybe_escape_symbol.has_value()) {
node->value = maybe_escape_symbol.value();
} else {
node->value = literal.back();
}
} else {
node->value = literal.back();
}
}
void BuildVisitor::Visit(UnitLiteral* node) {