1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
3module Core.CoreParser(parseTerm, parseFile, parseDef, pTerm, iName, idrisDef) where
7import Text.ParserCombinators.Parsec
8import Text.ParserCombinators.Parsec.Expr
9import Text.ParserCombinators.Parsec.Language
10import qualified Text.ParserCombinators.Parsec.Token as PTok
14type TokenParser a = PTok.TokenParser a
16idrisDef = haskellDef {
19 reservedOpNames = [":", "..", "=", "\\", "|", "<-", "->", "=>"],
20 reservedNames = ["let", "in", "data", "Set", "if", "then", "else",
21 "do", "dsl", "import",
22 "infix", "infixl", "infixr", "prefix",
23 "where", "forall", "syntax",
24 "using", "params", "namespace",
25 "Int", "Float", "Char", "String", "Ptr"]
28iOpStart = oneOf ":!#$%&*+./<=>?@\\^|-~"
29iOpLetter = oneOf ":!#$%&*+./<=>?@\\^|-~"
33lexer = PTok.makeTokenParser idrisDef
35whiteSpace= PTok.whiteSpace lexer
36lexeme = PTok.lexeme lexer
37symbol = PTok.symbol lexer
38natural = PTok.natural lexer
39parens = PTok.parens lexer
41comma = PTok.comma lexer
42identifier= PTok.identifier lexer
43reserved = PTok.reserved lexer
44operator = PTok.operator lexer
45reservedOp= PTok.reservedOp lexer
48type CParser a = GenParser Char a
50parseFile = runParser pTestFile () "(input)"
51parseDef = runParser pDef () "(input)"
52parseTerm = runParser pTerm () "(input)"
54pTestFile :: CParser a RProgram
55pTestFile = do p <- many1 pDef ; eof
58iName :: CParser a Name
59iName = do x <- identifier
62pDef :: CParser a (Name, RDef)
63pDef = try (do x <- iName; lchar ':'; ty <- pTerm
67 return (x, RFunction (RawFun ty tm)))
68 <|> do x <- iName; lchar ':'; ty <- pTerm; lchar ';'
70 <|> do (x, d) <- pData; lchar ';'
73app :: CParser a (Raw -> Raw -> Raw)
74app = do whiteSpace ; return RApp
76arrow :: CParser a (Raw -> Raw -> Raw)
77arrow = do symbol "->" ; return $ \s t -> RBind (MN 0 "X") (Pi s) t
80pTerm = try (do chainl1 pNoApp app)
83pNoApp :: CParser a Raw
84pNoApp = try (chainr1 pExp arrow)
87pExp = do lchar '\\'; x <- iName; lchar ':'; ty <- pTerm
90 return (RBind x (Lam ty) sc)
91 <|> try (do lchar '?'; x <- iName; lchar ':'; ty <- pTerm
94 return (RBind x (Hole ty) sc))
95 <|> try (do lchar '(';
96 x <- iName; lchar ':'; ty <- pTerm
100 return (RBind x (Pi ty) sc))
101 <|> try (do lchar '(';
105 <|> try (do symbol "??";
106 x <- iName; lchar ':'; ty <- pTerm
110 return (RBind x (Guess ty val) sc))
111 <|> try (do reserved "let";
112 x <- iName; lchar ':'; ty <- pTerm
117 return (RBind x (Let ty val) sc))
118 <|> try (do lchar '_';
119 x <- iName; lchar ':'; ty <- pTerm
122 return (RBind x (PVar ty) sc))
123 <|> try (do reserved "Set"; i <- option 0 natural
124 return (RSet (fromInteger i)))
125 <|> try (do x <- iName
128pData :: CParser a (Name, RawDatatype)
129pData = do reserved "data"; x <- iName; lchar ':'; ty <- pTerm; reserved "where"
130 cs <- many pConstructor
131 return (x, RDatatype x ty cs)
133pConstructor :: CParser a (Name, Raw)
134pConstructor = do lchar '|'
135 c <- iName; lchar ':'; ty <- pTerm
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list