Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Summpot committed Oct 28, 2023
1 parent 83c2ed2 commit 2a58079
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<PackageVersion Include="libtree-sitter-json" Version="0.4.1" />
<PackageVersion Include="libtree-sitter-lua" Version="0.4.1" />
<PackageVersion Include="GitVersion.MsBuild" Version="5.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageVersion Include="xunit" Version="2.5.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="xunit" Version="2.5.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>
</Project>
132 changes: 132 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# API

- [X] ts_parser_new
- [x] ts_parser_delete
- [x] ts_parser_language
- [x] ts_parser_set_language
- [ ] ts_parser_set_included_ranges
- [ ] ts_parser_included_ranges
- [ ] ts_parser_parse
- [x] ts_parser_parse_string
- [x] ts_parser_parse_string_encoding
- [ ] ts_parser_reset
- [ ] ts_parser_set_timeout_micros
- [ ] ts_parser_timeout_micros
- [ ] ts_parser_set_cancellation_flag
- [ ] ts_parser_cancellation_flag
- [ ] ts_parser_set_logger
- [ ] ts_parser_logger
- [ ] ts_parser_print_dot_graphs
- [ ] ts_tree_copy
- [x] ts_tree_delete
- [x] ts_tree_root_node
- [ ] ts_tree_root_node_with_offset
- [x] ts_tree_language
- [ ] ts_tree_included_ranges
- [ ] ts_tree_edit
- [ ] ts_tree_get_changed_ranges
- [ ] ts_tree_print_dot_graph
- [x] ts_node_type
- [ ] ts_node_symbol
- [ ] ts_node_language
- [ ] ts_node_grammar_type
- [ ] ts_node_grammar_symbol
- [ ] ts_node_start_byte
- [ ] ts_node_start_point
- [ ] ts_node_end_byte
- [ ] ts_node_end_point
- [x] ts_node_string
- [x] ts_node_is_null
- [x] ts_node_is_named
- [x] ts_node_is_missing
- [x] ts_node_is_extra
- [ ] ts_node_has_changes
- [ ] ts_node_has_error
- [ ] ts_node_is_error
- [ ] ts_node_parse_state
- [ ] ts_node_next_parse_state
- [x] ts_node_parent
- [x] ts_node_child
- [ ] ts_node_field_name_for_child
- [x] ts_node_child_count
- [x] ts_node_named_child
- [x] ts_node_named_child_count
- [x] ts_node_child_by_field_name
- [ ] ts_node_child_by_field_id
- [x] ts_node_next_sibling
- [x] ts_node_prev_sibling
- [x] ts_node_next_named_sibling
- [x] ts_node_prev_named_sibling
- [ ] ts_node_first_child_for_byte
- [ ] ts_node_first_named_child_for_byte
- [ ] ts_node_descendant_count
- [ ] ts_node_descendant_for_byte_range
- [ ] ts_node_descendant_for_point_range
- [ ] ts_node_named_descendant_for_byte_range
- [ ] ts_node_named_descendant_for_point_range
- [ ] ts_node_edit
- [ ] ts_node_eq
- [ ] ts_tree_cursor_new
- [ ] ts_tree_cursor_delete
- [ ] ts_tree_cursor_reset
- [ ] ts_tree_cursor_reset_to
- [ ] ts_tree_cursor_current_node
- [ ] ts_tree_cursor_current_field_name
- [ ] ts_tree_cursor_current_field_id
- [ ] ts_tree_cursor_goto_parent
- [ ] ts_tree_cursor_goto_next_sibling
- [ ] ts_tree_cursor_goto_previous_sibling
- [ ] ts_tree_cursor_goto_first_child
- [ ] ts_tree_cursor_goto_last_child
- [ ] ts_tree_cursor_goto_descendant
- [ ] ts_tree_cursor_current_descendant_index
- [ ] ts_tree_cursor_current_depth
- [ ] ts_tree_cursor_goto_first_child_for_byte
- [ ] ts_tree_cursor_goto_first_child_for_point
- [ ] ts_tree_cursor_copy
- [ ] ts_query_new
- [ ] ts_query_delete
- [ ] ts_query_pattern_count
- [ ] ts_query_capture_count
- [ ] ts_query_string_count
- [ ] ts_query_start_byte_for_pattern
- [ ] ts_query_predicates_for_pattern
- [ ] ts_query_is_pattern_rooted
- [ ] ts_query_is_pattern_non_local
- [ ] ts_query_is_pattern_guaranteed_at_step
- [ ] ts_query_capture_name_for_id
- [ ] ts_query_capture_quantifier_for_id
- [ ] ts_query_string_value_for_id
- [ ] ts_query_disable_capture
- [ ] ts_query_disable_pattern
- [ ] ts_query_cursor_new
- [ ] ts_query_cursor_delete
- [ ] ts_query_cursor_exec
- [ ] ts_query_cursor_did_exceed_match_limit
- [ ] ts_query_cursor_match_limit
- [ ] ts_query_cursor_set_match_limit
- [ ] ts_query_cursor_set_byte_range
- [ ] ts_query_cursor_set_point_range
- [ ] ts_query_cursor_next_match
- [ ] ts_query_cursor_remove_match
- [ ] ts_query_cursor_next_capture
- [ ] ts_query_cursor_set_max_start_depth
- [ ] ts_language_symbol_count
- [ ] ts_language_state_count
- [ ] ts_language_symbol_name
- [ ] ts_language_symbol_for_name
- [ ] ts_language_field_count
- [ ] ts_language_field_name_for_id
- [ ] ts_language_field_id_for_name
- [ ] ts_language_symbol_type
- [ ] ts_language_version
- [ ] ts_language_next_state
- [ ] ts_lookahead_iterator_new
- [ ] ts_lookahead_iterator_delete
- [ ] ts_lookahead_iterator_reset_state
- [ ] ts_lookahead_iterator_reset
- [ ] ts_lookahead_iterator_language
- [ ] ts_lookahead_iterator_next
- [ ] ts_lookahead_iterator_current_symbol
- [ ] ts_lookahead_iterator_current_symbol_name
- [ ] ts_set_allocator
9 changes: 1 addition & 8 deletions src/TreeSitterSharp/ConstantStringMarshaller.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;
using System.Threading.Tasks;

