Skip to content

Commit 2a58079

Browse files
committed
Update
1 parent 83c2ed2 commit 2a58079

File tree

9 files changed

+217
-41
lines changed

9 files changed

+217
-41
lines changed

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<PackageVersion Include="libtree-sitter-json" Version="0.4.1" />
1010
<PackageVersion Include="libtree-sitter-lua" Version="0.4.1" />
1111
<PackageVersion Include="GitVersion.MsBuild" Version="5.12.0" />
12-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
13-
<PackageVersion Include="xunit" Version="2.5.0" />
14-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
12+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
13+
<PackageVersion Include="xunit" Version="2.5.3" />
14+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
1515
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
1616
</ItemGroup>
1717
</Project>

api.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# API
2+
3+
- [X] ts_parser_new
4+
- [x] ts_parser_delete
5+
- [x] ts_parser_language
6+
- [x] ts_parser_set_language
7+
- [ ] ts_parser_set_included_ranges
8+
- [ ] ts_parser_included_ranges
9+
- [ ] ts_parser_parse
10+
- [x] ts_parser_parse_string
11+
- [x] ts_parser_parse_string_encoding
12+
- [ ] ts_parser_reset
13+
- [ ] ts_parser_set_timeout_micros
14+
- [ ] ts_parser_timeout_micros
15+
- [ ] ts_parser_set_cancellation_flag
16+
- [ ] ts_parser_cancellation_flag
17+
- [ ] ts_parser_set_logger
18+
- [ ] ts_parser_logger
19+
- [ ] ts_parser_print_dot_graphs
20+
- [ ] ts_tree_copy
21+
- [x] ts_tree_delete
22+
- [x] ts_tree_root_node
23+
- [ ] ts_tree_root_node_with_offset
24+
- [x] ts_tree_language
25+
- [ ] ts_tree_included_ranges
26+
- [ ] ts_tree_edit
27+
- [ ] ts_tree_get_changed_ranges
28+
- [ ] ts_tree_print_dot_graph
29+
- [x] ts_node_type
30+
- [ ] ts_node_symbol
31+
- [ ] ts_node_language
32+
- [ ] ts_node_grammar_type
33+
- [ ] ts_node_grammar_symbol
34+
- [ ] ts_node_start_byte
35+
- [ ] ts_node_start_point
36+
- [ ] ts_node_end_byte
37+
- [ ] ts_node_end_point
38+
- [x] ts_node_string
39+
- [x] ts_node_is_null
40+
- [x] ts_node_is_named
41+
- [x] ts_node_is_missing
42+
- [x] ts_node_is_extra
43+
- [ ] ts_node_has_changes
44+
- [ ] ts_node_has_error
45+
- [ ] ts_node_is_error
46+
- [ ] ts_node_parse_state
47+
- [ ] ts_node_next_parse_state
48+
- [x] ts_node_parent
49+
- [x] ts_node_child
50+
- [ ] ts_node_field_name_for_child
51+
- [x] ts_node_child_count
52+
- [x] ts_node_named_child
53+
- [x] ts_node_named_child_count
54+
- [x] ts_node_child_by_field_name
55+
- [ ] ts_node_child_by_field_id
56+
- [x] ts_node_next_sibling
57+
- [x] ts_node_prev_sibling
58+
- [x] ts_node_next_named_sibling
59+
- [x] ts_node_prev_named_sibling
60+
- [ ] ts_node_first_child_for_byte
61+
- [ ] ts_node_first_named_child_for_byte
62+
- [ ] ts_node_descendant_count
63+
- [ ] ts_node_descendant_for_byte_range
64+
- [ ] ts_node_descendant_for_point_range
65+
- [ ] ts_node_named_descendant_for_byte_range
66+
- [ ] ts_node_named_descendant_for_point_range
67+
- [ ] ts_node_edit
68+
- [ ] ts_node_eq
69+
- [ ] ts_tree_cursor_new
70+
- [ ] ts_tree_cursor_delete
71+
- [ ] ts_tree_cursor_reset
72+
- [ ] ts_tree_cursor_reset_to
73+
- [ ] ts_tree_cursor_current_node
74+
- [ ] ts_tree_cursor_current_field_name
75+
- [ ] ts_tree_cursor_current_field_id
76+
- [ ] ts_tree_cursor_goto_parent
77+
- [ ] ts_tree_cursor_goto_next_sibling
78+
- [ ] ts_tree_cursor_goto_previous_sibling
79+
- [ ] ts_tree_cursor_goto_first_child
80+
- [ ] ts_tree_cursor_goto_last_child
81+
- [ ] ts_tree_cursor_goto_descendant
82+
- [ ] ts_tree_cursor_current_descendant_index
83+
- [ ] ts_tree_cursor_current_depth
84+
- [ ] ts_tree_cursor_goto_first_child_for_byte
85+
- [ ] ts_tree_cursor_goto_first_child_for_point
86+
- [ ] ts_tree_cursor_copy
87+
- [ ] ts_query_new
88+
- [ ] ts_query_delete
89+
- [ ] ts_query_pattern_count
90+
- [ ] ts_query_capture_count
91+
- [ ] ts_query_string_count
92+
- [ ] ts_query_start_byte_for_pattern
93+
- [ ] ts_query_predicates_for_pattern
94+
- [ ] ts_query_is_pattern_rooted
95+
- [ ] ts_query_is_pattern_non_local
96+
- [ ] ts_query_is_pattern_guaranteed_at_step
97+
- [ ] ts_query_capture_name_for_id
98+
- [ ] ts_query_capture_quantifier_for_id
99+
- [ ] ts_query_string_value_for_id
100+
- [ ] ts_query_disable_capture
101+
- [ ] ts_query_disable_pattern
102+
- [ ] ts_query_cursor_new
103+
- [ ] ts_query_cursor_delete
104+
- [ ] ts_query_cursor_exec
105+
- [ ] ts_query_cursor_did_exceed_match_limit
106+
- [ ] ts_query_cursor_match_limit
107+
- [ ] ts_query_cursor_set_match_limit
108+
- [ ] ts_query_cursor_set_byte_range
109+
- [ ] ts_query_cursor_set_point_range
110+
- [ ] ts_query_cursor_next_match
111+
- [ ] ts_query_cursor_remove_match
112+
- [ ] ts_query_cursor_next_capture
113+
- [ ] ts_query_cursor_set_max_start_depth
114+
- [ ] ts_language_symbol_count
115+
- [ ] ts_language_state_count
116+
- [ ] ts_language_symbol_name
117+
- [ ] ts_language_symbol_for_name
118+
- [ ] ts_language_field_count
119+
- [ ] ts_language_field_name_for_id
120+
- [ ] ts_language_field_id_for_name
121+
- [ ] ts_language_symbol_type
122+
- [ ] ts_language_version
123+
- [ ] ts_language_next_state
124+
- [ ] ts_lookahead_iterator_new
125+
- [ ] ts_lookahead_iterator_delete
126+
- [ ] ts_lookahead_iterator_reset_state
127+
- [ ] ts_lookahead_iterator_reset
128+
- [ ] ts_lookahead_iterator_language
129+
- [ ] ts_lookahead_iterator_next
130+
- [ ] ts_lookahead_iterator_current_symbol
131+
- [ ] ts_lookahead_iterator_current_symbol_name
132+
- [ ] ts_set_allocator

