Skip to main content

Tree-sitter grammar for ArkTS (HarmonyOS development language)

Project description

ArkTS Tree-sitter 解析器

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

特性支持

✅ 已实现特性

  • 基础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

import tree_sitter_arkts_open 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.1.tar.gz (104.8 kB view details)

Uploaded Source

Built Distribution

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

tree_sitter_arkts_open-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (55.2 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: tree_sitter_arkts_open-0.1.1.tar.gz
  • Upload date:
  • Size: 104.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for tree_sitter_arkts_open-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c0ecd24b8dcb5b63af1880b91460bb46ec3f52e36caeefab3b8866bb287b6b2a
MD5 4097b7d54b4c3b72db944485a2752ab1
BLAKE2b-256 b3cf8b9c538d7dd508047ae5585ef87f45ad111a0b64b966da350494eb114556

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tree_sitter_arkts_open-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b28c79aaad5132d7ef84a7c977d99ca4b26e036a832ed5210aa6cd402eb6155c
MD5 d8745d12531a1951d3827488ece8e9a4
BLAKE2b-256 c66b1fb17f9837a2a05adb9a54bc6cadb26c4006ea8b1dd5538eb911e5f89c85

See more details on using hashes here.

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