Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Consolidate the parsetree printers

未关闭
#8,798 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
活跃
技术栈
ocaml
领域
compilers

调研方向

Start with compiler/ml/printast.ml, compiler/syntax/src/res_ast_debugger.ml, compiler/ml/pprintast.ml, and the roundtrip test in scripts/test_syntax.sh. Trace the printer call sites and parser fixture snapshots named in the issue, then check how bsc -dsource handles PPX and builtin rewrites. Done means removing the redundant printers and comparisons while keeping the listed print modes, fixtures, and tests working.

由索引模型根据 Issue 内容生成。

描述

The compiler has three printers for the parsetree, and each one matches over every node. Every new node (recently Pexp_dict, Ppat_dict, Pexp_ternary, Pmod_await, Ppat_variant_spread) has to be added to all three, on top of the parsetree, the v0 mappers and the formatter.

Printer Size Output Used by
compiler/ml/printast.ml 827 lines tree with source locations bsc -dparsetree, res_parser -print ast, the playground's parsetree view, ounit_parser_recovery_tests
Sexp_ast in compiler/syntax/src/res_ast_debugger.ml ~1,040 lines S-expression without locations, except the offsets of explicit braces (see below) res_parser -print sexp, used by the formatter roundtrip test
compiler/ml/pprintast.ml 1,471 lines OCaml syntax res_parser -print ml (expected output for the parser fixtures in tests/syntax_tests/data/parsing), bsc -dsource, a few ounit tests

Sexp_ast is redundant. The roundtrip test in scripts/test_syntax.sh formats a file twice and checks two things:

  • the two formatted texts are equal;
  • the ASTs parsed from them are equal (as sexp).

The parser is deterministic, so the AST check holds whenever the text check does. The original file's AST can't usefully be compared with the formatted one, because the formatter changes the AST on purpose, e.g. it removes braces in switch cases.

Sexp_ast prints no locations, with one exception: since #8678 it prints the character offsets of explicit braces (braces_loc of Pexp_braces). That was not intended and nothing depends on it. It should not be carried into any other printer or test.

Pprintast is a leftover from OCaml. ReScript has no other use for printing its AST as OCaml code.

Proposal

  1. Remove Sexp_ast, -print sexp and the redundant AST comparison from the roundtrip test.
  2. Snapshot the parser fixtures with Printast instead of -print ml.
  3. Make bsc -dsource print the parsetree after PPX and builtin rewrites with the ReScript printer instead of OCaml syntax.
  4. Move Pprintast.string_of_int_as_char (used by printlambda.ml and parmatch.ml), then remove Pprintast and res_driver_ml_printer.ml.

That removes about 2,500 lines, and new parsetree nodes need one printer case instead of three. -print comments and -print doc in res_ast_debugger.ml, Printtyped and Printlambda are not affected.

Compatibility: res_parser isn't shipped in the npm packages, so -print sexp and -print ml are internal. bsc -dsource is a debugging flag. Its output switches from OCaml-like to ReScript syntax.

主要语言
OCaml
星标
7.5k
派生
484
平均合并
20 小时 27 分钟
30 天内合并 PR
120

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rescript-lang/rescript 的其他 Issue

查看 rescript-lang/rescript 的全部 Issue

相似的 Issue

更多 Compilers Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。