Skip to content

Add parsetree support for wildcards in simple expressions - #300

Open
dra27 wants to merge 5 commits into
trunkfrom
upstream-Pexp_hole
Open

Add parsetree support for wildcards in simple expressions#300
dra27 wants to merge 5 commits into
trunkfrom
upstream-Pexp_hole

Conversation

@dra27

@dra27 dra27 commented Jul 30, 2026

Copy link
Copy Markdown
Owner

This PR first extends the parser to recognise syntax errors specifically related to invalid uses of _ in more places. It then converts these illegal _ to a new parsetree node, moving the error handling for encountering them to the type checker. The value of this change is that ppxes can both transform parsetrees which include the new node, and also use the OCaml parser to parse attribute and extension point payloads which include them.

The name Pexp_hole comes from the (highly experimental) feature in oxcaml/oxcaml#3310 which introduced it. Although the feature in OxCaml remains incomplete, we have found the node to be very useful in ppxes, and the parsing was extended in oxcaml/oxcaml#4956 entirely with ppx use in mind. We think the node is a useful addition to the parsetree, even without a language feature that directly requires it.

The PR itself is three fairly straightforward commits, which are worth reviewing separately:

  • The first adds a parse-errors test which shows the existing fun_expr uses of _ displaying the existing "unexpected wildcard" parsing error, and all the additional simple_expr points where a standard "syntax error" is displayed
  • The second commit (essentially taking the grammar changes of Parse Pexp_hole as a simple expression oxcaml/oxcaml#4956, without the parsetree node), extends the handling of _ to simple_expr, which can be seen in the parse-errors test output
  • The final commit then creates the parse tree node, removing the hack from the parser and instead converting the error to come directly from the type checker

There are various subtleties (visible in the reference files) to do with _ and ~/? corner cases. For information, the original work predates LLM-use - Claude's responsibility in this PR was refactoring the work to be a syntax change, then a parsetree change, and adding the tests to demonstrate the effects.

Repository owner deleted a comment from github-actions Bot Jul 30, 2026
@dra27 dra27 changed the title Upstream Pexp_hole Add parsetree support for wildcards in simple expressions Aug 6, 2026
@dra27-js
dra27-js force-pushed the upstream-Pexp_hole branch 3 times, most recently from 07d22b8 to e7bb8f4 Compare August 7, 2026 06:21
dra27 and others added 3 commits August 7, 2026 07:24
Record the current behaviour of the wildcard `_` appearing in expression
positions:
- `~_:` and `?_:` already lex as labels named `_`
- where a general expression can start, the parser recognizes `_` via
  an ad hoc rule in order to report a specific syntax error;
- where only a simple expression is allowed (function-argument
  positions), "_" is a plain syntax error.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@dra27-js
dra27-js force-pushed the upstream-Pexp_hole branch from e7bb8f4 to 53fcb48 Compare August 7, 2026 06:24
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@dra27-js
dra27-js force-pushed the upstream-Pexp_hole branch from 53fcb48 to 57ee73d Compare August 7, 2026 06:29
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@dra27-js
dra27-js force-pushed the upstream-Pexp_hole branch from 57ee73d to db22502 Compare August 7, 2026 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant