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
12import Control.Monad.State
15type TokenParser a = PTok.TokenParser a
17idrisDef = haskellDef {
20 identLetter = identLetter haskellDef <|> lchar '.',
21 reservedOpNames = [":", "..", "=", "\\", "|", "<-", "->", "=>", "**"],
22 reservedNames = ["let", "in", "data", "Set", -- "if", "then", "else",
23 "do", "dsl", "import", -- "refl",
24 "infix", "infixl", "infixr", "prefix",
25 "where", "with", "forall", "syntax", "proof",
26 "using", "params", "namespace", "class", "instance",
27 "Int", "Integer", "Float", "Char", "String", "Ptr"]
30iOpStart = oneOf ":!#$%&*+./<=>?@\\^|-~"
31iOpLetter = oneOf ":!#$%&*+./<=>?@\\^|-~"
35lexer = PTok.makeTokenParser idrisDef
37whiteSpace= PTok.whiteSpace lexer
38lexeme = PTok.lexeme lexer
39symbol = PTok.symbol lexer
40natural = PTok.natural lexer
41parens = PTok.parens lexer
43comma = PTok.comma lexer
44identifier= PTok.identifier lexer
45reserved = PTok.reserved lexer
46operator = PTok.operator lexer
47reservedOp= PTok.reservedOp lexer
50type CParser a = GenParser Char a
52parseFile = runParser pTestFile () "(input)"
53parseDef = runParser pDef () "(input)"
54parseTerm = runParser pTerm () "(input)"
56pTestFile :: CParser a RProgram
57pTestFile = do p <- many1 pDef ; eof
60iName :: [String] -> CParser a Name
61iName bad = do x <- identifier
62 when (x `elem` bad) $ fail "Reserved identifier"
65 parseName x = case span (/= '.') x of
67 (x, '.':y) -> NS x (parseName y)
69pDef :: CParser a (Name, RDef)
70pDef = try (do x <- iName []; lchar ':'; ty <- pTerm
74 return (x, RFunction (RawFun ty tm)))
75 <|> do x <- iName []; lchar ':'; ty <- pTerm; lchar ';'
77 <|> do (x, d) <- pData; lchar ';'
80app :: CParser a (Raw -> Raw -> Raw)
81app = do whiteSpace ; return RApp
83arrow :: CParser a (Raw -> Raw -> Raw)
84arrow = do symbol "->" ; return $ \s t -> RBind (MN 0 "X") (Pi s) t
87pTerm = try (do chainl1 pNoApp app)
90pNoApp :: CParser a Raw
91pNoApp = try (chainr1 pExp arrow)
94pExp = do lchar '\\'; x <- iName []; lchar ':'; ty <- pTerm
97 return (RBind x (Lam ty) sc)
98 <|> try (do lchar '?'; x <- iName []; lchar ':'; ty <- pTerm
101 return (RBind x (Hole ty) sc))
102 <|> try (do lchar '(';
103 x <- iName []; lchar ':'; ty <- pTerm
107 return (RBind x (Pi ty) sc))
108 <|> try (do lchar '(';
112 <|> try (do symbol "??";
113 x <- iName []; lchar ':'; ty <- pTerm
117 return (RBind x (Guess ty val) sc))
118 <|> try (do reserved "let";
119 x <- iName []; lchar ':'; ty <- pTerm
124 return (RBind x (Let ty val) sc))
125 <|> try (do lchar '_';
126 x <- iName []; lchar ':'; ty <- pTerm
129 return (RBind x (PVar ty) sc))
130 <|> try (do reserved "Set"; i <- option 0 natural
131 return (RSet (fromInteger i)))
132 <|> try (do x <- iName []
135pData :: CParser a (Name, RawDatatype)
136pData = do reserved "data"; x <- iName []; lchar ':'; ty <- pTerm; reserved "where"
137 cs <- many pConstructor
138 return (x, RDatatype x ty cs)
140pConstructor :: CParser a (Name, Raw)
141pConstructor = do lchar '|'
142 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