Skip to main content

Tree-sitter grammar for ArkTS (HarmonyOS development language)

Project description

ArkTS Tree-sitter 解析器

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

🎯 解析能力持续提升

我们的 ArkTS 解析器在真实项目中的表现持续改进!在 hmosworld 大型生产项目的验证中,解析成功率从 30% 显著提升至 46.29%(175个文件,81个成功解析)。这一进步得益于对自动分号插入(ASI)机制的完善支持和错误恢复能力的增强,充分证明了本项目在处理实际代码复杂性方面的不断进化。

特性支持

✅ 已实现特性

  • 基础TypeScript语法兼容
  • 装饰器语法(@Component、@State、@Prop、@Link等)
  • struct组件定义
  • build()方法和UI描述语法
  • 导入/导出声明
  • 接口和类型定义
  • 基础表达式和语句

🚧 开发中特性

  • 完整的UI组件调用语法
  • ForEach循环语法
  • 条件渲染语法
  • 高级装饰器支持(@Builder、@Styles等)
  • 错误恢复机制优化

📋 计划实现特性

  • 模块系统扩展
  • 泛型语法完整支持
  • 性能优化
  • 更多语言绑定

安装使用

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.5.tar.gz (200.4 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.5-cp310-abi3-win_amd64.whl (88.6 kB view details)

Uploaded CPython 3.10+Windows x86-64

tree_sitter_arkts_open-0.1.5-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (102.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.5-cp310-abi3-macosx_11_0_arm64.whl (89.6 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tree_sitter_arkts_open-0.1.5-cp310-abi3-macosx_10_9_x86_64.whl (84.1 kB view details)

Uploaded CPython 3.10+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tree_sitter_arkts_open-0.1.5.tar.gz
  • Upload date:
  • Size: 200.4 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.5.tar.gz
Algorithm Hash digest
SHA256 1979d2dbcbdb0dddccb997833ad3774806dd7cff0622b594797b879bb73eff98
MD5 0168b4560bd7202bf46c570f426c86d9
BLAKE2b-256 4d18e6390e20d906263bafe78a77ee6fbbe67c681901afff69bff01bc6d2bbe2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.5-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 69f5c30b5273c829131274059c6c58328640c5b8e61bfacfe0b619cd65e62ad4
MD5 1e88a11c3d32c894ff0d1daa377080ca
BLAKE2b-256 5ff6584d2a45f304a6fc7b48d65462734d8f77252b7655a34225605a6fdfadf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tree_sitter_arkts_open-0.1.5-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.5-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.5-cp310-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1efedead336c0bcfe1a534c0969f84babeee66c4f518bbb90d3aa9b1d7c12e86
MD5 ca141cbee07f481d79832a7a03e27529
BLAKE2b-256 e4ec3969f210ea7a27d51742cc1b2fad7685d6677676fd576ef2279ca7480617

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.5-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 139c7913fd4fbb3da086c0c549460d57c08a474629da85b5d0fa66ef0824306d
MD5 8c3065ce2801d45a8f3a88988cd7e268
BLAKE2b-256 7d3f6a184814cedcf62468a9e1b60e0891d42a11cd3015b55578d12ceebd9e6b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.5-cp310-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7af5ee0b9f48a9a5aa2a4f7b004f6cdfe1a7b559b3a88e648d59b4b5ad842d0a
MD5 93a63c3275cfc5c7d94ef89f1a9c75cc
BLAKE2b-256 4d7a8a1e6698385dc44dbb79fae56210142bbb5f56ec8360535b3a72c330d255

See more details on using hashes here.

Provenance

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