on 577fc17 help sign in
elk/compiler/Parse/Declaration.hs
58 lines 1.6 kB history raw
1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2module Parse.Declaration where
4import Control.Applicative ((<$>))
5import Text.Parsec hiding (newline,spaces)
7import Parse.Helpers
8import qualified Parse.Expression as Expr
9import qualified Parse.Type as Type
10import qualified AST.Declaration as D
12declaration :: IParser D.SourceDecl
13declaration = alias <|> datatype <|> infixDecl <|> port <|> definition
15definition :: IParser D.SourceDecl
16definition = D.Definition <$> Expr.def
18alias :: IParser D.SourceDecl
19alias = do
20 reserved "type" <?> "type alias (type Point = {x:Int, y:Int})"
21 forcedWS
22 name <- capVar
23 args <- spacePrefix lowVar
24 padded equals
25 tipe <- Type.expr
26 return (D.TypeAlias name args tipe)
28datatype :: IParser D.SourceDecl
29datatype = do
30 reserved "data" <?> "datatype definition (data T = A | B | ...)"
31 forcedWS
32 name <- capVar <?> "name of data-type"
33 args <- spacePrefix lowVar
34 padded equals
35 tcs <- pipeSep1 Type.constructor
36 return $ D.Datatype name args tcs
39infixDecl :: IParser D.SourceDecl
40infixDecl = do
41 assoc <- choice [ reserved "infixl" >> return D.L
42 , reserved "infix" >> return D.N
43 , reserved "infixr" >> return D.R ]
44 forcedWS
45 n <- digit
46 forcedWS
47 D.Fixity assoc (read [n]) <$> anyOp
50port :: IParser D.SourceDecl
51port =
52 do try (reserved "port")
53 whitespace
54 name <- lowVar
55 whitespace
56 let port' op ctor expr = do { try op ; whitespace ; ctor name <$> expr }
57 D.Port <$> choice [ port' hasType D.PPAnnotation Type.expr
58 , port' equals D.PPDef Expr.expr ]