@aster-cloud/aster-lang-ts

中文文档
1.3.0发布于 2026/11/06MITTypeScriptES Module构建来源已验证近 30 天下载 3195

Aster CNL (Controlled Natural Language) TypeScript Compiler - Lexer, Parser, AST, Core IR, Type Checker

原始使用文档
文档目录12 节
  1. Overview
  2. Installation
  3. Quick Start
  4. Multi-Language Support
  5. Current CNL Style
  6. Documentation
  7. Development
  8. Project Structure
  9. API Reference
  10. Core Exports
  11. Lexicon Exports
  12. License

Aster Lang TypeScript Compiler

License

TypeScript implementation of the Aster CNL (Controlled Natural Language) compiler pipeline.

Overview

Aster is a pragmatic, type-safe language with human-readable controlled natural language syntax. This package provides the pure TypeScript compiler infrastructure:

  • Canonicalizer: Normalizes CNL source code
  • Lexer: Tokenizes source with multi-language lexicon support
  • Parser: Generates AST from token stream
  • Core IR: Intermediate representation
  • Type Checker: Static type analysis
  • LSP Server: Language Server Protocol implementation

Installation

pnpm install @aster-cloud/aster-lang-ts

Quick Start

import { parseSource, resolveAndLowerChecked } from '@aster-cloud/aster-lang-ts';

// Compile pipeline: canonicalize → lex → parse → name resolution (R1) → lower → name resolution (R2)
const source = `
Module greeting.

Rule greet given name, produce:
  Return Text.concat("Hello, ", name).
`;

const { ast } = parseSource(source);
// Throws NameResolutionError (E8xx diagnostics) instead of returning Core with unresolved names
const { core, diagnostics } = resolveAndLowerChecked(ast);

Raw lowering (lowerModule) and the unchecked resolveAndLower are internal and not exported: every public path to Core IR runs name resolution and refuses to emit IR on resolution errors. Use compile / compileAndTypecheck from @aster-cloud/aster-lang-ts/browser, resolveAndLowerChecked, or resolvePolicyTypes.

Multi-Language Support

Aster supports writing policies in multiple natural languages:

import { canonicalize, lex, parse } from '@aster-cloud/aster-lang-ts';
import { ZH_CN } from '@aster-cloud/aster-lang-ts/lexicons/zh-CN';

const zhSource = `
模块 问候。

规则 问候 给定 姓名,产出:
  返回「你好,」 加 姓名。
`;

const tokens = lex(canonicalize(zhSource, ZH_CN), ZH_CN);
const ast = parse(tokens);

Supported languages:

  • English (en-US) - Default
  • Simplified Chinese (zh-CN)
  • German (de-DE)

Current CNL Style

  • Type inference is the default; examples avoid explicit type annotations.
  • Operator-call chaining is removed. Use standard calls or infix operators.
  • Statements end with periods, and block headers end with colons.

Documentation

  • docs/overview.md
  • docs/cnl-syntax.md
  • docs/type-inference.md
  • docs/operator-call.md
  • docs/examples.md
  • docs/contributing.md
  • Localized docs:
  • English (default): same list above.
  • 简体中文: docs/zh/overview.md, docs/zh/cnl-syntax.md, docs/zh/type-inference.md, docs/zh/operator-call.md, docs/zh/examples.md, docs/zh/contributing.md
  • Deutsch: docs/de/overview.md, docs/de/cnl-syntax.md, docs/de/type-inference.md, docs/de/operator-call.md, docs/de/examples.md, docs/de/contributing.md

Development

# Install dependencies
pnpm install

# Build
pnpm build

# Run tests
pnpm test:unit

# Start LSP server
pnpm lsp

# REPL
pnpm repl

Project Structure

src/
  ├── canonicalizer.ts    # Source normalization
  ├── lexer.ts            # Tokenization
  ├── parser.ts           # AST generation
  ├── ast.ts              # AST definitions
  ├── lower_to_core.ts    # Core IR lowering
  ├── core_ir.ts          # Core IR definitions
  ├── typecheck/          # Type checker
  ├── lsp/                # Language Server Protocol
  └── config/
      └── lexicons/       # Multi-language lexicons
          ├── en-US.ts
          ├── zh-CN.ts
          └── de-DE.ts

API Reference

Core Exports

ExportDescription
canonicalize(source, lexicon?)Normalize CNL source
lex(source, lexicon?)Tokenize source
parse(tokens)Parse to AST
parseSource(source, lexicon?, scopes?)Canonicalize, tokenize and parse
resolveAndLowerChecked(ast, scopes?)Name resolution + lowering to Core IR; throws NameResolutionError on E8xx
resolvePolicyTypes(source, options)Resolved Data/Enum declarations with original spellings
typecheck(module)Type check module

Lexicon Exports

import { EN_US } from '@aster-cloud/aster-lang-ts/lexicons/en-US';
import { ZH_CN } from '@aster-cloud/aster-lang-ts/lexicons/zh-CN';
import { DE_DE } from '@aster-cloud/aster-lang-ts/lexicons/de-DE';

License

MIT

版本与发布记录

@aster-cloud/aster-lang-ts 共发布 80 个版本,首次发布于 2026/01/11;过去 12 个月发布了 80 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.0latest2026/11/061 天前
1.2.62026/11/061 天前
1.2.52026/11/052 天前
1.2.42026/11/052 天前
1.2.32026/10/317 天前
1.2.22026/10/308 天前

关于 @aster-cloud/aster-lang-ts 的常见问题

@aster-cloud/aster-lang-ts 是什么?

Aster CNL (Controlled Natural Language) TypeScript Compiler - Lexer, Parser, AST, Core IR, Type Checker

如何安装 @aster-cloud/aster-lang-ts?

在项目目录运行 npm install @aster-cloud/aster-lang-ts,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 aster、aster-lsp、aster-core、aster-convert,可以用 npx aster 直接运行。

@aster-cloud/aster-lang-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aster-cloud/aster-lang-ts 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aster-cloud/aster-lang-ts 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aster-cloud/aster-lang-ts 可以商用吗?

@aster-cloud/aster-lang-ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aster-cloud/aster-lang-ts 的下载量有多少?

根据 npm 官方下载统计,@aster-cloud/aster-lang-ts 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,195 次,日均约 107 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aster-cloud/aster-lang-ts 还在维护吗?

最新版本 1.3.0 发布于 2026/11/06(1 天前);至今共发布 80 个版本,其中过去 12 个月发布了 80 个;首次发布于 2026/01/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。