1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
3module Core.CoreParser(parseTerm, parseFile, parseDef, pTerm, iName,
4 idrisLexer, maybeWithNS, pDocComment, opChars) where
8import Text.ParserCombinators.Parsec
9import Text.ParserCombinators.Parsec.Expr
10import Text.ParserCombinators.Parsec.Language
11import Text.ParserCombinators.Parsec.Prim
12import Text.ParserCombinators.Parsec.Char
13import Text.ParserCombinators.Parsec.Combinator
14import qualified Text.ParserCombinators.Parsec.Token as PTok
16import Control.Monad.State
17import Control.Monad.Identity
22type TokenParser a = PTok.TokenParser a
24idrisDef = haskellDef {
27 identLetter = identLetter haskellDef <|> lchar '.',
29 = [":", "..", "=", "\\", "|", "<-", "->", "=>", "**"],
31 = ["let", "in", "data", "codata", "record", "Type",
32 "do", "dsl", "import", "impossible",
33 "case", "of", "total", "partial", "mutual",
34 "infix", "infixl", "infixr", "rewrite",
35 "where", "with", "syntax", "proof", "postulate",
36 "using", "namespace", "class", "instance",
37 "public", "private", "abstract", "implicit",
39 "Int", "Integer", "Float", "Char", "String", "Ptr",
40 "Bits8", "Bits16", "Bits32", "Bits64",
41 "Bits8x16", "Bits16x8", "Bits32x4", "Bits64x2"]
44-- | The characters allowed in operator names
45opChars = ":!#$%&*+./<=>?@\\^|-~"
47iOpStart = oneOf opChars
48iOpLetter = oneOf opChars
51idrisLexer :: TokenParser a
52idrisLexer = idrisMakeTokenParser idrisDef
56-- Taken from Parsec source code
57lexWS = do i <- getInput
58 skipMany (simpleSpace <|> oneLineComment <|> multiLineComment <?> "")
61 simpleSpace = skipMany1 (satisfy isSpace)
63 try (void $ string "--\n") <|>
64 do try (do string "--"
65 satisfy (\x -> x /= '|' && x /= '^'))
66 skipMany (satisfy (/= '\n'))
68 try (void $ string "{--}") <|>
69 do try (do string "{-"
70 satisfy (\x -> x /= '|' && x /= '^'))
74 = do{ try (string "-}") ; return () }
75 <|> do{ multiLineComment ; inCommentMulti }
76 <|> do{ skipMany1 (noneOf startEnd) ; inCommentMulti }
77 <|> do{ oneOf startEnd ; inCommentMulti }
82whiteSpace= PTok.whiteSpace lexer
83lexeme = PTok.lexeme lexer
84symbol = PTok.symbol lexer
85natural = PTok.natural lexer
86parens = PTok.parens lexer
88comma = PTok.comma lexer
89identifier= PTok.identifier lexer
90reserved = PTok.reserved lexer
91operator = PTok.operator lexer
92reservedOp= PTok.reservedOp lexer
93strlit = PTok.stringLiteral lexer
96type CParser a = GenParser Char a
98parseFile = runParser pTestFile () "(input)"
99parseDef = runParser pDef () "(input)"
100parseTerm = runParser pTerm () "(input)"
102pTestFile :: CParser a RProgram
103pTestFile = do p <- many1 pDef ; eof
106iName :: [String] -> CParser a Name
107iName bad = maybeWithNS identifier False bad
109-- Enhances a given parser to accept an optional namespace. All possible
110-- namespace prefixes are tried in ascending / descending order, and
111-- identifiers of a given list fail.
112maybeWithNS :: CParser a String -> Bool -> [String] -> CParser a Name
113maybeWithNS parser ascend bad = do
114 i <- option "" (lookAhead identifier)
115 when (i `elem` bad) $ fail "Reserved identifier"
116 let transf = if ascend then id else reverse
117 (x, xs) <- choice $ transf (parserNoNS : parsersNS i)
118 return $ mkName (x, xs)
120 parserNoNS = do x <- parser; return (x, "")
121 parserNS ns = do xs <- string ns; lchar '.'; x <- parser; return (x, xs)
122 parsersNS i = [try (parserNS ns) | ns <- (initsEndAt (=='.') i)]
124-- List of all initial segments in ascending order of a list. Every such
125-- initial segment ends right before an element satisfying the given
127initsEndAt :: (a -> Bool) -> [a] -> [[a]]
129initsEndAt p (x:xs) | p x = [] : x_inits_xs
130 | otherwise = x_inits_xs
131 where x_inits_xs = [x : cs | cs <- initsEndAt p xs]
133-- Create a `Name' from a pair of strings representing a base name and its
135mkName :: (String, String) -> Name
137mkName (n, ns) = NS (UN n) (reverse (parseNS ns))
138 where parseNS x = case span (/= '.') x of
140 (x, '.':y) -> x : parseNS y
142pDocComment :: Char -> CParser a String
144 = try (do string ("--")
148 let (doc, rest) = span (/='\n') i
152 <|> try (do string ("{-")
157 let (doc, rest) = spanComment "" i
161 where spanComment acc ('-':'}':xs) = (reverse acc, xs)
162 spanComment acc (x:xs) = spanComment (x : acc) xs
163 spanComment acc [] = (acc, [])
164 simpleSpace = skipMany1 (satisfy isSpace)
166pDef :: CParser a (Name, RDef)
167pDef = try (do x <- iName []; lchar ':'; ty <- pTerm
171 return (x, RFunction (RawFun ty tm)))
172 <|> do x <- iName []; lchar ':'; ty <- pTerm; lchar ';'
173 return (x, RConst ty)
174 <|> do (x, d) <- pData; lchar ';'
177app :: CParser a (Raw -> Raw -> Raw)
178app = do whiteSpace ; return RApp
180arrow :: CParser a (Raw -> Raw -> Raw)
181arrow = do symbol "->" ; return $ \s t -> RBind (MN 0 "X") (Pi s) t
183pTerm :: CParser a Raw
184pTerm = try (do chainl1 pNoApp app)
187pNoApp :: CParser a Raw
188pNoApp = try (chainr1 pExp arrow)
191pExp = do lchar '\\'; x <- iName []; lchar ':'; ty <- pTerm
194 return (RBind x (Lam ty) sc)
195 <|> try (do lchar '?'; x <- iName []; lchar ':'; ty <- pTerm
198 return (RBind x (Hole ty) sc))
199 <|> try (do lchar '(';
200 x <- iName []; lchar ':'; ty <- pTerm
204 return (RBind x (Pi ty) sc))
205 <|> try (do lchar '(';
209 <|> try (do symbol "??";
210 x <- iName []; lchar ':'; ty <- pTerm
214 return (RBind x (Guess ty val) sc))
215 <|> try (do reserved "let";
216 x <- iName []; lchar ':'; ty <- pTerm
221 return (RBind x (Let ty val) sc))
222 <|> try (do lchar '_';
223 x <- iName []; lchar ':'; ty <- pTerm
226 return (RBind x (PVar ty) sc))
227 <|> try (do reserved "Type"
229 <|> try (do x <- iName []
232pData :: CParser a (Name, RawDatatype)
233pData = do reserved "data"; x <- iName []; lchar ':'; ty <- pTerm; reserved "where"
234 cs <- many pConstructor
235 return (x, RDatatype x ty cs)
237pConstructor :: CParser a (Name, Raw)
238pConstructor = do lchar '|'
239 c <- iName []; lchar ':'; ty <- pTerm
242------ borrowed from Parsec
243-- (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
245idrisMakeTokenParser languageDef
247 PTok.identifier = identifier
248 , PTok.reserved = reserved
249 , PTok.operator = operator
250 , PTok.reservedOp = reservedOp
252 , PTok.charLiteral = charLiteral
253 , PTok.stringLiteral = stringLiteral
254 , PTok.natural = natural
255 , PTok.integer = integer
257 , PTok.naturalOrFloat = naturalOrFloat
258 , PTok.decimal = decimal
259 , PTok.hexadecimal = hexadecimal
262 , PTok.symbol = symbol
263 , PTok.lexeme = lexeme
264 , PTok.whiteSpace = lexWS
266 , PTok.parens = parens
267 , PTok.braces = braces
268 , PTok.angles = angles
269 , PTok.brackets = brackets
270 , PTok.squares = brackets
275 , PTok.semiSep = semiSep
276 , PTok.semiSep1 = semiSep1
277 , PTok.commaSep = commaSep
278 , PTok.commaSep1 = commaSep1
282 -----------------------------------------------------------
284 -----------------------------------------------------------
285 parens p = between (symbol "(") (symbol ")") p
286 braces p = between (symbol "{") (symbol "}") p
287 angles p = between (symbol "<") (symbol ">") p
288 brackets p = between (symbol "[") (symbol "]") p
295 commaSep p = sepBy p comma
296 semiSep p = sepBy p semi
298 commaSep1 p = sepBy1 p comma
299 semiSep1 p = sepBy1 p semi
302 -----------------------------------------------------------
304 -----------------------------------------------------------
305 charLiteral = lexeme (between (char '\'')
306 (char '\'' <?> "end of character")
310 characterChar = charLetter <|> charEscape
311 <?> "literal character"
313 charEscape = do{ char '\\'; escapeCode }
314 charLetter = satisfy (\c -> (c /= '\'') && (c /= '\\') && (c > '\026'))
318 stringLiteral = lexeme (
319 do{ str <- between (char '"')
320 (char '"' <?> "end of string")
322 ; return (foldr (maybe id (:)) "" str)
324 <?> "literal string")
326 stringChar = do{ c <- stringLetter; return (Just c) }
328 <?> "string character"
330 stringLetter = satisfy (\c -> (c /= '"') && (c /= '\\') && (c > '\026'))
332 stringEscape = do{ char '\\'
333 ; do{ escapeGap ; return Nothing }
334 <|> do{ escapeEmpty; return Nothing }
335 <|> do{ esc <- escapeCode; return (Just esc) }
338 escapeEmpty = char '&'
339 escapeGap = do{ many1 space
340 ; char '\\' <?> "end of string gap"
346 escapeCode = charEsc <|> charNum <|> charAscii <|> charControl
349 charControl = do{ char '^'
351 ; return (toEnum (fromEnum code - fromEnum 'A'))
354 charNum = do{ code <- decimal
355 <|> do{ char 'o'; number 8 octDigit }
356 <|> do{ char 'x'; number 16 hexDigit }
357 ; return (toEnum (fromInteger code))
360 charEsc = choice (map parseEsc escMap)
362 parseEsc (c,code) = do{ char c; return code }
364 charAscii = choice (map parseAscii asciiMap)
366 parseAscii (asc,code) = try (do{ string asc; return code })
369 -- escape code tables
370 escMap = zip ("abfnrtv\\\"\'") ("\a\b\f\n\r\t\v\\\"\'")
371 asciiMap = zip (ascii3codes ++ ascii2codes) (ascii3 ++ ascii2)
373 ascii2codes = ["BS","HT","LF","VT","FF","CR","SO","SI","EM",
374 "FS","GS","RS","US","SP"]
375 ascii3codes = ["NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL",
376 "DLE","DC1","DC2","DC3","DC4","NAK","SYN","ETB",
377 "CAN","SUB","ESC","DEL"]
379 ascii2 = ['\BS','\HT','\LF','\VT','\FF','\CR','\SO','\SI',
380 '\EM','\FS','\GS','\RS','\US','\SP']
381 ascii3 = ['\NUL','\SOH','\STX','\ETX','\EOT','\ENQ','\ACK',
382 '\BEL','\DLE','\DC1','\DC2','\DC3','\DC4','\NAK',
383 '\SYN','\ETB','\CAN','\SUB','\ESC','\DEL']
386 -----------------------------------------------------------
388 -----------------------------------------------------------
389 naturalOrFloat = lexeme (natFloat) <?> "number"
391 float = lexeme floating <?> "float"
392 integer = lexeme int <?> "integer"
393 natural = lexeme nat <?> "natural"
397 floating = do{ n <- decimal
402 natFloat = do{ char '0'
407 zeroNumFloat = do{ n <- hexadecimal <|> octal
414 decimalFloat = do{ n <- decimal
419 fractFloat n = do{ f <- fractExponent n
423 fractExponent n = do{ fract <- fraction
424 ; expo <- option 1.0 exponent'
425 ; return ((fromInteger n + fract)*expo)
428 do{ expo <- exponent'
429 ; return ((fromInteger n)*expo)
432 fraction = do{ char '.'
433 ; digits <- many1 digit <?> "fraction"
434 ; return (foldr op 0.0 digits)
438 op d f = (f + fromIntegral (digitToInt d))/10.0
440 exponent' = do{ oneOf "eE"
442 ; e <- decimal <?> "exponent"
443 ; return (power (f e))
447 power e | e < 0 = 1.0/power(-e)
448 | otherwise = fromInteger (10^e)
451 -- integers and naturals
452 int = do{ f <- lexeme sign
457 sign = (char '-' >> return negate)
458 <|> (char '+' >> return id)
461 nat = zeroNumber <|> decimal
463 zeroNumber = do{ char '0'
464 ; hexadecimal <|> octal <|> decimal <|> return 0
468 decimal = number 10 digit
469 hexadecimal = do{ oneOf "xX"; number 16 hexDigit }
470 octal = do{ oneOf "oO"; number 8 octDigit }
472 number base baseDigit
473 = do{ digits <- many1 baseDigit
474 ; let n = foldl' (\x d -> base*x + toInteger (digitToInt d)) 0 digits
478 -----------------------------------------------------------
479 -- Operators & reserved ops
480 -----------------------------------------------------------
484 ; notFollowedBy (opLetter languageDef) <?> ("end of " ++ show name)
490 ; if (isReservedOp name)
491 then unexpected ("reserved operator " ++ show name)
496 do{ c <- (opStart languageDef)
497 ; cs <- many (opLetter languageDef)
503 isReserved (sort (reservedOpNames languageDef)) name
506 -----------------------------------------------------------
507 -- Identifiers & Reserved words
508 -----------------------------------------------------------
512 ; notFollowedBy (identLetter languageDef) <?> ("end of " ++ show name)
516 | caseSensitive languageDef = string name
517 | otherwise = do{ walk name; return name }
520 walk (c:cs) = do{ caseChar c <?> msg; walk cs }
522 caseChar c | isAlpha c = char (toLower c) <|> char (toUpper c)
531 ; if (isReservedName name)
532 then unexpected ("reserved word " ++ show name)
538 = do{ c <- identStart languageDef
539 ; cs <- many (identLetter languageDef)
545 = isReserved theReservedNames caseName
547 caseName | caseSensitive languageDef = name
548 | otherwise = map toLower name
551 isReserved names name
555 scan (r:rs) = case (compare r name) of
561 | caseSensitive languageDef = sortedNames
562 | otherwise = map (map toLower) sortedNames
564 sortedNames = sort (reservedNames languageDef)
568 -----------------------------------------------------------
569 -- White space & symbols
570 -----------------------------------------------------------
572 = lexeme (string name)
575 = do{ x <- p; whiteSpace; return x }
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