git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Classes and instances parsers
commit47eba262d0d826fbcce9bb0e3418cae4f2079bd6 authorPhil Freeman <paf31@cantab.net> dateSun 12 Jan 2014, 11:53 (13 years ago) parent76ee160
5 files changed+35−3
examples/passing/TypeClasses.purs+7 purescript.cabal+1−1 src/Language/PureScript/Declarations.hs+2 src/Language/PureScript/Parser/Common.hs+4−1 src/Language/PureScript/Parser/Declarations.hs+21−1
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 @@
11name: purescript
2−version: 0.2.13.1
2+version: 0.2.14
33cabal-version: >=1.8
44build-type: Simple
55license: MIT
src/Language/PureScript/Declarations.hs+2
@@ -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+4−1
@@ -71,7 +71,10 @@ reservedNames = [ "case"
7171 , "infixl"
7272 , "infixr"
7373 , "module"
74− , "let" ]
74+ , "let"
75+ , "class"
76+ , "instance"
77+ , "where" ]
7578
7679builtInOperators :: [String]
7780builtInOperators = [ "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
src/Language/PureScript/Parser/Declarations.hs+21−1
@@ -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+
99117parseDeclaration :: P.Parsec String ParseState Declaration
100118parseDeclaration = 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
109129parseModule :: P.Parsec String ParseState Module
110130parseModule = do