{ module Parser.Parser where import Parser.Tokens import Parser.ParserFunctions import Language.Haskell.Syntax import Text.ParserCombinators.Parsec.Prim } %token MODULE {TokKeyword "module"} WHERE {TokKeyword "where"} INFIX {TokKeyword "infix"} DATA {TokKeyword "data"} TYPE {TokKeyword "type"} NEWTYPE {TokKeyword "newtype"} IMPORT {TokKeyword "import"} HS {TokHaskell $$} QVAR {TokQVar $$} VAR {TokVar $$} QCON {TokQCon $$} CON {TokCon $$} QSYM {TokQSym $$} SYM {TokSym $$} '.' {TokComma} ':' {TokColon} '::' {TokDoubleColon} '=' {TokEquals} '<-' {TokLeftArrow} '@' {TokAt} '%' {TokPercent} '(' {TokLPar} ')' {TokRPar} '[' {TokLBox} ']' {TokRBox} '`' {TokBackQuote} %name parser %monad {CharParser Int} %lexer {lextok} {EOF} %tokentype {Token} %% topdecl :: { HsDecl } : srcloc 'type' simpletype '=' type { HsTypeDecl $1 (fst $3) (snd $3) $5 } | srcloc 'data' ctype '=' constrs deriving {% do { (cs,c,t) <- checkDataHeader $3; return (HsDataDecl $1 cs c t (reverse $5) $6) } } | srcloc 'newtype' ctype '=' constr deriving {% do { (cs,c,t) <- checkDataHeader $3; return (HsNewTypeDecl $1 cs c t $5 $6) } } simpletype :: { (HsName, [HsName]) } : tycon tyvars { ($1,reverse $2) } tyvars :: { [HsName] } : tyvars tyvar { $2 : $1 } | {- empty -} { [] } tycon :: { HsName } : conid { $1 } tyvar :: { HsName } : varid { $1 } types :: { [HsType] } : types ',' type { $3 : $1 } | type ',' type { [$3, $1] } type :: { HsType } : btype '-' type { HsTyFun $1 $3 } | btype { $1 } btype :: { HsType } : btype atype { HsTyApp $1 $2 } | atype { $1 } atype :: { HsType } : gtycon { HsTyCon $1 } | tyvar { HsTyVar $1 } | '(' types ')' { HsTyTuple (reverse $2) } | '[' type ']' { HsTyApp list_tycon $2 } | '(' type ')' { $2 } gtycon :: { HsQName } : qconid { $1 } | '(' ')' { unit_tycon_name } | '(' '-' ')' { fun_tycon_name } | '[' ']' { list_tycon_name } | '(' commas ')' { tuple_tycon_name $2 } qconid :: { HsQName } : conid { UnQual $1 } | QCON { Qual (Module (fst $1)) (HsIdent (snd $1)) } conid :: { HsName } : CON { HsIdent $1 } commas :: { Int } : commas ',' { $1 + 1 } | ',' { 1 } srcloc :: { SrcLoc } : {% getSrcLoc } { happyError :: Token -> CharParser st a happyError = parsecFail . show parseModule :: FilePath -> String -> Either ParseError HsDecl parseModule f = runParser parser 0 f }