Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Summpot committed Nov 5, 2023
1 parent 2a0ab53 commit 3841937
Show file tree
Hide file tree
Showing 27 changed files with 373 additions and 251 deletions.
4 changes: 3 additions & 1 deletion src/TreeSitterSharp.C/CLanguageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Runtime.InteropServices;
using TreeSitterSharp.Native;
using TreeSitterSharp;

namespace TreeSitterSharp.C;

[LanguageName("C")]
internal class CLanguageProvider : ILanguageProvider
{
[DllImport("tree-sitter-c", EntryPoint = "tree_sitter_c", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern unsafe TsLanguage* tree_sitter_c();

public static unsafe Language GetLanguage()
{
return new Language(tree_sitter_c());
Expand Down
13 changes: 2 additions & 11 deletions src/TreeSitterSharp.C/CParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@

namespace TreeSitterSharp.C;

public unsafe class CParser : Parser
public unsafe class CParser : Parser<CSyntaxTree, CSyntaxNode, CParser>
{
public CParser() : base(CLanguageProvider.GetLanguage())
{

}

public override CSyntaxTree Parse(string code)
{
return new CSyntaxTree(Ts.parser_parse_string(_parser, null, code, (uint)code.Length));
}

public override CSyntaxTree Parse(Span<byte> code, Encoding encoding)
{
byte[] bytes = Encoding.UTF8.GetBytes(encoding.GetString(code));
return new CSyntaxTree(Ts.parser_parse_string_encoding(_parser, null, bytes, (uint)bytes.Length, TsInputEncoding.TSInputEncodingUTF8));
}
public override CSyntaxTree Parse(Span<byte> code) => new(ParseCore(code));
}
6 changes: 4 additions & 2 deletions src/TreeSitterSharp.C/CSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
using TreeSitterSharp.Native;

namespace TreeSitterSharp.C;
public class CSyntaxNode : SyntaxNode

public partial class CSyntaxNode : SyntaxNode<CSyntaxTree, CSyntaxNode>, ISyntaxNodeCreation<CSyntaxTree, CSyntaxNode>
{
protected internal CSyntaxNode(TsNode node) : base(node)
{
}



public static partial CSyntaxNode Create(TsNode node);
}
7 changes: 5 additions & 2 deletions src/TreeSitterSharp.C/CSyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using TreeSitterSharp.Native;

namespace TreeSitterSharp.C;
public unsafe class CSyntaxTree : SyntaxTree
public unsafe class CSyntaxTree : SyntaxTree<CSyntaxNode, CSyntaxTree>, ISyntaxTreeCreation<CSyntaxNode, CSyntaxTree>
{
protected internal CSyntaxTree(TsTree* tree) : base(tree)
{
}
public TranslationUnit TranslationUnit => new(Ts.tree_root_node(_tree));
public override SyntaxNode Root => TranslationUnit;
public override CSyntaxNode Root => TranslationUnit;
public override CSyntaxTree Copy() => new(Ts.tree_copy(_tree));
public static CSyntaxTree Create(TsTree* tree) => new(tree);
}
3 changes: 2 additions & 1 deletion src/TreeSitterSharp.Json/JsonLanguageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Runtime.InteropServices;
using TreeSitterSharp.Native;
using TreeSitterSharp;

namespace TreeSitterSharp.Json
{
internal unsafe class JsonLanguageProvider : ILanguageProvider
{
[DllImport("tree-sitter-json", EntryPoint = "tree_sitter_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern TsLanguage* tree_sitter_json();

public static Language GetLanguage() => new(tree_sitter_json());
}
}
19 changes: 18 additions & 1 deletion src/TreeSitterSharp.Json/JsonParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@

using System.Text;

namespace TreeSitterSharp.Json;
public class JsonParser() : Parser(JsonLanguageProvider.GetLanguage());

public class JsonParser : Parser<JsonSyntaxTree, JsonSyntaxNode, JsonParser>
{
public JsonParser() : base(JsonLanguageProvider.GetLanguage())
{

}

public override JsonSyntaxTree Parse(Span<byte> code)
{
unsafe
{
return new(ParseCore(code));
}
}
}
16 changes: 16 additions & 0 deletions src/TreeSitterSharp.Json/JsonSyntaxNode.cs
Original file line number Diff line number Diff line change
@@ -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.Json;
public class JsonSyntaxNode : SyntaxNode<JsonSyntaxTree, JsonSyntaxNode>, ISyntaxNodeCreation<JsonSyntaxTree, JsonSyntaxNode>
{
protected internal JsonSyntaxNode(TsNode node) : base(node)
{
}

public static JsonSyntaxNode Create(TsNode node) => new(node);
}
25 changes: 25 additions & 0 deletions src/TreeSitterSharp.Json/JsonSyntaxTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TreeSitterSharp.Native;

namespace TreeSitterSharp.Json;
public class JsonSyntaxTree : SyntaxTree<JsonSyntaxNode, JsonSyntaxTree>, ISyntaxTreeCreation<JsonSyntaxNode, JsonSyntaxTree>
{
internal unsafe JsonSyntaxTree(TsTree* tree) : base(tree)
{
}

public override JsonSyntaxTree Copy()
{
unsafe
{
return new(Ts.tree_copy(_tree));
}
}

public override JsonSyntaxNode Root { get; }
public static unsafe JsonSyntaxTree Create(TsTree* tree) => new(tree);
}
22 changes: 22 additions & 0 deletions src/TreeSitterSharp.NodeTypesSourceGenerators/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,29 @@
namespace TreeSitterSharp.NodeTypesSourceGenerators;
internal static class Extensions
{
public static T? GetConstructorArgument<T>(this AttributeData attribute, int index)
{
if (TryGetConstructorArgument<T>(attribute, index, out var result))
{
return result;
}
return default;
}

public static bool TryGetConstructorArgument<T>(this AttributeData attribute, int index, out T? result)
{
if (index < attribute.ConstructorArguments.Length)
{
if (attribute.ConstructorArguments.ElementAtOrDefault(index).Value is T o)
{
result = o;
return true;
}
}
result = default;
return false;

}
public static T If<T>(this T syntaxNode, bool condition, Func<T, T> func) where T : SyntaxNode
{
return condition ? func(syntaxNode) : syntaxNode;
Expand Down
Loading

0 comments on commit 3841937

Please sign in to comment.