git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Classes and instances parsers
47eba26Phil Freeman
examples/passing/TypeClasses.pursnew file+7
@@ -0,0 +1,7 @@
1+module TypeClasses where
2+
3+class Show a where
4+ show :: a -> String
5+
6+instance Show String where
7+ show s = s
purescript.cabal+1−1
@@ -1,5 +1,5 @@
1name: purescript
1name: purescript
2−version: 0.2.13.1
2+version: 0.2.14
3cabal-version: >=1.8
3cabal-version: >=1.8
4build-type: Simple
4build-type: Simple
5license: MIT
5license: MIT
src/Language/PureScript/Declarations.hs+2
@@ -43,4 +43,6 @@ data Declaration
43 | ExternDataDeclaration ProperName Kind
43 | ExternDataDeclaration ProperName Kind
44 | FixityDeclaration Fixity String
44 | FixityDeclaration Fixity String
45 | ImportDeclaration ModuleName (Maybe [Either Ident ProperName])
45 | ImportDeclaration ModuleName (Maybe [Either Ident ProperName])
46+ | TypeClassDeclaration ProperName Ident [Declaration]
47+ | TypeInstanceDeclaration ProperName Type [Declaration]
46 deriving (Show, D.Data, D.Typeable)
48 deriving (Show, D.Data, D.Typeable)
src/Language/PureScript/Parser/Common.hs+4−1
@@ -71,7 +71,10 @@ reservedNames = [ "case"
71 , "infixl"
71 , "infixl"
72 , "infixr"
72 , "infixr"
73 , "module"
73 , "module"
74− , "let" ]
74+ , "let"
75+ , "class"
76+ , "instance"
77+ , "where" ]
75
78
76builtInOperators :: [String]
79builtInOperators :: [String]
77builtInOperators = [ "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
80builtInOperators = [ "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
src/Language/PureScript/Parser/Declarations.hs+21−1
@@ -96,6 +96,24 @@ parseImportDeclaration = do
96 idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
96 idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
97 return $ ImportDeclaration moduleName idents
97 return $ ImportDeclaration moduleName idents
98
98
99+parseTypeClassDeclaration :: P.Parsec String ParseState Declaration
100+parseTypeClassDeclaration = do
101+ reserved "class"
102+ className <- indented *> properName
103+ ident <- indented *> parseIdent
104+ indented *> reserved "where"
105+ members <- mark (P.many (same *> parseTypeDeclaration))
106+ return $ TypeClassDeclaration className ident members
107+
108+parseTypeInstanceDeclaration :: P.Parsec String ParseState Declaration
109+parseTypeInstanceDeclaration = do
110+ reserved "instance"
111+ className <- indented *> properName
112+ ty <- indented *> parseType
113+ indented *> reserved "where"
114+ members <- mark (P.many (same *> parseValueDeclaration))
115+ return $ TypeInstanceDeclaration className ty members
116+
99parseDeclaration :: P.Parsec String ParseState Declaration
117parseDeclaration :: P.Parsec String ParseState Declaration
100parseDeclaration = P.choice
118parseDeclaration = P.choice
101 [ parseDataDeclaration
119 [ parseDataDeclaration
@@ -104,7 +122,9 @@ parseDeclaration = P.choice
104 , parseValueDeclaration
122 , parseValueDeclaration
105 , parseExternDeclaration
123 , parseExternDeclaration
106 , parseFixityDeclaration
124 , parseFixityDeclaration
107− , parseImportDeclaration ] P.<?> "declaration"
125+ , parseImportDeclaration
126+ , parseTypeClassDeclaration
127+ , parseTypeInstanceDeclaration ] P.<?> "declaration"
108
128
109parseModule :: P.Parsec String ParseState Module
129parseModule :: P.Parsec String ParseState Module
110parseModule = do
130parseModule = do