Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions LayeredHaskell-make/src/LHMake/Feature/B/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Bin :: { LHsBin }
-------------------------------------------------------------------------------
-- Patterns

PatCon :: { L LHsPat }
: '-' int { pneg $1 $2 }

Comment on lines +112 to +114

@hshkk hshkk Nov 24, 2026 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main addition is a PatCon nonterminal that is subordinate to Pat, but greater in precedence over PatAtom. PatCon contains pattern constructors (added by D) and negation patterns (added by B, as seen here).

PatAtom :: { L LHsPat }
: int { pint $1 }
-- This rule is more general than it needs to be due to an
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion LayeredHaskell-make/src/LHMake/Feature/D/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion LayeredHaskell-make/src/LHMake/Feature/E/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down Expand Up @@ -146,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.
Expand Down
1 change: 0 additions & 1 deletion LayeredHaskell-make/src/LHMake/Feature/Features
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
P
B
2 changes: 1 addition & 1 deletion LayeredHaskell-make/src/LHMake/Feature/L/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
5 changes: 2 additions & 3 deletions src/LH/Text/Parser/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines +179 to +180

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message for lexical errors was slightly revised.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placement of these errors might be revised once we implement errors more properly.

Nothing
buildError file (UnclosedComment pos) =
errataSimple
Expand All @@ -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"

8 changes: 8 additions & 0 deletions test/LH/Test/Text/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down Expand Up @@ -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]
Expand Down
Loading