Skip to content

Commit

Permalink
fix parsing of constructor patterns with args without parens
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed May 5, 2024
1 parent 517a2b0 commit aadac49
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/Elara/Data/TopologicalGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Data.Containers.ListUtils (nubOrd)
import Data.Graph
import Elara.Data.Pretty
import Elara.Utils (uncurry3)
import Print (debugPretty)
import Relude.Extra (firstF)
import Text.Show qualified as Show

Expand Down
13 changes: 2 additions & 11 deletions src/Elara/Parse/Pattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ patParser :: Parser FrontendPattern
patParser =
choice
[ try literalPattern
, inParens apat
, inParens rpat
, varPattern
, zeroArgConstructorPattern
, constructorPattern
, wildcardPattern
, listPattern
]

apat :: Parser FrontendPattern
apat = constructorPattern <|> rpat

rpat :: Parser FrontendPattern
rpat =
makeExprParser
Expand Down Expand Up @@ -68,12 +65,6 @@ listPattern = locatedPattern $ do

-- pure $ ConsPattern head' tail'

-- To prevent ambiguity between space-separated patterns and constructor patterns
zeroArgConstructorPattern :: Parser FrontendPattern
zeroArgConstructorPattern = locatedPattern $ do
con <- located conName
pure $ ConstructorPattern con []

constructorPattern :: Parser FrontendPattern
constructorPattern = locatedPattern $ do
con <- located conName
Expand Down
4 changes: 3 additions & 1 deletion test/Arbitrary/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ genPattern =
]
[ Gen.subterm2 genPattern genPattern (\x y -> mkPat (ConsPattern x y))
, mkPat . ListPattern <$> Gen.list (Range.linear 0 5) genPattern
, (\x y -> mkPat (ConstructorPattern x y)) <$> genMaybeQualified genTypeName <*> Gen.list (Range.linear 0 5) genPattern
, (\x y -> mkPat (ConstructorPattern x y))
<$> genMaybeQualified genTypeName
<*> Gen.list (Range.linear 0 5) genPattern
]

genBinaryOperator :: Gen (BinaryOperator 'UnlocatedFrontend)
Expand Down
10 changes: 8 additions & 2 deletions test/Parse/Patterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ constructorPatterns = describe "Parses constructor parens" $ do
it "Parses constructor patterns correctly" $ hedgehog $ do
"ZeroArity" `shouldParsePattern` Pattern (ConstructorPattern "ZeroArity" [], Nothing)

it "Fails with multiple arguments without parens" $ hedgehog $ do
shouldFailToParse "TwoArgs 1 2"
it "Parses single constructor pattern without parens" $ hedgehog $ do
"TwoArgs 1 2"
`shouldParsePattern` Pattern
( ConstructorPattern
"TwoArgs"
[Pattern (IntegerPattern 1, Nothing), Pattern (IntegerPattern 2, Nothing)]
, Nothing
)

it "Parses constructor patterns with parens correctly" $ hedgehog $ do
"(TwoArgs one two)"
Expand Down

0 comments on commit aadac49

Please sign in to comment.