git.iko.soy/rowtype-yoga/purescript-native
on f96c9a1 help sign in
85 lines 2.5 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 parseRow
18) where
20import PureScript.Types
21import PureScript.Parser.Common
22import Control.Applicative
23import qualified Text.Parsec as P
24import Control.Arrow (Arrow(..))
26parseNumber :: P.Parsec String () Type
27parseNumber = const Number <$> P.string "Number"
29parseString :: P.Parsec String () Type
30parseString = const String <$> P.string "String"
32parseBoolean :: P.Parsec String () Type
33parseBoolean = const Boolean <$> P.string "Boolean"
35parseArray :: P.Parsec String () Type
36parseArray = squares $ Array <$> parseType
38parseObject :: P.Parsec String () Type
39parseObject = braces $ Object <$> parseRow
41parseFunction :: P.Parsec String () Type
42parseFunction = do
43 args <- lexeme $ parens $ commaSep parseType
44 lexeme $ P.string "->"
45 resultType <- parseType
46 return $ Function args resultType
48parseTypeVariable :: P.Parsec String () Type
49parseTypeVariable = TypeVar <$> identifier
51parseTypeConstructor :: P.Parsec String () Type
52parseTypeConstructor = TypeConstructor <$> properName
54parseForAllType :: P.Parsec String () Type
55parseForAllType = ForAll <$> (reserved "forall" *> many identifier)
56 <*> (dot *> parseType)
58parseTypeAtom :: P.Parsec String () Type
59parseTypeAtom = P.choice $ map P.try
60 [ parseNumber
61 , parseString
62 , parseBoolean
63 , parseArray
64 , parseObject
65 , parseFunction
66 , parseForAllType
67 , parseTypeVariable
68 , parseTypeConstructor
69 , parens 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)