Skip to content

Commit 88f2a40

Browse files
authored
Merge pull request #3 from s97712/readme
Add README files to NuGet packages and reorganize documentation
2 parents e4885f8 + 44e31ef commit 88f2a40

5 files changed

Lines changed: 192 additions & 48 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PackageProjectUrl>https://github.com/your-org/csharp-tree-sitter</PackageProjectUrl>
1515
<RepositoryUrl>https://github.com/your-org/csharp-tree-sitter</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
1718
<PackageReleaseNotes>基于Tree-sitter的TypeScript解析器 - 支持跨平台Native库自动管理</PackageReleaseNotes>
1819

1920
<!-- 构建配置 -->

README.md

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,46 @@
22

33
基于Tree-sitter的TypeScript解析器 - .NET绑定
44

5-
## 构建步骤
5+
## 快速开始
66

77
```bash
8-
# 1. 复制运行时文件
8+
# 1. 构建native库
99
(cd tree-sitter/ && make clean && make)
1010

11+
# 2. 复制运行时文件
1112
RID=$(dotnet --info | grep "RID:" | awk '{print $2}')
1213
mkdir -p TypeScriptParser.Native/runtimes/$RID/native
1314
cp -r tree-sitter/dist/* TypeScriptParser.Native/runtimes/$RID/native/
1415

15-
# 2. 恢复依赖
16+
# 3. 构建和测试
1617
dotnet restore
17-
18-
# 3. 构建项目
1918
dotnet build -c Release
19+
dotnet test --configuration Release
2020

21-
# 4. 运行测试
22-
dotnet test --configuration Release --no-build
21+
# 4. 打包NuGet包
22+
dotnet pack -c Release -o ./artifacts
23+
```
24+
25+
## 项目结构
2326

24-
# 5. 打包NuGet包
25-
dotnet pack -c Release --no-build -o ./artifacts
27+
- [`TypeScriptParser/`](TypeScriptParser/) - 主要的.NET绑定库
28+
- [`TypeScriptParser.Native/`](TypeScriptParser.Native/) - 跨平台Native库包
29+
- [`TypeScriptParser.Tests/`](TypeScriptParser.Tests/) - 单元测试
30+
31+
## 开发指南
32+
33+
### Debug模式构建
34+
```bash
35+
dotnet build -c Debug
36+
dotnet test --configuration Debug
2637
```
2738

28-
## 自动化构建
39+
### 支持平台
40+
- Linux x64
41+
- macOS ARM64
42+
- Windows x64
43+
44+
## CI/CD流程
2945

3046
### 开发流程
3147
1. 创建功能分支
@@ -36,41 +52,3 @@ dotnet pack -c Release --no-build -o ./artifacts
3652
1. 创建版本标签:`git tag v1.2.0 && git push origin v1.2.0`
3753
2. 自动构建测试发布到NuGet.org
3854
3. 版本号格式:`1.2.0.{构建号}`
39-
40-
## 手动构建
41-
42-
```bash
43-
# 1. 构建native库
44-
(cd tree-sitter/ && make clean && make)
45-
46-
# 2. 复制运行时文件
47-
RID=$(dotnet --info | grep "RID:" | awk '{print $2}')
48-
mkdir -p TypeScriptParser.Native/runtimes/$RID/native
49-
cp -r tree-sitter/dist/* TypeScriptParser.Native/runtimes/$RID/native/
50-
51-
# 3. .NET构建
52-
dotnet restore
53-
dotnet build -c Release
54-
dotnet test --configuration Release --no-build
55-
dotnet pack -c Release --no-build -o ./artifacts
56-
```
57-
58-
## 开发构建
59-
60-
```bash
61-
# Debug模式构建和测试
62-
dotnet build -c Debug
63-
dotnet test --configuration Debug --no-build
64-
```
65-
66-
## 项目结构
67-
68-
- `TypeScriptParser/` - 主要的.NET绑定库
69-
- `TypeScriptParser.Native/` - 跨平台Native库包
70-
- `TypeScriptParser.Tests/` - 单元测试
71-
72-
## 支持平台
73-
74-
- Linux x64
75-
- macOS ARM64
76-
- Windows x64

TypeScriptParser.Native/TypeScriptParser.Native.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<NoWarn>$(NoWarn);NU5128</NoWarn>
1111
</PropertyGroup>
1212

13+
<!-- README文件包含到包中 -->
14+
<ItemGroup>
15+
<None Include="README.md" Pack="true" PackagePath="\" />
16+
</ItemGroup>
17+
1318
<!-- 包含runtime和build文件到NuGet包 -->
1419
<ItemGroup>
1520
<Content Include="runtimes/**/*" Pack="true" PackagePath="runtimes/" />

