1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
3module Core.CoreParser(parseTerm, parseFile, parseDef, pTerm, iName, idrisDef,
8import Text.ParserCombinators.Parsec
9import Text.ParserCombinators.Parsec.Expr
10import Text.ParserCombinators.Parsec.Language
11import qualified Text.ParserCombinators.Parsec.Token as PTok
13import Control.Monad.State
16type TokenParser a = PTok.TokenParser a
18idrisDef = haskellDef {
21 identLetter = identLetter haskellDef <|> lchar '.',
22 reservedOpNames = [":", "..", "=", "\\", "|", "<-", "->", "=>", "**"],
23 reservedNames = ["let", "in", "data", "codata", "record", "Set",
24 "do", "dsl", "import", "impossible",
25 "case", "of", "total", "partial",
26 "infix", "infixl", "infixr",
27 "where", "with", "syntax", "proof", "postulate",
28 "using", "namespace", "class", "instance",
29 "public", "private", "abstract",
30 "Int", "Integer", "Float", "Char", "String", "Ptr"]
33iOpStart = oneOf ":!#$%&*+./<=>?@\\^|-~"
34iOpLetter = oneOf ":!#$%&*+./<=>?@\\^|-~"
38lexer = PTok.makeTokenParser idrisDef
40whiteSpace= PTok.whiteSpace lexer
41lexeme = PTok.lexeme lexer
42symbol = PTok.symbol lexer
43natural = PTok.natural lexer
44parens = PTok.parens lexer
46comma = PTok.comma lexer
47identifier= PTok.identifier lexer
48reserved = PTok.reserved lexer
49operator = PTok.operator lexer
50reservedOp= PTok.reservedOp lexer
53type CParser a = GenParser Char a
55parseFile = runParser pTestFile () "(input)"
56parseDef = runParser pDef () "(input)"
57parseTerm = runParser pTerm () "(input)"
59pTestFile :: CParser a RProgram
60pTestFile = do p <- many1 pDef ; eof
63iName :: [String] -> CParser a Name
64iName bad = maybeWithNS identifier False bad
66-- Enhances a given parser to accept an optional namespace. All possible
67-- namespace prefixes are tried in ascending / descending order, and
68-- identifiers of a given list fail.
69maybeWithNS :: CParser a String -> Bool -> [String] -> CParser a Name
70maybeWithNS parser ascend bad = do
71 i <- option "" (lookAhead identifier)
72 when (i `elem` bad) $ fail "Reserved identifier"
73 let transf = if ascend then id else reverse
74 (x, xs) <- choice $ transf (parserNoNS : parsersNS i)
75 return $ mkName (x, xs)
77 parserNoNS = do x <- parser; return (x, "")
78 parserNS ns = do xs <- string ns; lchar '.'; x <- parser; return (x, xs)
79 parsersNS i = [try (parserNS ns) | ns <- (initsEndAt (=='.') i)]
81-- List of all initial segments in ascending order of a list. Every such
82-- initial segment ends right before an element satisfying the given
84initsEndAt :: (a -> Bool) -> [a] -> [[a]]
86initsEndAt p (x:xs) | p x = [] : x_inits_xs
87 | otherwise = x_inits_xs
88 where x_inits_xs = [x : cs | cs <- initsEndAt p xs]
90-- Create a `Name' from a pair of strings representing a base name and its
92mkName :: (String, String) -> Name
94mkName (n, ns) = NS (UN n) (reverse (parseNS ns))
95 where parseNS x = case span (/= '.') x of
97 (x, '.':y) -> x : parseNS y
99pDef :: CParser a (Name, RDef)
100pDef = try (do x <- iName []; lchar ':'; ty <- pTerm
104 return (x, RFunction (RawFun ty tm)))
105 <|> do x <- iName []; lchar ':'; ty <- pTerm; lchar ';'
106 return (x, RConst ty)
107 <|> do (x, d) <- pData; lchar ';'
110app :: CParser a (Raw -> Raw -> Raw)
111app = do whiteSpace ; return RApp
113arrow :: CParser a (Raw -> Raw -> Raw)
114arrow = do symbol "->" ; return $ \s t -> RBind (MN 0 "X") (Pi s) t
116pTerm :: CParser a Raw
117pTerm = try (do chainl1 pNoApp app)
120pNoApp :: CParser a Raw
121pNoApp = try (chainr1 pExp arrow)
124pExp = do lchar '\\'; x <- iName []; lchar ':'; ty <- pTerm
127 return (RBind x (Lam ty) sc)
128 <|> try (do lchar '?'; x <- iName []; lchar ':'; ty <- pTerm
131 return (RBind x (Hole ty) sc))
132 <|> try (do lchar '(';
133 x <- iName []; lchar ':'; ty <- pTerm
137 return (RBind x (Pi ty) sc))
138 <|> try (do lchar '(';
142 <|> try (do symbol "??";
143 x <- iName []; lchar ':'; ty <- pTerm
147 return (RBind x (Guess ty val) sc))
148 <|> try (do reserved "let";
149 x <- iName []; lchar ':'; ty <- pTerm
154 return (RBind x (Let ty val) sc))
155 <|> try (do lchar '_';
156 x <- iName []; lchar ':'; ty <- pTerm
159 return (RBind x (PVar ty) sc))
160 <|> try (do reserved "Set"
162 <|> try (do x <- iName []
165pData :: CParser a (Name, RawDatatype)
166pData = do reserved "data"; x <- iName []; lchar ':'; ty <- pTerm; reserved "where"
167 cs <- many pConstructor
168 return (x, RDatatype x ty cs)
170pConstructor :: CParser a (Name, Raw)
171pConstructor = do lchar '|'
172 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