namespace TreeSitterSharp;

[CLSCompliant(false)]
[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(ConstantStringMarshaller))]
internal static unsafe class ConstantStringMarshaller
{
Expand Down
11 changes: 3 additions & 8 deletions src/TreeSitterSharp/Native/Ts.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace TreeSitterSharp.Native;

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

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

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

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

[DllImport("libtree-sitter", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ts_parser_timeout_micros",
Expand Down
3 changes: 3 additions & 0 deletions src/TreeSitterSharp/Native/TsLanguage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Runtime.InteropServices;

namespace TreeSitterSharp.Native;

[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct TsLanguage
{
[NativeTypeName("uint32_t")]
Expand Down
40 changes: 28 additions & 12 deletions src/TreeSitterSharp/Node.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -10,35 +11,50 @@ public unsafe struct Node
{
private TsNode _internalNode;

internal Node(TsNode internalNode) => _internalNode = internalNode;

public readonly string Type => Ts.node_type(_internalNode);
public readonly uint ChildCount => Ts.node_child_count(_internalNode);
public readonly uint NamedChildCount => Ts.node_named_child_count(_internalNode);
public readonly Node PreviousSibling => FromUnmanaged(Ts.node_prev_sibling(_internalNode));
public readonly Node NextSibling => FromUnmanaged(Ts.node_next_sibling(_internalNode));
public readonly Node PreviousNamedSibling => FromUnmanaged(Ts.node_prev_named_sibling(_internalNode));
public readonly Node NextNamedSibling => FromUnmanaged(Ts.node_next_named_sibling(_internalNode));
public readonly Node Parent => FromUnmanaged(Ts.node_parent(_internalNode));
public readonly Node PreviousSibling => new(Ts.node_prev_sibling(_internalNode));
public readonly Node NextSibling => new(Ts.node_next_sibling(_internalNode));
public readonly Node PreviousNamedSibling => new(Ts.node_prev_named_sibling(_internalNode));
public readonly Node NextNamedSibling => new(Ts.node_next_named_sibling(_internalNode));
public readonly Node Parent => new(Ts.node_parent(_internalNode));
public readonly bool IsNull => Ts.node_is_null(_internalNode);
public readonly bool IsNamed => Ts.node_is_named(_internalNode);
public readonly bool IsMissing => Ts.node_is_missing(_internalNode);
public readonly bool IsExtra => Ts.node_is_extra(_internalNode);

public readonly Node GetNamedChild(uint index)
{
return FromUnmanaged(Ts.node_named_child(_internalNode, index));
return new Node(Ts.node_named_child(_internalNode, index));
}

public readonly Node GetChildByFieldName(string fieldName)
public readonly IEnumerable<Node> GetNamedChildren()
{
return FromUnmanaged(Ts.node_child_by_field_name(_internalNode, fieldName, (uint)fieldName.Length));
for (uint i = 0; i < NamedChildCount; i++)
{
yield return GetNamedChild(i);
}
}

public readonly Node GetChild(uint index)
public readonly IEnumerable<Node> GetChildren()
{
for (uint i = 0; i < ChildCount; i++)
{
yield return GetChild(i);
}
}

public readonly Node GetChildByFieldName(string fieldName)
{
return FromUnmanaged(Ts.node_child(_internalNode, index));
return new Node(Ts.node_child_by_field_name(_internalNode, fieldName, (uint)fieldName.Length));
}

public static Node FromUnmanaged(TsNode node)
public readonly Node GetChild(uint index)
{
return new Node() { _internalNode = node };
return new Node(Ts.node_child(_internalNode, index));
}

public readonly TsNode ToUnmanaged()
Expand Down
35 changes: 30 additions & 5 deletions src/TreeSitterSharp/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Unicode;
using System.Threading.Tasks;
using TreeSitterSharp.Native;

namespace TreeSitterSharp;
public unsafe class Parser
{
private TsParser* _internalParser;
private Language? _language;

public Language? Language
{
get => _language;
set
{
_language = value;
if (_language is not null)
{
Ts.parser_set_language(_internalParser, _language.ToUnmanaged());
}
}
}

public static Parser Create(Language? language = null)
{
static void* NewMalloc(nuint byteCount) => NativeMemory.Alloc(byteCount);
Expand All @@ -21,7 +37,7 @@ public static Parser Create(Language? language = null)
var parser = new Parser() { _internalParser = Ts.parser_new() };
if (language is not null)
{
parser.SetLanguage(language);
parser.Language = language;
}
return parser;
}
Expand All @@ -30,14 +46,23 @@ public static Parser Create(Language? language = null)
Ts.parser_delete(_internalParser);
}

public void SetLanguage(Language language)
public Tree Parse(string code)
{
Ts.parser_set_language(_internalParser, language.ToUnmanaged());
if (_language is null)
{
throw new Exception("Language can't be null");
}
return new Tree(Ts.parser_parse_string(_internalParser, null, code, (uint)code.Length), _language);
}

public Tree Parse(string code)
public Tree Parse(Span<byte> code, Encoding encoding)
{
return Tree.FromNative(Ts.parser_parse_string(_internalParser, null, code, (uint)code.Length));
if (_language is null)
{
throw new Exception("Language can't be null");
}
byte[] bytes = Encoding.UTF8.GetBytes(encoding.GetString(code));
return new Tree(Ts.parser_parse_string_encoding(_internalParser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8), _language);
}


Expand Down
2 changes: 1 addition & 1 deletion src/TreeSitterSharp/StringMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading.Tasks;

namespace TreeSitterSharp;
[CLSCompliant(false)]

[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(StringMarshaller))]
internal static unsafe class StringMarshaller
{
Expand Down
20 changes: 16 additions & 4 deletions src/TreeSitterSharp/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@
namespace TreeSitterSharp;
public unsafe class Tree
{
private TsTree* _internalTree;
public static Tree FromNative(TsTree* tree)
private readonly TsTree* _internalTree;

~Tree()
{
Ts.tree_delete(_internalTree);
}
public Tree(TsTree* tree)
{
_internalTree = tree;
}

public Language Language { get; }

public Tree(TsTree* tree, Language language) : this(tree)
{
return new Tree() { _internalTree = tree };
Language = language;
}

public Node Root => Node.FromUnmanaged(Ts.tree_root_node(_internalTree));
public Node Root => new(Ts.tree_root_node(_internalTree));
}

0 comments on commit 2a58079

Please sign in to comment.