1{-# LANGUAGE GeneralizedNewtypeDeriving, ConstraintKinds, PatternGuards #-}
3module Idris.Parser(module Idris.Parser,
4 module Idris.ParseExpr,
5 module Idris.ParseData,
6 module Idris.ParseHelpers,
7 module Idris.ParseOps) where
9import Prelude hiding (pi)
11import Text.Trifecta.Delta
12import Text.Trifecta hiding (span, stringLiteral, charLiteral, natural, symbol, char, string, whiteSpace)
13import Text.Parser.LookAhead
14import Text.Parser.Expression
15import qualified Text.Parser.Token as Tok
16import qualified Text.Parser.Char as Chr
17import qualified Text.Parser.Token.Highlight as Hi
19import Text.PrettyPrint.ANSI.Leijen (Doc, plain)
21import Idris.AbsSyntax hiding (namespace, params)
24import Idris.Delaborate
33import Idris.ParseHelpers
38import Idris.Docstrings
42import Util.DynamicLinker
43import qualified Util.Pretty as P
46import Idris.Core.Evaluate
48import Control.Applicative
50import Control.Monad.Error (throwError, catchError)
51import Control.Monad.State.Strict
55import qualified Data.List.Split as Spl
60import qualified Data.Map as M
61import qualified Data.HashSet as HS
62import qualified Data.Text as T
63import qualified Data.ByteString.UTF8 as UTF8
64import qualified Data.Set as S
73 grammar shortcut notation:
74 ~CHARSEQ = complement of char sequence (i.e. any character except CHARSEQ)
75 RULE? = optional rule (i.e. RULE or nothing)
76 RULE* = repeated rule (i.e. RULE zero or more times)
77 RULE+ = repeated rule with at least one match (i.e. RULE one or more times)
78 RULE! = invalid rule (i.e. rule that is not valid in context, report meaningful error in case)
79 RULE{n} = rule repeated n times
85{- | Parses module definition
88 ModuleHeader ::= 'module' Identifier_t ';'?;
91moduleHeader :: IdrisParser [String]
92moduleHeader = try (do noDocCommentHere "Modules cannot have documentation comments"
95 option ';' (lchar ';')
96 return (moduleName i))
97 <|> try (do lchar '%'; reserved "unqualified"
99 <|> return (moduleName "Main")
100 where moduleName x = case span (/='.') x of
102 (x, '.':y) -> x : moduleName y
104{- | Parses an import statement
107 Import ::= 'import' Identifier_t ';'?;
110import_ :: IdrisParser (String, Maybe String, FC)
111import_ = do fc <- getFC
114 newName <- optional (reserved "as" *> identifier)
115 option ';' (lchar ';')
116 return (toPath id, toPath <$> newName, fc)
117 <?> "import statement"
118 where toPath = foldl1' (</>) . Spl.splitOn "."
120{- | Parses program source
126prog :: SyntaxInfo -> IdrisParser [PDecl]
127prog syn = do whiteSpace
128 decls <- many (decl syn)
131 let c = (concat decls)
134{-| Parses a top-level declaration
153decl :: SyntaxInfo -> IdrisParser [PDecl]
154decl syn = do notEndBlock
156 where declBody :: IdrisParser [PDecl]
164 <|> do d <- dsl syn; return [d]
168 <|> do import_; fail "imports must be at top of file"
170 declBody' :: IdrisParser [PDecl]
171 declBody' = do d <- decl' syn
173 let d' = fmap (debindApp syn . (desugar syn i)) d
176{- | Parses a top-level declaration with possible syntax sugar
188decl' :: SyntaxInfo -> IdrisParser PDecl
196{- | Parses a syntax extension declaration (and adds the rule to parser state)
199 SyntaxDecl ::= SyntaxRule;
202syntaxDecl :: SyntaxInfo -> IdrisParser PDecl
203syntaxDecl syn = do s <- syntaxRule syn
205 let rs = syntax_rules i
206 let ns = syntax_keywords i
207 let ibc = ibc_write i
208 let ks = map show (names s)
209 put (i { syntax_rules = s : rs,
210 syntax_keywords = ks ++ ns,
211 ibc_write = IBCSyntax s : map IBCKeyword ks ++ ibc })
213 return (PSyntax fc s)
214 where names (Rule syms _ _) = mapMaybe ename syms
215 ename (Keyword n) = Just n
218{- | Parses a syntax extension declaration
221SyntaxRuleOpts ::= 'term' | 'pattern';
226 SyntaxRuleOpts? 'syntax' SyntaxSym+ '=' TypeExpr Terminator;
230SyntaxSym ::= '[' Name_t ']'
237syntaxRule :: SyntaxInfo -> IdrisParser Syntax
241 sty <- option AnySyntax (do reserved "term"; return TermSyntax
242 <|> do reserved "pattern"; return PatternSyntax)
245 syms <- some syntaxSym
246 when (all isExpr syms) $ unexpected "missing keywords in syntax rule"
247 let ns = mapMaybe getName syms
248 when (length ns /= length (nub ns))
249 $ unexpected "repeated variable in syntax rule"
251 tm <- typeExpr (allowImp syn)
253 return (Rule (mkSimple syms) tm sty)
255 isExpr (Expr _) = True
257 getName (Expr n) = Just n
259 -- Can't parse two full expressions (i.e. expressions with application) in a row
260 -- so change them both to a simple expression
261 mkSimple (Expr e : es) = SimpleExpr e : mkSimple' es
262 mkSimple xs = mkSimple' xs
264 mkSimple' (Expr e : Expr e1 : es) = SimpleExpr e : SimpleExpr e1 :
266 mkSimple' (e : es) = e : mkSimple' es
270{- | Parses a syntax symbol (either binding variable, keyword or expression)
273SyntaxSym ::= '[' Name_t ']'
280syntaxSym :: IdrisParser SSymbol
281syntaxSym = try (do lchar '['; n <- name; lchar ']'
283 <|> try (do lchar '{'; n <- name; lchar '}'
287 <|> do sym <- stringLiteral
291{- | Parses a function declaration with possible syntax sugar
294 FunDecl ::= FunDecl';
297fnDecl :: SyntaxInfo -> IdrisParser [PDecl]
298fnDecl syn = try (do notEndBlock
301 let d' = fmap (desugar syn i) d
302 return [d']) <?> "function declaration"
304{-| Parses a function declaration
308 DocComment_t? FnOpts* Accessibility? FnOpts* FnName TypeSig Terminator
315fnDecl' :: SyntaxInfo -> IdrisParser PDecl
316fnDecl' syn = checkFixity $
317 do (doc, fc, opts', n, acc) <- try (do
320 doc <- option noDocs docComment
322 let initOpts = if default_total ist
325 opts <- fnOpts initOpts
326 acc <- optional accessibility
329 let n = expandNS syn n_in
332 return (doc, fc, opts', n, acc))
333 ty <- typeExpr (allowImp syn)
336 return (PTy (fst doc) (snd doc) syn fc opts' n ty)
340 <?> "function declaration"
341 where checkFixity :: IdrisParser PDecl -> IdrisParser PDecl
342 checkFixity p = do decl <- p
344 Nothing -> return decl
345 Just n -> do fOk <- fixityOK n
347 "Missing fixity declaration for " ++ show n
349 getName (PTy _ _ _ _ _ n _) = Just n
351 fixityOK (NS n _) = fixityOK n
352 fixityOK (UN n) | all (flip elem opChars) (str n) =
353 do fixities <- fmap idris_infixes get
354 return . elem (str n) . map (\ (Fix _ op) -> op) $ fixities
355 | otherwise = return True
356 fixityOK _ = return True
358{-| Parses function options given initial options
365 | '%' 'error_handler'
367 | '%' 'specialise' '[' NameTimesList? ']'
372NameTimes ::= FnName Natural?;
378 | NameTimes ',' NameTimesList
382-- FIXME: Check compatability for function options (i.e. partal/total)
383fnOpts :: [FnOpt] -> IdrisParser [FnOpt]
385 = do reserved "total"; fnOpts (TotalFn : opts)
386 <|> do reserved "partial"; fnOpts (PartialFn : (opts \\ [TotalFn]))
387 <|> do reserved "covering"; fnOpts (CoveringFn : (opts \\ [TotalFn]))
388 <|> do try (lchar '%' *> reserved "export"); c <- stringLiteral;
389 fnOpts (CExport c : opts)
390 <|> do try (lchar '%' *> reserved "assert_total");
391 fnOpts (AssertTotal : opts)
392 <|> do try (lchar '%' *> reserved "error_handler");
393 fnOpts (ErrorHandler : opts)
394 <|> do try (lchar '%' *> reserved "error_reverse");
395 fnOpts (ErrorReverse : opts)
396 <|> do try (lchar '%' *> reserved "reflection");
397 fnOpts (Reflection : opts)
398 <|> do lchar '%'; reserved "specialise";
399 lchar '['; ns <- sepBy nameTimes (lchar ','); lchar ']'
400 fnOpts (Specialise ns : opts)
401 <|> do reserved "implicit"; fnOpts (Implicit : opts)
403 <?> "function modifier"
404 where nameTimes :: IdrisParser (Name, Maybe Int)
405 nameTimes = do n <- fnName
406 t <- option Nothing (do reds <- natural
407 return (Just (fromInteger reds)))
410{- | Parses a postulate
414 DocComment_t? 'postulate' FnOpts* Accesibility? FnOpts* FnName TypeSig Terminator
418postulate :: SyntaxInfo -> IdrisParser PDecl
419postulate syn = do doc <- try $ do doc <- option noDocs docComment
424 let initOpts = if default_total ist
427 opts <- fnOpts initOpts
428 acc <- optional accessibility
431 let n = expandNS syn n_in
433 ty <- typeExpr (allowImp syn)
437 return (PPostulate (fst doc) syn fc opts' n ty)
440{- | Parses a using declaration
444 'using' '(' UsingDeclList ')' OpenBlock Decl* CloseBlock
448using_ :: SyntaxInfo -> IdrisParser [PDecl]
450 do reserved "using"; lchar '('; ns <- usingDeclList syn; lchar ')'
452 let uvars = using syn
453 ds <- many (decl (syn { using = uvars ++ ns }))
456 <?> "using declaration"
458{- | Parses a parameters declaration
462 'parameters' '(' TypeDeclList ')' OpenBlock Decl* CloseBlock
466params :: SyntaxInfo -> IdrisParser [PDecl]
468 do reserved "parameters"; lchar '('; ns <- typeDeclList syn; lchar ')'
470 let pvars = syn_params syn
471 ds <- many (decl syn { syn_params = pvars ++ ns })
474 return [PParams fc ns (concat ds)]
475 <?> "parameters declaration"
477{- | Parses a mutual declaration (for mutually recursive functions)
481 'mutual' OpenBlock Decl* CloseBlock
485mutual :: SyntaxInfo -> IdrisParser [PDecl]
489 let pvars = syn_params syn
490 ds <- many (decl syn)
493 return [PMutual fc (concat ds)]
496{-| Parses a namespace declaration
500 'namespace' identifier OpenBlock Decl+ CloseBlock
504namespace :: SyntaxInfo -> IdrisParser [PDecl]
506 do reserved "namespace"; n <- identifier;
508 ds <- some (decl syn { syn_namespace = n : syn_namespace syn })
510 return [PNamespace n (concat ds)]
511 <?> "namespace declaration"
513{- | Parses a methods block (for instances)
516 InstanceBlock ::= 'where' OpenBlock FnDecl* CloseBlock
519instanceBlock :: SyntaxInfo -> IdrisParser [PDecl]
520instanceBlock syn = do reserved "where"
522 ds <- many (fnDecl syn)
527{- | Parses a methods and instances block (for type classes)
538 'where' OpenBlock MethodOrInstance* CloseBlock
542classBlock :: SyntaxInfo -> IdrisParser [PDecl]
543classBlock syn = do reserved "where"
545 ds <- many ((notEndBlock >> instance_ syn) <|> fnDecl syn)
550{-| Parses a type class declaration
555 | '(' Name ':' Expr ')'
561 DocComment_t? Accessibility? 'class' ConstraintList? Name ClassArgument* ClassBlock?
565class_ :: SyntaxInfo -> IdrisParser [PDecl]
566class_ syn = do (doc, acc) <- try (do
567 doc <- option noDocs docComment
568 acc <- optional accessibility
570 reserved "class"; fc <- getFC; cons <- constraintList syn; n_in <- fnName
571 let n = expandNS syn n_in
573 ds <- option [] (classBlock syn)
574 accData acc n (concatMap declared ds)
575 return [PClass (fst doc) syn fc cons n cs ds]
576 <?> "type-class declaration"
578 carg :: IdrisParser (Name, PTerm)
579 carg = do lchar '('; i <- name; lchar ':'; ty <- expr syn; lchar ')'
584{- | Parses a type class instance declaration
588 'instance' InstanceName? ConstraintList? Name SimpleExpr* InstanceBlock?
593InstanceName ::= '[' Name ']';
596instance_ :: SyntaxInfo -> IdrisParser [PDecl]
597instance_ syn = do reserved "instance"; fc <- getFC
598 en <- optional instanceName
599 cs <- constraintList syn
601 args <- many (simpleExpr syn)
602 let sc = PApp fc (PRef fc cn) (map pexp args)
603 let t = bindList (PPi constraint) (map (\x -> (sMN 0 "constraint", x)) cs) sc
604 ds <- option [] (instanceBlock syn)
605 return [PInstance syn fc cs cn args t en ds]
606 <?> "instance declaration"
607 where instanceName :: IdrisParser Name
608 instanceName = do lchar '['; n_in <- fnName; lchar ']'
609 let n = expandNS syn n_in
614{- | Parses a using declaration list
626 | UsingDecl ',' UsingDeclList'
637usingDeclList :: SyntaxInfo -> IdrisParser [Using]
639 = try (sepBy1 (usingDecl syn) (lchar ','))
640 <|> do ns <- sepBy1 name (lchar ',')
642 t <- typeExpr (disallowImp syn)
643 return (map (\x -> UImplicit x t) ns)
644 <?> "using declaration list"
646{- |Parses a using declaration
655usingDecl :: SyntaxInfo -> IdrisParser Using
656usingDecl syn = try (do x <- fnName
658 t <- typeExpr (disallowImp syn)
659 return (UImplicit x t))
662 return (UConstraint c xs)
663 <?> "using declaration"
665{- | Parse a clause with patterns
671pattern :: SyntaxInfo -> IdrisParser PDecl
672pattern syn = do fc <- getFC
674 return (PClauses fc [] (sMN 2 "_") [clause]) -- collect together later
677{- | Parse a constant applicative form declaration
680CAF ::= 'let' FnName '=' Expr Terminator;
683caf :: SyntaxInfo -> IdrisParser PDecl
684caf syn = do reserved "let"
685 n_in <- fnName; let n = expandNS syn n_in
691 <?> "constant applicative form declaration"
693{- | Parse an argument expression
696ArgExpr ::= HSimpleExpr | {- In Pattern External (User-defined) Expression -};
699argExpr :: SyntaxInfo -> IdrisParser PTerm
700argExpr syn = let syn' = syn { inPattern = True } in
701 try (hsimpleExpr syn') <|> simpleExternalExpr syn'
702 <?> "argument expression"
704{- | Parse a right hand side of a function
714RHSName ::= '{' FnName '}';
717rhs :: SyntaxInfo -> Name -> IdrisParser PTerm
718rhs syn n = do lchar '='; expr syn
720 name <- option n' (do symbol "{"; n <- fnName; symbol "}";
723 return (addLet name r)
724 <|> do reserved "impossible"; return PImpossible
725 <?> "function right hand side"
726 where mkN :: Name -> Name
727 mkN (UN x) = sUN (str x++"_lemma_1")
728 mkN (NS x n) = NS (mkN x) n
731 addLet :: Name -> PTerm -> PTerm
732 addLet nm (PLet n ty val r) = PLet n ty val (addLet nm r)
733 addLet nm (PCase fc t cs) = PCase fc t (map addLetC cs)
734 where addLetC (l, r) = (l, addLet nm r)
735 addLet nm r = (PLet (sUN "value") Placeholder r (PMetavar nm))
737{- |Parses a function clause
740RHSOrWithBlock ::= RHS WhereOrTerminator
741 | 'with' SimpleExpr OpenBlock FnDecl+ CloseBlock
746Clause ::= WExpr+ RHSOrWithBlock
747 | SimpleExpr '<==' FnName RHS WhereOrTerminator
748 | ArgExpr Operator ArgExpr WExpr* RHSOrWithBlock {- Except "=" and "?=" operators to avoid ambiguity -}
749 | FnName ConstraintArg* ImplicitOrArgExpr* WExpr* RHSOrWithBlock
754ImplicitOrArgExpr ::= ImplicitArg | ArgExpr;
758WhereOrTerminator ::= WhereBlock | Terminator;
761clause :: SyntaxInfo -> IdrisParser PClause
763 = do wargs <- try (do pushIndent; some (wExpr syn))
766 n <- case lastParse ist of
768 Nothing -> fail "Invalid clause"
770 let ctxt = tt_ctxt ist
771 let wsyn = syn { syn_namespace = [] }
772 (wheres, nmap) <- choice [do x <- whereBlock n wsyn
777 return $ PClauseR fc wargs r wheres) <|> (do
780 wval <- simpleExpr syn
782 ds <- some $ fnDecl syn
783 let withs = concat ds
785 return $ PWithR fc wargs wval withs)
786 <|> do ty <- try (do pushIndent
791 n_in <- fnName; let n = expandNS syn n_in
794 let ctxt = tt_ctxt ist
795 let wsyn = syn { syn_namespace = [] }
796 (wheres, nmap) <- choice [do x <- whereBlock n wsyn
801 let capp = PLet (sMN 0 "match")
804 (PRef fc (sMN 0 "match"))
806 put (ist { lastParse = Just n })
807 return $ PClause fc n capp [] r wheres
808 <|> do (l, op) <- try (do
812 when (op == "=" || op == "?=" ) $
813 fail "infix clause definition with \"=\" and \"?=\" not supported "
815 let n = expandNS syn (sUN op)
818 wargs <- many (wExpr syn)
820 let wsyn = syn { syn_namespace = [] }
821 (wheres, nmap) <- choice [do x <- whereBlock n wsyn
827 let capp = PApp fc (PRef fc n) [pexp l, pexp r]
828 put (ist { lastParse = Just n })
829 return $ PClause fc n capp wargs rs wheres) <|> (do
832 lchar '(' <?> "parenthesized expression"
833 wval <- bracketed syn
835 ds <- some $ fnDecl syn
838 let capp = PApp fc (PRef fc n) [pexp l, pexp r]
839 let withs = map (fillLHSD n capp wargs) $ concat ds
840 put (ist { lastParse = Just n })
841 return $ PWith fc n capp wargs wval withs)
843 n_in <- fnName; let n = expandNS syn n_in
844 cargs <- many (constraintArg syn)
846 args <- many (try (implicitArg (syn { inPattern = True } ))
847 <|> (fmap pexp (argExpr syn)))
848 wargs <- many (wExpr syn)
849 let capp = PApp fc (PRef fc n)
853 let ctxt = tt_ctxt ist
854 let wsyn = syn { syn_namespace = [] }
855 (wheres, nmap) <- choice [do x <- whereBlock n wsyn
861 put (ist { lastParse = Just n })
862 return $ PClause fc n capp wargs r wheres) <|> (do
865 put (ist { lastParse = Just n })
866 lchar '(' <?> "parenthesized expression"
867 wval <- bracketed syn
869 ds <- some $ fnDecl syn
870 let withs = map (fillLHSD n capp wargs) $ concat ds
873 return $ PWith fc n capp wargs wval withs)
874 <?> "function clause"
876 fillLHS :: Name -> PTerm -> [PTerm] -> PClause -> PClause
877 fillLHS n capp owargs (PClauseR fc wargs v ws)
878 = PClause fc n capp (owargs ++ wargs) v ws
879 fillLHS n capp owargs (PWithR fc wargs v ws)
880 = PWith fc n capp (owargs ++ wargs) v
881 (map (fillLHSD n capp (owargs ++ wargs)) ws)
884 fillLHSD :: Name -> PTerm -> [PTerm] -> PDecl -> PDecl
885 fillLHSD n c a (PClauses fc o fn cs) = PClauses fc o fn (map (fillLHS n c a) cs)
888{-| Parses with pattern
894wExpr :: SyntaxInfo -> IdrisParser PTerm
895wExpr syn = do lchar '|'
899{- | Parses a where block
902WhereBlock ::= 'where' OpenBlock Decl+ CloseBlock;
905whereBlock :: Name -> SyntaxInfo -> IdrisParser ([PDecl], [(Name, Name)])
907 = do reserved "where"
908 ds <- indentedBlock1 (decl syn)
909 let dns = concatMap (concatMap declared) ds
910 return (concat ds, map (\x -> (x, decoration syn x)) dns)
913{- |Parses a code generation target language name
925codegen_ :: IdrisParser Codegen
926codegen_ = do reserved "C"; return ViaC
927 <|> do reserved "Java"; return ViaJava
928 <|> do reserved "JavaScript"; return ViaJavaScript
929 <|> do reserved "Node"; return ViaNode
930 <|> do reserved "LLVM"; return ViaLLVM
931 <|> do reserved "Bytecode"; return Bytecode
932 <?> "code generation language"
934{- |Parses a compiler directive
938 | String ',' StringList
943Directive ::= '%' Directive';
947Directive' ::= 'lib' CodeGen String_t
948 | 'link' CodeGen String_t
949 | 'flag' CodeGen String_t
950 | 'include' CodeGen String_t
953 | 'access' Accessibility
956 | 'dynamic' StringList
957 | 'name' Name NameList
958 | 'error_handlers' Name NameList
959 | 'language' 'TypeProviders'
960 | 'language' 'ErrorReflection'
964directive :: SyntaxInfo -> IdrisParser [PDecl]
965directive syn = do try (lchar '%' *> reserved "lib"); cgn <- codegen_; lib <- stringLiteral;
966 return [PDirective (do addLib cgn lib
967 addIBC (IBCLib cgn lib))]
968 <|> do try (lchar '%' *> reserved "link"); cgn <- codegen_; obj <- stringLiteral;
969 return [PDirective (do dirs <- allImportDirs
970 o <- liftIO $ findInPath dirs obj
971 addIBC (IBCObj cgn obj) -- just name, search on loading ibc
972 addObjectFile cgn o)]
973 <|> do try (lchar '%' *> reserved "flag"); cgn <- codegen_;
974 flag <- stringLiteral
975 return [PDirective (do addIBC (IBCCGFlag cgn flag)
977 <|> do try (lchar '%' *> reserved "include"); cgn <- codegen_; hdr <- stringLiteral;
978 return [PDirective (do addHdr cgn hdr
979 addIBC (IBCHeader cgn hdr))]
980 <|> do try (lchar '%' *> reserved "hide"); n <- fnName
981 return [PDirective (do setAccessibility n Hidden
982 addIBC (IBCAccess n Hidden))]
983 <|> do try (lchar '%' *> reserved "freeze"); n <- iName []
984 return [PDirective (do setAccessibility n Frozen
985 addIBC (IBCAccess n Frozen))]
986 <|> do try (lchar '%' *> reserved "access"); acc <- accessibility
987 return [PDirective (do i <- get
988 put(i { default_access = acc }))]
989 <|> do try (lchar '%' *> reserved "default"); tot <- totality
991 put (i { default_total = tot } )
992 return [PDirective (do i <- get
993 put(i { default_total = tot }))]
994 <|> do try (lchar '%' *> reserved "logging"); i <- natural;
995 return [PDirective (setLogLevel (fromInteger i))]
996 <|> do try (lchar '%' *> reserved "dynamic"); libs <- sepBy1 stringLiteral (lchar ',');
997 return [PDirective (do added <- addDyLib libs
999 Left lib -> addIBC (IBCDyLib (lib_name lib))
1002 <|> do try (lchar '%' *> reserved "name")
1004 ns <- sepBy1 name (lchar ',')
1005 return [PDirective (do ty' <- disambiguate ty
1006 mapM_ (addNameHint ty') ns
1007 mapM_ (\n -> addIBC (IBCNameHint (ty', n))) ns)]
1008 <|> do try (lchar '%' *> reserved "error_handlers")
1011 ns <- sepBy1 name (lchar ',')
1012 return [PDirective (do fn' <- disambiguate fn
1013 ns' <- mapM disambiguate ns
1014 addFunctionErrorHandlers fn' arg ns'
1015 mapM_ (addIBC . IBCFunctionErrorHandler fn' arg) ns')]
1016 <|> do try (lchar '%' *> reserved "language"); ext <- pLangExt;
1017 return [PDirective (addLangExt ext)]
1019 where disambiguate :: Name -> Idris Name
1020 disambiguate n = do i <- getIState
1021 case lookupCtxtName n (idris_implicits i) of
1022 [(n', _)] -> return n'
1023 [] -> throwError (NoSuchVariable n)
1024 more -> throwError (CantResolveAlts (map (show . fst) more))
1026pLangExt :: IdrisParser LanguageExt
1027pLangExt = (reserved "TypeProviders" >> return TypeProviders)
1028 <|> (reserved "ErrorReflection" >> return ErrorReflection)
1030{- | Parses a totality
1033Totality ::= 'partial' | 'total'
1037totality :: IdrisParser Bool
1039 = do reserved "total"; return True
1040 <|> do reserved "partial"; return False
1042{- | Parses a type provider
1045Provider ::= '%' 'provide' Provider_What? '(' FnName TypeSig ')' 'with' Expr;
1046ProviderWhat ::= 'proof' | 'term' | 'type' | 'postulate'
1049provider :: SyntaxInfo -> IdrisParser [PDecl]
1050provider syn = do try (lchar '%' *> reserved "provide");
1052 lchar '('; n <- fnName; lchar ':'; t <- typeExpr syn; lchar ')'
1056 return [PProvider syn fc what n t e]
1058 where provideWhat :: IdrisParser ProvideWhat
1059 provideWhat = option ProvAny
1060 ( ((reserved "proof" <|> reserved "term" <|> reserved "type") *>
1062 <|> (reserved "postulate" *> pure ProvPostulate)
1063 <?> "provider variety")
1064{- | Parses a transform
1067Transform ::= '%' 'transform' Expr '==>' Expr
1070transform :: SyntaxInfo -> IdrisParser [PDecl]
1071transform syn = do try (lchar '%' *> reserved "transform")
1072 -- leave it unchecked, until we work out what this should
1074-- safety <- option True (do reserved "unsafe"
1080 return [PTransform fc False l r]
1084{- * Loading and parsing -}
1085{- | Parses an expression from input -}
1086parseExpr :: IState -> String -> Result PTerm
1087parseExpr st = runparser (fullExpr defaultSyntax) st "(input)"
1089{- | Parses a tactic from input -}
1090parseTactic :: IState -> String -> Result PTactic
1091parseTactic st = runparser (fullTactic defaultSyntax) st "(input)"
1093-- | Parse module header and imports
1094parseImports :: FilePath -> String -> Idris ([String], [(String, Maybe String, FC)], Maybe Delta)
1095parseImports fname input
1097 case parseString (runInnerParser (evalStateT imports i)) (Directed (UTF8.fromString fname) 0 0 0 0) input of
1098 Failure err -> fail (show err)
1099 Success (x, i) -> do putIState i
1101 where imports :: IdrisParser (([String], [(String, Maybe String, FC)], Maybe Delta), IState)
1102 imports = do whiteSpace
1103 mname <- moduleHeader
1106 isEof <- lookAheadMatches eof
1111 return ((mname, ps, mrk'), i)
1113-- | There should be a better way of doing this...
1114findFC :: Doc -> (FC, String)
1115findFC x = let s = show (plain x) in
1116 case span (/= ':') s of
1117 (failname, ':':rest) -> case span isDigit rest of
1118 (line, ':':rest') -> case span isDigit rest' of
1119 (col, ':':msg) -> let pos = (read line, read col) in
1120 (FC failname pos pos, msg)
1122-- | A program is a list of declarations, possibly with associated
1123-- documentation strings.
1124parseProg :: SyntaxInfo -> FilePath -> String -> Maybe Delta ->
1126parseProg syn fname input mrk
1128 case runparser mainProg i fname input of
1129 Failure doc -> do -- FIXME: Get error location from trifecta
1130 -- this can't be the solution!
1131 let (fc, msg) = findFC doc
1133 case idris_outputmode i of
1134 RawOutput -> ihputStrLn (idris_outh i) (show doc)
1135 IdeSlave n -> ihWarn (idris_outh i) fc (P.text msg)
1136 putIState (i { errSpan = Just fc })
1138 Success (x, i) -> do putIState i
1139 reportParserWarnings
1141 where mainProg :: IdrisParser ([PDecl], IState)
1142 mainProg = case mrk of
1143 Nothing -> do i <- get; return ([], i)
1150{- | Load idris module and show error if something wrong happens -}
1151loadModule :: Handle -> FilePath -> Idris String
1153 = idrisCatch (loadModule' outh f)
1154 (\e -> do setErrSpan (getErrSpan e)
1160{- | Load idris module -}
1161loadModule' :: Handle -> FilePath -> Idris String
1164 let file = takeWhile (/= ' ') f
1165 ibcsd <- valIBCSubDir i
1166 ids <- allImportDirs
1167 fp <- liftIO $ findImport ids ibcsd file
1168 if file `elem` imported i
1169 then iLOG $ "Already read " ++ file
1170 else do putIState (i { imported = file : imported i })
1172 IDR fn -> loadSource outh False fn
1173 LIDR fn -> loadSource outh True fn
1175 idrisCatch (loadIBC fn)
1176 (\c -> do iLOG $ fn ++ " failed " ++ pshow i c
1178 IDR sfn -> loadSource outh False sfn
1179 LIDR sfn -> loadSource outh True sfn)
1180 let (dir, fh) = splitFileName file
1181 return (dropExtension fh)
1184{- | Load idris code from file -}
1185loadFromIFile :: Handle -> IFileType -> Idris ()
1186loadFromIFile h i@(IBC fn src)
1187 = do iLOG $ "Skipping " ++ getSrcFile i
1188 idrisCatch (loadIBC fn)
1189 (\err -> ierror $ LoadingFailed fn err)
1191 getSrcFile (IDR fn) = fn
1192 getSrcFile (LIDR fn) = fn
1193 getSrcFile (IBC f src) = getSrcFile src
1195loadFromIFile h (IDR fn) = loadSource' h False fn
1196loadFromIFile h (LIDR fn) = loadSource' h True fn
1198{-| Load idris source code and show error if something wrong happens -}
1199loadSource' :: Handle -> Bool -> FilePath -> Idris ()
1201 = idrisCatch (loadSource h lidr r)
1202 (\e -> do setErrSpan (getErrSpan e)
1204 ihRenderError h (pprintErr ist e))
1206{- | Load Idris source code-}
1207loadSource :: Handle -> Bool -> FilePath -> Idris ()
1209 = do iLOG ("Reading " ++ f)
1211 let def_total = default_total i
1212 file_in <- runIO $ readFile f
1213 file <- if lidr then tclift $ unlit f file_in else return file_in
1214 (mname, imports, pos) <- parseImports f file
1215 reportParserWarnings
1217 -- process and check module aliases
1218 let modAliases = M.fromList
1219 [(prep alias, prep realName) | (realName, Just alias, fc) <- imports]
1220 prep = map T.pack . reverse . Spl.splitOn "/"
1221 aliasNames = [(alias, fc) | (_, Just alias, fc) <- imports]
1222 histogram = groupBy ((==) `on` fst) . sortBy (comparing fst) $ aliasNames
1223 case map head . filter ((/= 1) . length) $ histogram of
1224 [] -> logLvl 3 $ "Module aliases: " ++ show (M.toList modAliases)
1225 (n,fc):_ -> throwError . At fc . Msg $ "import alias not unique: " ++ show n
1228 putIState (i { default_access = Hidden, module_aliases = modAliases })
1229 clearIBC -- start a new .ibc file
1230 mapM_ (addIBC . IBCImport) [realName | (realName, alias, fc) <- imports]
1231 let syntax = defaultSyntax{ syn_namespace = reverse mname }
1232 ds' <- parseProg syntax f file pos
1234 -- Parsing done, now process declarations
1236 let ds = namespaces mname ds'
1237 logLvl 3 (show $ showDecls True ds)
1239 logLvl 10 (show (toAlist (idris_implicits i)))
1240 logLvl 3 (show (idris_infixes i))
1241 -- Now add all the declarations to the context
1243 when v $ ihputStrLn h $ "Type checking " ++ f
1244 -- we totality check after every Mutual block, so if
1245 -- anything is a single definition, wrap it in a
1246 -- mutual block on its own
1247 elabDecls toplevel (map toMutual ds)
1249 -- simplify every definition do give the totality checker
1251 mapM_ (\n -> do logLvl 5 $ "Simplifying " ++ show n
1252 updateContext (simplifyCasedef n))
1253 (map snd (idris_totcheck i))
1254 -- build size change graph from simplified definitions
1255 iLOG "Totality checking"
1257 mapM_ buildSCG (idris_totcheck i)
1258 mapM_ checkDeclTotality (idris_totcheck i)
1260 -- Redo totality check for deferred names
1261 let deftots = idris_defertotcheck i
1262 iLOG $ "Totality checking " ++ show deftots
1263 mapM_ (\x -> do tot <- getTotality x
1265 Total _ -> setTotality x Unchecked
1266 _ -> return ()) (map snd deftots)
1267 mapM_ buildSCG deftots
1268 mapM_ checkDeclTotality deftots
1270 iLOG ("Finished " ++ f)
1271 ibcsd <- valIBCSubDir i
1272 iLOG "Universe checking"
1274 let ibc = ibcPathNoFallback ibcsd f
1276 addHides (hide_list i)
1278 -- Finally, write an ibc if checking was successful
1282 idrisCatch (do writeIBC f ibc; clearIBC)
1283 (\c -> return ()) -- failure is harmless
1285 putIState (i { default_total = def_total,
1289 namespaces :: [String] -> [PDecl] -> [PDecl]
1290 namespaces [] ds = ds
1291 namespaces (x:xs) ds = [PNamespace x (namespaces xs ds)]
1293 toMutual :: PDecl -> PDecl
1294 toMutual m@(PMutual _ d) = m
1295 toMutual (PNamespace x ds) = PNamespace x (map toMutual ds)
1296 toMutual x = let r = PMutual (fileFC "single mutual") [x] in
1298 PClauses _ _ _ _ -> r
1299 PClass _ _ _ _ _ _ _ -> r
1300 PInstance _ _ _ _ _ _ _ _ -> r
1303{- | Adds names to hide list -}
1304addHides :: [(Name, Maybe Accessibility)] -> Idris ()
1305addHides xs = do i <- getIState
1306 let defh = default_access i
1307 let (hs, as) = partition isNothing xs
1310 (map (\ (n, _) -> (n, defh)) hs ++
1311 map (\ (n, Just a) -> (n, a)) as)
1312 where isNothing (_, Nothing) = True
1315 doHide (n, a) = do setAccessibility n a
1316 addIBC (IBCAccess n a)
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