From a07b8421e2ecc1cc0c2792c6d49855fa1464cea3 Mon Sep 17 00:00:00 2001 From: Summpot Date: Wed, 1 Nov 2023 16:01:17 +0800 Subject: [PATCH] Update --- src/TreeSitterSharp.C/CLanguageProvider.cs | 13 ++-- src/TreeSitterSharp.C/CParser.cs | 10 +-- .../JsonLanguageProvider.cs | 11 +--- src/TreeSitterSharp.Json/JsonParser.cs | 8 +-- src/TreeSitterSharp/ILanguageProvider.cs | 10 +-- src/TreeSitterSharp/INativeObject.cs | 11 ++++ src/TreeSitterSharp/Language.cs | 17 +++++ .../Native/NativeTypeNameAttribute.cs | 5 +- src/TreeSitterSharp/Native/Ts.cs | 66 +++++++++---------- src/TreeSitterSharp/Native/TsFieldMapEntry.cs | 2 +- src/TreeSitterSharp/Native/TsFieldMapSlice.cs | 2 +- src/TreeSitterSharp/Native/TsInput.cs | 4 +- src/TreeSitterSharp/Native/TsInputEdit.cs | 8 +-- src/TreeSitterSharp/Native/TsLanguage.cs | 2 +- src/TreeSitterSharp/Native/TsLexMode.cs | 2 +- src/TreeSitterSharp/Native/TsLexer.cs | 2 +- src/TreeSitterSharp/Native/TsLogger.cs | 2 +- .../Native/TsLookaheadIterator.cs | 2 +- src/TreeSitterSharp/Native/TsNode.cs | 4 +- src/TreeSitterSharp/Native/TsParseAction.cs | 4 +- .../Native/TsParseActionEntry.cs | 2 +- src/TreeSitterSharp/Native/TsParser.cs | 2 +- src/TreeSitterSharp/Native/TsQuery.cs | 2 +- src/TreeSitterSharp/Native/TsQueryCapture.cs | 2 +- src/TreeSitterSharp/Native/TsQueryCursor.cs | 2 +- src/TreeSitterSharp/Native/TsQueryMatch.cs | 2 +- .../Native/TsQueryPredicateStep.cs | 2 +- src/TreeSitterSharp/Native/TsRange.cs | 6 +- .../Native/TsSymbolMetadata.cs | 2 +- src/TreeSitterSharp/Native/TsTreeCursor.cs | 2 +- src/TreeSitterSharp/{TsNode.cs => Node.cs} | 63 +++++++++--------- .../{TsParser.cs => Parser.cs} | 31 ++++----- src/TreeSitterSharp/{TSPoint.cs => Point.cs} | 2 +- src/TreeSitterSharp/Query.cs | 25 +++++++ src/TreeSitterSharp/QueryCursor.cs | 33 ++++++++++ src/TreeSitterSharp/QueryMatch.cs | 16 +++++ src/TreeSitterSharp/StringMarshaller.cs | 7 +- src/TreeSitterSharp/Tree.cs | 23 +++++++ .../{TsTreeCursor.cs => TreeCursor.cs} | 22 +++---- src/TreeSitterSharp/TsLanguage.cs | 22 ------- src/TreeSitterSharp/TsTree.cs | 28 -------- tests/TreeSitterSharp.C.Tests/CParserTests.cs | 6 +- .../JsonParserTests.cs | 11 ++-- 43 files changed, 266 insertions(+), 232 deletions(-) create mode 100644 src/TreeSitterSharp/INativeObject.cs create mode 100644 src/TreeSitterSharp/Language.cs rename src/TreeSitterSharp/{TsNode.cs => Node.cs} (63%) rename src/TreeSitterSharp/{TsParser.cs => Parser.cs} (62%) rename src/TreeSitterSharp/{TSPoint.cs => Point.cs} (78%) create mode 100644 src/TreeSitterSharp/Query.cs create mode 100644 src/TreeSitterSharp/QueryCursor.cs create mode 100644 src/TreeSitterSharp/QueryMatch.cs create mode 100644 src/TreeSitterSharp/Tree.cs rename src/TreeSitterSharp/{TsTreeCursor.cs => TreeCursor.cs} (69%) delete mode 100644 src/TreeSitterSharp/TsLanguage.cs delete mode 100644 src/TreeSitterSharp/TsTree.cs diff --git a/src/TreeSitterSharp.C/CLanguageProvider.cs b/src/TreeSitterSharp.C/CLanguageProvider.cs index 4f2cc72..3f7513c 100644 --- a/src/TreeSitterSharp.C/CLanguageProvider.cs +++ b/src/TreeSitterSharp.C/CLanguageProvider.cs @@ -1,20 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.InteropServices; using TreeSitterSharp.Native; namespace TreeSitterSharp.C; internal class CLanguageProvider : ILanguageProvider { [DllImport("tree-sitter-c", EntryPoint = "tree_sitter_c", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - private static extern unsafe Native.TsLanguage* tree_sitter_c(); + private static extern unsafe TsLanguage* tree_sitter_c(); - public static unsafe TsLanguage GetLanguage() + public static unsafe Language GetLanguage() { - return new TsLanguage(tree_sitter_c()); + return new Language(tree_sitter_c()); } } diff --git a/src/TreeSitterSharp.C/CParser.cs b/src/TreeSitterSharp.C/CParser.cs index 072dab0..6af2f38 100644 --- a/src/TreeSitterSharp.C/CParser.cs +++ b/src/TreeSitterSharp.C/CParser.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TreeSitterSharp.C; -public class CParser : TsParser +namespace TreeSitterSharp.C; +public class CParser : Parser { public CParser() : base(CLanguageProvider.GetLanguage()) { diff --git a/src/TreeSitterSharp.Json/JsonLanguageProvider.cs b/src/TreeSitterSharp.Json/JsonLanguageProvider.cs index f16367c..2ba930a 100644 --- a/src/TreeSitterSharp.Json/JsonLanguageProvider.cs +++ b/src/TreeSitterSharp.Json/JsonLanguageProvider.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.InteropServices; using TreeSitterSharp.Native; namespace TreeSitterSharp.Json @@ -11,8 +6,8 @@ namespace TreeSitterSharp.Json internal unsafe class JsonLanguageProvider : ILanguageProvider { [DllImport("tree-sitter-json", EntryPoint = "tree_sitter_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - private static extern unsafe Native.TsLanguage* tree_sitter_json(); + private static extern TsLanguage* tree_sitter_json(); - public static TsLanguage GetLanguage() => new(tree_sitter_json()); + public static Language GetLanguage() => new(tree_sitter_json()); } } diff --git a/src/TreeSitterSharp.Json/JsonParser.cs b/src/TreeSitterSharp.Json/JsonParser.cs index 637e767..7dccfa3 100644 --- a/src/TreeSitterSharp.Json/JsonParser.cs +++ b/src/TreeSitterSharp.Json/JsonParser.cs @@ -1,12 +1,6 @@  -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace TreeSitterSharp.Json; -public class JsonParser : TsParser +public class JsonParser : Parser { public JsonParser() : base(JsonLanguageProvider.GetLanguage()) { diff --git a/src/TreeSitterSharp/ILanguageProvider.cs b/src/TreeSitterSharp/ILanguageProvider.cs index db50d14..e2a31ba 100644 --- a/src/TreeSitterSharp/ILanguageProvider.cs +++ b/src/TreeSitterSharp/ILanguageProvider.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TreeSitterSharp; +namespace TreeSitterSharp; public interface ILanguageProvider { - static abstract TsLanguage GetLanguage(); + static abstract Language GetLanguage(); } diff --git a/src/TreeSitterSharp/INativeObject.cs b/src/TreeSitterSharp/INativeObject.cs new file mode 100644 index 0000000..18e98fa --- /dev/null +++ b/src/TreeSitterSharp/INativeObject.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TreeSitterSharp; +internal interface INativeObject where T : struct +{ + unsafe T* ToUnmanaged(); +} diff --git a/src/TreeSitterSharp/Language.cs b/src/TreeSitterSharp/Language.cs new file mode 100644 index 0000000..58274ec --- /dev/null +++ b/src/TreeSitterSharp/Language.cs @@ -0,0 +1,17 @@ +using TreeSitterSharp.Native; + +namespace TreeSitterSharp; +public unsafe class Language:INativeObject +{ + private TsLanguage* _language; + + public Language(TsLanguage* language) + { + _language = language; + } + + public TsLanguage* ToUnmanaged() + { + return _language; + } +} diff --git a/src/TreeSitterSharp/Native/NativeTypeNameAttribute.cs b/src/TreeSitterSharp/Native/NativeTypeNameAttribute.cs index 9692917..139d5f5 100644 --- a/src/TreeSitterSharp/Native/NativeTypeNameAttribute.cs +++ b/src/TreeSitterSharp/Native/NativeTypeNameAttribute.cs @@ -1,12 +1,11 @@ -using System; using System.Diagnostics; namespace TreeSitterSharp.Native; /// Defines the type of a member as it was used in the native signature. -[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = true)] +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)] [Conditional("DEBUG")] -internal sealed partial class NativeTypeNameAttribute : Attribute +internal sealed class NativeTypeNameAttribute : Attribute { private readonly string _name; diff --git a/src/TreeSitterSharp/Native/Ts.cs b/src/TreeSitterSharp/Native/Ts.cs index d603b75..ed3a475 100644 --- a/src/TreeSitterSharp/Native/Ts.cs +++ b/src/TreeSitterSharp/Native/Ts.cs @@ -16,7 +16,7 @@ public static unsafe partial class Ts [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_language", ExactSpelling = true)] [return: NativeTypeName("const TsLanguage *")] - public static extern TsLanguage* parser_language([NativeTypeName("const TsParser *")] TsParser* self); + public static extern TsLanguage* parser_language([NativeTypeName("const Parser *")] TsParser* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_set_language", ExactSpelling = true)] @@ -32,12 +32,12 @@ public static extern byte parser_set_included_ranges(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_included_ranges", ExactSpelling = true)] [return: NativeTypeName("const TsRange *")] - public static extern TsRange* parser_included_ranges([NativeTypeName("const TsParser *")] TsParser* self, + public static extern TsRange* parser_included_ranges([NativeTypeName("const Parser *")] TsParser* self, [NativeTypeName("uint32_t *")] uint* count); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_parse", ExactSpelling = true)] - public static extern TsTree* parser_parse(TsParser* self, [NativeTypeName("const TsTree *")] TsTree* old_tree, + public static extern TsTree* parser_parse(TsParser* self, [NativeTypeName("const Tree *")] TsTree* old_tree, TsInput input); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_parse_string", @@ -59,7 +59,7 @@ public static extern void parser_set_timeout_micros(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_timeout_micros", ExactSpelling = true)] [return: NativeTypeName("uint64_t")] - public static extern ulong parser_timeout_micros([NativeTypeName("const TsParser *")] TsParser* self); + public static extern ulong parser_timeout_micros([NativeTypeName("const Parser *")] TsParser* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_set_cancellation_flag", ExactSpelling = true)] @@ -69,7 +69,7 @@ public static extern void parser_set_cancellation_flag(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_cancellation_flag", ExactSpelling = true)] [return: NativeTypeName("const size_t *")] - public static extern nuint* parser_cancellation_flag([NativeTypeName("const TsParser *")] TsParser* self); + public static extern nuint* parser_cancellation_flag([NativeTypeName("const Parser *")] TsParser* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_set_logger", ExactSpelling = true)] @@ -77,7 +77,7 @@ public static extern void parser_set_cancellation_flag(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_logger", ExactSpelling = true)] - public static extern TsLogger parser_logger([NativeTypeName("const TsParser *")] TsParser* self); + public static extern TsLogger parser_logger([NativeTypeName("const Parser *")] TsParser* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_print_dot_graphs", ExactSpelling = true)] @@ -85,7 +85,7 @@ public static extern void parser_set_cancellation_flag(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_copy", ExactSpelling = true)] - public static extern TsTree* tree_copy([NativeTypeName("const TsTree *")] TsTree* self); + public static extern TsTree* tree_copy([NativeTypeName("const Tree *")] TsTree* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_delete", ExactSpelling = true)] @@ -97,17 +97,17 @@ public static extern void parser_set_cancellation_flag(TsParser* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_root_node_with_offset", ExactSpelling = true)] - public static extern TsNode tree_root_node_with_offset([NativeTypeName("const TsTree *")] TsTree* self, - [NativeTypeName("uint32_t")] uint offset_bytes, TsPoint offset_extent); + public static extern TsNode tree_root_node_with_offset([NativeTypeName("const Tree *")] TsTree* self, + [NativeTypeName("uint32_t")] uint offset_bytes, Point offset_extent); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_language", ExactSpelling = true)] [return: NativeTypeName("const TsLanguage *")] - public static extern TsLanguage* tree_language([NativeTypeName("const TsTree *")] TsTree* self); + public static extern TsLanguage* tree_language([NativeTypeName("const Tree *")] TsTree* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_included_ranges", ExactSpelling = true)] - public static extern TsRange* tree_included_ranges([NativeTypeName("const TsTree *")] TsTree* self, + public static extern TsRange* tree_included_ranges([NativeTypeName("const Tree *")] TsTree* self, [NativeTypeName("uint32_t *")] uint* length); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_edit", @@ -116,13 +116,13 @@ public static extern TsNode tree_root_node_with_offset([NativeTypeName("const Ts [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_get_changed_ranges", ExactSpelling = true)] - public static extern TsRange* tree_get_changed_ranges([NativeTypeName("const TsTree *")] TsTree* old_tree, - [NativeTypeName("const TsTree *")] TsTree* new_tree, [NativeTypeName("uint32_t *")] uint* length); + public static extern TsRange* tree_get_changed_ranges([NativeTypeName("const Tree *")] TsTree* old_tree, + [NativeTypeName("const Tree *")] TsTree* new_tree, [NativeTypeName("uint32_t *")] uint* length); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_print_dot_graph", ExactSpelling = true)] public static extern void - tree_print_dot_graph([NativeTypeName("const TsTree *")] TsTree* self, int file_descriptor); + tree_print_dot_graph([NativeTypeName("const Tree *")] TsTree* self, int file_descriptor); [LibraryImport("tree-sitter", EntryPoint = "ts_node_type", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(ConstantStringMarshaller))] @@ -154,7 +154,7 @@ public static extern void [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_start_point", ExactSpelling = true)] - public static extern TsPoint node_start_point(TsNode self); + public static extern Point node_start_point(TsNode self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_end_byte", ExactSpelling = true)] @@ -163,7 +163,7 @@ public static extern void [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_end_point", ExactSpelling = true)] - public static extern TsPoint node_end_point(TsNode self); + public static extern Point node_end_point(TsNode self); [LibraryImport("tree-sitter", EntryPoint = "ts_node_string", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(StringMarshaller))] @@ -276,7 +276,7 @@ public static extern TsNode node_descendant_for_byte_range(TsNode self, [NativeT [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_descendant_for_point_range", ExactSpelling = true)] - public static extern TsNode node_descendant_for_point_range(TsNode self, TsPoint start, TsPoint end); + public static extern TsNode node_descendant_for_point_range(TsNode self, Point start, Point end); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_named_descendant_for_byte_range", ExactSpelling = true)] @@ -285,7 +285,7 @@ public static extern TsNode node_named_descendant_for_byte_range(TsNode self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_named_descendant_for_point_range", ExactSpelling = true)] - public static extern TsNode node_named_descendant_for_point_range(TsNode self, TsPoint start, TsPoint end); + public static extern TsNode node_named_descendant_for_point_range(TsNode self, Point start, Point end); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_node_edit", ExactSpelling = true)] @@ -376,7 +376,7 @@ public static extern uint tree_cursor_current_descendant_index( [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_cursor_goto_first_child_for_point", ExactSpelling = true)] [return: NativeTypeName("int64_t")] - public static extern long tree_cursor_goto_first_child_for_point(in TsTreeCursor self, TsPoint goal_point); + public static extern long tree_cursor_goto_first_child_for_point(in TsTreeCursor self, Point goal_point); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_tree_cursor_copy", ExactSpelling = true)] @@ -395,64 +395,64 @@ public static extern uint tree_cursor_current_descendant_index( [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_pattern_count", ExactSpelling = true)] [return: NativeTypeName("uint32_t")] - public static extern uint query_pattern_count([NativeTypeName("const TsQuery *")] TsQuery* self); + public static extern uint query_pattern_count([NativeTypeName("const Query *")] TsQuery* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_capture_count", ExactSpelling = true)] [return: NativeTypeName("uint32_t")] - public static extern uint query_capture_count([NativeTypeName("const TsQuery *")] TsQuery* self); + public static extern uint query_capture_count([NativeTypeName("const Query *")] TsQuery* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_string_count", ExactSpelling = true)] [return: NativeTypeName("uint32_t")] - public static extern uint query_string_count([NativeTypeName("const TsQuery *")] TsQuery* self); + public static extern uint query_string_count([NativeTypeName("const Query *")] TsQuery* self); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_start_byte_for_pattern", ExactSpelling = true)] [return: NativeTypeName("uint32_t")] - public static extern uint query_start_byte_for_pattern([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern uint query_start_byte_for_pattern([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_predicates_for_pattern", ExactSpelling = true)] [return: NativeTypeName("const TsQueryPredicateStep *")] public static extern TsQueryPredicateStep* query_predicates_for_pattern( - [NativeTypeName("const TsQuery *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index, + [NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index, [NativeTypeName("uint32_t *")] uint* step_count); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_is_pattern_rooted", ExactSpelling = true)] [return: NativeTypeName("bool")] - public static extern byte query_is_pattern_rooted([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern byte query_is_pattern_rooted([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_is_pattern_non_local", ExactSpelling = true)] [return: NativeTypeName("bool")] - public static extern byte query_is_pattern_non_local([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern byte query_is_pattern_non_local([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_is_pattern_guaranteed_at_step", ExactSpelling = true)] [return: NativeTypeName("bool")] - public static extern byte query_is_pattern_guaranteed_at_step([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern byte query_is_pattern_guaranteed_at_step([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint byte_offset); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_capture_name_for_id", ExactSpelling = true)] [return: NativeTypeName("const char *")] - public static extern string query_capture_name_for_id([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern string query_capture_name_for_id([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint index, [NativeTypeName("uint32_t *")] uint* length); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_capture_quantifier_for_id", ExactSpelling = true)] - public static extern TsQuantifier query_capture_quantifier_for_id([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern TsQuantifier query_capture_quantifier_for_id([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint pattern_index, [NativeTypeName("uint32_t")] uint capture_index); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_string_value_for_id", ExactSpelling = true)] [return: NativeTypeName("const char *")] - public static extern string query_string_value_for_id([NativeTypeName("const TsQuery *")] TsQuery* self, + public static extern string query_string_value_for_id([NativeTypeName("const Query *")] TsQuery* self, [NativeTypeName("uint32_t")] uint index, [NativeTypeName("uint32_t *")] uint* length); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_disable_capture", @@ -474,7 +474,7 @@ public static extern void query_disable_capture(TsQuery* self, [NativeTypeName(" [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_cursor_exec", ExactSpelling = true)] - public static extern void query_cursor_exec(TsQueryCursor* self, [NativeTypeName("const TsQuery *")] TsQuery* query, + public static extern void query_cursor_exec(TsQueryCursor* self, [NativeTypeName("const Query *")] TsQuery* query, TsNode node); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, @@ -500,12 +500,12 @@ public static extern void query_cursor_set_byte_range(TsQueryCursor* self, [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_cursor_set_point_range", ExactSpelling = true)] - public static extern void query_cursor_set_point_range(TsQueryCursor* self, TsPoint start_point, TsPoint end_point); + public static extern void query_cursor_set_point_range(TsQueryCursor* self, Point start_point, Point end_point); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_cursor_next_match", ExactSpelling = true)] [return: NativeTypeName("bool")] - public static extern byte query_cursor_next_match(TsQueryCursor* self, TsQueryMatch* match); + public static extern bool query_cursor_next_match(TsQueryCursor* self, TsQueryMatch* match); [DllImport("tree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_query_cursor_remove_match", ExactSpelling = true)] diff --git a/src/TreeSitterSharp/Native/TsFieldMapEntry.cs b/src/TreeSitterSharp/Native/TsFieldMapEntry.cs index faf12bd..f2a3c3e 100644 --- a/src/TreeSitterSharp/Native/TsFieldMapEntry.cs +++ b/src/TreeSitterSharp/Native/TsFieldMapEntry.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsFieldMapEntry +public struct TsFieldMapEntry { [NativeTypeName("TSFieldId")] public ushort field_id; diff --git a/src/TreeSitterSharp/Native/TsFieldMapSlice.cs b/src/TreeSitterSharp/Native/TsFieldMapSlice.cs index 9bd9668..c3967e6 100644 --- a/src/TreeSitterSharp/Native/TsFieldMapSlice.cs +++ b/src/TreeSitterSharp/Native/TsFieldMapSlice.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsFieldMapSlice +public struct TsFieldMapSlice { [NativeTypeName("uint16_t")] public ushort index; diff --git a/src/TreeSitterSharp/Native/TsInput.cs b/src/TreeSitterSharp/Native/TsInput.cs index 1130deb..a569d5d 100644 --- a/src/TreeSitterSharp/Native/TsInput.cs +++ b/src/TreeSitterSharp/Native/TsInput.cs @@ -1,11 +1,11 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsInput +public unsafe struct TsInput { public void* payload; [NativeTypeName("const char *(*)(void *, uint32_t, TSPoint, uint32_t *)")] - public delegate* unmanaged[Cdecl] read; + public delegate* unmanaged[Cdecl] read; public TsInputEncoding encoding; } diff --git a/src/TreeSitterSharp/Native/TsInputEdit.cs b/src/TreeSitterSharp/Native/TsInputEdit.cs index 5b8f97f..e126a84 100644 --- a/src/TreeSitterSharp/Native/TsInputEdit.cs +++ b/src/TreeSitterSharp/Native/TsInputEdit.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsInputEdit +public struct TsInputEdit { [NativeTypeName("uint32_t")] public uint start_byte; @@ -11,9 +11,9 @@ public partial struct TsInputEdit [NativeTypeName("uint32_t")] public uint new_end_byte; - public TsPoint start_point; + public Point start_point; - public TsPoint old_end_point; + public Point old_end_point; - public TsPoint new_end_point; + public Point new_end_point; } diff --git a/src/TreeSitterSharp/Native/TsLanguage.cs b/src/TreeSitterSharp/Native/TsLanguage.cs index 475ecf0..5b867ef 100644 --- a/src/TreeSitterSharp/Native/TsLanguage.cs +++ b/src/TreeSitterSharp/Native/TsLanguage.cs @@ -89,7 +89,7 @@ public unsafe partial struct TsLanguage [NativeTypeName("const TSStateId *")] public ushort* primary_state_ids; - public unsafe partial struct _external_scanner_e__Struct + public struct _external_scanner_e__Struct { [NativeTypeName("const bool *")] public bool* states; diff --git a/src/TreeSitterSharp/Native/TsLexMode.cs b/src/TreeSitterSharp/Native/TsLexMode.cs index 888c8b7..a44869c 100644 --- a/src/TreeSitterSharp/Native/TsLexMode.cs +++ b/src/TreeSitterSharp/Native/TsLexMode.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsLexMode +public struct TsLexMode { [NativeTypeName("uint16_t")] public ushort lex_state; diff --git a/src/TreeSitterSharp/Native/TsLexer.cs b/src/TreeSitterSharp/Native/TsLexer.cs index 1301af8..047d79f 100644 --- a/src/TreeSitterSharp/Native/TsLexer.cs +++ b/src/TreeSitterSharp/Native/TsLexer.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsLexer +public unsafe struct TsLexer { [NativeTypeName("int32_t")] public int lookahead; diff --git a/src/TreeSitterSharp/Native/TsLogger.cs b/src/TreeSitterSharp/Native/TsLogger.cs index e115a44..449e984 100644 --- a/src/TreeSitterSharp/Native/TsLogger.cs +++ b/src/TreeSitterSharp/Native/TsLogger.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsLogger +public unsafe struct TsLogger { public void* payload; diff --git a/src/TreeSitterSharp/Native/TsLookaheadIterator.cs b/src/TreeSitterSharp/Native/TsLookaheadIterator.cs index e8c023c..d29206b 100644 --- a/src/TreeSitterSharp/Native/TsLookaheadIterator.cs +++ b/src/TreeSitterSharp/Native/TsLookaheadIterator.cs @@ -1,5 +1,5 @@ namespace TreeSitterSharp.Native; -public partial struct TsLookaheadIterator +public struct TsLookaheadIterator { } diff --git a/src/TreeSitterSharp/Native/TsNode.cs b/src/TreeSitterSharp/Native/TsNode.cs index 80b4eb6..8e4b1cd 100644 --- a/src/TreeSitterSharp/Native/TsNode.cs +++ b/src/TreeSitterSharp/Native/TsNode.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsNode +public unsafe struct TsNode { [NativeTypeName("uint32_t[4]")] public fixed uint context[4]; @@ -8,6 +8,6 @@ public unsafe partial struct TsNode [NativeTypeName("const void *")] public void* id; - [NativeTypeName("const TsTree *")] + [NativeTypeName("const Tree *")] public TsTree* tree; } diff --git a/src/TreeSitterSharp/Native/TsParseAction.cs b/src/TreeSitterSharp/Native/TsParseAction.cs index 28e0542..3070f99 100644 --- a/src/TreeSitterSharp/Native/TsParseAction.cs +++ b/src/TreeSitterSharp/Native/TsParseAction.cs @@ -17,7 +17,7 @@ public partial struct TsParseAction [NativeTypeName("uint8_t")] public byte type; - public partial struct _shift_e__Struct + public struct _shift_e__Struct { [NativeTypeName("uint8_t")] public byte type; @@ -32,7 +32,7 @@ public partial struct _shift_e__Struct public byte repetition; } - public partial struct _reduce_e__Struct + public struct _reduce_e__Struct { [NativeTypeName("uint8_t")] public byte type; diff --git a/src/TreeSitterSharp/Native/TsParseActionEntry.cs b/src/TreeSitterSharp/Native/TsParseActionEntry.cs index f99a233..0dee460 100644 --- a/src/TreeSitterSharp/Native/TsParseActionEntry.cs +++ b/src/TreeSitterSharp/Native/TsParseActionEntry.cs @@ -12,7 +12,7 @@ public partial struct TsParseActionEntry [NativeTypeName("__AnonymousRecord_parser_L83_C3")] public EntryEStruct entry; - public partial struct EntryEStruct + public struct EntryEStruct { [NativeTypeName("uint8_t")] public byte Count; diff --git a/src/TreeSitterSharp/Native/TsParser.cs b/src/TreeSitterSharp/Native/TsParser.cs index 7162f59..b6cc2f5 100644 --- a/src/TreeSitterSharp/Native/TsParser.cs +++ b/src/TreeSitterSharp/Native/TsParser.cs @@ -1,5 +1,5 @@ namespace TreeSitterSharp.Native; -public partial struct TsParser +public struct TsParser { } diff --git a/src/TreeSitterSharp/Native/TsQuery.cs b/src/TreeSitterSharp/Native/TsQuery.cs index c68ebbe..29b7cfb 100644 --- a/src/TreeSitterSharp/Native/TsQuery.cs +++ b/src/TreeSitterSharp/Native/TsQuery.cs @@ -1,5 +1,5 @@ namespace TreeSitterSharp.Native; -public partial struct TsQuery +public struct TsQuery { } diff --git a/src/TreeSitterSharp/Native/TsQueryCapture.cs b/src/TreeSitterSharp/Native/TsQueryCapture.cs index 718a4dd..279f5b3 100644 --- a/src/TreeSitterSharp/Native/TsQueryCapture.cs +++ b/src/TreeSitterSharp/Native/TsQueryCapture.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsQueryCapture +public struct TsQueryCapture { public TsNode Node; diff --git a/src/TreeSitterSharp/Native/TsQueryCursor.cs b/src/TreeSitterSharp/Native/TsQueryCursor.cs index c224685..9753a81 100644 --- a/src/TreeSitterSharp/Native/TsQueryCursor.cs +++ b/src/TreeSitterSharp/Native/TsQueryCursor.cs @@ -1,5 +1,5 @@ namespace TreeSitterSharp.Native; -public partial struct TsQueryCursor +public struct TsQueryCursor { } diff --git a/src/TreeSitterSharp/Native/TsQueryMatch.cs b/src/TreeSitterSharp/Native/TsQueryMatch.cs index 27a5b11..060ccaa 100644 --- a/src/TreeSitterSharp/Native/TsQueryMatch.cs +++ b/src/TreeSitterSharp/Native/TsQueryMatch.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsQueryMatch +public unsafe struct TsQueryMatch { [NativeTypeName("uint32_t")] public uint Id; diff --git a/src/TreeSitterSharp/Native/TsQueryPredicateStep.cs b/src/TreeSitterSharp/Native/TsQueryPredicateStep.cs index e692fac..79859eb 100644 --- a/src/TreeSitterSharp/Native/TsQueryPredicateStep.cs +++ b/src/TreeSitterSharp/Native/TsQueryPredicateStep.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsQueryPredicateStep +public struct TsQueryPredicateStep { public TsQueryPredicateStepType Type; diff --git a/src/TreeSitterSharp/Native/TsRange.cs b/src/TreeSitterSharp/Native/TsRange.cs index b5a510e..874fd51 100644 --- a/src/TreeSitterSharp/Native/TsRange.cs +++ b/src/TreeSitterSharp/Native/TsRange.cs @@ -1,10 +1,10 @@ namespace TreeSitterSharp.Native; -public partial struct TsRange +public struct TsRange { - public TsPoint start_point; + public Point start_point; - public TsPoint end_point; + public Point end_point; public uint start_byte; diff --git a/src/TreeSitterSharp/Native/TsSymbolMetadata.cs b/src/TreeSitterSharp/Native/TsSymbolMetadata.cs index 9f64e58..24bd9bf 100644 --- a/src/TreeSitterSharp/Native/TsSymbolMetadata.cs +++ b/src/TreeSitterSharp/Native/TsSymbolMetadata.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public partial struct TsSymbolMetadata +public struct TsSymbolMetadata { [NativeTypeName("bool")] public byte Visible; diff --git a/src/TreeSitterSharp/Native/TsTreeCursor.cs b/src/TreeSitterSharp/Native/TsTreeCursor.cs index e472520..6f85004 100644 --- a/src/TreeSitterSharp/Native/TsTreeCursor.cs +++ b/src/TreeSitterSharp/Native/TsTreeCursor.cs @@ -1,6 +1,6 @@ namespace TreeSitterSharp.Native; -public unsafe partial struct TsTreeCursor +public unsafe struct TsTreeCursor { public TsTree* tree; diff --git a/src/TreeSitterSharp/TsNode.cs b/src/TreeSitterSharp/Node.cs similarity index 63% rename from src/TreeSitterSharp/TsNode.cs rename to src/TreeSitterSharp/Node.cs index bd255fc..49a1edd 100644 --- a/src/TreeSitterSharp/TsNode.cs +++ b/src/TreeSitterSharp/Node.cs @@ -1,31 +1,31 @@ -using System.Diagnostics; +using System.Collections.Immutable; using TreeSitterSharp.Native; namespace TreeSitterSharp; -public unsafe class TsNode +public unsafe class Node { - private readonly Native.TsNode _node; + private readonly TsNode _node; - internal TsNode(Native.TsNode node) + internal Node(TsNode node) { _node = node; - Tree = new TsTree(_node.tree); + Tree = new Tree(_node.tree); } - public TsTree Tree { get; } + public Tree Tree { get; } public nint Id => (nint)_node.id; public string Type => Ts.node_type(_node); public ushort Symbol => Ts.node_symbol(_node); - public TsLanguage Language => new(Ts.node_language(_node)); + public Language Language => new(Ts.node_language(_node)); public string GrammarType => Ts.node_grammar_type(_node); public ushort GrammarSymbol => Ts.node_grammar_symbol(_node); public uint StartByte => Ts.node_start_byte(_node); public uint EndByte => Ts.node_end_byte(_node); - public TsPoint StartPoint => Ts.node_start_point(_node); - public TsPoint EndPoint => Ts.node_end_point(_node); + public Point StartPoint => Ts.node_start_point(_node); + public Point EndPoint => Ts.node_end_point(_node); public uint ChildCount => Ts.node_child_count(_node); public uint NamedChildCount => Ts.node_named_child_count(_node); - public TsNode? PreviousSibling + public Node? PreviousSibling { get { @@ -34,7 +34,7 @@ public TsNode? PreviousSibling } } - public TsNode? NextSibling + public Node? NextSibling { get { @@ -43,7 +43,7 @@ public TsNode? NextSibling } } - public TsNode? PreviousNamedSibling + public Node? PreviousNamedSibling { get { @@ -52,7 +52,7 @@ public TsNode? PreviousNamedSibling } } - public TsNode? NextNamedSibling + public Node? NextNamedSibling { get { @@ -61,7 +61,7 @@ public TsNode? NextNamedSibling } } - public TsNode? Parent + public Node? Parent { get { @@ -75,21 +75,29 @@ public TsNode? Parent public bool IsExtra => Ts.node_is_extra(_node); public bool IsNull => Ts.node_is_null(_node); - public TsNode GetChildByFieldName(string fieldName) + public ImmutableArray Children => GetChildren().ToImmutableArray(); + public ImmutableArray NamedChildren => GetNamedChildren().ToImmutableArray(); + + public Node GetChildByFieldName(string fieldName) { - return new TsNode(Ts.node_child_by_field_name(_node, fieldName, (uint)fieldName.Length)); + return new Node(Ts.node_child_by_field_name(_node, fieldName, (uint)fieldName.Length)); } - public TsNode GetChildByFieldId(ushort fieldId) => new(Ts.node_child_by_field_id(_node, fieldId)); + public Node GetChildByFieldId(ushort fieldId) => new(Ts.node_child_by_field_id(_node, fieldId)); public string GetFieldNameForChild(uint childIndex) => Ts.node_field_name_for_child(_node, childIndex); - public TsNode GetNamedChild(uint index) + public Node GetNamedChild(uint index) + { + return new Node(Ts.node_named_child(_node, index)); + } + + public Node GetChild(uint index) { - return new TsNode(Ts.node_named_child(_node, index)); + return new Node(Ts.node_child(_node, index)); } - public IEnumerable GetNamedChildren() + private IEnumerable GetNamedChildren() { for (uint i = 0; i < NamedChildCount; i++) { @@ -97,7 +105,7 @@ public IEnumerable GetNamedChildren() } } - public IEnumerable GetChildren() + private IEnumerable GetChildren() { for (uint i = 0; i < ChildCount; i++) { @@ -105,12 +113,7 @@ public IEnumerable GetChildren() } } - public TsNode GetChild(uint index) - { - return new TsNode(Ts.node_child(_node, index)); - } - - public Native.TsNode ToUnmanaged() + public TsNode ToUnmanaged() { return _node; } @@ -120,7 +123,7 @@ public string GetSExpression() return Ts.node_string(_node); } - protected bool Equals(TsNode other) => Ts.node_eq(_node, other._node); + protected bool Equals(Node other) => Ts.node_eq(_node, other._node); public override bool Equals(object? obj) { @@ -134,12 +137,12 @@ public override bool Equals(object? obj) return true; } - if (obj.GetType() != this.GetType()) + if (obj.GetType() != GetType()) { return false; } - return Equals((TsNode)obj); + return Equals((Node)obj); } public override int GetHashCode() => _node.GetHashCode(); diff --git a/src/TreeSitterSharp/TsParser.cs b/src/TreeSitterSharp/Parser.cs similarity index 62% rename from src/TreeSitterSharp/TsParser.cs rename to src/TreeSitterSharp/Parser.cs index 04f7e7b..7bd3365 100644 --- a/src/TreeSitterSharp/TsParser.cs +++ b/src/TreeSitterSharp/Parser.cs @@ -1,22 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Text; -using System.Text.Unicode; -using System.Threading.Tasks; using TreeSitterSharp.Native; namespace TreeSitterSharp; -public unsafe class TsParser +public unsafe class Parser : INativeObject { - private Native.TsParser* _parser; - private TsLanguage? _language; + private TsParser* _parser; + private Language? _language; - public TsParser(TsLanguage language) + public Parser(Language language) { static void* NewMalloc(nuint byteCount) => NativeMemory.Alloc(byteCount); static void* NewCalloc(nuint count, nuint size) => NativeMemory.AllocZeroed(count * size); @@ -26,12 +20,15 @@ public TsParser(TsLanguage language) _parser = Ts.parser_new(); Language = language; } - ~TsParser() + + ~Parser() { Ts.parser_delete(_parser); } - public TsLanguage? Language + public TsParser* ToUnmanaged() => _parser; + + public Language? Language { get => _language; set @@ -44,23 +41,23 @@ public TsLanguage? Language } } - public TsTree Parse(string code) + public Tree Parse(string code) { if (_language is null) { throw new Exception("Language can't be null"); } - return new TsTree(Ts.parser_parse_string(_parser, null, code, (uint)code.Length)); + return new Tree(Ts.parser_parse_string(_parser, null, code, (uint)code.Length)); } - public TsTree Parse(Span code, Encoding encoding) + public Tree Parse(Span code, Encoding encoding) { if (_language is null) { throw new Exception("Language can't be null"); } byte[] bytes = Encoding.UTF8.GetBytes(encoding.GetString(code)); - return new TsTree(Ts.parser_parse_string_encoding(_parser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8)); + return new Tree(Ts.parser_parse_string_encoding(_parser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8)); } diff --git a/src/TreeSitterSharp/TSPoint.cs b/src/TreeSitterSharp/Point.cs similarity index 78% rename from src/TreeSitterSharp/TSPoint.cs rename to src/TreeSitterSharp/Point.cs index 7515b65..11b2157 100644 --- a/src/TreeSitterSharp/TSPoint.cs +++ b/src/TreeSitterSharp/Point.cs @@ -1,5 +1,5 @@ namespace TreeSitterSharp; -public struct TsPoint +public struct Point { public uint Row; diff --git a/src/TreeSitterSharp/Query.cs b/src/TreeSitterSharp/Query.cs new file mode 100644 index 0000000..57babc9 --- /dev/null +++ b/src/TreeSitterSharp/Query.cs @@ -0,0 +1,25 @@ +using System.Runtime.CompilerServices; +using TreeSitterSharp.Native; + +namespace TreeSitterSharp; +internal unsafe class Query : INativeObject +{ + private TsQuery* _query; + + + private Query() + { + + } + + TsQuery* INativeObject.ToUnmanaged() => _query; + + public static Query Create(Language language, string source, ref uint errorOffset, ref TsQueryError error) + { + return new Query + { + _query = Ts.query_new(language.ToUnmanaged(), source, (uint)source.Length, (uint*)Unsafe.AsPointer(ref errorOffset), + (TsQueryError*)Unsafe.AsPointer(ref error)) + }; + } +} diff --git a/src/TreeSitterSharp/QueryCursor.cs b/src/TreeSitterSharp/QueryCursor.cs new file mode 100644 index 0000000..27651aa --- /dev/null +++ b/src/TreeSitterSharp/QueryCursor.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using TreeSitterSharp.Native; + +namespace TreeSitterSharp; + +internal unsafe class QueryCursor +{ + private TsQueryCursor* _queryCursor; + + public QueryCursor() + { + _queryCursor = Ts.query_cursor_new(); + } + + public void Execute(Query query, Node node) + { + Ts.query_cursor_exec(_queryCursor, (query as INativeObject).ToUnmanaged(), node.ToUnmanaged()); + } + + public bool NextMatch([MaybeNullWhen(false)] out QueryMatch queryMatch) + { + TsQueryMatch match; + bool succeed = Ts.query_cursor_next_match(_queryCursor, &match); + queryMatch = succeed ? new QueryMatch(&match) : null; + return succeed; + } +} diff --git a/src/TreeSitterSharp/QueryMatch.cs b/src/TreeSitterSharp/QueryMatch.cs new file mode 100644 index 0000000..a75df63 --- /dev/null +++ b/src/TreeSitterSharp/QueryMatch.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TreeSitterSharp.Native; + +namespace TreeSitterSharp; +internal class QueryMatch : INativeObject +{ + private readonly unsafe TsQueryMatch* _queryMatch; + + public unsafe QueryMatch(TsQueryMatch* queryMatch) => _queryMatch = queryMatch; + + public unsafe TsQueryMatch* ToUnmanaged() => _queryMatch; +} diff --git a/src/TreeSitterSharp/StringMarshaller.cs b/src/TreeSitterSharp/StringMarshaller.cs index 92c4d8d..1ddfd67 100644 --- a/src/TreeSitterSharp/StringMarshaller.cs +++ b/src/TreeSitterSharp/StringMarshaller.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace TreeSitterSharp; diff --git a/src/TreeSitterSharp/Tree.cs b/src/TreeSitterSharp/Tree.cs new file mode 100644 index 0000000..d3fd100 --- /dev/null +++ b/src/TreeSitterSharp/Tree.cs @@ -0,0 +1,23 @@ +using TreeSitterSharp.Native; + +namespace TreeSitterSharp; +public unsafe class Tree +{ + private readonly TsTree* _tree; + + public Tree(TsTree* tree) + { + _tree = tree; + } + + ~Tree() + { + Ts.tree_delete(_tree); + } + + public Tree Copy() => new(Ts.tree_copy(_tree)); + + public Language Language => new(Ts.tree_language(_tree)); + + public Node Root => new(Ts.tree_root_node(_tree)); +} diff --git a/src/TreeSitterSharp/TsTreeCursor.cs b/src/TreeSitterSharp/TreeCursor.cs similarity index 69% rename from src/TreeSitterSharp/TsTreeCursor.cs rename to src/TreeSitterSharp/TreeCursor.cs index 254e5aa..78a1dcd 100644 --- a/src/TreeSitterSharp/TsTreeCursor.cs +++ b/src/TreeSitterSharp/TreeCursor.cs @@ -1,26 +1,26 @@ using TreeSitterSharp.Native; namespace TreeSitterSharp; -public class TsTreeCursor +public class TreeCursor { - private readonly Native.TsTreeCursor _treeCursor; - private readonly Native.TsNode _node; + private readonly TsTreeCursor _treeCursor; + private readonly TsNode _node; - public TsTreeCursor(TsNode node) + public TreeCursor(Node node) { _node = node.ToUnmanaged(); _treeCursor = Ts.tree_cursor_new(_node); } - private TsTreeCursor(Native.TsTreeCursor treeCursor) => _treeCursor = treeCursor; + private TreeCursor(TsTreeCursor treeCursor) => _treeCursor = treeCursor; - ~TsTreeCursor() + ~TreeCursor() { Ts.tree_cursor_delete(_treeCursor); } public uint CurrentDepth => Ts.tree_cursor_current_depth(in _treeCursor); - public TsNode CurrentNode => new(Ts.tree_cursor_current_node(in _treeCursor)); + public Node CurrentNode => new(Ts.tree_cursor_current_node(in _treeCursor)); public string CurrentFieldName => Ts.tree_cursor_current_field_name(in _treeCursor); public ushort CurrentFieldId => Ts.tree_cursor_current_field_id(in _treeCursor); public uint CurrentDescendantIndex => Ts.tree_cursor_current_descendant_index(in _treeCursor); @@ -28,7 +28,7 @@ public TsTreeCursor(TsNode node) public bool GotoParent() => Ts.tree_cursor_goto_parent(in _treeCursor); public bool GotoFirstChild() => Ts.tree_cursor_goto_first_child(in _treeCursor); public long GotoFirstChildForByte(uint goalByte) => Ts.tree_cursor_goto_first_child_for_byte(in _treeCursor, goalByte); - public long GotoFirstChildForPoint(TsPoint point) => Ts.tree_cursor_goto_first_child_for_point(in _treeCursor, point); + public long GotoFirstChildForPoint(Point point) => Ts.tree_cursor_goto_first_child_for_point(in _treeCursor, point); public bool GotoLastChild() => Ts.tree_cursor_goto_last_child(in _treeCursor); public bool GotoNextSibling() => Ts.tree_cursor_goto_next_sibling(in _treeCursor); public bool GotoPreviousSibling() => Ts.tree_cursor_goto_previous_sibling(in _treeCursor); @@ -39,13 +39,13 @@ public void Reset() Ts.tree_cursor_reset(_treeCursor, _node); } - public void ResetTo(TsTreeCursor dst) + public void ResetTo(TreeCursor dst) { Ts.tree_cursor_reset_to(dst._treeCursor, _treeCursor); } - public TsTreeCursor Copy() + public TreeCursor Copy() { - return new TsTreeCursor(Ts.tree_cursor_copy(_treeCursor)); + return new TreeCursor(Ts.tree_cursor_copy(_treeCursor)); } } diff --git a/src/TreeSitterSharp/TsLanguage.cs b/src/TreeSitterSharp/TsLanguage.cs deleted file mode 100644 index 019a07e..0000000 --- a/src/TreeSitterSharp/TsLanguage.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TreeSitterSharp.Native; - -namespace TreeSitterSharp; -public unsafe class TsLanguage -{ - private Native.TsLanguage* _language; - - public TsLanguage(Native.TsLanguage* language) - { - _language = language; - } - - public Native.TsLanguage* ToUnmanaged() - { - return _language; - } -} diff --git a/src/TreeSitterSharp/TsTree.cs b/src/TreeSitterSharp/TsTree.cs deleted file mode 100644 index ffac68b..0000000 --- a/src/TreeSitterSharp/TsTree.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TreeSitterSharp.Native; - -namespace TreeSitterSharp; -public unsafe class TsTree -{ - private readonly Native.TsTree* _tree; - - public TsTree(Native.TsTree* tree) - { - _tree = tree; - } - - ~TsTree() - { - Ts.tree_delete(_tree); - } - - public TsTree Copy() => new(Ts.tree_copy(_tree)); - - public TsLanguage Language => new(Ts.tree_language(_tree)); - - public TsNode Root => new(Ts.tree_root_node(_tree)); -} diff --git a/tests/TreeSitterSharp.C.Tests/CParserTests.cs b/tests/TreeSitterSharp.C.Tests/CParserTests.cs index 766437c..5604cd3 100644 --- a/tests/TreeSitterSharp.C.Tests/CParserTests.cs +++ b/tests/TreeSitterSharp.C.Tests/CParserTests.cs @@ -1,7 +1,3 @@ -using System.Runtime.CompilerServices; -using System.Xml.Linq; -using TreeSitterSharp.Native; - namespace TreeSitterSharp.C.Tests; public class CParserTests @@ -30,7 +26,7 @@ int fibonacci(int n) { var tree = parser.Parse(code); - var treeCursor = new TsTreeCursor(tree.Root); + var treeCursor = new TreeCursor(tree.Root); var a = treeCursor.GotoFirstChild(); Assert.Equal(expected, tree.Root.GetSExpression()); } diff --git a/tests/TreeSitterSharp.Json.Tests/JsonParserTests.cs b/tests/TreeSitterSharp.Json.Tests/JsonParserTests.cs index 02aff38..ca82015 100644 --- a/tests/TreeSitterSharp.Json.Tests/JsonParserTests.cs +++ b/tests/TreeSitterSharp.Json.Tests/JsonParserTests.cs @@ -1,6 +1,3 @@ -using System.Xml.Linq; -using TreeSitterSharp.Native; - namespace TreeSitterSharp.Json.Tests; public class JsonParserTests @@ -11,9 +8,9 @@ public void BasicParsing() var parser = new JsonParser(); string code = "[1, null]"; var tree = parser.Parse(code); - TsNode rootNode = tree.Root; - TsNode arrayNode = rootNode.GetNamedChild(0); - TsNode numberNode = arrayNode.GetNamedChild(0); + Node rootNode = tree.Root; + Node arrayNode = rootNode.GetNamedChild(0); + Node numberNode = arrayNode.GetNamedChild(0); Assert.Equal("document", rootNode.Type); Assert.Equal("array", arrayNode.Type); @@ -31,7 +28,7 @@ public void PrintTree() var parser = new JsonParser(); string code = "[1, null]"; var tree = parser.Parse(code); - TsNode rootNode = tree.Root; + Node rootNode = tree.Root; string expected = """ (document (array (number) (null)))