tweag / nickel

Better configuration for less
https://nickel-lang.org/
MIT License
2.23k stars 85 forks source link

Or patterns #1916

Closed yannham closed 1 month ago

yannham commented 1 month ago

Depends on #1912.

This PR introduces or-patterns, which allows to express alternatives within patterns, including in a deep subpattern.

The compilation of or-patterns is rather simple: we simply try each alternative until one matches, and use the corresponding bindings.

Typechecking of or-patterns can be done following the same process as for typechecking a whole match expression (which is also a disjunction of patterns), although the treatment of bound variables is a bit different.

Most of the complexity of this PR comes from the fact that we don't want to make or a reserved language keyword, which would break backward compatibility. This is possible, because or in pattern can't be confused with an identifier, but it requires some tweaking to make our LALR(1) parser accept this.

Syntax

Beside the technicalities around making the grammar unambiguous, this PR also imposes parentheses around enum variant patterns which are part of an or-pattern. That is, it forces to write ('Foo x) or ('Bar x) instead of 'Foo x or 'Bar x, which is arguably less readable. Even less so when the identifier is or: 'Foo or or 'Bar or :cold_sweat:

dpulls[bot] commented 1 month ago

:tada: All dependencies have been resolved !