Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Summpot committed Nov 3, 2023
1 parent ca4480a commit 3f3d32c
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/TreeSitterSharp.C/CParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace TreeSitterSharp.C;
public class CParser : TreeSitterParser
public class CParser : Parser
{
public CParser() : base(CLanguageProvider.GetLanguage())
{
Expand Down
2 changes: 1 addition & 1 deletion src/TreeSitterSharp.Json/JsonParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

namespace TreeSitterSharp.Json;
public class JsonParser : TreeSitterParser
public class JsonParser : Parser
{
public JsonParser() : base(JsonLanguageProvider.GetLanguage())
{
Expand Down
2 changes: 1 addition & 1 deletion src/TreeSitterSharp/ConstantStringMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ internal static unsafe class ConstantStringMarshaller
/// <param name="unmanaged">The memory allocated for the unmanaged string.</param>
public static void Free(byte* unmanaged)
{

// We shouldn't free constant string.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using TreeSitterSharp.Native;

namespace TreeSitterSharp;
public unsafe class TreeSitterParser : INativeObject<TsParser>
public unsafe class Parser : INativeObject<TsParser>
{
private TsParser* _parser;
private Language? _language;



public TreeSitterParser(Language language)
public Parser(Language language)
{
static void* NewMalloc(nuint byteCount) => NativeMemory.Alloc(byteCount);
static void* NewCalloc(nuint count, nuint size) => NativeMemory.AllocZeroed(count * size);
Expand All @@ -21,7 +21,7 @@ public TreeSitterParser(Language language)
Language = language;
}

~TreeSitterParser()
~Parser()
{
Ts.parser_delete(_parser);
}
Expand All @@ -41,23 +41,23 @@ public Language? Language
}
}

public TreeSitterSyntaxTree Parse(string code)
public SyntaxTree Parse(string code)
{
if (_language is null)
{
throw new Exception("Language can't be null");
}
return new TreeSitterSyntaxTree(Ts.parser_parse_string(_parser, null, code, (uint)code.Length));
return new SyntaxTree(Ts.parser_parse_string(_parser, null, code, (uint)code.Length));
}

public TreeSitterSyntaxTree Parse(Span<byte> code, Encoding encoding)
public SyntaxTree Parse(Span<byte> 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 TreeSitterSyntaxTree(Ts.parser_parse_string_encoding(_parser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8));
return new SyntaxTree(Ts.parser_parse_string_encoding(_parser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

namespace TreeSitterSharp;

public unsafe class TreeSitterQueryCursor
public unsafe class QueryCursor
{
private TsQueryCursor* _queryCursor;

public TreeSitterQueryCursor()
public QueryCursor()
{
_queryCursor = Ts.query_cursor_new();
}

public void Execute(Query query, TreeSitterSyntaxNode node)
public void Execute(Query query, SyntaxNode node)
{
Ts.query_cursor_exec(_queryCursor, (query as INativeObject<TsQuery>).ToUnmanaged(), node.ToUnmanaged());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
using TreeSitterSharp.Native;

namespace TreeSitterSharp;
public unsafe class TreeSitterSyntaxNode
public unsafe class SyntaxNode
{
private readonly TsNode _node;

internal TreeSitterSyntaxNode(TsNode node)
internal SyntaxNode(TsNode node)
{
_node = node;
Tree = new TreeSitterSyntaxTree(_node.tree);
Tree = new SyntaxTree(_node.tree);
}

public TreeSitterSyntaxTree Tree { get; }
public SyntaxTree Tree { get; }

public nint Id => (nint)_node.id;

Expand All @@ -29,7 +29,7 @@ internal TreeSitterSyntaxNode(TsNode 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 TreeSitterSyntaxNode? PreviousSibling
public SyntaxNode? PreviousSibling
{
get
{
Expand All @@ -38,7 +38,7 @@ public TreeSitterSyntaxNode? PreviousSibling
}
}

public TreeSitterSyntaxNode? NextSibling
public SyntaxNode? NextSibling
{
get
{
Expand All @@ -47,7 +47,7 @@ public TreeSitterSyntaxNode? NextSibling
}
}

public TreeSitterSyntaxNode? PreviousNamedSibling
public SyntaxNode? PreviousNamedSibling
{
get
{
Expand All @@ -56,7 +56,7 @@ public TreeSitterSyntaxNode? PreviousNamedSibling
}
}

public TreeSitterSyntaxNode? NextNamedSibling
public SyntaxNode? NextNamedSibling
{
get
{
Expand All @@ -65,7 +65,7 @@ public TreeSitterSyntaxNode? NextNamedSibling
}
}

public TreeSitterSyntaxNode? Parent
public SyntaxNode? Parent
{
get
{
Expand All @@ -79,37 +79,37 @@ public TreeSitterSyntaxNode? Parent
public bool IsExtra => Ts.node_is_extra(_node);
public bool IsNull => Ts.node_is_null(_node);

public ImmutableArray<TreeSitterSyntaxNode> Children => GetChildren().ToImmutableArray();
public ImmutableArray<TreeSitterSyntaxNode> NamedChildren => GetNamedChildren().ToImmutableArray();
public ImmutableArray<SyntaxNode> Children => GetChildren().ToImmutableArray();
public ImmutableArray<SyntaxNode> NamedChildren => GetNamedChildren().ToImmutableArray();

public TreeSitterSyntaxNode GetChildByFieldName(string fieldName)
public SyntaxNode GetChildByFieldName(string fieldName)
{
return new(Ts.node_child_by_field_name(_node, fieldName, (uint)fieldName.Length));
}

public TreeSitterSyntaxNode GetChildByFieldId(ushort fieldId) => new(Ts.node_child_by_field_id(_node, fieldId));
public SyntaxNode 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 TreeSitterSyntaxNode GetNamedChild(uint index)
public SyntaxNode GetNamedChild(uint index)
{
return new(Ts.node_named_child(_node, index));
}

public TreeSitterSyntaxNode GetChild(uint index)
public SyntaxNode GetChild(uint index)
{
return new(Ts.node_child(_node, index));
}

private IEnumerable<TreeSitterSyntaxNode> GetNamedChildren()
private IEnumerable<SyntaxNode> GetNamedChildren()
{
for (uint i = 0; i < NamedChildCount; i++)
{
yield return GetNamedChild(i);
}
}

private IEnumerable<TreeSitterSyntaxNode> GetChildren()
private IEnumerable<SyntaxNode> GetChildren()
{
for (uint i = 0; i < ChildCount; i++)
{
Expand All @@ -127,7 +127,7 @@ public string GetSExpression()
return Ts.node_string(_node);
}

protected bool Equals(TreeSitterSyntaxNode other) => Ts.node_eq(_node, other._node);
protected bool Equals(SyntaxNode other) => Ts.node_eq(_node, other._node);

public override bool Equals(object? obj)
{
Expand All @@ -146,7 +146,7 @@ public override bool Equals(object? obj)
return false;
}

return Equals((TreeSitterSyntaxNode)obj);
return Equals((SyntaxNode)obj);
}

public override int GetHashCode() => _node.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
using TreeSitterSharp.Native;

namespace TreeSitterSharp;
public unsafe class TreeSitterSyntaxTree
public unsafe class SyntaxTree
{
private readonly TsTree* _tree;

public TreeSitterSyntaxTree(TsTree* tree)
public SyntaxTree(TsTree* tree)
{
_tree = tree;
}

~TreeSitterSyntaxTree()
~SyntaxTree()
{
Ts.tree_delete(_tree);
}

public TreeSitterSyntaxTree Copy() => new(Ts.tree_copy(_tree));
public SyntaxTree Copy() => new(Ts.tree_copy(_tree));

public Language Language => new(Ts.tree_language(_tree));

public TreeSitterSyntaxNode Root => new(Ts.tree_root_node(_tree));
public SyntaxNode Root => new(Ts.tree_root_node(_tree));
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
using TreeSitterSharp.Native;

namespace TreeSitterSharp;
public class TreeSitterSyntaxTreeCursor
public class SyntaxTreeCursor
{
private readonly TsTreeCursor _treeCursor;
private readonly TsNode _node;

public TreeSitterSyntaxTreeCursor(TreeSitterSyntaxNode node)
public SyntaxTreeCursor(SyntaxNode node)
{
_node = node.ToUnmanaged();
_treeCursor = Ts.tree_cursor_new(_node);
}

private TreeSitterSyntaxTreeCursor(TsTreeCursor treeCursor) => _treeCursor = treeCursor;
private SyntaxTreeCursor(TsTreeCursor treeCursor) => _treeCursor = treeCursor;

~TreeSitterSyntaxTreeCursor()
~SyntaxTreeCursor()
{
Ts.tree_cursor_delete(_treeCursor);
}

public uint CurrentDepth => Ts.tree_cursor_current_depth(in _treeCursor);
public TreeSitterSyntaxNode CurrentNode => new(Ts.tree_cursor_current_node(in _treeCursor));
public SyntaxNode 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);
Expand All @@ -39,13 +39,13 @@ public void Reset()
Ts.tree_cursor_reset(_treeCursor, _node);
}

public void ResetTo(TreeSitterSyntaxTreeCursor dst)
public void ResetTo(SyntaxTreeCursor dst)
{
Ts.tree_cursor_reset_to(dst._treeCursor, _treeCursor);
}

public TreeSitterSyntaxTreeCursor Copy()
public SyntaxTreeCursor Copy()
{
return new TreeSitterSyntaxTreeCursor(Ts.tree_cursor_copy(_treeCursor));
return new SyntaxTreeCursor(Ts.tree_cursor_copy(_treeCursor));
}
}
4 changes: 2 additions & 2 deletions tests/TreeSitterSharp.C.Tests/CParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int fibonacci(int n) {


var tree = parser.Parse(code);
var treeCursor = new TreeSitterSyntaxTreeCursor(tree.Root);
var treeCursor = new SyntaxTreeCursor(tree.Root);
var a = treeCursor.GotoFirstChild();
Assert.Equal(expected, tree.Root.GetSExpression());
}
Expand All @@ -51,7 +51,7 @@ int fibonacci(int n) {

var tree = parser.Parse(code);

var cursor = new TreeSitterQueryCursor();
var cursor = new QueryCursor();

cursor.Execute(Query.Create(tree.Language, "(preproc_include)", out var errorOffset, out var error), tree.Root);

Expand Down
8 changes: 4 additions & 4 deletions tests/TreeSitterSharp.Json.Tests/JsonParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public void BasicParsing()
var parser = new JsonParser();
string code = "[1, null]";
var tree = parser.Parse(code);
TreeSitterSyntaxNode rootNode = tree.Root;
TreeSitterSyntaxNode arrayNode = rootNode.GetNamedChild(0);
TreeSitterSyntaxNode numberNode = arrayNode.GetNamedChild(0);
SyntaxNode rootNode = tree.Root;
SyntaxNode arrayNode = rootNode.GetNamedChild(0);
SyntaxNode numberNode = arrayNode.GetNamedChild(0);

Assert.Equal("document", rootNode.Type);
Assert.Equal("array", arrayNode.Type);
Expand All @@ -28,7 +28,7 @@ public void PrintTree()
var parser = new JsonParser();
string code = "[1, null]";
var tree = parser.Parse(code);
TreeSitterSyntaxNode rootNode = tree.Root;
SyntaxNode rootNode = tree.Root;

string expected = """
(document (array (number) (null)))
Expand Down

0 comments on commit 3f3d32c

Please sign in to comment.