Skip to main content

Tree-sitter grammar for ArkTS (HarmonyOS development language)

Project description

ArkTS Tree-sitter 解析器

这是一个为华为ArkTS语言开发的Tree-sitter语法解析器,支持ArkTS语言的完整语法特性,包括装饰器、组件化语法、状态管理等核心特性。

🎯 解析能力持续提升

我们的 ArkTS 解析器在真实项目中的表现持续改进!在 hmosworld 大型生产项目的验证中,解析成功率从最初的 30% 跃升至最新的 86.29%(175个文件中151个成功解析),实现了近3倍提升!

近期重大突破

v0.1.6 版本更新(2025-10-20):

  • 🚀 解析成功率突破 86%:在 hmosworld 项目中达到 86.29% 成功率(175 个文件中 151 个成功解析)
  • 持续优化解析能力:相比 v0.1.5 版本的 61.71%,新版本提升了 24.58 个百分点
  • 生产级代码支持:已能成功解析绝大多数真实 ArkTS 应用代码
  • UI 组件支持增强:新增 NavDestinationListItemGroup 等导航和列表组件支持
  • 语法兼容性提升:持续优化对复杂嵌套结构和边缘情况的处理

v0.1.5 版本更新(2025-10-18):

  • 完整泛型支持:支持泛型类、函数、接口及复杂类型参数
  • 装饰器函数扩展:支持 @Builder@Styles@Extend 装饰器函数及导出声明
  • 控制流完善:支持 try/catch/finallyfor/while/break/continue 等控制流语句
  • 枚举声明:完整支持 enum 声明及其导出语法
  • 高级表达式:支持函数表达式、箭头函数、nullish合并操作符(??)、可选链(?.)
  • ASI兼容性优化:完善自动分号插入(ASI)机制,提升错误恢复能力
  • 布局容器扩展:支持 GridRowGridCol 等响应式布局组件

这一进步充分证明了本项目在处理实际代码复杂性方面的不断进化,已经能够支持绝大多数生产环境中的 ArkTS 代码

特性支持

✅ 已实现特性