src/TreeSitterSharp/ConstantStringMarshaller.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Runtime.CompilerServices;
5-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
62
using System.Runtime.InteropServices.Marshalling;
7-
using System.Text;
8-
using System.Threading.Tasks;
93

104
namespace TreeSitterSharp;
115

12-
[CLSCompliant(false)]
136
[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(ConstantStringMarshaller))]
147
internal static unsafe class ConstantStringMarshaller
158
{

src/TreeSitterSharp/Native/Ts.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Linq.Expressions;
21
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
4-
using System.Text;
53

64
namespace TreeSitterSharp.Native;
75

@@ -46,19 +44,16 @@ public static extern byte parser_set_included_ranges(TsParser* self,
4644
ExactSpelling = true)]
4745
public static extern TsTree* parser_parse_string(TsParser* self, TsTree* old_tree, string code, uint length);
4846

49-
[DllImport("libtree-sitter", CallingConvention = CallingConvention.Cdecl,
50-
EntryPoint = "ts_parser_parse_string_encoding", ExactSpelling = true)]
51-
public static extern TsTree* parser_parse_string_encoding(TsParser* self,
52-
[NativeTypeName("const TsTree *")] TsTree* old_tree, [NativeTypeName("const char *")] string @string,
53-
[NativeTypeName("uint32_t")] uint length, TsInputEncoding encoding);
47+
[LibraryImport("libtree-sitter", EntryPoint = "ts_parser_parse_string_encoding")]
48+
internal static partial TsTree* parser_parse_string_encoding(TsParser* self, TsTree* oldTree, byte[] code, uint length, TsInputEncoding encoding);
5449

