forked from gren-lang/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt at allowing wildcard patterns '_' to have a variable na…
…me after, whih is intended to allow documentation of the value that is being ignored: gren-lang#111
- Loading branch information
1 parent
dfe645f
commit 860ca4f
Showing
5 changed files
with
92 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Parse.UnderscorePatternSpec where | ||
|
||
import AST.Source qualified as Src | ||
import Data.ByteString qualified as BS | ||
import Helpers.Instances () | ||
import Parse.Pattern qualified as Pattern | ||
import Parse.Primitives qualified as P | ||
import Reporting.Annotation qualified as A | ||
import Test.Hspec ( Spec ) | ||
import Test.Hspec qualified as Hspec | ||
|
||
data ParseError | ||
= ExprError P.Row P.Col | ||
| OtherError String P.Row P.Col | ||
deriving (Show, Eq) | ||
|
||
spec :: Spec | ||
spec = do | ||
Hspec.describe "Wildcard patterns" $ do | ||
Hspec.it "regression test" $ | ||
parse "_" | ||
Hspec.it "Newly allowed named wildcard pattern" $ do | ||
parse "_argument" | ||
Hspec.it "You can have underscores as part of the lower variable which follows the underscore" $ do | ||
parse "_hello_world" | ||
Hspec.it "Keywords are not allowed as the whole variable part of an underscore pattern" $ do | ||
failToParse "_let" | ||
Hspec.it "But you can have a keyword as **part** of a variable name just as for normal variable names." $ do | ||
parse "_let_down" | ||
Hspec.it "But you cannot start with multiple underscores" $ do | ||
failToParse "__hello" | ||
Hspec.it "But it must be an lower name, for an underscore pattern" $ do | ||
failToParse "_Hello" | ||
|
||
attemptParse :: (Either ParseError (Src.Pattern, A.Position) -> Bool) -> BS.ByteString -> IO () | ||
attemptParse checkResult str = | ||
Hspec.shouldSatisfy | ||
( P.fromByteString | ||
(P.specialize (\_ row col -> ExprError row col) Pattern.expression) | ||
(OtherError "fromByteString failed") | ||
str | ||
) | ||
checkResult | ||
|
||
parse :: BS.ByteString -> IO () | ||
parse = | ||
let | ||
isWildCardPattern :: Either x (Src.Pattern, A.Position) -> Bool | ||
isWildCardPattern result = | ||
case result of | ||
Right (A.At _ Src.PAnything, _) -> True | ||
_ -> False | ||
in | ||
attemptParse isWildCardPattern | ||
|
||
|
||
failToParse :: BS.ByteString -> IO () | ||
failToParse = | ||
let | ||
isError :: Either x (Src.Pattern, A.Position) -> Bool | ||
isError result = | ||
case result of | ||
Left _ -> | ||
True | ||
_ -> | ||
False | ||
in | ||
attemptParse isError |