on d87affe help sign in
elk/src/Parse/Declaration.hs
62 lines 1.7 kB history raw
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2module Parse.Declaration where
4import Control.Applicative ((<$>))
5import Text.Parsec ((<|>), (<?>), choice, digit, optionMaybe, string, try)
7import qualified AST.Declaration as D
8import qualified Parse.Expression as Expr
9import Parse.Helpers
10import qualified Parse.Type as Type
13declaration :: IParser D.SourceDecl
14declaration =
15 typeDecl <|> infixDecl <|> port <|> definition
18definition :: IParser D.SourceDecl
19definition = D.Definition <$> Expr.def
22typeDecl :: IParser D.SourceDecl
23typeDecl =
24 do reserved "type" <?> "type declaration"
25 forcedWS
26 isAlias <- optionMaybe (string "alias" >> forcedWS)
27 case isAlias of
28 Just _ ->
29 do name <- capVar
30 args <- spacePrefix lowVar
31 padded equals
32 tipe <- Type.expr
33 return (D.TypeAlias name args tipe)
35 Nothing ->
36 do name <- capVar <?> "name of data-type"
37 args <- spacePrefix lowVar
38 padded equals
39 tcs <- pipeSep1 Type.constructor
40 return $ D.Datatype name args tcs
43infixDecl :: IParser D.SourceDecl
44infixDecl = do
45 assoc <- choice [ reserved "infixl" >> return D.L
46 , reserved "infix" >> return D.N
47 , reserved "infixr" >> return D.R ]
48 forcedWS
49 n <- digit
50 forcedWS
51 D.Fixity assoc (read [n]) <$> anyOp
54port :: IParser D.SourceDecl
55port =
56 do try (reserved "port")
57 whitespace
58 name <- lowVar
59 whitespace
60 let port' op ctor expr = do { try op ; whitespace ; ctor name <$> expr }
61 D.Port <$> choice [ port' hasType D.PPAnnotation Type.expr
62 , port' equals D.PPDef Expr.expr ]