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 {
17 reservedOpNames = [":", "..", "=", "\\", "|", "<-", "->", "=>"],
18 reservedNames = ["let", "in", "data", "Set", "if", "then", "else",
19 "do", "dsl", "import", "infix", "infixl", "infixr",
20 "where", "forall", "syntax",
21 "using", "params", "namespace"]
25lexer = PTok.makeTokenParser idrisDef
27whiteSpace= PTok.whiteSpace lexer
28lexeme = PTok.lexeme lexer
29symbol = PTok.symbol lexer
30natural = PTok.natural lexer
31parens = PTok.parens lexer
33comma = PTok.comma lexer
34identifier= PTok.identifier lexer
35reserved = PTok.reserved lexer
36operator = PTok.operator lexer
37reservedOp= PTok.reservedOp lexer
40type CParser a = GenParser Char a
42parseFile = runParser pTestFile () "(input)"
43parseDef = runParser pDef () "(input)"
44parseTerm = runParser pTerm () "(input)"
46pTestFile :: CParser a RProgram
47pTestFile = do p <- many1 pDef ; eof
50iName :: CParser a Name
51iName = do x <- identifier
54pDef :: CParser a (Name, RDef)
55pDef = try (do x <- iName; lchar ':'; ty <- pTerm
59 return (x, RFunction (RawFun ty tm)))
60 <|> do x <- iName; lchar ':'; ty <- pTerm; lchar ';'
62 <|> do (x, d) <- pData; lchar ';'
65app :: CParser a (Raw -> Raw -> Raw)
66app = do whiteSpace ; return RApp
68arrow :: CParser a (Raw -> Raw -> Raw)
69arrow = do symbol "->" ; return $ \s t -> RBind (MN 0 "X") (Pi s) t
72pTerm = try (do chainl1 pNoApp app)
75pNoApp :: CParser a Raw
76pNoApp = try (chainr1 pExp arrow)
79pExp = do lchar '\\'; x <- iName; lchar ':'; ty <- pTerm
82 return (RBind x (Lam ty) sc)
83 <|> try (do lchar '?'; x <- iName; lchar ':'; ty <- pTerm
86 return (RBind x (Hole ty) sc))
87 <|> try (do lchar '(';
88 x <- iName; lchar ':'; ty <- pTerm
92 return (RBind x (Pi ty) sc))
93 <|> try (do lchar '(';
97 <|> try (do symbol "??";
98 x <- iName; lchar ':'; ty <- pTerm
102 return (RBind x (Guess ty val) sc))
103 <|> try (do reserved "let";
104 x <- iName; lchar ':'; ty <- pTerm
109 return (RBind x (Let ty val) sc))
110 <|> try (do lchar '_';
111 x <- iName; lchar ':'; ty <- pTerm
114 return (RBind x (PVar ty) sc))
115 <|> try (do reserved "Set"; i <- option 0 natural
116 return (RSet (fromInteger i)))
117 <|> try (do x <- iName
120pData :: CParser a (Name, RawDatatype)
121pData = do reserved "data"; x <- iName; lchar ':'; ty <- pTerm; reserved "where"
122 cs <- many pConstructor
123 return (x, RDatatype x ty cs)
125pConstructor :: CParser a (Name, Raw)
126pConstructor = do lchar '|'
127 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