一个 Rust 编写的 YAML 1.2 解析器,生成保留注释且精确记录字节偏移的抽象语法树,专为构建格式化工具(如 Prettier)而设计。

Stars

14

7 天增长

暂无数据

Fork 数

0

开放 Issue

0

开源协议

MIT

最近更新

2026-07-28

AI 仓库情报摘要
FR-AI / ANALYSIS

为什么值得关注

它复刻了 Prettier 的 YAML 打印器所使用的 AST 结构,保留注释和精确跨度,并基于 libyaml 扫描算法的移植版本,为格式化及工具开发提供了可靠的基础。

适合谁使用

  • 需要构建 YAML 格式化工具或检查器的 Rust 开发者
  • Prettier 等代码格式化工具的贡献者
  • 需要语法级别、兼顾跨度信息的 YAML 解析器的工具链作者
  • 从 yaml-unist-parser 迁移到 Rust 生态的开发者

典型使用场景

  • 格式化 YAML 文件,同时保留注释和精确源码跨度
  • 构建静态分析工具,检查 YAML 结构而不解析值
  • 基于精确源码跨度生成 YAML 语法高亮输出
  • 针对标准测试套件验证 YAML 解析器的正确性

项目优势

  • 保留注释和精确字节跨度的 AST,支持忠实重建源码
  • 快速失败的错误处理,返回包含跨度信息的语法错误
  • 高合规性:通过官方 yaml-test-suite 和 Prettier 用例的所有有效输入
  • 轻量且仅关注语法,避免模式/标签解析,专注于格式化场景

使用前须知

  • 不进行模式/标签解析、锚点/别名处理或值类型推断
  • 接受无效 YAML 不是目标;错误情况仅记录但不严格拒绝
  • 仅支持 YAML 1.2,可能无法覆盖 YAML 1.1 或自定义方言的所有边界情况

README 快速开始

oxc-yaml-parser

oxc-yaml-parser parses YAML 1.2 into a comment-preserving, span-faithful typed AST, designed for building formatters.

  • The AST mirrors yaml-unist-parser's node shapes — the AST Prettier's YAML printer consumes.
  • Scalar values are not cooked: consumers slice the original source through spans.
  • Comments are retained as trivia with exact spans.
  • Syntax-only: no schema/tag resolution, no anchor/alias resolution, no value typing.
  • Fail-fast: a syntax error returns Err with a span; no partial AST is produced.

The scanner is a port of the libyaml scanning algorithm (by way of saphyr), adapted to byte-offset spans and trivia retention.

Example

use oxc_yaml_parser::{Allocator, Parser};

let allocator = Allocator::default();
let parser = Parser::new(&allocator, "key: value # comment");
let root = parser.parse().unwrap();
println!("{root:#?}");

More examples are available in examples.

Conformance

cargo run -p conformance (or just conformance) parses three corpora and snapshots the results under tasks/conformance/snapshots:

  • the official yaml-test-suite (data branch, including its invalid-input cases),
  • Prettier's YAML format fixtures,
  • local edge fixtures covering Prettier's input-acceptance tolerances.

All valid inputs in all three corpora parse successfully. Rejecting invalid YAML is a non-goal; outcomes on invalid inputs are tracked in the snapshots for review.

License

MIT

The scanner derives from libyaml (MIT) by way of saphyr (MIT); their copyright notices are included in LICENSE.

Sponsored By

相关仓库与替代方案

根据分类、Topic 和编程语言匹配的相似项目。

l0ng-ai
精选
l0ng-ai GitHub avatar

tty7

tty7 is a fast, GPU-accelerated terminal workbench with persistent sessions, built-in input tools, SSH support, and coding agent awareness.

开发者工具CLI 与终端
359
steelbrain
精选
steelbrain GitHub avatar

reims-vgpu

reims-vgpu is an experimental virtual GPU for macOS guests that uses QEMU to decode the guest's GPU command stream and execute it through Metal or Vulkan, leveraging the built-in AppleParavirtGPU driver without requiring custom kexts.

桌面应用
143
m-novotny
精选
m-novotny GitHub avatar

memguard-rs

A Rust library that provides secure memory handling primitives including zeroization on drop, memory locking, constant-time comparison, and compile-time guarded regions, with zero dependencies and no_std support.

嵌入式与物联网安全
131