on 501c607 help sign in
elk/src/Parse/Declaration.hs
95 lines 2.4 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 <|> loopback <|> input <|> output <|> definition
18definition :: IParser D.SourceDecl
19definition =
20 D.Definition <$> Expr.def
23typeDecl :: IParser D.SourceDecl
24typeDecl =
25 do try (reserved "type") <?> "type declaration"
26 forcedWS
27 isAlias <- optionMaybe (string "alias" >> forcedWS)
29 name <- capVar
30 args <- spacePrefix lowVar
31 padded equals
33 case isAlias of
34 Just _ ->
35 do tipe <- Type.expr <?> "a type"
36 return (D.TypeAlias name args tipe)
38 Nothing ->
39 do tcs <- pipeSep1 Type.constructor <?> "a constructor for a union type"
40 return $ D.Datatype name args tcs
43infixDecl :: IParser D.SourceDecl
44infixDecl =
45 do assoc <-
46 choice
47 [ try (reserved "infixl") >> return D.L
48 , try (reserved "infixr") >> return D.R
49 , try (reserved "infix") >> return D.N
50 ]
51 forcedWS
52 n <- digit
53 forcedWS
54 D.Fixity assoc (read [n]) <$> anyOp
57input :: IParser D.SourceDecl
58input =
59 do name <- wireBegining "input"
60 wireEnding (D.InputAnnotation name) hasType Type.expr
63output :: IParser D.SourceDecl
64output =
65 do name <- wireBegining "output"
66 choice
67 [ wireEnding (D.OutputAnnotation name) hasType Type.expr
68 , wireEnding (D.OutputDefinition name) equals Expr.expr
69 ]
72loopback :: IParser D.SourceDecl
73loopback =
74 do name <- wireBegining "loopback"
75 choice
76 [ wireEnding (D.LoopbackAnnotation name) hasType Type.expr
77 --, wireEnding (D.LoopbackDefinition name) equals Expr.expr
78 ]
81wireBegining :: String -> IParser String
82wireBegining keyword =
83 do try (reserved keyword)
84 whitespace
85 name <- lowVar
86 whitespace
87 return name
90wireEnding :: (a -> D.RawWire) -> IParser String -> IParser a -> IParser D.SourceDecl
91wireEnding makeWire op valueParser =
92 do op
93 whitespace
94 value <- valueParser
95 return (D.Wire (makeWire value))