1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Parse.Declaration
10import Data.Text (Text)
11import qualified Data.Text.Encoding as Text
13import qualified AST.Source as Src
14import qualified AST.Utils.Binop as Binop
15import qualified Parse.Expression as Expr
16import qualified Parse.Pattern as Pattern
17import Parse.Primitives
18import qualified Parse.Primitives.Keyword as Keyword
19import qualified Parse.Primitives.Number as Number
20import qualified Parse.Primitives.Symbol as Symbol
21import qualified Parse.Primitives.Variable as Var
22import qualified Parse.Primitives.Whitespace as W
23import qualified Parse.Type as Type
24import qualified Reporting.Annotation as A
25import qualified Reporting.Error.Syntax as E
26import qualified Reporting.Region as R
33declaration :: SParser Src.Decl
36 do start <- getPosition
50doc_ :: R.Position -> SParser Src.Decl
52 do doc <- W.docComment
55 return ( A.at start end (Src.Docs (Text.decodeUtf8 doc)), end, pos )
59-- DEFINITION and ANNOTATION
63def_ :: R.Position -> SParser Src.Decl
66 nameEnd <- getPosition
67 let locatedName = A.at start nameEnd name
71 inContext start (E.Annotation name) $
73 (tipe, end, space) <- Type.expression
74 return ( A.at start end (Src.Annotation locatedName tipe), end, space )
75 , inContext start (E.Definition name) $
76 definitionHelp start locatedName []
80definitionHelp :: R.Position -> A.Located Text -> [Src.Pattern] -> SParser Src.Decl
81definitionHelp start name revArgs =
83 [ do arg <- hint E.Arg Pattern.term
85 definitionHelp start name (arg : revArgs)
88 (body, end, space) <- Expr.expression
89 let def = A.at start end (Src.Definition name (reverse revArgs) body)
90 return ( def, end, space )
95-- TYPE ALIAS and UNION TYPES
99type_ :: R.Position -> SParser Src.Decl
105 inContext start E.TypeAlias $
107 (name, args) <- nameArgsEquals
108 (tipe, end, pos) <- Type.expression
109 return ( A.at start end (Src.Alias name args tipe), end, pos )
110 , inContext start E.TypeUnion $
111 do (name, args) <- nameArgsEquals
112 (firstCtor, firstEnd, firstSpace) <- Type.unionConstructor
113 (ctors, end, pos) <- chompConstructors [firstCtor] firstEnd firstSpace
114 return ( A.at start end (Src.Union name args ctors), end, pos )
118nameArgsEquals :: Parser (A.Located Text, [A.Located Text])
120 do name <- addLocation Var.upper
122 nameArgsEqualsHelp name []
125nameArgsEqualsHelp :: A.Located Text -> [A.Located Text] -> Parser (A.Located Text, [A.Located Text])
126nameArgsEqualsHelp name args =
128 [ do arg <- addLocation Var.lower
130 nameArgsEqualsHelp name (arg:args)
133 return ( name, reverse args )
137chompConstructors :: [(A.Located Text, [Src.Type])] -> R.Position -> SPos -> SParser [(A.Located Text, [Src.Type])]
138chompConstructors ctors end pos =
143 (ctor, newEnd, newSpace) <- Type.unionConstructor
144 chompConstructors (ctor:ctors) newEnd newSpace
145 , return ( reverse ctors, end, pos )
154port_ :: R.Position -> SParser Src.Decl
157 inContext start E.Port $
159 name <- addLocation Var.lower
163 (tipe, end, pos) <- Type.expression
164 return ( A.at start end (Src.Port name tipe), end, pos )
171-- INVARIANT: always chomps to a freshline
173infix_ :: Parser Src.Decl
175 do start <- getPosition
177 inContext start E.Infix $
181 [ Keyword.left_ >> return Binop.Left
182 , Keyword.right_ >> return Binop.Right
183 , Keyword.non_ >> return Binop.Non
186 precedence <- Number.precedence
196 checkFreshLine =<< W.whitespace
197 return (A.at start end (Src.Binop op associativity precedence name))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list