核心语法

  • ✅ 完整 TypeScript 语法兼容
  • ✅ 装饰器语法(@Component@State@Prop@Link@Builder@Styles@Extend 等)
  • ✅ struct 组件定义与方法
  • build() 方法和 UI 描述语法
  • ✅ 泛型支持(类、函数、接口、类型参数)
  • ✅ 枚举声明(enum

类型系统

  • ✅ 接口和类型定义
  • ✅ 类型注解与函数类型
  • ✅ 类型断言(as
  • ✅ 对象类型(支持逗号/分号分隔符)

表达式与操作符

  • ✅ 箭头函数与函数表达式
  • ✅ 异步表达式(await
  • ✅ Nullish 合并操作符(??
  • ✅ 可选链操作符(?.
  • ✅ 修饰符链表达式(.modifier()

控制流

  • try/catch/finally/throw 异常处理
  • for/while/do-while 循环
  • break/continue 控制语句
  • if/else 条件分支

UI 组件

  • ✅ 基础组件(TextButtonImage 等)
  • ✅ 容器组件(ColumnRowStackFlex 等)
  • ✅ 响应式布局(GridRowGridCol
  • ForEach 循环渲染
  • ✅ 条件渲染

模块系统

  • ✅ 导入/导出声明
  • ✅ 装饰器函数导出(export @Builder function
  • ✅ 默认导出(export default interface/type/enum

🚧 开发中特性

  • 更多 UI 组件变体支持
  • 性能优化与增量解析
  • 语法高亮查询优化

📋 计划实现特性

  • 完整的语言服务器协议(LSP)集成
  • 更多语言绑定(C#、Java)
  • 代码格式化支持

安装使用

Node.js

npm install tree-sitter-arkts-open
const Parser = require('tree-sitter');
const ArkTS = require('tree-sitter-arkts');

const parser = new Parser();
parser.setLanguage(ArkTS);

const sourceCode = `
@Component
struct HelloWorld {
  @State message: string = 'Hello'
  
  build() {
    Text(this.message)
  }
}
`;

const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());

Python

pip install tree-sitter-arkts-open
import tree_sitter_arkts as arkts
from tree_sitter import Language, Parser

ARKTS_LANGUAGE = Language(arkts.language())
parser = Parser(ARKTS_LANGUAGE)

source_code = '''
@Component  
struct MyComponent {
  build() {
    Text('Hello ArkTS')
  }
}
'''

tree = parser.parse(bytes(source_code, 'utf8'))
print(tree.root_node)

语法支持示例

组件定义

@Component
struct MyComponent {
  @State count: number = 0;
  @Prop title: string = 'Default';
  
  build() {
    Column() {
      Text(this.title)
      Button('Click')
        .onClick(() => {
          this.count++
        })
    }
  }
}

状态管理

@Component
struct StateExample {
  @State private items: string[] = [];
  @Link shared: boolean;
  
  build() {
    List() {
      ForEach(this.items, (item: string) => {
        ListItem() {
          Text(item)
        }
      })
    }
  }
}

开发

构建解析器

tree-sitter generate

测试

tree-sitter test

解析文件

tree-sitter parse example.ets

语言绑定

本解析器支持多种编程语言绑定:

  • Node.js: bindings/node/
  • Python: bindings/python/
  • Rust: bindings/rust/
  • Go: bindings/go/
  • Swift: bindings/swift/

贡献

欢迎提交Issues和Pull Requests!

开发环境

  • Tree-sitter CLI 0.25.3+
  • Node.js 18+
  • 支持的构建工具链

测试用例

测试用例位于 test/ 目录,包含:

  • 基础组件语法测试
  • 装饰器语法测试
  • 状态管理语法测试
  • 错误恢复测试

许可证

MIT License

相关链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_arkts_open-0.1.6.tar.gz (247.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_arkts_open-0.1.6-cp310-abi3-win_amd64.whl (99.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_arkts_open-0.1.6-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (113.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_11_0_arm64.whl (100.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_10_9_x86_64.whl (93.9 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

Details for the file tree_sitter_arkts_open-0.1.6.tar.gz.

File metadata

  • Download URL: tree_sitter_arkts_open-0.1.6.tar.gz
  • Upload date:
  • Size: 247.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tree_sitter_arkts_open-0.1.6.tar.gz
Algorithm Hash digest
SHA256 13fadacb33bc8aa7ceb394240c4dd60106514d9d4e6aa3472793d4aac05a48a4
MD5 5a8f5b1e3b87fff7b62a84cf2e8cc4d2
BLAKE2b-256 87be9cd0e304c16638fd2f48983137d5d2e4b7b03f78d1ea68e0746bdaf07b96

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.6.tar.gz:

Publisher: publish.yml on Million-mo/tree-sitter-arkts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_arkts_open-0.1.6-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.6-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ab934f57cbdc41ab62a7abf8cf6007b42b96b44ec38f02e4b778371fa0d82f0b
MD5 2772772c76916491690038acd9891620
BLAKE2b-256 df1cc419b26be89ea25afaea3c1bdd16b41b7ece77ad9c3775c3e1aa10c8fc56

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.6-cp310-abi3-win_amd64.whl:

Publisher: publish.yml on Million-mo/tree-sitter-arkts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_arkts_open-0.1.6-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.6-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 857503540b1e9e338c76cc0d54a4c70adef2663a634036e6b28b4290d68f0c76
MD5 ccdeedd07168b07a4316e9dc9f81380c
BLAKE2b-256 89ca33dfa29b757683c42f44ec1ccccaab4a184718a7d7aacf2d070dc521bffd

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.6-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on Million-mo/tree-sitter-arkts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f26d68a4010538e14e15cf2399fa13ae1cc4d3c75d473e6b057ddcaf82aa3593
MD5 9cf55f5714a48f8791d60726eb9bff36
BLAKE2b-256 0b6f25f8e6faf25358cb24e55b5889004c6e765ab1fea9ceee7e7b0c5d7d24b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on Million-mo/tree-sitter-arkts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 31a69f4497877916adbd17863ff36e7431233b8636bec22821c2cf388879b0e3
MD5 b3e38ac41fe6f699ec87e21f0f0294de
BLAKE2b-256 f02012be4ebe35426b8e4493e5aac6bc7e03ca7136a1cd04059b785b2571ed54

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.6-cp310-abi3-macosx_10_9_x86_64.whl:

Publisher: publish.yml on Million-mo/tree-sitter-arkts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page