TypeScriptParser/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# TypeScriptParser
2+
3+
基于Tree-sitter的TypeScript解析器.NET绑定库,提供高性能的TypeScript代码解析功能。
4+
5+
## 安装
6+
7+
```bash
8+
dotnet add package TypeScriptParser
9+
```
10+
11+
## 快速开始
12+
13+
```csharp
14+
using System;
15+
using TreeSitter.TypeScript;
16+
using GitHub.TreeSitter;
17+
18+
// 创建解析器实例
19+
using var parser = new TypeScriptParser();
20+
21+
// 解析TypeScript代码
22+
string code = @"
23+
function greet(name: string): string {
24+
return `Hello, ${name}!`;
25+
}
26+
";
27+
28+
using var tree = parser.ParseString(code);
29+
var rootNode = tree.root_node();
30+
31+
// 遍历语法树
32+
Console.WriteLine($"语法树类型: {rootNode.type()}");
33+
Console.WriteLine($"子节点数量: {rootNode.child_count()}");
34+
35+
// 获取源码文本
36+
Console.WriteLine($"源码内容: {rootNode.text(code)}");
37+
```
38+
39+
## 主要功能
40+
41+
- ✅ 完整的TypeScript语法支持
42+
- ✅ 高性能的增量解析
43+
- ✅ 跨平台支持 (Windows, Linux, macOS)
44+
- ✅ 详细的语法错误信息
45+
- ✅ 语法树遍历和查询
46+
47+
## API 参考
48+
49+
### TypeScriptParser 类
50+
51+
位于 `TreeSitter.TypeScript` 命名空间。
52+
53+
#### 构造函数
54+
```csharp
55+
var parser = new TypeScriptParser();
56+
```
57+
58+
#### 主要方法
59+
60+
**ParseString(string sourceCode)**
61+
- 解析TypeScript源代码字符串
62+
- 返回: `TSTree` - 解析后的语法树
63+
64+
```csharp
65+
var tree = parser.ParseString("const x = 42;");
66+
```
67+
68+
**CreateCursor(TSTree tree)**
69+
- 创建语法树游标用于高效遍历
70+
- 返回: `TSCursor` - 语法树游标对象
71+
72+
```csharp
73+
using var cursor = parser.CreateCursor(tree);
74+
```
75+
76+
#### 属性
77+
78+
**Language**
79+
- 获取TypeScript语言对象
80+
- 类型: `TSLanguage`
81+
82+
**IsAvailable**
83+
- 检查解析器是否可用
84+
- 类型: `bool`
85+
86+
### TSTree 类 (来自 GitHub.TreeSitter)
87+
88+
**主要方法:**
89+
- `root_node()` - 获取根节点
90+
- `copy()` - 复制语法树
91+
- `edit(TSInputEdit edit)` - 编辑语法树
92+
93+
### TSNode 结构 (来自 GitHub.TreeSitter)
94+
95+
**主要方法:**
96+
- `type()` - 获取节点类型名称
97+
- `child_count()` - 获取子节点数量
98+
- `child(uint index)` - 获取指定索引的子节点
99+
- `start_point()` / `end_point()` - 获取节点位置
100+
- `text(string sourceCode)` - 获取节点对应的源码文本
101+
102+
## 语法树遍历示例
103+
104+
```csharp
105+
using var parser = new TypeScriptParser();
106+
var tree = parser.ParseString(@"
107+
class Calculator {
108+
add(a: number, b: number): number {
109+
return a + b;
110+
}
111+
}
112+
");
113+
114+
var root = tree.root_node();
115+
116+
// 递归遍历所有节点
117+
void TraverseNode(TSNode node, string source, int depth = 0)
118+
{
119+
var indent = new string(' ', depth * 2);
120+
Console.WriteLine($"{indent}{node.type()}: {node.text(source)}");
121+
122+
for (uint i = 0; i < node.child_count(); i++)
123+
{
124+
TraverseNode(node.child(i), source, depth + 1);
125+
}
126+
}
127+
128+
TraverseNode(root, code);
129+
```
130+
131+
## 错误处理
132+
133+
```csharp
134+
using var parser = new TypeScriptParser();
135+
var tree = parser.ParseString("const x = ;"); // 语法错误
136+
137+
var root = tree.root_node();
138+
if (root.has_error())
139+
{
140+
Console.WriteLine("语法树包含错误");
141+
}
142+
```
143+
144+
## 系统要求
145+
146+
- .NET 9.0 或更高版本
147+
- 支持的平台:Windows (x64), Linux (x64), macOS (ARM64/x64)
148+
149+
## 相关包
150+
151+
- `TypeScriptParser.Native` - 包含跨平台原生库的运行时包
152+
153+
## 许可证
154+
155+
本项目基于MIT许可证开源。

TypeScriptParser/TypeScriptParser.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<NoWarn>$(NoWarn);CS1591</NoWarn>
1212
</PropertyGroup>
1313

14+
<!-- README文件包含到包中 -->
15+
<ItemGroup>
16+
<None Include="..\README.md" Pack="true" PackagePath="\" />
17+
</ItemGroup>
18+
1419
<!-- Native库依赖 -->
1520
<ItemGroup>
1621
<ProjectReference Include="..\TypeScriptParser.Native\TypeScriptParser.Native.csproj" />

0 commit comments

Comments
 (0)