1{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Parse.Type (expression, unionConstructor) where
7import qualified AST.Source as Src
8import Parse.Primitives (Parser, SParser, addLocation, checkSpace, getPosition, hint, spaces, oneOf)
9import qualified Parse.Primitives as P
10import qualified Parse.Primitives.Symbol as Symbol
11import qualified Parse.Primitives.Variable as Var
12import Parse.Primitives.Whitespace (SPos, whitespace)
14import qualified Reporting.Annotation as A
15import qualified Reporting.Error.Syntax as E
16import qualified Reporting.Region as R
23term :: Parser Src.Type
26 do start <- getPosition
39variable :: R.Position -> Parser Src.Type
43 return (A.at start end (Src.TVar var))
50expression :: SParser Src.Type
53 do start <- getPosition
54 (tipe1, end1, pos1) <-
57 , (,,) <$> term <*> getPosition <*> whitespace
63 (tipe2, end2, pos2) <- expression
64 let tipe = A.at start end2 (Src.TLambda tipe1 tipe2)
65 return ( tipe, end2, pos2 )
66 , return ( tipe1, end1, pos1 )
74app0 :: Parser Src.Type
76 do start <- getPosition
77 upper <- Var.foreignUpper
79 let region = R.Region start end
80 return $ A.At region $
82 Var.Unqualified name ->
83 Src.TType region name []
85 Var.Qualified home name ->
86 Src.TTypeQual region home name []
89app :: R.Position -> SParser Src.Type
91 do upper <- Var.foreignUpper
92 nameEnd <- getPosition
94 (args, end, pos) <- eatArgs [] nameEnd namePos
96 let region = R.Region start nameEnd
99 Var.Unqualified name ->
100 Src.TType region name args
102 Var.Qualified home name ->
103 Src.TTypeQual region home name args
105 return ( A.at start end tipe, end, pos )
108unionConstructor :: SParser (A.Located Text, [Src.Type])
110 do start <- getPosition
112 nameEnd <- getPosition
113 namePos <- whitespace
114 (args, end, pos) <- eatArgs [] nameEnd namePos
115 return ( (A.at start nameEnd name, args), end, pos )
118eatArgs :: [Src.Type] -> R.Position -> SPos -> SParser [Src.Type]
119eatArgs args end pos =
123 newEnd <- getPosition
124 newSpace <- whitespace
125 eatArgs (arg:args) newEnd newSpace
126 , return ( reverse args, end, pos )
134tuple :: R.Position -> Parser Src.Type
137 P.inContext start E.TypeTuple $
139 [ do Symbol.rightParen
141 return (A.at start end Src.TUnit)
143 (tipe, _, pos) <- expression
145 tupleEnding start tipe []
149tupleEnding :: R.Position -> Src.Type -> [Src.Type] -> Parser Src.Type
150tupleEnding start firstType revTypes =
154 (tipe, _, pos) <- expression
156 tupleEnding start firstType (tipe : revTypes)
157 , do Symbol.rightParen
159 case reverse revTypes of
163 secondType : otherTypes ->
164 return $ A.at start end $
165 Src.TTuple firstType secondType otherTypes
173record :: R.Position -> Parser Src.Type
176 P.inContext start E.TypeRecord $
179 [ do Symbol.rightCurly
181 return (A.at start end (Src.TRecord [] Nothing))
182 , do var <- addLocation Var.lower
188 fields <- chompFields [firstField]
190 return (A.at start end (Src.TRecord fields (Just (A.map Src.TVar var))))
193 (tipe, _, nextPos) <- expression
195 fields <- chompFields [(var, tipe)]
197 return (A.at start end (Src.TRecord fields Nothing))
202type Field = ( A.Located Text, Src.Type )
205chompFields :: [Field] -> Parser [Field]
211 chompFields (f : fields)
212 , do Symbol.rightCurly
213 return (reverse fields)
219 do name <- addLocation Var.lower
223 (tipe, _, endPos) <- expression
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