Compare commits
No commits in common. "5ccd6dd2ab79cab09cff1942a4cf077ef2835f41" and "324a78ae1e0832d1662ba20811fcaf5a0c1508ed" have entirely different histories.
5ccd6dd2ab
...
324a78ae1e
|
@ -15,42 +15,6 @@ source "$HELPERS_DIR/print-routines.sh"
|
|||
# Build Tree Sitter
|
||||
# =============================================================================
|
||||
|
||||
build_tree_sitter_language() {
|
||||
LANG_DIR=$1
|
||||
LANG_NAME=$2
|
||||
|
||||
PARSER_SRC="${CUSTOM_ROOT}/lang/$LANG_DIR/parser.c"
|
||||
PARSER_OUT="$TEMP_OUT_DIR/${LANG_DIR}_parser.o"
|
||||
|
||||
SCANNER_SRC="${CUSTOM_ROOT}/lang/$LANG_DIR/scanner.cc"
|
||||
SCANNER_OUT="$TEMP_OUT_DIR/${LANG_DIR}_scanner.o"
|
||||
|
||||
print_step "Building tree-sitter $LANG_NAME Language Lib ($LANG_DIR)"
|
||||
BUILT_ANYTHING=0
|
||||
|
||||
if [ -f $PARSER_SRC ]; then
|
||||
echo " Building Parser..."
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "$PARSER_SRC" -o "$PARSER_OUT"
|
||||
if [ $? == 0 ]; then
|
||||
BUILT_ANYTHING=1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -f $SCANNER_SRC ]; then
|
||||
echo " Building Scanner..."
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "$SCANNER_SRC" -o "$SCANNER_OUT"
|
||||
if [ $? == 0 ]; then
|
||||
BUILT_ANYTHING=1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ $BUILT_ANYTHING == 1 ]; then
|
||||
print_success "Complete"
|
||||
else
|
||||
print_warning "Failed to build anything."
|
||||
fi
|
||||
}
|
||||
|
||||
build_tree_sitter() {
|
||||
BIN_NAME="custom_4coder"
|
||||
|
||||
|
@ -68,17 +32,23 @@ build_tree_sitter() {
|
|||
|
||||
TEMP_OUT_DIR=$BUILD_TEMP_DIR/tree-sitter
|
||||
mkdir -p $TEMP_OUT_DIR
|
||||
rm $TEMP_OUT_DIR/*.o
|
||||
rm "$BUILD_TEMP_DIR/tree-sitter.a"
|
||||
|
||||
# Build tree-sitter.lib/.a
|
||||
print_step "Building tree-sitter lib"
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "$CUSTOM_ROOT/lib/src/lib.c" -o $TEMP_OUT_DIR/tree-sitter.o
|
||||
print_success "Complete"
|
||||
|
||||
build_tree_sitter_language "cpp" "C++"
|
||||
build_tree_sitter_language "jai" "Jai"
|
||||
build_tree_sitter_language "bash" "Bash"
|
||||
# Lang: C++ (This needs to be two calls to clang so that you can specify the obj file names)
|
||||
print_step "Building tree-sitter C++ Language Lib"
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "${CUSTOM_ROOT}/lang/cpp/parser.c" -o $TEMP_OUT_DIR/cpp_parser.o
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "${CUSTOM_ROOT}/lang/cpp/scanner.cc" -o $TEMP_OUT_DIR/cpp_scanner.o
|
||||
print_success "Complete"
|
||||
|
||||
# Lang: Jai
|
||||
print_step "Building tree-sitter Jai Language Lib"
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "${CUSTOM_ROOT}/lang/jai/parser.c" -o $TEMP_OUT_DIR/jai_parser.o
|
||||
clang $CLANG_OPTS "${INCLUDES[@]}" "${CUSTOM_ROOT}/lang/jai/scanner.cc" -o $TEMP_OUT_DIR/jai_scanner.o
|
||||
print_success "Complete"
|
||||
|
||||
# Link tree-sitter lib and parser obj files into a static library to link into main custom dll
|
||||
print_step "Linking tree-sitter static library"
|
||||
|
|
|
@ -505,12 +505,10 @@ api_definition_generate_api_includes(Arena *arena, API_Definition *api, Generate
|
|||
return(false);
|
||||
}
|
||||
|
||||
#if 0
|
||||
printf("%s:1:\n", fname_ml.str);
|
||||
printf("%s:1:\n", fname_h.str);
|
||||
printf("%s:1:\n", fname_cpp.str);
|
||||
printf("%s:1:\n", fname_con.str);
|
||||
#endif
|
||||
|
||||
////////////////////////////////
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ function Event_Code*
|
|||
add_code(Arena *arena, Event_Code_List *list, String_Const_u8 name){
|
||||
Event_Code *code = push_array(arena, Event_Code, 1);
|
||||
sll_queue_push(list->first, list->last, code);
|
||||
list->count += 1;
|
||||
list->count;
|
||||
code->name = push_string_copy(arena, name);
|
||||
return(code);
|
||||
}
|
||||
|
|
|
@ -3,9 +3,390 @@
|
|||
// TEMP until I implement more generic language stuff
|
||||
/////////////////////////////////////////////
|
||||
|
||||
#include "languages/tree_sitter_cpp.h"
|
||||
#include "languages/tree_sitter_jai.h"
|
||||
#include "languages/tree_sitter_bash.h"
|
||||
/////////////////////////////////////////////
|
||||
// C
|
||||
|
||||
/////////////////////////////////////////////
|
||||
// C++
|
||||
|
||||
TSQuery* tree_sitter_cpp_index_query;
|
||||
String_Const_u8 TS_CPP_INDEX_QUERY = string_u8_litexpr("(_ \"{\" @Start \"}\" @End ) @ScopeNest\n");
|
||||
|
||||
String_Const_u8 TS_CPP_FUNCTION_QUERY = string_u8_litexpr(R"DONE(
|
||||
(function_declarator) @function_identifier
|
||||
)DONE");
|
||||
|
||||
String_Const_u8 TS_CPP_TYPE_QUERY = string_u8_litexpr(R"DONE(
|
||||
(struct_specifier
|
||||
name: (type_identifier) @prefixStruct
|
||||
)
|
||||
(enum_specifier
|
||||
name: (type_identifier) @prefixEnum
|
||||
)
|
||||
(class_specifier
|
||||
name: (type_identifier) @prefixClass
|
||||
)
|
||||
)DONE");
|
||||
|
||||
String_Const_u8 TS_CPP_HIGHLIGHT_QUERY = string_u8_litexpr(R"DONE(
|
||||
(call_expression function: [
|
||||
(identifier) @defcolor_function
|
||||
(field_expression field: (field_identifier) @defcolor_function)])
|
||||
|
||||
(function_declarator
|
||||
declarator: [(identifier) (field_identifier)] @defcolor_function)
|
||||
|
||||
(preproc_def
|
||||
name: (identifier) @defcolor_macro)
|
||||
|
||||
(preproc_function_def
|
||||
name: (identifier) @defcolor_macro)
|
||||
|
||||
(type_identifier) @defcolor_type
|
||||
|
||||
(call_expression
|
||||
function: (parenthesized_expression
|
||||
(identifier) @defcolor_type))
|
||||
|
||||
[(primitive_type) (type_qualifier) (storage_class_specifier)
|
||||
(break_statement) (continue_statement) "union" "return" "do"
|
||||
"while" "for" "if" "class" "struct" "enum" "sizeof"
|
||||
"else" "switch" "case"] @defcolor_keyword
|
||||
|
||||
[(number_literal) (string_literal) (raw_string_literal)] @defcolor_str_constant
|
||||
|
||||
[(preproc_directive) "#define" "#if" "#elif" "#else" "#endif"
|
||||
"#include"] @defcolor_preproc
|
||||
|
||||
["{" "}" ";" ":" ","] @defcolor_text_default
|
||||
|
||||
(comment) @defcolor_comment
|
||||
)DONE");
|
||||
|
||||
/////////////////////////////////////////////
|
||||
// Jai
|
||||
|
||||
String_Const_u8 TS_JAI_FUNCTION_QUERY = string_u8_litexpr(R"DONE(
|
||||
|
||||
(procedure_declaration
|
||||
name: (identifier) @print1
|
||||
(procedure
|
||||
(named_parameters) @print2
|
||||
(procedure_returns) @print
|
||||
)
|
||||
)
|
||||
|
||||
)DONE");
|
||||
|
||||
String_Const_u8 TS_JAI_TYPE_QUERY = string_u8_litexpr(R"DONE(
|
||||
|
||||
(struct_declaration
|
||||
name: (identifier) @prefixStruct
|
||||
)
|
||||
|
||||
(enum_declaration
|
||||
name: (identifier) @prefixEnum
|
||||
)
|
||||
|
||||
)DONE");
|
||||
|
||||
String_Const_u8 TS_JAI_HIGHLIGHT_QUERY = string_u8_litexpr(R"DONE(
|
||||
|
||||
; keywords
|
||||
[
|
||||
"if"
|
||||
"else"
|
||||
"break"
|
||||
"continue"
|
||||
"return"
|
||||
"struct"
|
||||
"enum"
|
||||
"for"
|
||||
"defer"
|
||||
"cast"
|
||||
"xx"
|
||||
"ifx"
|
||||
"null"
|
||||
] @defcolor_keyword
|
||||
|
||||
; # preceeded
|
||||
[
|
||||
(compiler_directive)
|
||||
(import)
|
||||
(char_string)
|
||||
] @defcolor_macro
|
||||
|
||||
(import (identifier) @defcolor_type)
|
||||
|
||||
; Identifiers
|
||||
(struct_declaration
|
||||
name: (identifier) @defcolor_type
|
||||
)
|
||||
|
||||
(struct_literal
|
||||
type: (identifier) @defcolor_type
|
||||
)
|
||||
|
||||
(enum_declaration
|
||||
name: (identifier) @defcolor_type
|
||||
)
|
||||
|
||||
(enum_declaration "{" (identifier) @defcolor_type)
|
||||
|
||||
(variable_declaration
|
||||
type: (types) @defcolor_type
|
||||
)
|
||||
|
||||
(procedure_declaration
|
||||
name: (identifier) @defcolor_function
|
||||
)
|
||||
|
||||
(call_expression
|
||||
function: (identifier) @defcolor_function
|
||||
)
|
||||
|
||||
(procedure
|
||||
result: (procedure_returns) @defcolor_type
|
||||
)
|
||||
|
||||
; Constants & Literals
|
||||
[
|
||||
(string)
|
||||
(string_directive)
|
||||
] @defcolor_str_constant
|
||||
(escape_sequence) @defcolor_special_character
|
||||
(integer) @defcolor_int_constant
|
||||
(float) @defcolor_float_constant
|
||||
(boolean) @defcolor_bool_constant
|
||||
|
||||
(array_literal
|
||||
type: (identifier) @defcolor_type
|
||||
)
|
||||
|
||||
; Comments
|
||||
(note) @defcolor_comment
|
||||
(block_comment) @defcolor_comment
|
||||
(block_comment_text) @defcolor_comment
|
||||
|
||||
)DONE");
|
||||
|
||||
// NOTE(PS): source: https://github.com/St0wy/tree-sitter-jai/blob/main/queries/highlights.scm
|
||||
String_Const_u8 TS_JAI_HIGHLIGHT_QUERY_ = string_u8_litexpr(R"DONE(
|
||||
|
||||
[
|
||||
(compiler_directive)
|
||||
(import)
|
||||
] @defcolor_macro
|
||||
|
||||
; Keywords
|
||||
; TODO : complete this list
|
||||
[
|
||||
"struct"
|
||||
"enum"
|
||||
"defer"
|
||||
"cast"
|
||||
"xx"
|
||||
"return"
|
||||
] @defcolor_keyword
|
||||
|
||||
; Conditionals
|
||||
[
|
||||
"if"
|
||||
"else"
|
||||
"case"
|
||||
"break"
|
||||
] @defcolor_keyword
|
||||
|
||||
((if_expression
|
||||
[
|
||||
"then"
|
||||
"ifx"
|
||||
"else"
|
||||
] @defcolor_keyword)
|
||||
(#set! "priority" 105))
|
||||
|
||||
; Repeats
|
||||
|
||||
[
|
||||
"for"
|
||||
"while"
|
||||
"continue"
|
||||
] @defcolor_keyword
|
||||
|
||||
; Variables
|
||||
|
||||
(identifier) @defcolor_text_default
|
||||
|
||||
; Namespaces
|
||||
|
||||
(import (identifier) @defcolor_text_default)
|
||||
|
||||
; Parameters
|
||||
|
||||
(parameter (identifier) @defcolor_text_default ":" "="? (identifier)? @defcolor_str_constant)
|
||||
|
||||
(default_parameter (identifier) @defcolor_text_default ":=")
|
||||
|
||||
(call_expression argument: (identifier) @defcolor_text_default "=")
|
||||
|
||||
; Functions
|
||||
|
||||
(procedure_declaration (identifier) @defcolor_function)
|
||||
|
||||
(procedure_declaration (identifier) @defcolor_function (procedure (block)))
|
||||
|
||||
(call_expression function: (identifier) @defcolor_function)
|
||||
|
||||
; Types
|
||||
|
||||
(type (identifier) @defcolor_type)
|
||||
|
||||
((type (identifier) @defcolor_type)
|
||||
(#any-of? @type.builtin
|
||||
"bool"
|
||||
"int" "s8" "s16" "s32" "s64"
|
||||
"u8" "u16" "u32" "u64"
|
||||
"string"))
|
||||
|
||||
|
||||
(struct_declaration (identifier) @defcolor_type "::")
|
||||
|
||||
(enum_declaration (identifier) @defcolor_type "::")
|
||||
|
||||
;(union_declaration (identifier) @defcolor_type "::")
|
||||
|
||||
(const_declaration (identifier) @defcolor_type "::" [(array_type) (pointer_type)])
|
||||
|
||||
(struct . (identifier) @defcolor_type)
|
||||
|
||||
;(field_type . (identifier) @namespace "." (identifier) @defcolor_type)
|
||||
|
||||
;(bit_set_type (identifier) @defcolor_type ";")
|
||||
|
||||
;(procedure_type (parameters (parameter (identifier) @defcolor_type)))
|
||||
|
||||
;(polymorphic_parameters (identifier) @defcolor_type)
|
||||
|
||||
((identifier) @defcolor_type
|
||||
(#lua-match? @defcolor_type "^[A-Z][a-zA-Z0-9]*$")
|
||||
(#not-has-parent? @defcolor_type parameter procedure_declaration call_expression))
|
||||
|
||||
; Fields
|
||||
|
||||
(member_expression "." (identifier) @defcolor_text_default)
|
||||
|
||||
;(struct_type "{" (identifier) @defcolor_text_default)
|
||||
|
||||
(struct_field (identifier) @defcolor_text_default "="?)
|
||||
|
||||
(field (identifier) @defcolor_text_default)
|
||||
|
||||
; Constants
|
||||
|
||||
((identifier) @defcolor_text_default
|
||||
(#lua-match? @defcolor_str_constnat "^_*[A-Z][A-Z0-9_]*$")
|
||||
(#not-has-parent? @text_default type parameter))
|
||||
|
||||
(member_expression . "." (identifier) @defcolor_text_default)
|
||||
|
||||
(enum_declaration "{" (identifier) @defcolor_text_default)
|
||||
|
||||
; Literals
|
||||
|
||||
(number) @defcolor_int_constant
|
||||
|
||||
(float) @defcolor_float_constant
|
||||
|
||||
(string) @defcolor_str_constnat
|
||||
|
||||
;(character) @defcolor_str_constnat
|
||||
|
||||
(escape_sequence) @defcolor_str_constant
|
||||
|
||||
(boolean) @defcolor_bool_constant
|
||||
|
||||
[
|
||||
(uninitialized)
|
||||
(null)
|
||||
] @defcolor_text_default
|
||||
|
||||
((identifier) @defcolor_text_default
|
||||
(#any-of? @defcolor_text_default "context"))
|
||||
|
||||
; Operators
|
||||
|
||||
[
|
||||
":="
|
||||
"="
|
||||
"+"
|
||||
"-"
|
||||
"*"
|
||||
"/"
|
||||
"%"
|
||||
"%%"
|
||||
">"
|
||||
">="
|
||||
"<"
|
||||
"<="
|
||||
"=="
|
||||
"!="
|
||||
"~="
|
||||
"|"
|
||||
"~"
|
||||
"&"
|
||||
"&~"
|
||||
"<<"
|
||||
">>"
|
||||
"||"
|
||||
"&&"
|
||||
"!"
|
||||
".."
|
||||
"+="
|
||||
"-="
|
||||
"*="
|
||||
"/="
|
||||
"%="
|
||||
"&="
|
||||
"|="
|
||||
"^="
|
||||
"<<="
|
||||
">>="
|
||||
"||="
|
||||
"&&="
|
||||
"&~="
|
||||
;"..="
|
||||
;"..<"
|
||||
;"?"
|
||||
] @defcolor_operator
|
||||
|
||||
; Punctuation
|
||||
|
||||
[ "{" "}" ] @defcolor_text_default
|
||||
|
||||
[ "(" ")" ] @defcolor_text_default
|
||||
|
||||
[ "[" "]" ] @defcolor_text_default
|
||||
|
||||
[
|
||||
"::"
|
||||
"->"
|
||||
"."
|
||||
","
|
||||
":"
|
||||
";"
|
||||
] @defcolor_text_default
|
||||
|
||||
; Comments
|
||||
|
||||
[
|
||||
(comment)
|
||||
(block_comment)
|
||||
] @defcolor_comment
|
||||
|
||||
; Errors
|
||||
|
||||
(ERROR) @defcolor_comment_pop
|
||||
)DONE");
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Language Management
|
||||
|
@ -109,9 +490,32 @@ tree_sitter_init(Application_Links* app)
|
|||
|
||||
tree_sitter_languages.arena = make_arena_system(KB(16));
|
||||
|
||||
tree_sitter_register_cpp(app);
|
||||
tree_sitter_register_jai(app);
|
||||
tree_sitter_register_bash(app);
|
||||
u32 error_offset;
|
||||
TSQueryError query_error;
|
||||
|
||||
{ // Register CPP
|
||||
TSLanguage* language = tree_sitter_cpp();
|
||||
String_Const_u8 highlight_query_str = TS_CPP_HIGHLIGHT_QUERY;
|
||||
|
||||
Tree_Sitter_Language_Queries queries = {};
|
||||
queries.ptr[Tree_Sitter_Language_Query_Highlights] = tree_sitter_query_new(app, language, TS_CPP_HIGHLIGHT_QUERY);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Functions] = tree_sitter_query_new(app, language, TS_CPP_FUNCTION_QUERY);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Types] = tree_sitter_query_new(app, language, TS_CPP_TYPE_QUERY);
|
||||
tree_sitter_register_language(SCu8("c"), language, queries);
|
||||
tree_sitter_register_language(SCu8("cpp"), language, queries);
|
||||
tree_sitter_register_language(SCu8("h"), language, queries);
|
||||
tree_sitter_register_language(SCu8("hpp"), language, queries);
|
||||
tree_sitter_register_language(SCu8("cc"), language, queries);
|
||||
}
|
||||
|
||||
{ // Register Jai
|
||||
TSLanguage* language = tree_sitter_jai();
|
||||
Tree_Sitter_Language_Queries queries = {};
|
||||
queries.ptr[Tree_Sitter_Language_Query_Highlights] = tree_sitter_query_new(app, language, TS_JAI_HIGHLIGHT_QUERY);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Functions] = tree_sitter_query_new(app, language, TS_JAI_FUNCTION_QUERY);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Types] = tree_sitter_query_new(app, language, TS_JAI_TYPE_QUERY);
|
||||
tree_sitter_register_language(SCu8("jai"), language, queries);
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
|
@ -151,67 +555,6 @@ tree_sitter_buffer_get_tree_copy(Buffer_Tree_Sitter_Data* tree_data)
|
|||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Queries
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Tree_Sitter_Query_Cursor
|
||||
{
|
||||
Buffer_ID buffer_id;
|
||||
TSQuery* query;
|
||||
TSQueryCursor* query_cursor;
|
||||
TSTree* tree;
|
||||
TSNode first_node;
|
||||
bool ok;
|
||||
};
|
||||
|
||||
function Tree_Sitter_Query_Cursor
|
||||
tree_sitter_query_init(Application_Links* app, Buffer_ID buffer_id, TSQuery* query)
|
||||
{
|
||||
Tree_Sitter_Query_Cursor result = {};
|
||||
result.buffer_id = buffer_id;
|
||||
result.query = query;
|
||||
result.ok = false;
|
||||
|
||||
Managed_Scope buffer_scope = buffer_get_managed_scope(app, buffer_id);
|
||||
Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, buffer_scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data);
|
||||
result.tree = tree_sitter_buffer_get_tree_copy(tree_data);
|
||||
if (result.tree) result.first_node = ts_tree_root_node(result.tree);
|
||||
|
||||
result.ok = (
|
||||
result.query != 0 &&
|
||||
result.tree != 0
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
function bool
|
||||
tree_sitter_query_continue(Tree_Sitter_Query_Cursor* cursor, TSQueryMatch* match, u32* capture_index)
|
||||
{
|
||||
if (cursor->ok)
|
||||
{
|
||||
if (!cursor->query_cursor)
|
||||
{
|
||||
cursor->query_cursor = ts_query_cursor_new();
|
||||
ts_query_cursor_exec(cursor->query_cursor, cursor->query, cursor->first_node);
|
||||
}
|
||||
|
||||
cursor->ok = ts_query_cursor_next_capture(cursor->query_cursor, match, capture_index);
|
||||
}
|
||||
return cursor->ok;
|
||||
}
|
||||
|
||||
function void
|
||||
tree_sitter_query_end(Tree_Sitter_Query_Cursor* cursor)
|
||||
{
|
||||
if (cursor->query_cursor) ts_query_cursor_delete(cursor->query_cursor);
|
||||
ts_tree_delete(cursor->tree);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Tree Parsing
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
function void
|
||||
tree_sitter_parse_async__inner(Async_Context* actx, Buffer_ID buffer_id)
|
||||
{
|
||||
|
@ -223,12 +566,6 @@ tree_sitter_parse_async__inner(Async_Context* actx, Buffer_ID buffer_id)
|
|||
ts_parser_set_timeout_micros(parser, 5000);
|
||||
|
||||
acquire_global_frame_mutex(app);
|
||||
Scratch_Block scratch(app);
|
||||
String_Const_u8 buffer_name = push_buffer_unique_name(app, scratch, buffer_id);
|
||||
print_message(app, SCu8("Parsing "));
|
||||
print_message(app, buffer_name);
|
||||
print_message(app, SCu8("\n"));
|
||||
|
||||
Tree_Sitter_Language_Definition* lang = tree_sitter_language_for_buffer(app, buffer_id);
|
||||
String_Const_u8 src = push_whole_buffer(app, &arena, buffer_id);
|
||||
Managed_Scope scope = buffer_get_managed_scope(app, buffer_id);
|
||||
|
@ -269,6 +606,8 @@ tree_sitter_parse_async__inner(Async_Context* actx, Buffer_ID buffer_id)
|
|||
tree_data->tree = new_tree;
|
||||
system_mutex_acquire(tree_data->tree_mutex);
|
||||
|
||||
print_message(app, SCu8("Finished Parse\n"));
|
||||
|
||||
// TODO(PS): Just put the code index update call here
|
||||
// NOTE(jack): This feels kinda hacky, this is here to trigger
|
||||
// the code index update tick. The buffer is also makred by the
|
||||
|
@ -307,162 +646,60 @@ tree_sitter_node_to_range(TSNode node)
|
|||
return result;
|
||||
}
|
||||
|
||||
struct Code_Index_Nest_Stack
|
||||
{
|
||||
Code_Index_Nest_Stack* prev;
|
||||
Code_Index_Nest_Stack* next;
|
||||
Code_Index_Nest* nest;
|
||||
u32 match_id;
|
||||
};
|
||||
|
||||
function Code_Index_Note*
|
||||
code_index_new_note(Code_Index_File* index, Arena* arena, Code_Index_Note_Kind kind, Range_i64 range, Code_Index_Nest_Stack* parent)
|
||||
{
|
||||
Code_Index_Note *result = push_array(arena, Code_Index_Note, 1);
|
||||
sll_queue_push(index->note_list.first, index->note_list.last, result);
|
||||
index->note_list.count += 1;
|
||||
result->file = index;
|
||||
result->note_kind = kind;
|
||||
result->pos = range;
|
||||
if (parent != 0) result->parent = parent->nest;
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
tree_sitter_code_index_update_tick(Application_Links* app)
|
||||
{
|
||||
Scratch_Block scratch(app);
|
||||
|
||||
#if 0
|
||||
// TODO(PS): this should be done when we register the language
|
||||
if (!tree_sitter_cpp_index_query)
|
||||
{
|
||||
u32 error_offset;
|
||||
TSQueryError query_error;
|
||||
tree_sitter_cpp_index_query = ts_query_new(
|
||||
tree_sitter_cpp(), TS_CPP_INDEX_QUERY, (u32)TS_CPP_INDEX_QUERY.size, &error_offset, &query_error
|
||||
);
|
||||
if (!tree_sitter_cpp_index_query)
|
||||
{
|
||||
print_message(app, string_u8_litexpr("Failed to create cpp index query\n");
|
||||
}
|
||||
}
|
||||
|
||||
for (Buffer_Modified_Node* modified_node = global_buffer_modified_set.first;
|
||||
modified_node != 0;
|
||||
modified_node = modified_node->next
|
||||
){
|
||||
Temp_Memory_Block temp(scratch);
|
||||
|
||||
Buffer_ID buffer_id = modified_node->buffer;
|
||||
|
||||
Arena arena = make_arena_system(KB(16));
|
||||
Code_Index_File* index = push_array_zero(&arena, Code_Index_File, 1);
|
||||
index->buffer = buffer_id;
|
||||
|
||||
String_Const_u8 buffer_contents = push_whole_buffer(app, scratch, buffer_id);
|
||||
Tree_Sitter_Code_Index_Nest_List nests = {};
|
||||
|
||||
Tree_Sitter_Language_Definition* lang = tree_sitter_language_for_buffer(app, buffer_id);
|
||||
if (!lang) continue;
|
||||
|
||||
TSQuery* ts_query = lang->queries.ptr[Tree_Sitter_Language_Query_Tags];
|
||||
Tree_Sitter_Query_Cursor query = tree_sitter_query_init(
|
||||
app, buffer_id, ts_query
|
||||
);
|
||||
|
||||
Code_Index_Nest_Stack* nest_stack_first = 0;
|
||||
Code_Index_Nest_Stack* nest_stack_last = 0;
|
||||
|
||||
Code_Index_Note* last_note = 0;
|
||||
u32 last_note_match_id = max_u32;
|
||||
Managed_Scope buffer_scope = buffer_get_managed_scope(app, buffer_id);
|
||||
Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, buffer_scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data);
|
||||
TSTree* tree = tree_sitter_buffer_get_tree_copy(tree_data);
|
||||
if (tree)
|
||||
{
|
||||
TSQueryCursor* query_cursor = ts_query_cursor_new();
|
||||
ts_query_cursor_exec(query_cursor, tree_sitter_cpp_index_query, ts_tree_root_node(tree));
|
||||
|
||||
TSQueryMatch query_match;
|
||||
u32 capture_index;
|
||||
while (tree_sitter_query_continue(&query, &query_match, &capture_index))
|
||||
while (ts_query_cursor_next_match(query_cursor, &match))
|
||||
{
|
||||
TSQueryCapture type_capture = query_match.captures[capture_index];
|
||||
TSQueryCapture type_capture = match.captures[0];
|
||||
TSNode type_node = type_capture.node;
|
||||
Range_i64 type_range = tree_sitter_node_to_range(type_node);
|
||||
|
||||
u32 length;
|
||||
const char* tmp = ts_query_capture_name_for_id(query.query, type_capture.index, &length);
|
||||
String_Const_u8 capture_name = SCu8((char*)tmp, length);
|
||||
|
||||
if (string_match(capture_name, SCu8("scope_begin")))
|
||||
{
|
||||
Code_Index_Nest* nest = push_array_zero(&arena, Code_Index_Nest, 1);
|
||||
nest->kind = CodeIndexNest_Scope;
|
||||
nest->open = type_range;
|
||||
nest->close = Ii64(max_i64);
|
||||
nest->file = index;
|
||||
if (nest_stack_last != 0)
|
||||
{
|
||||
nest->parent = nest_stack_last->nest;
|
||||
code_index_push_nest(&nest->parent->nest_list, nest);
|
||||
}
|
||||
|
||||
Code_Index_Nest_Stack* stack = push_array_zero(scratch, Code_Index_Nest_Stack, 1);
|
||||
stack->nest = nest;
|
||||
stack->prev = nest_stack_last;
|
||||
stack->match_id = query_match.id;
|
||||
if (nest_stack_last != 0) nest_stack_last->next = stack;
|
||||
else nest_stack_first = stack;
|
||||
nest_stack_last = stack;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("scope_end")))
|
||||
{
|
||||
Assert(nest_stack_last != 0);
|
||||
Assert(nest_stack_last->match_id == query_match.id);
|
||||
|
||||
Code_Index_Nest* nest = nest_stack_last->nest;
|
||||
nest->close = type_range;
|
||||
nest->is_closed = true;
|
||||
nest->nest_array = code_index_nest_ptr_array_from_list(&arena, &nest->nest_list);
|
||||
if (nest->parent == 0) code_index_push_nest(&index->nest_list, nest);
|
||||
|
||||
nest_stack_last = nest_stack_last->prev;
|
||||
if (nest_stack_last != 0) nest_stack_last->next = 0;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("definition.class")))
|
||||
{
|
||||
last_note = code_index_new_note(index, &arena, CodeIndexNote_Type, type_range, nest_stack_last);
|
||||
last_note_match_id = query_match.id;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("definition.function")))
|
||||
{
|
||||
last_note = code_index_new_note(index, &arena, CodeIndexNote_Function, type_range, nest_stack_last);
|
||||
last_note_match_id = query_match.id;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("definition.method")))
|
||||
{
|
||||
last_note = code_index_new_note(index, &arena, CodeIndexNote_Function, type_range, nest_stack_last);;
|
||||
last_note_match_id = query_match.id;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("definition.type")))
|
||||
{
|
||||
last_note = code_index_new_note(index, &arena, CodeIndexNote_Type, type_range, nest_stack_last);
|
||||
last_note_match_id = query_match.id;
|
||||
}
|
||||
else if (string_match(capture_name, SCu8("name")))
|
||||
{
|
||||
if (last_note != 0 && last_note_match_id == query_match.id)
|
||||
{
|
||||
last_note->pos = Ii64_size(type_range.start, type_range.end - type_range.start);
|
||||
// TODO(PS): PICK UP HERE
|
||||
}
|
||||
}
|
||||
ts_tree_delete(tree);
|
||||
}
|
||||
|
||||
while (nest_stack_last != 0)
|
||||
{
|
||||
Code_Index_Nest* nest = nest_stack_last->nest;
|
||||
if (nest->parent != 0) code_index_push_nest(&nest->parent->nest_list, nest);
|
||||
else code_index_push_nest(&index->nest_list, nest);
|
||||
nest->nest_array = code_index_nest_ptr_array_from_list(&arena, &nest->nest_list);
|
||||
nest_stack_last = nest_stack_last->prev;
|
||||
}
|
||||
|
||||
for (Code_Index_Note* note = index->note_list.first; note != 0 && note->next != note; note = note->next)
|
||||
{
|
||||
note->text = push_string_copy(&arena, string_substring(buffer_contents, note->pos));
|
||||
}
|
||||
|
||||
// Finish the Index
|
||||
index->nest_array = code_index_nest_ptr_array_from_list(&arena, &index->nest_list);
|
||||
index->note_array = code_index_note_ptr_array_from_list(&arena, &index->note_list);
|
||||
|
||||
code_index_lock();
|
||||
code_index_set_file(buffer_id, arena, index);
|
||||
code_index_unlock();
|
||||
buffer_clear_layout_cache(app, buffer_id);
|
||||
|
||||
tree_sitter_query_end(&query);
|
||||
}
|
||||
|
||||
buffer_modified_set_clear();
|
||||
#endif
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
@ -497,7 +734,7 @@ async_task_cancel_nowait(Application_Links* app, Async_System* async_system, Asy
|
|||
// Token Highlighting
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
function bool
|
||||
function void
|
||||
tree_sitter_highlight_node(
|
||||
Application_Links* app,
|
||||
TSQuery* query,
|
||||
|
@ -505,7 +742,6 @@ tree_sitter_highlight_node(
|
|||
TSQueryCursor* query_cursor,
|
||||
Text_Layout_ID text_layout_id
|
||||
){
|
||||
bool highlighted_any_code = false;
|
||||
ts_query_cursor_exec(query_cursor, query, top_node);
|
||||
|
||||
TSQueryMatch query_match;
|
||||
|
@ -519,33 +755,13 @@ tree_sitter_highlight_node(
|
|||
const char* tmp = ts_query_capture_name_for_id(query, capture.index, &length);
|
||||
String_Const_u8 capture_name = SCu8((char*)tmp, length);
|
||||
|
||||
// TODO(PS): let each language provide a custom override
|
||||
String_Const_u8 color_name = SCu8("defcolor_text_default");
|
||||
if (string_match(capture_name, SCu8("function"))) {
|
||||
color_name = SCu8("defcolor_function");
|
||||
} else if (string_match(capture_name, SCu8("type"))) {
|
||||
color_name = SCu8("defcolor_type");
|
||||
} else if (string_match(capture_name, SCu8("constant")) || string_match(capture_name, SCu8("number"))) {
|
||||
color_name = SCu8("defcolor_int_constant");
|
||||
} else if (string_match(capture_name, SCu8("keyword"))) {
|
||||
color_name = SCu8("defcolor_keyword");
|
||||
} else if (string_match(capture_name, SCu8("string"))) {
|
||||
color_name = SCu8("defcolor_str_constant");
|
||||
} else if (string_match(capture_name, SCu8("comment"))) {
|
||||
color_name = SCu8("defcolor_comment");
|
||||
}
|
||||
|
||||
Range_i64 highlight_range = tree_sitter_node_to_range(node);
|
||||
Managed_ID color_id = managed_id_get(app, SCu8("colors"), color_name);
|
||||
Managed_ID color_id = managed_id_get(app, SCu8("colors"), capture_name);
|
||||
if (color_id != 0)
|
||||
{
|
||||
paint_text_color_fcolor(app, text_layout_id, highlight_range, fcolor_id(color_id));
|
||||
}
|
||||
|
||||
highlighted_any_code = true;
|
||||
}
|
||||
|
||||
return highlighted_any_code;
|
||||
}
|
||||
|
||||
function void
|
||||
|
@ -584,17 +800,70 @@ draw_tree_sitter_node_colors(Application_Links* app, Text_Layout_ID text_layout_
|
|||
}
|
||||
else
|
||||
{
|
||||
bool should_ascend = false;
|
||||
do {
|
||||
should_ascend = !tree_sitter_highlight_node(app, query, visible_container, query_cursor, text_layout_id);
|
||||
if (should_ascend) visible_container = ts_node_parent(visible_container);
|
||||
} while (should_ascend && !ts_node_is_null(visible_container));
|
||||
tree_sitter_highlight_node(app, query, visible_container, query_cursor, text_layout_id);
|
||||
}
|
||||
ts_query_cursor_delete(query_cursor);
|
||||
}
|
||||
ts_tree_delete(tree);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Queries
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Tree_Sitter_Query_Cursor
|
||||
{
|
||||
Buffer_ID buffer_id;
|
||||
TSQuery* query;
|
||||
TSQueryCursor* query_cursor;
|
||||
TSTree* tree;
|
||||
TSNode first_node;
|
||||
bool ok;
|
||||
};
|
||||
|
||||
function Tree_Sitter_Query_Cursor
|
||||
tree_sitter_query_init(Application_Links* app, Buffer_ID buffer_id, TSQuery* query)
|
||||
{
|
||||
Tree_Sitter_Query_Cursor result = {};
|
||||
result.buffer_id = buffer_id;
|
||||
result.query = query;
|
||||
result.ok = false;
|
||||
|
||||
Managed_Scope buffer_scope = buffer_get_managed_scope(app, buffer_id);
|
||||
Buffer_Tree_Sitter_Data* tree_data = scope_attachment(app, buffer_scope, buffer_tree_sitter_data_id, Buffer_Tree_Sitter_Data);
|
||||
result.tree = tree_sitter_buffer_get_tree_copy(tree_data);
|
||||
result.first_node = ts_tree_root_node(result.tree);
|
||||
|
||||
result.ok = (
|
||||
result.query != 0 &&
|
||||
result.tree != 0
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
function bool
|
||||
tree_sitter_query_continue(Tree_Sitter_Query_Cursor* cursor, TSQueryMatch* match, u32* capture_index)
|
||||
{
|
||||
if (cursor->ok)
|
||||
{
|
||||
if (!cursor->query_cursor)
|
||||
{
|
||||
cursor->query_cursor = ts_query_cursor_new();
|
||||
ts_query_cursor_exec(cursor->query_cursor, cursor->query, cursor->first_node);
|
||||
}
|
||||
|
||||
cursor->ok = ts_query_cursor_next_capture(cursor->query_cursor, match, capture_index);
|
||||
}
|
||||
return cursor->ok;
|
||||
}
|
||||
|
||||
function void
|
||||
tree_sitter_query_end(Tree_Sitter_Query_Cursor* cursor)
|
||||
{
|
||||
if (cursor->query_cursor) ts_query_cursor_delete(cursor->query_cursor);
|
||||
ts_tree_delete(cursor->tree);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Lists
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
@ -610,10 +879,6 @@ convert_to_single_line_in_place(String_Const_u8 str)
|
|||
{
|
||||
while (src < str.size && character_is_whitespace(str.str[src])) { src += 1; }
|
||||
if (src >= str.size) break;
|
||||
else {
|
||||
result.str[dst] = ' ';
|
||||
dst += 1;
|
||||
}
|
||||
}
|
||||
result.str[dst] = str.str[src];
|
||||
dst += 1;
|
||||
|
@ -651,7 +916,7 @@ function void
|
|||
tree_sitter_list_all_query_results(
|
||||
Application_Links *app,
|
||||
Buffer_ID optional_target_buffer,
|
||||
Code_Index_Note_Kind note_kind
|
||||
Tree_Sitter_Language_Query_Kind query_kind
|
||||
){
|
||||
String_Const_u8 decls_name = string_u8_litexpr("*decls*");
|
||||
Buffer_ID decls_buffer = get_buffer_by_name(app, decls_name, Access_Always);
|
||||
|
@ -672,32 +937,79 @@ tree_sitter_list_all_query_results(
|
|||
Cursor insertion_cursor = make_cursor(push_array(scratch, u8, KB(256)), KB(256));
|
||||
Buffer_Insertion out = begin_buffer_insertion_at_buffered(app, decls_buffer, 0, &insertion_cursor);
|
||||
|
||||
code_index_lock();
|
||||
for (Buffer_ID buffer_it = get_buffer_next(app, 0, Access_Always);
|
||||
buffer_it != 0;
|
||||
buffer_it = get_buffer_next(app, buffer_it, Access_Always))
|
||||
{
|
||||
Buffer_ID buffer = buffer_it;
|
||||
if (optional_target_buffer != 0) buffer = optional_target_buffer;
|
||||
|
||||
String_Const_u8 buffer_name = push_buffer_unique_name(app, scratch, buffer);
|
||||
Code_Index_File* file = code_index_get_file(buffer);
|
||||
if (file != 0)
|
||||
|
||||
Token_Array array = get_token_array_from_buffer(app, buffer);
|
||||
if (array.tokens != 0)
|
||||
{
|
||||
for (i32 i = 0; i < file->note_array.count; i += 1)
|
||||
Tree_Sitter_Language_Definition* lang = tree_sitter_language_for_buffer(app, buffer);
|
||||
if (!lang) continue;
|
||||
TSQuery* ts_query = lang->queries.ptr[query_kind];
|
||||
Tree_Sitter_Query_Cursor query = tree_sitter_query_init(app, buffer, ts_query);
|
||||
|
||||
i64 last_query_match_id = -1;
|
||||
i64 last_query_match_printed = -1;
|
||||
i64 last_query_line_number = 0;
|
||||
Range_i64 last_query_range = {};
|
||||
String_Const_u8 last_query_prefix = {};
|
||||
|
||||
TSQueryMatch query_match;
|
||||
u32 capture_index;
|
||||
bool reached_end = false;
|
||||
while (tree_sitter_query_continue(&query, &query_match, &capture_index))
|
||||
{
|
||||
Code_Index_Note *note = file->note_array.ptrs[i];
|
||||
if (note->note_kind == note_kind)
|
||||
TSQueryCapture capture = query_match.captures[capture_index];
|
||||
|
||||
if (last_query_match_id != query_match.id)
|
||||
{
|
||||
if (last_query_match_id >= 0)
|
||||
{
|
||||
print_position(
|
||||
app, &out, buffer, buffer_name, note->pos, {}, scratch, true
|
||||
app, &out, buffer, buffer_name, last_query_range, last_query_prefix, scratch, true
|
||||
);
|
||||
last_query_match_printed = last_query_match_id;
|
||||
}
|
||||
last_query_range.start = (i64)ts_node_start_byte(capture.node);
|
||||
last_query_range.end = last_query_range.start;
|
||||
last_query_prefix = {};
|
||||
}
|
||||
|
||||
last_query_match_id = query_match.id;
|
||||
last_query_range.end = Max((i64)ts_node_end_byte(capture.node), last_query_range.end);
|
||||
|
||||
String_Const_u8 name;
|
||||
u32 name_length;
|
||||
name.str = (u8*)ts_query_capture_name_for_id(ts_query, capture.index, &name_length);
|
||||
name.size = (u64)name_length;
|
||||
|
||||
String_Const_u8 prefix_identifier = SCu8("prefix");
|
||||
u64 prefix_loc = string_find_first(name, prefix_identifier);
|
||||
if (prefix_loc < name.size)
|
||||
{
|
||||
last_query_prefix = name;
|
||||
last_query_prefix.str += prefix_loc + prefix_identifier.size;
|
||||
last_query_prefix.size -= prefix_loc + prefix_identifier.size;
|
||||
}
|
||||
}
|
||||
|
||||
if (last_query_match_printed != last_query_match_id)
|
||||
{
|
||||
print_position(
|
||||
app, &out, buffer, buffer_name, last_query_range, last_query_prefix, scratch, true
|
||||
);
|
||||
}
|
||||
tree_sitter_query_end(&query);
|
||||
|
||||
if (optional_target_buffer != 0) break;
|
||||
}
|
||||
code_index_unlock();
|
||||
}
|
||||
|
||||
end_buffer_insertion(&out);
|
||||
|
||||
|
@ -712,7 +1024,7 @@ CUSTOM_DOC("Creates a jump list of lines of the current buffer that appear to de
|
|||
{
|
||||
View_ID view = get_active_view(app, Access_ReadVisible);
|
||||
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
|
||||
if (buffer != 0) tree_sitter_list_all_query_results(app, buffer, CodeIndexNote_Function);
|
||||
if (buffer != 0) tree_sitter_list_all_query_results(app, buffer, Tree_Sitter_Language_Query_Functions);
|
||||
}
|
||||
|
||||
CUSTOM_UI_COMMAND_SIG(tree_sitter_list_all_functions_current_buffer_lister)
|
||||
|
@ -723,7 +1035,7 @@ CUSTOM_DOC("Creates a lister of locations that look like function definitions an
|
|||
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
|
||||
if (buffer != 0)
|
||||
{
|
||||
tree_sitter_list_all_query_results(app, buffer, CodeIndexNote_Function);
|
||||
tree_sitter_list_all_query_results(app, buffer, Tree_Sitter_Language_Query_Functions);
|
||||
view = get_active_view(app, Access_Always);
|
||||
buffer = view_get_buffer(app, view, Access_Always);
|
||||
Marker_List *list = get_or_make_list_for_buffer(app, heap, buffer);
|
||||
|
@ -738,14 +1050,14 @@ CUSTOM_DOC("Creates a lister of locations that look like function definitions an
|
|||
CUSTOM_COMMAND_SIG(tree_sitter_list_all_functions_all_buffers)
|
||||
CUSTOM_DOC("Creates a jump list of lines from all buffers that appear to define or declare functions. Uses tree sitter")
|
||||
{
|
||||
tree_sitter_list_all_query_results(app, 0, CodeIndexNote_Function);
|
||||
tree_sitter_list_all_query_results(app, 0, Tree_Sitter_Language_Query_Functions);
|
||||
}
|
||||
|
||||
CUSTOM_UI_COMMAND_SIG(tree_sitter_list_all_functions_all_buffers_lister)
|
||||
CUSTOM_DOC("Creates a lister of locations that look like function definitions and declarations all buffers. Uses tree sitter")
|
||||
{
|
||||
Heap *heap = &global_heap;
|
||||
tree_sitter_list_all_query_results(app, 0, CodeIndexNote_Function);
|
||||
tree_sitter_list_all_query_results(app, 0, Tree_Sitter_Language_Query_Functions);
|
||||
View_ID view = get_active_view(app, Access_Always);
|
||||
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
|
||||
Marker_List *list = get_or_make_list_for_buffer(app, heap, buffer);
|
||||
|
@ -761,7 +1073,7 @@ CUSTOM_DOC("Creates a jump list of lines of the current buffer that appear to de
|
|||
{
|
||||
View_ID view = get_active_view(app, Access_ReadVisible);
|
||||
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
|
||||
if (buffer != 0) tree_sitter_list_all_query_results(app, buffer, CodeIndexNote_Type);
|
||||
if (buffer != 0) tree_sitter_list_all_query_results(app, buffer, Tree_Sitter_Language_Query_Types);
|
||||
}
|
||||
|
||||
CUSTOM_UI_COMMAND_SIG(tree_sitter_list_all_types_current_buffer_lister)
|
||||
|
@ -772,7 +1084,7 @@ CUSTOM_DOC("Creates a lister of locations that look like function definitions an
|
|||
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
|
||||
if (buffer != 0)
|
||||
{
|
||||
tree_sitter_list_all_query_results(app, buffer, CodeIndexNote_Type);
|
||||
tree_sitter_list_all_query_results(app, buffer, Tree_Sitter_Language_Query_Types);
|
||||
view = get_active_view(app, Access_Always);
|
||||
buffer = view_get_buffer(app, view, Access_Always);
|
||||
Marker_List *list = get_or_make_list_for_buffer(app, heap, buffer);
|
||||
|
@ -787,14 +1099,14 @@ CUSTOM_DOC("Creates a lister of locations that look like function definitions an
|
|||
CUSTOM_COMMAND_SIG(tree_sitter_list_all_types_all_buffers)
|
||||
CUSTOM_DOC("Creates a jump list of lines from all buffers that appear to define or declare types. Uses tree sitter")
|
||||
{
|
||||
tree_sitter_list_all_query_results(app, 0, CodeIndexNote_Type);
|
||||
tree_sitter_list_all_query_results(app, 0, Tree_Sitter_Language_Query_Types);
|
||||
}
|
||||
|
||||
CUSTOM_UI_COMMAND_SIG(tree_sitter_list_all_types_all_buffers_lister)
|
||||
CUSTOM_DOC("Creates a lister of locations that look like type definitions and declarations all buffers. Uses tree sitter")
|
||||
{
|
||||
Heap *heap = &global_heap;
|
||||
tree_sitter_list_all_query_results(app, 0, CodeIndexNote_Type);
|
||||
tree_sitter_list_all_query_results(app, 0, Tree_Sitter_Language_Query_Types);
|
||||
View_ID view = get_active_view(app, Access_Always);
|
||||
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
|
||||
Marker_List *list = get_or_make_list_for_buffer(app, heap, buffer);
|
||||
|
|
|
@ -8,7 +8,9 @@
|
|||
enum Tree_Sitter_Language_Query_Kind
|
||||
{
|
||||
Tree_Sitter_Language_Query_Highlights,
|
||||
Tree_Sitter_Language_Query_Tags,
|
||||
Tree_Sitter_Language_Query_Functions,
|
||||
Tree_Sitter_Language_Query_Types,
|
||||
|
||||
Tree_Sitter_Language_Query_Count,
|
||||
};
|
||||
|
||||
|
@ -36,6 +38,10 @@ struct Tree_Sitter_Languages
|
|||
|
||||
global Tree_Sitter_Languages tree_sitter_languages;
|
||||
|
||||
extern "C" {
|
||||
TSLanguage *tree_sitter_cpp();
|
||||
TSLanguage *tree_sitter_jai();
|
||||
}
|
||||
|
||||
CUSTOM_ID(attachment, buffer_tree_sitter_data_id);
|
||||
CUSTOM_ID(attachment, buffer_tree_sitter_parse_task_id);
|
||||
|
@ -63,9 +69,6 @@ struct Tree_Sitter_Code_Index_Nest_List
|
|||
Tree_Sitter_Code_Index_Nest_Node* last;
|
||||
};
|
||||
|
||||
function TSQuery* tree_sitter_query_new(Application_Links* app, TSLanguage* language, String_Const_u8 query_string);
|
||||
function void tree_sitter_register_language(String_Const_u8 ext, TSLanguage* language, Tree_Sitter_Language_Queries queries);
|
||||
|
||||
b8 use_tree_sitter_code_indexing = true;
|
||||
b8 use_tree_sitter_token_coloring = true;
|
||||
function void tree_sitter_code_index_update_tick(Application_Links *app);
|
||||
|
|
|
@ -35,8 +35,8 @@ debug=-g
|
|||
|
||||
opts="-Wno-macro-redefined -Wno-write-strings -Wno-null-dereference -Wno-comment -Wno-switch -Wno-missing-declarations -Wno-logical-op-parentheses -g -DOS_LINUX=1 -DOS_WINDOWS=0 -DOS_MAC=0"
|
||||
|
||||
pushd $dst > /dev/null
|
||||
pushd $dst
|
||||
g++ -I"$CUSTOM_ROOT" $opts $full_target -o one_time
|
||||
popd > /dev/null
|
||||
popd
|
||||
|
||||
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
/* date = July 11th 2025 6:02 pm */
|
||||
|
||||
#ifndef TREE_SITTER_BASH_H
|
||||
#define TREE_SITTER_BASH_H
|
||||
|
||||
// Source: https://github.com/tree-sitter/tree-sitter-bash/blob/master/queries/highlights.scm
|
||||
String_Const_u8 TS_BASH_HIGHLIGHT_QUERY = string_u8_litexpr(R"DONE(
|
||||
[
|
||||
(string)
|
||||
(raw_string)
|
||||
(heredoc_body)
|
||||
(heredoc_start)
|
||||
] @string
|
||||
|
||||
(command_name) @function.call
|
||||
|
||||
(variable_name) @property
|
||||
|
||||
[
|
||||
"case"
|
||||
"do"
|
||||
"done"
|
||||
"elif"
|
||||
"else"
|
||||
"esac"
|
||||
"export"
|
||||
"fi"
|
||||
"for"
|
||||
"function"
|
||||
"if"
|
||||
"in"
|
||||
"select"
|
||||
"then"
|
||||
"unset"
|
||||
"until"
|
||||
"while"
|
||||
] @keyword
|
||||
|
||||
(comment) @comment
|
||||
|
||||
(function_definition name: (word) @function)
|
||||
|
||||
(file_descriptor) @number
|
||||
|
||||
[
|
||||
(command_substitution)
|
||||
(process_substitution)
|
||||
(expansion)
|
||||
] @embedded
|
||||
|
||||
[
|
||||
"$"
|
||||
"&&"
|
||||
">"
|
||||
">>"
|
||||
"<"
|
||||
"|"
|
||||
] @operator
|
||||
|
||||
(
|
||||
(command (_) @constant)
|
||||
(#match? @constant "^-")
|
||||
)
|
||||
|
||||
)DONE");
|
||||
|
||||
extern "C" {
|
||||
TSLanguage* tree_sitter_bash();
|
||||
}
|
||||
|
||||
void
|
||||
tree_sitter_register_bash(Application_Links* app)
|
||||
{
|
||||
TSLanguage* language = tree_sitter_bash();
|
||||
Tree_Sitter_Language_Queries queries = {};
|
||||
queries.ptr[Tree_Sitter_Language_Query_Highlights] = tree_sitter_query_new(app, language, TS_BASH_HIGHLIGHT_QUERY);
|
||||
tree_sitter_register_language(SCu8("sh"), language, queries);
|
||||
}
|
||||
|
||||
#endif //TREE_SITTER_BASH_H
|
|
@ -1,211 +0,0 @@
|
|||
/* date = July 11th 2025 6:04 pm */
|
||||
|
||||
#ifndef TREE_SITTER_CPP_H
|
||||
#define TREE_SITTER_CPP_H
|
||||
|
||||
// Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/queries/tags.scm
|
||||
String_Const_u8 TS_CPP_TAGS_QUERY_SCM = string_u8_litexpr(R"DONE(
|
||||
(struct_specifier name: (type_identifier) @name body:(_)) @definition.class
|
||||
|
||||
(declaration type: (union_specifier name: (type_identifier) @name)) @definition.class
|
||||
|
||||
(function_declarator declarator: (identifier) @name) @definition.function
|
||||
|
||||
(function_declarator declarator: (field_identifier) @name) @definition.function
|
||||
|
||||
(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @local.scope name: (identifier) @name)) @definition.method
|
||||
|
||||
(type_definition declarator: (type_identifier) @name) @definition.type
|
||||
|
||||
(enum_specifier name: (type_identifier) @name) @definition.type
|
||||
|
||||
(class_specifier name: (type_identifier) @name) @definition.class
|
||||
|
||||
(_ "{" @scope_begin "}" @scope_end )
|
||||
(_ "(" @scope_begin ")" @scope_end )
|
||||
(_ "[" @scope_begin "]" @scope_end )
|
||||
|
||||
)DONE");
|
||||
|
||||
// Source: https://github.com/tree-sitter/tree-sitter-cpp/blob/master/queries/highlights.scm
|
||||
String_Const_u8 TS_CPP_HIGHLIGHT_QUERY_SCM = string_u8_litexpr(R"DONE(
|
||||
(identifier) @variable
|
||||
|
||||
((identifier) @constant
|
||||
(#match? @constant "^[A-Z][A-Z\\d_]*$"))
|
||||
|
||||
"break" @keyword
|
||||
"case" @keyword
|
||||
"const" @keyword
|
||||
"continue" @keyword
|
||||
"default" @keyword
|
||||
"do" @keyword
|
||||
"else" @keyword
|
||||
"enum" @keyword
|
||||
"extern" @keyword
|
||||
"for" @keyword
|
||||
"if" @keyword
|
||||
"inline" @keyword
|
||||
"return" @keyword
|
||||
"sizeof" @keyword
|
||||
"static" @keyword
|
||||
"struct" @keyword
|
||||
"switch" @keyword
|
||||
"typedef" @keyword
|
||||
"union" @keyword
|
||||
"volatile" @keyword
|
||||
"while" @keyword
|
||||
|
||||
"#define" @keyword
|
||||
"#elif" @keyword
|
||||
"#else" @keyword
|
||||
"#endif" @keyword
|
||||
"#if" @keyword
|
||||
"#ifdef" @keyword
|
||||
"#ifndef" @keyword
|
||||
"#include" @keyword
|
||||
(preproc_directive) @keyword
|
||||
|
||||
"--" @operator
|
||||
"-" @operator
|
||||
"-=" @operator
|
||||
"->" @operator
|
||||
"=" @operator
|
||||
"!=" @operator
|
||||
"*" @operator
|
||||
"&" @operator
|
||||
"&&" @operator
|
||||
"+" @operator
|
||||
"++" @operator
|
||||
"+=" @operator
|
||||
"<" @operator
|
||||
"==" @operator
|
||||
">" @operator
|
||||
"||" @operator
|
||||
|
||||
"." @delimiter
|
||||
";" @delimiter
|
||||
|
||||
(string_literal) @string
|
||||
(system_lib_string) @string
|
||||
|
||||
(null) @constant
|
||||
(number_literal) @number
|
||||
(char_literal) @number
|
||||
|
||||
(field_identifier) @property
|
||||
(statement_identifier) @label
|
||||
(type_identifier) @type
|
||||
(primitive_type) @type
|
||||
(sized_type_specifier) @type
|
||||
|
||||
(call_expression
|
||||
function: (identifier) @function)
|
||||
(call_expression
|
||||
function: (field_expression
|
||||
field: (field_identifier) @function))
|
||||
(function_declarator
|
||||
declarator: (identifier) @function)
|
||||
(preproc_function_def
|
||||
name: (identifier) @function.special)
|
||||
|
||||
(comment) @comment
|
||||
|
||||
; Functions
|
||||
|
||||
(call_expression
|
||||
function: (qualified_identifier
|
||||
name: (identifier) @function))
|
||||
|
||||
(template_function
|
||||
name: (identifier) @function)
|
||||
|
||||
(template_method
|
||||
name: (field_identifier) @function)
|
||||
|
||||
(template_function
|
||||
name: (identifier) @function)
|
||||
|
||||
(function_declarator
|
||||
declarator: (qualified_identifier
|
||||
name: (identifier) @function))
|
||||
|
||||
(function_declarator
|
||||
declarator: (field_identifier) @function)
|
||||
|
||||
; Types
|
||||
|
||||
((namespace_identifier) @type
|
||||
(#match? @type "^[A-Z]"))
|
||||
|
||||
(auto) @type
|
||||
|
||||
; Constants
|
||||
|
||||
(this) @variable.builtin
|
||||
(null "nullptr" @constant)
|
||||
|
||||
; Modules
|
||||
(module_name
|
||||
(identifier) @module)
|
||||
|
||||
; Keywords
|
||||
|
||||
[
|
||||
"catch"
|
||||
"class"
|
||||
"co_await"
|
||||
"co_return"
|
||||
"co_yield"
|
||||
"constexpr"
|
||||
"constinit"
|
||||
"consteval"
|
||||
"delete"
|
||||
"explicit"
|
||||
"final"
|
||||
"friend"
|
||||
"mutable"
|
||||
"namespace"
|
||||
"noexcept"
|
||||
"new"
|
||||
"override"
|
||||
"private"
|
||||
"protected"
|
||||
"public"
|
||||
"template"
|
||||
"throw"
|
||||
"try"
|
||||
"typename"
|
||||
"using"
|
||||
"concept"
|
||||
"requires"
|
||||
"virtual"
|
||||
"import"
|
||||
"export"
|
||||
"module"
|
||||
] @keyword
|
||||
|
||||
; Strings
|
||||
|
||||
(raw_string_literal) @string
|
||||
)DONE");
|
||||
|
||||
extern "C" {
|
||||
TSLanguage *tree_sitter_cpp();
|
||||
}
|
||||
|
||||
void
|
||||
tree_sitter_register_cpp(Application_Links* app)
|
||||
{
|
||||
TSLanguage* language = tree_sitter_cpp();
|
||||
Tree_Sitter_Language_Queries queries = {};
|
||||
queries.ptr[Tree_Sitter_Language_Query_Highlights] = tree_sitter_query_new(app, language, TS_CPP_HIGHLIGHT_QUERY_SCM);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Tags] = tree_sitter_query_new(app, language, TS_CPP_TAGS_QUERY_SCM);
|
||||
tree_sitter_register_language(SCu8("c"), language, queries);
|
||||
tree_sitter_register_language(SCu8("cpp"), language, queries);
|
||||
tree_sitter_register_language(SCu8("h"), language, queries);
|
||||
tree_sitter_register_language(SCu8("hpp"), language, queries);
|
||||
tree_sitter_register_language(SCu8("cc"), language, queries);
|
||||
}
|
||||
|
||||
#endif //TREE_SITTER_CPP_H
|
|
@ -1,298 +0,0 @@
|
|||
/* date = July 11th 2025 6:03 pm */
|
||||
|
||||
#ifndef TREE_SITTER_JAI_H
|
||||
#define TREE_SITTER_JAI_H
|
||||
|
||||
String_Const_u8 TS_JAI_TAGS_QUERY = string_u8_litexpr(R"DONE(
|
||||
|
||||
(procedure_declaration
|
||||
name: (identifier) @definition.function
|
||||
)
|
||||
|
||||
(struct_declaration
|
||||
name: (identifier) @definition.type
|
||||
)
|
||||
|
||||
(enum_declaration
|
||||
name: (identifier) @definition.type
|
||||
)
|
||||
|
||||
)DONE");
|
||||
|
||||
// NOTE(PS): source: https://github.com/St0wy/tree-sitter-jai/blob/main/queries/highlights.scm
|
||||
String_Const_u8 TS_JAI_HIGHLIGHT_QUERY = string_u8_litexpr(R"DONE(
|
||||
; Includes
|
||||
|
||||
[
|
||||
(import)
|
||||
(load)
|
||||
] @include
|
||||
|
||||
; Keywords
|
||||
[
|
||||
; from modules/Jai_Lexer
|
||||
"if"
|
||||
"xx"
|
||||
|
||||
"ifx"
|
||||
"for"
|
||||
|
||||
"then"
|
||||
"else"
|
||||
"null"
|
||||
"case"
|
||||
"enum"
|
||||
"true"
|
||||
"cast"
|
||||
|
||||
"while"
|
||||
"break"
|
||||
"using"
|
||||
"defer"
|
||||
"false"
|
||||
"union"
|
||||
|
||||
"return"
|
||||
"struct"
|
||||
"inline"
|
||||
"remove"
|
||||
|
||||
; "size_of"
|
||||
"type_of"
|
||||
; "code_of"
|
||||
; "context"
|
||||
|
||||
"continue"
|
||||
"operator"
|
||||
|
||||
; "type_info"
|
||||
"no_inline"
|
||||
"interface"
|
||||
|
||||
"enum_flags"
|
||||
|
||||
; "is_constant"
|
||||
|
||||
"push_context"
|
||||
|
||||
; "initializer_of"
|
||||
] @keyword
|
||||
|
||||
[
|
||||
"return"
|
||||
] @keyword.return
|
||||
|
||||
[
|
||||
"if"
|
||||
"else"
|
||||
"case"
|
||||
"break"
|
||||
] @keyword.conditional
|
||||
|
||||
((if_expression
|
||||
[
|
||||
"then"
|
||||
"ifx"
|
||||
"else"
|
||||
] @keyword.conditional.ternary)
|
||||
(#set! "priority" 105))
|
||||
|
||||
; Repeats
|
||||
|
||||
[
|
||||
"for"
|
||||
"while"
|
||||
"continue"
|
||||
] @keyword.repeat
|
||||
|
||||
; Variables
|
||||
|
||||
; (identifier) @variable
|
||||
name: (identifier) @variable
|
||||
argument: (identifier) @variable
|
||||
named_argument: (identifier) @variable
|
||||
(member_expression (identifier) @variable)
|
||||
(parenthesized_expression (identifier) @variable)
|
||||
|
||||
((identifier) @variable.builtin
|
||||
(#any-of? @variable.builtin "context"))
|
||||
|
||||
; Namespaces
|
||||
|
||||
(import (identifier) @namespace)
|
||||
|
||||
; Parameters
|
||||
|
||||
(parameter (identifier) @parameter ":" "="? (identifier)? @constant)
|
||||
|
||||
; (call_expression argument: (identifier) @parameter "=")
|
||||
|
||||
; Functions
|
||||
|
||||
; (procedure_declaration (identifier) @function (procedure (block)))
|
||||
(procedure_declaration (identifier) @function (block))
|
||||
|
||||
(call_expression function: (identifier) @function.call)
|
||||
|
||||
; Types
|
||||
|
||||
type: (types) @type
|
||||
type: (identifier) @type
|
||||
((types) @type)
|
||||
|
||||
modifier: (identifier) @keyword
|
||||
keyword: (identifier) @keyword
|
||||
|
||||
((types (identifier) @type.builtin)
|
||||
(#any-of? @type.builtin
|
||||
"bool" "int" "string"
|
||||
"s8" "s16" "s32" "s64"
|
||||
"u8" "u16" "u32" "u64"
|
||||
"Type" "Any"))
|
||||
|
||||
(struct_declaration (identifier) @type ":" ":")
|
||||
|
||||
(enum_declaration (identifier) @type ":" ":")
|
||||
|
||||
; (const_declaration (identifier) @type ":" ":" [(array_type) (pointer_type)])
|
||||
|
||||
; ; I don't like this
|
||||
; ((identifier) @type
|
||||
; (#lua-match? @type "^[A-Z][a-zA-Z0-9]*$")
|
||||
; (#not-has-parent? @type parameter procedure_declaration call_expression))
|
||||
|
||||
; Fields
|
||||
|
||||
(member_expression "." (identifier) @field)
|
||||
|
||||
(assignment_statement (identifier) @field "="?)
|
||||
(update_statement (identifier) @field)
|
||||
|
||||
; Constants
|
||||
|
||||
((identifier) @constant
|
||||
(#lua-match? @constant "^_*[A-Z][A-Z0-9_]*$")
|
||||
(#not-has-parent? @constant type parameter))
|
||||
|
||||
(member_expression . "." (identifier) @constant)
|
||||
|
||||
(enum_declaration "{" (identifier) @constant)
|
||||
|
||||
; Literals
|
||||
|
||||
(integer) @number
|
||||
(float) @number
|
||||
|
||||
(string) @string
|
||||
|
||||
;(character) @character
|
||||
|
||||
(string (escape_sequence) @string.escape)
|
||||
|
||||
(boolean) @boolean
|
||||
|
||||
[
|
||||
(uninitialized)
|
||||
(null)
|
||||
] @constant.builtin
|
||||
|
||||
; Operators
|
||||
|
||||
[
|
||||
":"
|
||||
"="
|
||||
"+"
|
||||
"-"
|
||||
"*"
|
||||
"/"
|
||||
"%"
|
||||
">"
|
||||
">="
|
||||
"<"
|
||||
"<="
|
||||
"=="
|
||||
"!="
|
||||
"|"
|
||||
"~"
|
||||
"&"
|
||||
"&~"
|
||||
"<<"
|
||||
">>"
|
||||
"<<<"
|
||||
">>>"
|
||||
"||"
|
||||
"&&"
|
||||
"!"
|
||||
".."
|
||||
"+="
|
||||
"-="
|
||||
"*="
|
||||
"/="
|
||||
"%="
|
||||
"&="
|
||||
"|="
|
||||
"^="
|
||||
"<<="
|
||||
">>="
|
||||
"<<<="
|
||||
">>>="
|
||||
"||="
|
||||
"&&="
|
||||
] @operator
|
||||
|
||||
; Punctuation
|
||||
|
||||
[ "{" "}" ] @punctuation.bracket
|
||||
|
||||
[ "(" ")" ] @punctuation.bracket
|
||||
|
||||
[ "[" "]" ] @punctuation.bracket
|
||||
|
||||
[
|
||||
"`"
|
||||
"->"
|
||||
"."
|
||||
","
|
||||
":"
|
||||
";"
|
||||
] @punctuation.delimiter
|
||||
|
||||
; Comments
|
||||
|
||||
[
|
||||
(block_comment)
|
||||
(comment)
|
||||
] @comment @spell
|
||||
|
||||
; Errors
|
||||
|
||||
(ERROR) @error
|
||||
|
||||
(block_comment) @comment
|
||||
|
||||
directive: ("#") @keyword ; #if
|
||||
type: ("type_of") @type
|
||||
|
||||
(compiler_directive) @keyword
|
||||
(heredoc_start) @none
|
||||
(heredoc_end) @none
|
||||
(heredoc_body) @string
|
||||
(note) @string
|
||||
|
||||
)DONE");
|
||||
|
||||
extern "C" {
|
||||
TSLanguage *tree_sitter_jai();
|
||||
}
|
||||
|
||||
void
|
||||
tree_sitter_register_jai (Application_Links* app)
|
||||
{
|
||||
TSLanguage* language = tree_sitter_jai();
|
||||
Tree_Sitter_Language_Queries queries = {};
|
||||
queries.ptr[Tree_Sitter_Language_Query_Highlights] = tree_sitter_query_new(app, language, TS_JAI_HIGHLIGHT_QUERY);
|
||||
queries.ptr[Tree_Sitter_Language_Query_Tags] = tree_sitter_query_new(app, language, TS_JAI_TAGS_QUERY);
|
||||
tree_sitter_register_language(SCu8("jai"), language, queries);
|
||||
}
|
||||
|
||||
#endif //TREE_SITTER_JAI_H
|
|
@ -732,8 +732,8 @@ smi_field_set_intersect(Arena *arena, Field_Set a, Field_Set b){
|
|||
for (Field_Pin *pin = new_list->first;
|
||||
pin != 0;
|
||||
pin = pin->next){
|
||||
if (pin->flag == b_pin->flag){
|
||||
if (pin->value != b_pin->value){
|
||||
if (pin->flag == pin->flag){
|
||||
if (pin->value != pin->value){
|
||||
end_temp(restore_point);
|
||||
has_conflicts = true;
|
||||
goto double_break;
|
||||
|
@ -4036,8 +4036,8 @@ int main(void){
|
|||
fclose(out_h_file);
|
||||
fclose(out_cpp_file);
|
||||
|
||||
//printf("%.*s:1:\n", string_expand(out_h_name));
|
||||
//printf("%.*s:1:\n", string_expand(out_cpp_name));
|
||||
printf("%.*s:1:\n", string_expand(out_h_name));
|
||||
printf("%.*s:1:\n", string_expand(out_cpp_name));
|
||||
|
||||
// NOTE(allen): Simplifying the state machine
|
||||
// Isolate the state machine's parts into small L.U.T. then generate tables?
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -12,7 +12,6 @@ patterns = {
|
|||
};
|
||||
blacklist_patterns = {
|
||||
".*",
|
||||
"non-source",
|
||||
};
|
||||
load_paths_base = {
|
||||
{ ".", .relative = true, .recursive = true, },
|
||||
|
|
Loading…
Reference in New Issue