on 4bec310 help sign in
elk/compiler/Parse/Declaration.hs
59 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 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 return (D.TypeAlias name args tipe)
29datatype :: IParser D.ParseDeclaration
30datatype = do
31 reserved "data" <?> "datatype definition (data T = A | B | ...)"
32 forcedWS
33 name <- capVar <?> "name of data-type"
34 args <- spacePrefix lowVar
35 padded equals
36 tcs <- pipeSep1 Type.constructor
37 return $ D.Datatype name args tcs
40infixDecl :: IParser D.ParseDeclaration
41infixDecl = do
42 assoc <- choice [ reserved "infixl" >> return D.L
43 , reserved "infix" >> return D.N
44 , reserved "infixr" >> return D.R ]
45 forcedWS
46 n <- digit
47 forcedWS
48 D.Fixity assoc (read [n]) <$> anyOp
51port :: IParser D.ParseDeclaration
52port =
53 do try (reserved "port")
54 whitespace
55 name <- lowVar
56 whitespace
57 let port' op ctor expr = do { try op ; whitespace ; ctor name <$> expr }
58 D.Port <$> choice [ port' hasType D.PPAnnotation Type.expr
59 , port' equals D.PPDef Expr.expr ]