From c6bc1e0de579ea9cff446a707ba15182735c4526 Mon Sep 17 00:00:00 2001 From: Christoph Hegemann Date: Mon, 27 Oct 2025 09:46:20 +0100 Subject: [PATCH] feat: relaxes grammar to allow un-parenthesized unary operator patterns --- doc/md/examples/grammar.txt | 2 +- src/mo_frontend/parser.mly | 14 +++++++------- test/run/ok/pat-subtyping.tc.ok | 2 +- test/run/pat-subtyping.mo | 2 +- test/run/switch.mo | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/doc/md/examples/grammar.txt b/doc/md/examples/grammar.txt index 27736fcc8d9..33a367f4934 100644 --- a/doc/md/examples/grammar.txt +++ b/doc/md/examples/grammar.txt @@ -309,6 +309,7 @@ '_' + '(' , ',')> ')' ::= @@ -320,7 +321,6 @@ '#' '#' '?' - ::= diff --git a/src/mo_frontend/parser.mly b/src/mo_frontend/parser.mly index 6d203254512..499f7e82d46 100644 --- a/src/mo_frontend/parser.mly +++ b/src/mo_frontend/parser.mly @@ -891,6 +891,13 @@ pat_plain : { VarP(x) @! at $sloc } | l=lit { LitP(ref l) @! at $sloc } + | op=unop l=lit + { match op, l with + | (PosOp | NegOp), PreLit (s, (Type.(Nat | Float) as typ)) -> + let signed = match op with NegOp -> "-" ^ s | _ -> "+" ^ s in + LitP(ref (PreLit (signed, Type.(if typ = Nat then Int else typ)))) @! at $sloc + | _ -> SignP(op, ref l) @! at $sloc + } | LPAR ps=seplist(pat_bin, COMMA) RPAR { (match ps with [p] -> ParP(p) | _ -> TupP(ps)) @! at $sloc } @@ -909,13 +916,6 @@ pat_un : { TagP(x, p) @! at $sloc } | QUEST p=pat_un { OptP(p) @! at $sloc } - | op=unop l=lit - { match op, l with - | (PosOp | NegOp), PreLit (s, (Type.(Nat | Float) as typ)) -> - let signed = match op with NegOp -> "-" ^ s | _ -> "+" ^ s in - LitP(ref (PreLit (signed, Type.(if typ = Nat then Int else typ)))) @! at $sloc - | _ -> SignP(op, ref l) @! at $sloc - } pat_bin : | p=pat_un diff --git a/test/run/ok/pat-subtyping.tc.ok b/test/run/ok/pat-subtyping.tc.ok index ee6436c0bd3..82b14385aad 100644 --- a/test/run/ok/pat-subtyping.tc.ok +++ b/test/run/ok/pat-subtyping.tc.ok @@ -1,5 +1,5 @@ pat-subtyping.mo:6.8-6.9: warning [M0146], this pattern is never matched -pat-subtyping.mo:7.8-7.12: warning [M0146], this pattern is never matched +pat-subtyping.mo:7.8-7.10: warning [M0146], this pattern is never matched pat-subtyping.mo:10.8-10.17: warning [M0146], this pattern is never matched pat-subtyping.mo:11.8-11.17: warning [M0146], this pattern is never matched pat-subtyping.mo:18.8-18.25: warning [M0146], this pattern is never matched diff --git a/test/run/pat-subtyping.mo b/test/run/pat-subtyping.mo index dc6fb56345b..4c9a7fd2937 100644 --- a/test/run/pat-subtyping.mo +++ b/test/run/pat-subtyping.mo @@ -4,7 +4,7 @@ if false { switch (magic ()) { case 1 {}; // redundant - case (-1) {}; // redundant + case -1 {}; // redundant }; switch (magic ()) { case (1 : Nat) {}; // redundant diff --git a/test/run/switch.mo b/test/run/switch.mo index 057ff370079..d12cb2fa4dd 100644 --- a/test/run/switch.mo +++ b/test/run/switch.mo @@ -6,10 +6,10 @@ let x1 = switch 2 { assert (x1 == 1); let x2 : Int = switch (-3) { - case (0) 0; - case (-1) 2; - case (-3) 1; - case (x) x; + case 0 0; + case -1 2; + case -3 1; + case x x; }; assert (x2 == 1);