git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Empty data decls and empty type class decls
commit64103694e0aa2545149819932566ac4f1b34f1e9 authorPhil Freeman <paf31@cantab.net> dateThu 27 Feb 2014, 15:10 (13 years ago) parent3f63a37
3 files changed+49−8
examples/passing/EmptyDataDecls.purs+20 examples/passing/EmptyTypeClass.purs+18 src/Language/PureScript/Parser/Declarations.hs+11−8
examples/passing/EmptyDataDecls.pursnew file+20
@@ -0,0 +1,20 @@
1+module Main where
2+
3+import Prelude
4+import Data.Array
5+
6+data Z
7+data S n
8+
9+data Array n a = Array [a]
10+
11+nil :: forall a. Array Z a
12+nil = Array []
13+
14+cons :: forall a n. a -> Array n a -> Array (S n) a
15+cons x (Array xs) = Array $ x : xs
16+
17+main = let (Array xs) = cons 1 $ cons 2 $ cons 3 nil
18+ in if xs == [1, 2, 3]
19+ then Debug.Trace.trace "Done"
20+ else Control.Monad.Error.throwError "Failed"
examples/passing/EmptyTypeClass.pursnew file+18
@@ -0,0 +1,18 @@
1+module EmptyTypeClass where
2+
3+class Partial
4+
5+head :: forall a. (Partial) => [a] -> a
6+head (x:xs) = x
7+
8+module AllowPartialFns where
9+
10+instance EmptyTypeClass.Partial
11+
12+module Main where
13+
14+import Prelude
15+import AllowPartialFns
16+import EmptyTypeClass
17+
18+main = Debug.Trace.trace $ head ["Done"]
src/Language/PureScript/Parser/Declarations.hs+11−8
@@ -36,8 +36,9 @@ parseDataDeclaration = do
3636 reserved "data"
3737 name <- indented *> properName
3838 tyArgs <- many (indented *> identifier)
39− _ <- lexeme $ indented *> P.char '='
40− ctors <- sepBy1 ((,) <$> properName <*> P.many (indented *> parseTypeAtom)) pipe
39+ ctors <- P.option [] $ do
40+ _ <- lexeme $ indented *> P.char '='
41+ sepBy1 ((,) <$> properName <*> P.many (indented *> parseTypeAtom)) pipe
4142 return $ DataDeclaration name tyArgs ctors
4243
4344parseTypeDeclaration :: P.Parsec String ParseState Declaration
@@ -94,9 +95,10 @@ parseTypeClassDeclaration :: P.Parsec String ParseState Declaration
9495parseTypeClassDeclaration = do
9596 reserved "class"
9697 className <- indented *> properName
97− idents <- indented *> P.many identifier
98− indented *> reserved "where"
99− members <- mark (P.many (same *> parseTypeDeclaration))
98+ idents <- P.many (indented *> identifier)
99+ members <- P.option [] . P.try $ do
100+ indented *> reserved "where"
101+ mark (P.many (same *> parseTypeDeclaration))
100102 return $ TypeClassDeclaration className idents members
101103
102104parseTypeInstanceDeclaration :: P.Parsec String ParseState Declaration
@@ -108,9 +110,10 @@ parseTypeInstanceDeclaration = do
108110 reservedOp "=>"
109111 return deps
110112 className <- indented *> parseQualified properName
111− ty <- indented *> P.many parseTypeAtom
112− indented *> reserved "where"
113− members <- mark (P.many (same *> parseValueDeclaration))
113+ ty <- P.many (indented *> parseTypeAtom)
114+ members <- P.option [] . P.try $ do
115+ indented *> reserved "where"
116+ mark (P.many (same *> parseValueDeclaration))
114117 return $ TypeInstanceDeclaration (fromMaybe [] deps) className ty members
115118
116119-- |