mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2026-01-25 13:07:13 +00:00
highlighting improvements
This commit is contained in:
parent
195a26f9b7
commit
bf7fe1f821
9 changed files with 139 additions and 22 deletions
2
deps/tree-sitter-lang
vendored
2
deps/tree-sitter-lang
vendored
|
|
@ -1 +1 @@
|
||||||
Subproject commit 24015fd8df34dfb1c4c3b1d1622b261d01cb5a4c
|
Subproject commit b71b732797ce7fc203322b5ce2b1bbd9333bfb51
|
||||||
|
|
@ -15,11 +15,6 @@
|
||||||
</list>
|
</list>
|
||||||
<list name="keywords">
|
<list name="keywords">
|
||||||
<item>import</item>
|
<item>import</item>
|
||||||
<item>out</item>
|
|
||||||
<item>in</item>
|
|
||||||
<item>ref</item>
|
|
||||||
<item>or_out</item>
|
|
||||||
<item>or_in</item>
|
|
||||||
<item>let</item>
|
<item>let</item>
|
||||||
<item>var</item>
|
<item>var</item>
|
||||||
<item>lambda</item>
|
<item>lambda</item>
|
||||||
|
|
@ -27,6 +22,12 @@
|
||||||
<item>test</item>
|
<item>test</item>
|
||||||
<item>example</item>
|
<item>example</item>
|
||||||
</list>
|
</list>
|
||||||
|
<list name="references">
|
||||||
|
<item>out</item>
|
||||||
|
<item>in</item>
|
||||||
|
<item>ref</item>
|
||||||
|
<item>const</item>
|
||||||
|
</list>
|
||||||
<list name="constants">
|
<list name="constants">
|
||||||
<item>null</item>
|
<item>null</item>
|
||||||
<item>true</item>
|
<item>true</item>
|
||||||
|
|
@ -39,11 +40,12 @@
|
||||||
<DetectSpaces/>
|
<DetectSpaces/>
|
||||||
<keyword String="controlflow" attribute="Control Flow" context="#stay"/>
|
<keyword String="controlflow" attribute="Control Flow" context="#stay"/>
|
||||||
<keyword String="keywords" attribute="Keyword" context="#stay"/>
|
<keyword String="keywords" attribute="Keyword" context="#stay"/>
|
||||||
|
<keyword String="references" attribute="Reference" context="#stay"/>
|
||||||
<keyword String="constants" attribute="Constant" context="#stay"/>
|
<keyword String="constants" attribute="Constant" context="#stay"/>
|
||||||
|
|
||||||
<Detect2Chars char="/" char1="/" attribute="Comment" context="Comment"/>
|
<Detect2Chars char="/" char1="/" attribute="Comment" context="Comment"/>
|
||||||
<RegExpr String="^\: [^\n]*" attribute="Documentation" context="#stay"/>
|
<RegExpr String="^\: [^\n]*" attribute="Documentation" context="#stay"/>
|
||||||
<RegExpr String="^\@[^\n]*" attribute="Documentation" context="#stay"/>
|
<RegExpr String="^\@[a-z_][a-z0-9_]* " attribute="Annotation" context="AnnotationDocumentation"/>
|
||||||
|
|
||||||
<Detect2Chars char="(" char1=")" attribute="Constant" context="#stay"/>
|
<Detect2Chars char="(" char1=")" attribute="Constant" context="#stay"/>
|
||||||
|
|
||||||
|
|
@ -51,14 +53,16 @@
|
||||||
|
|
||||||
<RegExpr String="\@[a-z_][a-z0-9_]*(?![a-z0-9_])" attribute="Annotation" context="#stay"/>
|
<RegExpr String="\@[a-z_][a-z0-9_]*(?![a-z0-9_])" attribute="Annotation" context="#stay"/>
|
||||||
|
|
||||||
<RegExpr String="((\:\=)|(\=\:)|\%|\\|\$|(\:\:)|(\|?\-\>)|(<\-\|?)|(<\>)|^)(?![a\+\\\-\*/%\^\!\?\|&,<>=\.])" attribute="Keyword" context="#stay"/>
|
<RegExpr String="((\:\=)|(\=\:)|\%|\\|\$|(\:\:)|^)(?![a\+\\\-\*/%\^\!\?&,<>=\.])" attribute="Keyword" context="#stay"/>
|
||||||
|
|
||||||
|
<RegExpr String="((\-\>)|(<\-)|(<\>)|(\-\-))(?![a\+\\\-\*/%\^\!\?&,<>=\.])" attribute="Reference" context="#stay"/>
|
||||||
|
|
||||||
<RegExpr String="(\@|\:|(\?\?)|(\!\!)|(\=\>)|(\!\!\=\>))(?![a\+\\\-\*/%\^\!\?\|&,<>=\.])" attribute="Control Flow" context="#stay"/>
|
<RegExpr String="(\@|\:|(\?\?)|(\!\!)|(\=\>)|(\!\!\=\>))(?![a\+\\\-\*/%\^\!\?\|&,<>=\.])" attribute="Control Flow" context="#stay"/>
|
||||||
|
|
||||||
<RegExpr String="\b[0-9]+\.[0-9]+\b" attribute="Float" context="#stay"/>
|
<RegExpr String="\b[0-9]+\.[0-9]+\b" attribute="Float" context="#stay"/>
|
||||||
<RegExpr String="''([^\\\/]|(\\.))''" attribute="Character" context="#stay"/>
|
<RegExpr String="''([^\\\/]|(\\.))''" attribute="Character" context="#stay"/>
|
||||||
|
|
||||||
<RegExpr String="(\.+)|([\+\\\-\*/%\^\!\?\|&,<>=]+\.?\.?\.?)" attribute="Operator" context="#stay"/>
|
<RegExpr String="(\|)|((?<![a\+\\\-\*/%\^\!\?\|&,<>=\.])((\.+)|([\+\\\-\*/%\^\!\?\|&,<>=]+\.?\.?\.?)))" attribute="Operator" context="#stay"/>
|
||||||
|
|
||||||
<RegExpr String="([a-z_][a-z0-9_]*\.)*(([a-z_][a-z0-9_]*)|_)(?![a-z0-9_])" attribute="Name" context="#stay"/>
|
<RegExpr String="([a-z_][a-z0-9_]*\.)*(([a-z_][a-z0-9_]*)|_)(?![a-z0-9_])" attribute="Name" context="#stay"/>
|
||||||
|
|
||||||
|
|
@ -91,7 +95,6 @@
|
||||||
</context>
|
</context>
|
||||||
|
|
||||||
<context attribute="Comment" lineEndContext="#pop" name="Comment">
|
<context attribute="Comment" lineEndContext="#pop" name="Comment">
|
||||||
<LineContinue attribute="Comment" context="#stay"/>
|
|
||||||
<DetectSpaces />
|
<DetectSpaces />
|
||||||
<IncludeRules context="##Comments" />
|
<IncludeRules context="##Comments" />
|
||||||
<DetectIdentifier />
|
<DetectIdentifier />
|
||||||
|
|
@ -103,11 +106,15 @@
|
||||||
<IncludeRules context="##Comments" />
|
<IncludeRules context="##Comments" />
|
||||||
<DetectIdentifier />
|
<DetectIdentifier />
|
||||||
</context>
|
</context>
|
||||||
|
|
||||||
|
<context attribute="Documentation" lineEndContext="#pop" name="AnnotationDocumentation">
|
||||||
|
</context>
|
||||||
</contexts>
|
</contexts>
|
||||||
<itemDatas>
|
<itemDatas>
|
||||||
<itemData name="Normal Text" defStyleNum="dsNormal"/>
|
<itemData name="Normal Text" defStyleNum="dsNormal"/>
|
||||||
<itemData name="Control Flow" defStyleNum="dsControlFlow" spellChecking="false"/>
|
<itemData name="Control Flow" defStyleNum="dsControlFlow" spellChecking="false"/>
|
||||||
<itemData name="Keyword" defStyleNum="dsKeyword" spellChecking="false"/>
|
<itemData name="Keyword" defStyleNum="dsKeyword" spellChecking="false"/>
|
||||||
|
<itemData name="Reference" defStyleNum="dsOperator" spellChecking="false"/>
|
||||||
<itemData name="Type" defStyleNum="dsDataType" spellChecking="false"/>
|
<itemData name="Type" defStyleNum="dsDataType" spellChecking="false"/>
|
||||||
<itemData name="ArgumentTypeOrName" defStyleNum="dsBuiltIn" spellChecking="false"/>
|
<itemData name="ArgumentTypeOrName" defStyleNum="dsBuiltIn" spellChecking="false"/>
|
||||||
<itemData name="Constructor" defStyleNum="dsConstant" spellChecking="false"/>
|
<itemData name="Constructor" defStyleNum="dsConstant" spellChecking="false"/>
|
||||||
|
|
@ -122,7 +129,7 @@
|
||||||
<itemData name="String" defStyleNum="dsString"/>
|
<itemData name="String" defStyleNum="dsString"/>
|
||||||
<itemData name="Escape" defStyleNum="dsSpecialChar"/>
|
<itemData name="Escape" defStyleNum="dsSpecialChar"/>
|
||||||
<itemData name="Character" defStyleNum="dsChar"/>
|
<itemData name="Character" defStyleNum="dsChar"/>
|
||||||
<itemData name="Operator" defStyleNum="dsFunction"/>
|
<itemData name="Operator" defStyleNum="dsNormal"/>
|
||||||
<itemData name="Symbol" defStyleNum="dsFunction"/>
|
<itemData name="Symbol" defStyleNum="dsFunction"/>
|
||||||
<itemData name="Error" defStyleNum="dsError"/>
|
<itemData name="Error" defStyleNum="dsError"/>
|
||||||
</itemDatas>
|
</itemDatas>
|
||||||
|
|
|
||||||
46
include/expression_type_check.hpp
Normal file
46
include/expression_type_check.hpp
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "expression_nodes.hpp"
|
||||||
|
|
||||||
|
// IN PROGRESS
|
||||||
|
|
||||||
|
namespace type_check {
|
||||||
|
|
||||||
|
void type_check_expression(const nodes::Expression &expression);
|
||||||
|
|
||||||
|
// --- flow control
|
||||||
|
|
||||||
|
void type_check_case(const nodes::Match::Case &expression);
|
||||||
|
|
||||||
|
void type_check_match(const nodes::Match &expression);
|
||||||
|
|
||||||
|
void type_check_condition(const nodes::Condition &expression);
|
||||||
|
|
||||||
|
void type_check_loop(const nodes::Loop &expression);
|
||||||
|
|
||||||
|
// --- containers
|
||||||
|
|
||||||
|
void type_check_container(const nodes::Container &expression);
|
||||||
|
|
||||||
|
// --- modifiers
|
||||||
|
|
||||||
|
void type_check_return(const nodes::Return &expression);
|
||||||
|
|
||||||
|
void type_check_name_definition(const nodes::NameDefinition &expression);
|
||||||
|
|
||||||
|
void type_check_access(const nodes::Access &expression);
|
||||||
|
|
||||||
|
void type_check_loop_control(const nodes::LoopControl &expression);
|
||||||
|
|
||||||
|
void type_check_modifier_expression(
|
||||||
|
const nodes::ModifierExpression &expression);
|
||||||
|
|
||||||
|
// --- other
|
||||||
|
|
||||||
|
void type_check_name_expression(const nodes::NameExpression &expression);
|
||||||
|
|
||||||
|
void type_check_constructor(const nodes::Constructor &expression);
|
||||||
|
|
||||||
|
void type_check_lambda(const nodes::Lambda &expression);
|
||||||
|
|
||||||
|
} // namespace type_check
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "basic_printers.hpp"
|
||||||
#include "statement_nodes.hpp"
|
#include "statement_nodes.hpp"
|
||||||
#include "tree_sitter_wrapper.hpp"
|
#include "tree_sitter_wrapper.hpp"
|
||||||
|
|
||||||
|
|
@ -19,12 +20,20 @@ public:
|
||||||
|
|
||||||
bool insert(const std::string &path, nodes::Statement &&statement);
|
bool insert(const std::string &path, nodes::Statement &&statement);
|
||||||
|
|
||||||
bool insert(const std::string &path, const nodes::Statement &statement);
|
// bool insert(const std::string &path, const nodes::Statement &statement);
|
||||||
|
|
||||||
|
nodes::CombineResult insert_combine(const std::string &path,
|
||||||
|
nodes::Statement &&statement);
|
||||||
|
|
||||||
std::optional<nodes::Statement *> find(const std::string &path);
|
std::optional<nodes::Statement *> find(const std::string &path);
|
||||||
|
|
||||||
std::optional<const nodes::Statement *> find(const std::string &path) const;
|
std::optional<const nodes::Statement *> find(const std::string &path) const;
|
||||||
|
|
||||||
|
void print(printers::Printer &printer) const;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
void add_statement_children_to_tree();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Node {
|
struct Node {
|
||||||
public:
|
public:
|
||||||
|
|
@ -76,6 +85,20 @@ private:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print(const std::vector<Node> &nodes,
|
||||||
|
printers::Printer &printer) const {
|
||||||
|
printer.print("Node - has statement: ");
|
||||||
|
printer.print(statement_.has_value() ? "true" : "false");
|
||||||
|
printer.indent();
|
||||||
|
for (auto &iter : children_) {
|
||||||
|
printer.new_indent_line();
|
||||||
|
printer.print(std::to_string(iter.first) + " -> ");
|
||||||
|
nodes[iter.second].print(nodes, printer);
|
||||||
|
}
|
||||||
|
printer.deindent();
|
||||||
|
printer.new_indent_line();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<size_t, size_t> children_;
|
std::unordered_map<size_t, size_t> children_;
|
||||||
std::optional<nodes::Statement> statement_;
|
std::optional<nodes::Statement> statement_;
|
||||||
|
|
|
||||||
|
|
@ -473,7 +473,7 @@ public:
|
||||||
Statement &operator=(const Statement &) = default;
|
Statement &operator=(const Statement &) = default;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
Statement(T &&statement) : expression_(std::forward<T>(statement)) {}
|
explicit Statement(T &&statement) : expression_(std::forward<T>(statement)) {}
|
||||||
|
|
||||||
template <typename T> std::optional<T *> get() {
|
template <typename T> std::optional<T *> get() {
|
||||||
if (std::holds_alternative<T>(expression_)) {
|
if (std::holds_alternative<T>(expression_)) {
|
||||||
|
|
|
||||||
3
src/expression_type_check.cpp
Normal file
3
src/expression_type_check.cpp
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#include "expression_type_check.hpp"
|
||||||
|
|
||||||
|
namespace type_check {} // namespace type_check
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include "name_tree.hpp"
|
#include "name_tree.hpp"
|
||||||
|
#include "error_handling.hpp"
|
||||||
|
#include "statement_nodes.hpp"
|
||||||
|
|
||||||
namespace names {
|
namespace names {
|
||||||
|
|
||||||
|
|
@ -17,18 +19,18 @@ bool NameTree::insert(const std::string &path, nodes::Statement &&statement) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NameTree::insert(const std::string &path,
|
nodes::CombineResult NameTree::insert_combine(const std::string &path,
|
||||||
const nodes::Statement &statement) {
|
nodes::Statement &&statement) {
|
||||||
auto name_ids = slice_path_to_name_ids(path);
|
auto name_ids = slice_path_to_name_ids(path);
|
||||||
|
|
||||||
size_t node_id = add_node(get_root(), name_ids);
|
size_t node_id = add_node(get_root(), name_ids);
|
||||||
|
|
||||||
if (nodes_[node_id].get_statement().has_value()) {
|
if (!nodes_[node_id].get_statement().has_value()) {
|
||||||
return false;
|
nodes_[node_id].set_statement(std::move(statement));
|
||||||
|
return nodes::CombineResult::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes_[node_id].set_statement(statement);
|
return nodes_[node_id].get_statement().value()->combine(std::move(statement));
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<nodes::Statement *> NameTree::find(const std::string &path) {
|
std::optional<nodes::Statement *> NameTree::find(const std::string &path) {
|
||||||
|
|
@ -64,6 +66,41 @@ NameTree::find(const std::string &path) const {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NameTree::print(printers::Printer &printer) const {
|
||||||
|
nodes_[get_root()].print(nodes_, printer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
void NameTree::add_statement_children_to_tree() {
|
||||||
|
for (auto &node : nodes_) {
|
||||||
|
if (!node.get_statement().has_value()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (node.get_statement().value()->get_any()->index()) {
|
||||||
|
case 0: // Import
|
||||||
|
// TODO: link imported symbols, if named import, otherwise link imported
|
||||||
|
// symbols to root
|
||||||
|
break;
|
||||||
|
case 1: // FunctionDefinition
|
||||||
|
// TODO: link to result type
|
||||||
|
break;
|
||||||
|
case 2: // TypeDefinition
|
||||||
|
// TODO: link methods + link children to types + connect to typeclasses
|
||||||
|
break;
|
||||||
|
case 3: // TypeclassDefinition
|
||||||
|
// TODO: link methods + connect typeclasses
|
||||||
|
break;
|
||||||
|
case 4: // EmptyLines
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error_handling::handle_general_error(
|
||||||
|
"Unexpected statement type in name tree");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --- private
|
// --- private
|
||||||
|
|
||||||
size_t NameTree::add_node(size_t current_node,
|
size_t NameTree::add_node(size_t current_node,
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ nodes::Statement build_statement(parser::ParseTree::Node parser_node,
|
||||||
->get();
|
->get();
|
||||||
break;
|
break;
|
||||||
case tokens::Type::EMPTY_LINES:
|
case tokens::Type::EMPTY_LINES:
|
||||||
statement = build_empty_lines(parser_node);
|
statement = nodes::Statement(build_empty_lines(parser_node));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error_handling::handle_parsing_error("Unexprected statement node type",
|
error_handling::handle_parsing_error("Unexprected statement node type",
|
||||||
|
|
@ -97,7 +97,8 @@ nodes::Statement build_statement(parser::ParseTree::Node parser_node,
|
||||||
|
|
||||||
if (statement_name.has_value()) {
|
if (statement_name.has_value()) {
|
||||||
// TODO: combine statements with same name
|
// TODO: combine statements with same name
|
||||||
if (!name_tree.insert(statement_name.value(), statement.value())) {
|
auto statement_copy = statement.value();
|
||||||
|
if (!name_tree.insert(statement_name.value(), std::move(statement_copy))) {
|
||||||
error_handling::handle_parsing_error(
|
error_handling::handle_parsing_error(
|
||||||
"Two or more statements in file have the same name", parser_node);
|
"Two or more statements in file have the same name", parser_node);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -216,7 +216,7 @@ parse_number : Unit! = {
|
||||||
}
|
}
|
||||||
|
|
||||||
: example of or_in and or_out usage for template operators (tuple input and tuple output)
|
: example of or_in and or_out usage for template operators (tuple input and tuple output)
|
||||||
( & ) |-> 'a |-> 'b <-| 'x <-| 'y = ('a := 'x) && ('b := 'y);
|
( & ) --|-> 'a --|-> 'b <-|-- 'x <-|-- 'y = ('a := 'x) && ('b := 'y);
|
||||||
|
|
||||||
: function, that return result ('!' not used on calls)
|
: function, that return result ('!' not used on calls)
|
||||||
: useful when tuples returned
|
: useful when tuples returned
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue