Skip to content

Commit 47eba26

Browse files
committed
Classes and instances parsers
1 parent 76ee160 commit 47eba26

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

examples/passing/TypeClasses.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: purescript
2-
version: 0.2.13.1
2+
version: 0.2.14
33
cabal-version: >=1.8
44
build-type: Simple
55
license: MIT

src/Language/PureScript/Declarations.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ data Declaration
4343
| ExternDataDeclaration ProperName Kind
4444
| FixityDeclaration Fixity String
4545
| ImportDeclaration ModuleName (Maybe [Either Ident ProperName])
46+
| TypeClassDeclaration ProperName Ident [Declaration]
47+
| TypeInstanceDeclaration ProperName Type [Declaration]
4648
deriving (Show, D.Data, D.Typeable)

src/Language/PureScript/Parser/Common.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ reservedNames = [ "case"
7171
, "infixl"
7272
, "infixr"
7373
, "module"
74-
, "let" ]
74+
, "let"
75+
, "class"
76+
, "instance"
77+
, "where" ]
7578

7679
builtInOperators :: [String]
7780
builtInOperators = [ "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"

src/Language/PureScript/Parser/Declarations.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ parseImportDeclaration = do
9696
idents <- P.optionMaybe $ parens $ commaSep1 (Left <$> parseIdent <|> Right <$> properName)
9797
return $ ImportDeclaration moduleName idents
9898

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+
99117
parseDeclaration :: P.Parsec String ParseState Declaration
100118
parseDeclaration = P.choice
101119
[ parseDataDeclaration
@@ -104,7 +122,9 @@ parseDeclaration = P.choice
104122
, parseValueDeclaration
105123
, parseExternDeclaration
106124
, parseFixityDeclaration
107-
, parseImportDeclaration ] P.<?> "declaration"
125+
, parseImportDeclaration
126+
, parseTypeClassDeclaration
127+
, parseTypeInstanceDeclaration ] P.<?> "declaration"
108128

109129
parseModule :: P.Parsec String ParseState Module
110130
parseModule = do

0 commit comments

Comments
 (0)