解析器
🌐 Parser
Oxc 解析器被设计为最快且最符合标准的 JavaScript 和 TypeScript 解析器。为解析器做贡献需要理解实现细节以及庞大的测试架构。
🌐 The Oxc parser is designed to be the fastest and most conformant JavaScript and TypeScript parser available. Contributing to the parser requires understanding both the implementation details and the extensive test infrastructure.
架构概览
🌐 Architecture Overview
解析器遵循传统的编译器前端架构:
🌐 The parser follows a traditional compiler frontend architecture:
Source Text → Lexer → Tokens → Parser → AST关键组件
🌐 Key Components
- 词法分析器:将源代码文本分解为结构化的记号
- 解析器:构建 AST 的递归下降解析器
- AST:内存高效的抽象语法树
- 错误恢复:高级错误处理与恢复
- 语义分析:符号解析与作用域管理
设计目标
🌐 Design Goals
我们的目标是成为最快的基于 Rust 的可投入生产的解析器,具有以下特点:
🌐 We aim to be the fastest Rust-based ready-for-production parser with:
- 速度:比 SWC 快 3 倍,比 Biome 快 5 倍
- 符合性:100% 符合 Test262,99% 以上兼容 Babel/TypeScript
- 内存效率:基于区域的分配,最小堆使用
- 错误质量:提供有帮助的错误信息并可恢复
开发工作流程
🌐 Development Workflow
设置
🌐 Setting Up
# Run parser tests
cargo test -p oxc_parser
# Run conformance tests
just c # or `just coverage`项目结构
🌐 Project Structure
crates/oxc_parser/
├── src/
│ ├── lib.rs # Public API
│ ├── lexer/ # Tokenization
│ ├── parser/ # Parsing logic
│ ├── cursor.rs # Token stream management
│ └── diagnostics.rs # Error handling
├── tests/ # Unit tests
└── examples/ # Usage examples核心解析器文件
🌐 Core Parser Files
parser/mod.rs:主解析器入口点parser/statement.rs: 语句解析parser/expression.rs:表达式解析parser/typescript.rs:TypeScript 特有的解析parser/jsx.rs:JSX 解析逻辑
符合性测试
🌐 Conformance Testing
运行一致性测试
🌐 Running Conformance Tests
just c这使用 tasks/coverage 中的运行程序运行一致性测试套件:
🌐 This runs conformance test suites using the runner in tasks/coverage:
Test262 - ECMAScript 符合性
🌐 Test262 - ECMAScript Conformance
JavaScript 有名为 Test262 的 ECMAScript 测试套件。 Test262 的目标是提供覆盖规范中指定的每一个可观察行为的测试材料。
🌐 JavaScript has the ECMAScript Test Suite called Test262. The goal of Test262 is to provide test material that covers every observable behavior specified in the specification.
解析器符合性使用解析阶段测试。
🌐 Parser conformance uses the parse phase tests.
当前状态: 43765/43765 (100.00%)
Babel 解析器测试
🌐 Babel Parser Tests
当 JavaScript 添加新的语言特性时,Babel 会首先实现它们。Babel 对前沿特性有全面的解析器测试。
🌐 When new language features are added to JavaScript, Babel implements them first. Babel has comprehensive parser tests for cutting-edge features.
当前状态: 2093/2101 (99.62%)
TypeScript 一致性
🌐 TypeScript Conformance
TypeScript 一致性测试可以在这里找到。
🌐 The TypeScript conformance tests can be found here.
当前状态: 6470/6479 (99.86%)
查看结果
🌐 Viewing Results
测试结果存储在快照文件中以跟踪变化:
🌐 Test results are stored in snapshot files for tracking changes:
