git.iko.soy/rowtype-yoga/purescript-native
on d6ab2e7 help sign in
86 lines 2.7 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : PureScript.Parser.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15module PureScript.Parser.Types (
16 parseType,
17 parsePolyType,
18 parseRow
19) where
21import PureScript.Types
22import PureScript.Parser.Common
23import Control.Applicative
24import qualified Text.Parsec as P
25import qualified Text.Parsec.Indent as I
26import Control.Arrow (Arrow(..))
28parseNumber :: I.IndentParser String () Type
29parseNumber = const Number <$> P.string "Number"
31parseString :: I.IndentParser String () Type
32parseString = const String <$> P.string "String"
34parseBoolean :: I.IndentParser String () Type
35parseBoolean = const Boolean <$> P.string "Boolean"
37parseArray :: I.IndentParser String () Type
38parseArray = squares $ Array <$> parseType
40parseObject :: I.IndentParser String () Type
41parseObject = braces $ Object <$> parseRow
43parseFunction :: I.IndentParser String () Type
44parseFunction = do
45 args <- lexeme $ parens $ commaSep parseType
46 lexeme $ P.string "->"
47 resultType <- parseType
48 return $ Function args resultType
50parseTypeVariable :: I.IndentParser String () Type
51parseTypeVariable = TypeVar <$> identifier
53parseTypeConstructor :: I.IndentParser String () Type
54parseTypeConstructor = TypeConstructor <$> properName
56parseTypeAtom :: I.IndentParser String () Type
57parseTypeAtom = P.choice $ map P.try
58 [ parseNumber
59 , parseString
60 , parseBoolean
61 , parseArray
62 , parseObject
63 , parseFunction
64 , parseTypeVariable
65 , parseTypeConstructor
66 , parens parseType ]
68parsePolyType :: I.IndentParser String () PolyType
69parsePolyType = PolyType <$> (P.option [] (reserved "forall" *> many identifier <* dot))
70 <*> parseType
72parseType :: I.IndentParser String () Type
73parseType = fold (lexeme parseTypeAtom) (lexeme parseTypeAtom) TypeApp
75parseNameAndType :: I.IndentParser String () (String, Type)
76parseNameAndType = (,) <$> (identifier <* lexeme (P.string "::")) <*> parseType
78parseRowEnding :: I.IndentParser String () Row
79parseRowEnding = P.option REmpty (RowVar <$> (lexeme (P.char '|') *> identifier))
81parseRow :: I.IndentParser String () Row
82parseRow = fromList <$> semiSep parseNameAndType <*> parseRowEnding
83 where
84 fromList :: [(String, Type)] -> Row -> Row
85 fromList [] r = r
86 fromList ((name, t):ts) r = RCons name t (fromList ts r)