git.iko.soy/rowtype-yoga/purescript-native
on 0c961be help sign in
85 lines 2.6 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 Control.Arrow (Arrow(..))
27parseNumber :: P.Parsec String () Type
28parseNumber = const Number <$> P.string "Number"
30parseString :: P.Parsec String () Type
31parseString = const String <$> P.string "String"
33parseBoolean :: P.Parsec String () Type
34parseBoolean = const Boolean <$> P.string "Boolean"
36parseArray :: P.Parsec String () Type
37parseArray = squares $ Array <$> parseType
39parseObject :: P.Parsec String () Type
40parseObject = braces $ Object <$> parseRow
42parseFunction :: P.Parsec String () Type
43parseFunction = do
44 args <- lexeme $ parens $ commaSep parseType
45 lexeme $ P.string "->"
46 resultType <- parseType
47 return $ Function args resultType
49parseTypeVariable :: P.Parsec String () Type
50parseTypeVariable = TypeVar <$> identifier
52parseTypeConstructor :: P.Parsec String () Type
53parseTypeConstructor = TypeConstructor <$> properName
55parseTypeAtom :: P.Parsec String () Type
56parseTypeAtom = P.choice $ map P.try
57 [ parseNumber
58 , parseString
59 , parseBoolean
60 , parseArray
61 , parseObject
62 , parseFunction
63 , parseTypeVariable
64 , parseTypeConstructor
65 , parens parseType ]
67parsePolyType :: P.Parsec String () PolyType
68parsePolyType = PolyType <$> (P.option [] (reserved "forall" *> many identifier <* dot))
69 <*> parseType
71parseType :: P.Parsec String () Type
72parseType = fold (lexeme parseTypeAtom) (lexeme parseTypeAtom) TypeApp
74parseNameAndType :: P.Parsec String () (String, Type)
75parseNameAndType = (,) <$> (identifier <* lexeme (P.string "::")) <*> parseType
77parseRowEnding :: P.Parsec String () Row
78parseRowEnding = P.option REmpty (RowVar <$> (lexeme (P.char '|') *> identifier))
80parseRow :: P.Parsec String () Row
81parseRow = fromList <$> semiSep parseNameAndType <*> parseRowEnding
82 where
83 fromList :: [(String, Type)] -> Row -> Row
84 fromList [] r = r
85 fromList ((name, t):ts) r = RCons name t (fromList ts r)