5550
[DllImport("libtree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_reset",
5651
ExactSpelling = true)]
5752
public static extern void parser_reset(TsParser* self);
5853

5954
[DllImport("libtree-sitter", CallingConvention = CallingConvention.Cdecl,
6055
EntryPoint = "ts_parser_set_timeout_micros", ExactSpelling = true)]
61-
public static extern void parser_set_timeout_micros(TsParser* self,
56+
public static extern void parser_set_timeout_micros(TsParser* self,
6257
[NativeTypeName("uint64_t")] ulong timeout_micros);
6358

6459
[DllImport("libtree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_timeout_micros",

src/TreeSitterSharp/Native/TsLanguage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System.Runtime.InteropServices;
2+
13
namespace TreeSitterSharp.Native;
24

5+
[StructLayout(LayoutKind.Sequential)]
36
public unsafe partial struct TsLanguage
47
{
58
[NativeTypeName("uint32_t")]

src/TreeSitterSharp/Node.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -10,35 +11,50 @@ public unsafe struct Node
1011
{
1112
private TsNode _internalNode;
1213

14+
internal Node(TsNode internalNode) => _internalNode = internalNode;
15+
1316
public readonly string Type => Ts.node_type(_internalNode);
1417
public readonly uint ChildCount => Ts.node_child_count(_internalNode);
1518
public readonly uint NamedChildCount => Ts.node_named_child_count(_internalNode);
16-
public readonly Node PreviousSibling => FromUnmanaged(Ts.node_prev_sibling(_internalNode));
17-
public readonly Node NextSibling => FromUnmanaged(Ts.node_next_sibling(_internalNode));
18-
public readonly Node PreviousNamedSibling => FromUnmanaged(Ts.node_prev_named_sibling(_internalNode));
19-
public readonly Node NextNamedSibling => FromUnmanaged(Ts.node_next_named_sibling(_internalNode));
20-
public readonly Node Parent => FromUnmanaged(Ts.node_parent(_internalNode));
19+
public readonly Node PreviousSibling => new(Ts.node_prev_sibling(_internalNode));
20+
public readonly Node NextSibling => new(Ts.node_next_sibling(_internalNode));
21+
public readonly Node PreviousNamedSibling => new(Ts.node_prev_named_sibling(_internalNode));
22+
public readonly Node NextNamedSibling => new(Ts.node_next_named_sibling(_internalNode));
23+
public readonly Node Parent => new(Ts.node_parent(_internalNode));
2124
public readonly bool IsNull => Ts.node_is_null(_internalNode);
2225
public readonly bool IsNamed => Ts.node_is_named(_internalNode);
26+
public readonly bool IsMissing => Ts.node_is_missing(_internalNode);
27+
public readonly bool IsExtra => Ts.node_is_extra(_internalNode);
2328

2429
public readonly Node GetNamedChild(uint index)
2530
{
26-
return FromUnmanaged(Ts.node_named_child(_internalNode, index));
31+
return new Node(Ts.node_named_child(_internalNode, index));
2732
}
2833

29-
public readonly Node GetChildByFieldName(string fieldName)
34+
public readonly IEnumerable<Node> GetNamedChildren()
3035
{
31-
return FromUnmanaged(Ts.node_child_by_field_name(_internalNode, fieldName, (uint)fieldName.Length));
36+
for (uint i = 0; i < NamedChildCount; i++)
37+
{
38+
yield return GetNamedChild(i);
39+
}
3240
}
3341

34-
public readonly Node GetChild(uint index)
42+
public readonly IEnumerable<Node> GetChildren()
43+
{
44+
for (uint i = 0; i < ChildCount; i++)
45+
{
46+
yield return GetChild(i);
47+
}
48+
}
49+
50+
public readonly Node GetChildByFieldName(string fieldName)
3551
{
36-
return FromUnmanaged(Ts.node_child(_internalNode, index));
52+
return new Node(Ts.node_child_by_field_name(_internalNode, fieldName, (uint)fieldName.Length));
3753
}
3854

39-
public static Node FromUnmanaged(TsNode node)
55+
public readonly Node GetChild(uint index)
4056
{
41-
return new Node() { _internalNode = node };
57+
return new Node(Ts.node_child(_internalNode, index));
4258
}
4359

4460
public readonly TsNode ToUnmanaged()

src/TreeSitterSharp/Parser.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@
44
using System.Runtime.CompilerServices;
55
using System.Runtime.InteropServices;
66
using System.Text;
7+
using System.Text.Unicode;
78
using System.Threading.Tasks;
89
using TreeSitterSharp.Native;
910

1011
namespace TreeSitterSharp;
1112
public unsafe class Parser
1213
{
1314
private TsParser* _internalParser;
15+
private Language? _language;
16+
17+
public Language? Language
18+
{
19+
get => _language;
20+
set
21+
{
22+
_language = value;
23+
if (_language is not null)
24+
{
25+
Ts.parser_set_language(_internalParser, _language.ToUnmanaged());
26+
}
27+
}
28+
}
29+
1430
public static Parser Create(Language? language = null)
1531
{
1632
static void* NewMalloc(nuint byteCount) => NativeMemory.Alloc(byteCount);
@@ -21,7 +37,7 @@ public static Parser Create(Language? language = null)
2137
var parser = new Parser() { _internalParser = Ts.parser_new() };
2238
if (language is not null)
2339
{
24-
parser.SetLanguage(language);
40+
parser.Language = language;
2541
}
2642
return parser;
2743
}
@@ -30,14 +46,23 @@ public static Parser Create(Language? language = null)
3046
Ts.parser_delete(_internalParser);
3147
}
3248

33-
public void SetLanguage(Language language)
49+
public Tree Parse(string code)
3450
{
35-
Ts.parser_set_language(_internalParser, language.ToUnmanaged());
51+
if (_language is null)
52+
{
53+
throw new Exception("Language can't be null");
54+
}
55+
return new Tree(Ts.parser_parse_string(_internalParser, null, code, (uint)code.Length), _language);
3656
}
3757

38-
public Tree Parse(string code)
58+
public Tree Parse(Span<byte> code, Encoding encoding)
3959
{
40-
return Tree.FromNative(Ts.parser_parse_string(_internalParser, null, code, (uint)code.Length));
60+
if (_language is null)
61+
{
62+
throw new Exception("Language can't be null");
63+
}
64+
byte[] bytes = Encoding.UTF8.GetBytes(encoding.GetString(code));
65+
return new Tree(Ts.parser_parse_string_encoding(_internalParser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8), _language);
4166
}
4267

4368

src/TreeSitterSharp/StringMarshaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Threading.Tasks;
88

99
namespace TreeSitterSharp;
10-
[CLSCompliant(false)]
10+
1111
[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(StringMarshaller))]
1212
internal static unsafe class StringMarshaller
1313
{

src/TreeSitterSharp/Tree.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@
88
namespace TreeSitterSharp;
99
public unsafe class Tree
1010
{
11-
private TsTree* _internalTree;
12-
public static Tree FromNative(TsTree* tree)
11+
private readonly TsTree* _internalTree;
12+
13+
~Tree()
14+
{
15+
Ts.tree_delete(_internalTree);
16+
}
17+
public Tree(TsTree* tree)
18+
{
19+
_internalTree = tree;
20+
}
21+
22+
public Language Language { get; }
23+
24+
public Tree(TsTree* tree, Language language) : this(tree)
1325
{
14-
return new Tree() { _internalTree = tree };
26+
Language = language;
1527
}
1628

17-
public Node Root => Node.FromUnmanaged(Ts.tree_root_node(_internalTree));
29+
public Node Root => new(Ts.tree_root_node(_internalTree));
1830
}

0 commit comments

Comments
 (0)