on 63243ca help sign in
elk/compiler/Parse/Declaration.hs
63 lines 1.8 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 SourceSyntax.Declaration as D
13declaration :: IParser D.ParseDeclaration
14declaration = alias <|> datatype <|> infixDecl <|> port <|> definition
16definition :: IParser D.ParseDeclaration
17definition = D.Definition <$> Expr.def
19alias :: IParser D.ParseDeclaration
20alias = do
21 reserved "type" <?> "type alias (type Point = {x:Int, y:Int})"
22 forcedWS
23 name <- capVar
24 args <- spacePrefix lowVar
25 padded equals
26 tipe <- Type.expr
27 json <- option [] $ do
28 try $ padded (reserved "deriving")
29 string "Json"
30 return [D.Json]
31 return (D.TypeAlias name args tipe json)
33datatype :: IParser D.ParseDeclaration
34datatype = do
35 reserved "data" <?> "datatype definition (data T = A | B | ...)"
36 forcedWS
37 name <- capVar <?> "name of data-type"
38 args <- spacePrefix lowVar
39 padded equals
40 tcs <- pipeSep1 Type.constructor
41 return $ D.Datatype name args tcs []
44infixDecl :: IParser D.ParseDeclaration
45infixDecl = do
46 assoc <- choice [ reserved "infixl" >> return D.L
47 , reserved "infix" >> return D.N
48 , reserved "infixr" >> return D.R ]
49 forcedWS
50 n <- digit
51 forcedWS
52 D.Fixity assoc (read [n]) <$> anyOp
55port :: IParser D.ParseDeclaration
56port =
57 do try (reserved "port")
58 whitespace
59 name <- lowVar
60 whitespace
61 let port' op ctor expr = do { try op ; whitespace ; ctor name <$> expr }
62 D.Port <$> choice [ port' hasType D.PPAnnotation Type.expr
63 , port' equals D.PPDef Expr.expr ]