Skip to main content

Tree-sitter grammar for ArkTS (HarmonyOS development language)

Reason this release was yanked:

this version is not good

Project description

ArkTS Tree-sitter 解析器

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

🎯 解析能力持续提升

我们的 ArkTS 解析器在真实项目中的表现持续改进!在 hmosworld 大型生产项目的验证中,解析成功率从最初的 30% 跃升至 100%(175个文件全部成功解析),实现了超过 3.3 倍的飞跃提升,达到完美解析!

🏆 近期重大突破

🎉 v0.1.8 版本 - 100% 完美解析!(2025-10-20):

  • 🏆 历史性里程碑:在 hmosworld 项目中实现 100% 解析成功率(175/175 文件)
  • 🚀 Grammar 简化重构:引入通用内容解析规则 _non_brace_content
    • 统一简化了 build_bodycontainer_content_bodyblock_statement 等 6 个核心规则
    • 代码更简洁,维护更容易,扩展性更强
    • 提升解析器的通用性和鲁棒性
  • 导出语法增强:支持 export default interface/type/enum
  • ASI 兼容性改进:属性签名分号改为可选,更好地兼容真实代码
  • 🔩 优先级调整:优化 builder_function_bodyextend_function_body 的解析优先级

🎉 v0.1.7 版本 - 历史性突破(2025-10-20):

  • 🚀 解析成功率突破 93%! 在 hmosworld 项目中达到 93.71% 成功率(175 个文件中 164 个成功解析)
  • 🎯 距离 100% 仅差 11 个文件:已达到生产可用级别,可支持构建语言服务器、代码分析工具等实际应用
  • 动态 import() 表达式支持(ES2020+):
    • 支持顶层独立调用:import('@ohos/module')
    • 支持 async/await 模式:await import('./Module')
    • 支持 Promise 链式调用:import('./Module').then(...)
    • 支持动态路径变量:import(modulePath)
    • 支持条件导入、批量导入等所有场景
  • 🐛 修复 @Component export struct 解析错误:确保装饰器导出语法正确解析
  • 🔧 语法解析精度大幅提升:针对生产代码中的边缘案例进行精细优化
  • 📈 相比 v0.1.6 提升 7.42 个百分点:从 86.29% 到 93.71%

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 等响应式布局组件

📊 解析成功率发展历程

30.00% (初始版本) → 61.71% (v0.1.5) → 86.29% (v0.1.6) → 93.71% (v0.1.7) → 100% (v0.1.8) 🎉🏆

这一历史性突破充分证明了本项目在处理实际代码复杂性方面的不断进化,已经能够完美支持所有生产环境中的 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+)- v0.1.7
    • 支持顶层调用:import('@ohos/module')
    • 支持 async/await:await import('./Module')
    • 支持 Promise 链式调用:import('./Module').then(...)
    • 支持动态路径变量:import(modulePath)
  • ✅ 装饰器函数导出(export @Builder function
  • ✅ 默认导出(export default interface/type/enum)- v0.1.8

🚧 开发中特性

  • 更多 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.8.tar.gz (145.6 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.8-cp310-abi3-win_amd64.whl (75.3 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_arkts_open-0.1.8-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.6 kB view details)

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

tree_sitter_arkts_open-0.1.8-cp310-abi3-macosx_11_0_arm64.whl (74.7 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_arkts_open-0.1.8-cp310-abi3-macosx_10_9_x86_64.whl (70.7 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_arkts_open-0.1.8.tar.gz
  • Upload date:
  • Size: 145.6 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.8.tar.gz
Algorithm Hash digest
SHA256 03459a5865a22d16def3d4416d7e2bbf9007190fe2be87b463cf7d7d49450025
MD5 3067726ec89beb7cb40e0c68dcb1cdba
BLAKE2b-256 4ec6e030b9d1910d9a57f15bf0e2ca5af861b00fb1ad1014c7dae987b65cfe11

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.8-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f5a3ff68665dbcf07743e5dccb3a9da5c2d09406cf5ad02144905e520a389365
MD5 2866be8a5b296d77e487cd4961c13ff6
BLAKE2b-256 a7a47685c32f50ffbc552117570f3ac35e2b06e5fff3251539fb3b7d2a224a22

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.8-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.8-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.8-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b45a6e1a9b6376ecf7ff112b6260ce9ea8325fb839a64aac1595a5437213b000
MD5 8ab56c8e7bb523dd88fbf38c1609691f
BLAKE2b-256 cfa7cbd771a558c60ec99c2f59f9c707b4e59b1b27c00c378ae3713315cf1ac6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.8-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2e8820a70555dded14bbeb382379be4b1f997cab3ccbf87ce5baca8d1e57b500
MD5 bddf5177e96f5510a976fa9c46591d23
BLAKE2b-256 fd6450d427b3dee8879e9fb7df8b7c8eb743121eab2d43fc27cd1ecda38a94b9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.8-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b5c5bf32644236daff627f1fd85905d19ef5ae1afa742bb38804daa5e70fec51
MD5 46e22c0c58c0ad9728909384a9f32791
BLAKE2b-256 93f1eddf8e6447d62632c39d5ee4c5e17b44bfa45e33b6fd7dfdf0480c510513

See more details on using hashes here.

Provenance

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