From 27b8d73b72cb521ec761ed476eca699fff5a89d4 Mon Sep 17 00:00:00 2001 From: hshkk <190803096+hshkk@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:13:42 -0800 Subject: [PATCH 1/2] Incorporating negative integers into patterns --- LayeredHaskell-make/src/LHMake/Feature/B/Parser.y | 10 ++++++++++ LayeredHaskell-make/src/LHMake/Feature/D/Parser.y | 2 +- LayeredHaskell-make/src/LHMake/Feature/E/Parser.y | 3 +++ LayeredHaskell-make/src/LHMake/Feature/Features | 1 - LayeredHaskell-make/src/LHMake/Feature/L/Parser.y | 2 +- test/LH/Test/Text/Parser.hs | 8 ++++++++ 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/LayeredHaskell-make/src/LHMake/Feature/B/Parser.y b/LayeredHaskell-make/src/LHMake/Feature/B/Parser.y index ee61db8..10b88c8 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/B/Parser.y +++ b/LayeredHaskell-make/src/LHMake/Feature/B/Parser.y @@ -109,6 +109,9 @@ Bin :: { LHsBin } ------------------------------------------------------------------------------- -- Patterns +PatCon :: { L LHsPat } + : '-' int { pneg $1 $2 } + PatAtom :: { L LHsPat } : int { pint $1 } -- This rule is more general than it needs to be due to an @@ -137,4 +140,11 @@ pint :: LHsLexeme -> L LHsPat pint (LHsLexeme sp (LHsTkInt n)) = L sp (LHsPInt n) -- The below case will never happen. pint _ = undefined + +-- | Parses a negative integer as a pattern. +pneg :: LHsLexeme -> LHsLexeme -> L LHsPat +pneg (LHsLexeme sp1 _) (LHsLexeme sp2 (LHsTkInt n)) + = L (sp1 >< sp2) (LHsPInt (negate n)) +-- The below case will never happen. +pneg _ _ = undefined } \ No newline at end of file diff --git a/LayeredHaskell-make/src/LHMake/Feature/D/Parser.y b/LayeredHaskell-make/src/LHMake/Feature/D/Parser.y index f5187af..cb4bcf6 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/D/Parser.y +++ b/LayeredHaskell-make/src/LHMake/Feature/D/Parser.y @@ -78,7 +78,7 @@ Decl :: { L LHsDecl } ------------------------------------------------------------------------------- -- Patterns -Pat :: { L LHsPat } +PatCon :: { L LHsPat } : con PatApp { L ($1 >< $2) (LHsPCon (ident $1) (un $2)) } PatAtom :: { L LHsPat } diff --git a/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y b/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y index 8aa5a71..87596f2 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y +++ b/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y @@ -112,6 +112,9 @@ Pats :: { Seq (L LHsPat) } : {- ε -} { Seq.empty } Pat :: { L LHsPat } + : PatCon { $1 } + +PatCon :: { L LHsPat } : PatAtom { $1 } PatApp :: { L (Seq (L LHsPat)) } diff --git a/LayeredHaskell-make/src/LHMake/Feature/Features b/LayeredHaskell-make/src/LHMake/Feature/Features index a5c48a0..223b783 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/Features +++ b/LayeredHaskell-make/src/LHMake/Feature/Features @@ -1,2 +1 @@ -P B diff --git a/LayeredHaskell-make/src/LHMake/Feature/L/Parser.y b/LayeredHaskell-make/src/LHMake/Feature/L/Parser.y index 6d637ba..e95cd26 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/L/Parser.y +++ b/LayeredHaskell-make/src/LHMake/Feature/L/Parser.y @@ -81,7 +81,7 @@ CommaExprs :: { Seq (L LHsExpr) } -- Patterns Pat :: { L LHsPat } - : PatAtom ':' Pat { L ($1 >< $3) (LHsPListCons $1 $3) } + : PatCon ':' Pat { L ($1 >< $3) (LHsPListCons $1 $3) } PatAtom :: { L LHsPat } : '[' ListPat ']' { L ($1 >< $3) (LHsPList $2) } diff --git a/test/LH/Test/Text/Parser.hs b/test/LH/Test/Text/Parser.hs index 5682f26..dfed04b 100644 --- a/test/LH/Test/Text/Parser.hs +++ b/test/LH/Test/Text/Parser.hs @@ -292,6 +292,10 @@ fb = "function definition with integer pattern matching" "fac 0 = 1" "LHsEqn \"fac\" (fromList [LHsPInt 0]) (LHsInt 1)" + , source + "function definition with negative integer pattern matching" + "negate (-1) = 1" + "LHsEqn \"negate\" (fromList [LHsPInt (-1)]) (LHsInt 1)" , source "recursive function definition with an integer binary operation and function application" "fac n = n * fac (n-1)" @@ -624,6 +628,10 @@ lb = "arithmetic operators bind stronger than cons" "f = 1+2:3*4:[] == [7]" "LHsEqn \"f\" (fromList []) (LHsBin LHsEq (LHsBin LHsListCons (LHsBin LHsAdd (LHsInt 1) (LHsInt 2)) (LHsBin LHsListCons (LHsBin LHsMul (LHsInt 3) (LHsInt 4)) (LHsList (fromList [])))) (LHsList (fromList [LHsInt 7])))" + , source + "pattern matching with cons and a negative integer" + "f (-1:n) = n" + "LHsEqn \"f\" (fromList [LHsPListCons (LHsPInt (-1)) (LHsPVar \"n\")]) (LHsVar \"n\")" ] pl :: [Source] From 0ea3395efc28ba529fd1b6780bf03065eb1941e1 Mon Sep 17 00:00:00 2001 From: hshkk <190803096+hshkk@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:42:50 -0800 Subject: [PATCH 2/2] Simplifying lexical errors --- LayeredHaskell-make/src/LHMake/Feature/E/Parser.y | 1 - src/LH/Text/Parser/Monad.hs | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y b/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y index 87596f2..c2411ad 100644 --- a/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y +++ b/LayeredHaskell-make/src/LHMake/Feature/E/Parser.y @@ -149,7 +149,6 @@ parseProgram = runLHsParser (beginProgram >> program) -- | Reports a token that the parser did not expect. parseError :: LHsLexeme -> LHsParser a parseError (LHsLexeme sp tok) = do - end <- fmap aiPos getInput throwError (ParseError sp tok) -- | Parses an identifier. diff --git a/src/LH/Text/Parser/Monad.hs b/src/LH/Text/Parser/Monad.hs index f5cc9e0..ab26203 100644 --- a/src/LH/Text/Parser/Monad.hs +++ b/src/LH/Text/Parser/Monad.hs @@ -176,8 +176,8 @@ data LHsTextError instance LHsError LHsTextError where buildError file (LexicalError pos bad) = errataSimple - (Just ("Lexical error: unexpected `" <> bad <> "`")) - (underline file (pos +> Text.length bad) "this is not a valid token") + (Just ("Unexpected '" <> bad <> "'")) + (underline file (pos +> Text.length bad) "not sure how to interpret this symbol") Nothing buildError file (UnclosedComment pos) = errataSimple @@ -195,4 +195,3 @@ instance LHsError LHsTextError where LHsTkVSemi -> "this starts a new declaration, but the previous one is unfinished" LHsTkVClose -> "the block ends here, but something before it is unfinished" _ -> "this was not expected here" -