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 循环渲染
  • ✅ 条件渲染

模块系统

  • ✅ 导入/导出声明
  • ✅ 动态 import() 表达式(ES2020+)
  • ✅ 装饰器函数导出(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.7.tar.gz (288.7 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.7-cp310-abi3-win_amd64.whl (109.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_arkts_open-0.1.7-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (124.2 kB view details)

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

tree_sitter_arkts_open-0.1.7-cp310-abi3-macosx_11_0_arm64.whl (112.3 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_arkts_open-0.1.7-cp310-abi3-macosx_10_9_x86_64.whl (104.6 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_arkts_open-0.1.7.tar.gz
  • Upload date:
  • Size: 288.7 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.7.tar.gz
Algorithm Hash digest
SHA256 149238bb9e3ff1e6c3ed41f45c805f34f8ffa892f9ddf9a00105152352273176
MD5 04c11f0cc8b33d5895a6abacf56970c8
BLAKE2b-256 f6b81a94649b57654514af13029781bfcc7ff7590737fa535fe843351f873808

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.7.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.7-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.7-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7211ae6aef01cd92803ce2256648d53eac35cf3f6368674b2f5106d7ceb2ad7f
MD5 fcb5faa35583e3373368eba9cb6fc5cc
BLAKE2b-256 fe5f85be6612eda6eabe8a840424c9d94900da90da90a4ce64e26e4dfb37911f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.7-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.7-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.7-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e892f337c50f620e34a2cb87121ed01ae9e2182ddd11d10909fe203340cfbd72
MD5 0a543683a07e3963fa15acd40763b014
BLAKE2b-256 d37168bddeb06e183e27ed210ba8e4130f76a2b608a157d05324d61727624c1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.7-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.7-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.7-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 43d3303e66a8e4ff42eae74f90dd029e8e10a5db8001a4ab6eed8e7caa19b5f4
MD5 fe10c362adecea1ded41f30e5c6af7e6
BLAKE2b-256 5902deb18d76ad91c64b82fd0b217b457e3ae4f73c10a6f945027ded078ccf13

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.7-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.7-cp310-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.7-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2fc501bfd8e33775085264dab994535d50b19ac68c9cd5849cadc68cb6dad0ee
MD5 de84949d0f1fe8c79778119f672b507a
BLAKE2b-256 f04eba2b9f625b3ee65eb29e00fa8c59050b578a6ff00607b75c682bb8ca946a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.7-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