@@ -85,8 +85,8 @@ import qualified Text.PrettyPrint.ANSI.Leijen as PP
8585moduleHeader :: IdrisParser (Maybe (Docstring ()), [String], [(FC, OutputAnnotation)])
8686moduleHeader = P.try (do docs <- optional docComment
8787 noArgs docs
88− reservedHL "module"
89− (i, ifc) <- identifier
88+ keyword "module"
89+ (i, ifc) <- withExtent identifier
9090 P.option ';' (lchar ';')
9191 let modName = moduleName i
9292 return (fmap fst docs,
@@ -116,12 +116,11 @@ data ImportInfo = ImportInfo { import_reexport :: Bool
116116@
117117 -}
118118import_ :: IdrisParser ImportInfo
119−import_ = do fc <- getFC
120− reservedHL "import"
121− reexport <- P.option False (True <$ reservedHL "public")
122− (id, idfc) <- identifier
123− newName <- optional (do reservedHL "as"
124− identifier)
119+import_ = do fc <- extent $ keyword "import"
120+ reexport <- P.option False (True <$ keyword "public")
121+ (id, idfc) <- withExtent identifier
122+ newName <- optional (do keyword "as"
123+ withExtent identifier)
125124 P.option ';' (lchar ';')
126125 return $ ImportInfo reexport (toPath id)
127126 (fmap (\(n, fc) -> (toPath n, fc)) newName)
@@ -137,17 +136,17 @@ import_ = do fc <- getFC
137136@
138137 -}
139138prog :: SyntaxInfo -> IdrisParser [PDecl]
140−prog syn = do whiteSpace
141− decls <- many (decl syn)
142− let c = concat decls
143− case maxline syn of
144− Nothing -> do notOpenBraces; P.eof
145− _ -> return ()
139+prog syn = do (decls, fc) <- withExtent $ do
140+ whiteSpace
141+ decls <- concat <$> many (decl syn)
142+ case maxline syn of
143+ Nothing -> do notOpenBraces; P.eof
144+ _ -> return ()
145+ return decls
146146 ist <- get
147− fc <- getFC
148147 put ist { idris_parsedSpan = Just (FC (fc_fname fc) (0,0) (fc_end fc)),
149148 ibc_write = IBCParsedRegion fc : ibc_write ist }
150− return c
149+ return decls
151150
152151{-| Parses a top-level declaration
153152
@@ -198,7 +197,7 @@ internalDecl syn
198197 <|> fail "End of readable input"
199198 where declBody :: Bool -> IdrisParser [PDecl]
200199 declBody b =
201− P.try (implementation True syn)
200+ P.try (implementation syn)
202201 <|> P.try (openInterface syn)
203202 <|> declBody' b
204203 <|> using_ syn
@@ -248,13 +247,8 @@ decl' syn = fixity
248247externalDecl :: SyntaxInfo -> IdrisParser [PDecl]
249248externalDecl syn = do i <- get
250249 notEndBlock
251− FC fn start _ <- getFC
252− decls <- declExtensions syn (syntaxRulesList $ syntax_rules i)
253− FC _ _ end <- getFC
254− let outerFC = FC fn start end
255− return $ map (mapPDeclFC (fixFC outerFC)
256− (fixFCH fn outerFC))
257− decls
250+ (decls, fc@(FC fn _ _)) <- withExtent $ declExtensions syn (syntaxRulesList $ syntax_rules i)
251+ return $ map (mapPDeclFC (fixFC fc) (fixFCH fn fc)) decls
258252 where
259253 -- | Fix non-highlighting FCs to prevent spurious error location reports
260254 fixFC :: FC -> FC -> FC
@@ -360,18 +354,14 @@ declExtension syn ns rules =
360354 ruleGroup _ _ = False
361355
362356 extSymbol :: SSymbol -> IdrisParser (Maybe (Name, SynMatch))
363− extSymbol (Keyword n) = do fc <- reservedFC (show n)
364− highlightP fc AnnKeyword
365− return Nothing
357+ extSymbol (Keyword n) = Nothing <$ keyword (show n)
366358 extSymbol (Expr n) = do tm <- expr syn
367359 return $ Just (n, SynTm tm)
368360 extSymbol (SimpleExpr n) = do tm <- simpleExpr syn
369361 return $ Just (n, SynTm tm)
370− extSymbol (Binding n) = do (b, fc) <- name
362+ extSymbol (Binding n) = do (b, fc) <- withExtent name
371363 return $ Just (n, SynBind fc b)
372− extSymbol (Symbol s) = do fc <- symbolFC s
373− highlightP fc AnnKeyword
374− return Nothing
364+ extSymbol (Symbol s) = Nothing <$ highlight AnnKeyword (symbol s)
375365
376366{-| Parses a syntax extension declaration (and adds the rule to parser state)
377367
@@ -380,10 +370,8 @@ declExtension syn ns rules =
380370@
381371-}
382372syntaxDecl :: SyntaxInfo -> IdrisParser PDecl
383−syntaxDecl syn = do s <- syntaxRule syn
384− i <- get
385− put (i `addSyntax` s)
386− fc <- getFC
373+syntaxDecl syn = do (s, fc) <- withExtent $ syntaxRule syn
374+ modify $ \i -> i `addSyntax` s
387375 return (PSyntax fc s)
388376
389377-- | Extend an 'IState' with a new syntax extension. See also 'addReplSyntax'.
@@ -428,9 +416,9 @@ syntaxRule syn
428416 = do sty <- P.try (do
429417 pushIndent
430418 sty <- P.option AnySyntax
431− (TermSyntax <$ reservedHL "term"
432− <|> PatternSyntax <$ reservedHL "pattern")
433− reservedHL "syntax"
419+ (TermSyntax <$ keyword "term"
420+ <|> PatternSyntax <$ keyword "pattern")
421+ keyword "syntax"
434422 return sty)
435423 syms <- some syntaxSym
436424 when (all isExpr syms) $ P.unexpected . P.Label . NonEmpty.fromList $ "missing keywords in syntax rule"
@@ -441,7 +429,7 @@ syntaxRule syn
441429 tm <- typeExpr (allowImp syn) >>= uniquifyBinders [n | Binding n <- syms]
442430 terminator
443431 return (Rule (mkSimple syms) tm sty)
444− <|> do reservedHL "decl"; reservedHL "syntax"
432+ <|> do keyword "decl"; keyword "syntax"
445433 syms <- some syntaxSym
446434 when (all isExpr syms) $ P.unexpected . P.Label . NonEmpty.fromList $ "missing keywords in syntax rule"
447435 let ns = mapMaybe getName syms
@@ -542,13 +530,13 @@ SyntaxSym ::= '[' Name_t ']'
542530@
543531 -}
544532syntaxSym :: IdrisParser SSymbol
545−syntaxSym = P.try (do lchar '['; n <- fst <$> name; lchar ']'
533+syntaxSym = P.try (do lchar '['; n <- name; lchar ']'
546534 return (Expr n))
547− <|> P.try (do lchar '{'; n <- fst <$> name; lchar '}'
535+ <|> P.try (do lchar '{'; n <- name; lchar '}'
548536 return (Binding n))
549− <|> do n <- fst <$> iName []
537+ <|> do n <- iName []
550538 return (Keyword n)
551− <|> do sym <- fmap fst stringLiteral
539+ <|> do sym <- stringLiteral
552540 return (Symbol sym)
553541 <?> "syntax symbol"
554542
@@ -582,10 +570,9 @@ fnDecl' syn = (checkDeclFixity $
582570 pushIndent
583571 (doc, argDocs) <- docstring syn
584572 (opts, acc) <- fnOpts
585− (n_in, nfc) <- fnName
573+ (n_in, nfc) <- withExtent fnName
586574 let n = expandNS syn n_in
587− fc <- getFC
588− lchar ':'
575+ fc <- extent $ lchar ':'
589576 return (doc, argDocs, fc, opts, n, nfc, acc))
590577 ty <- typeExpr (allowImp syn)
591578 terminator
@@ -655,16 +642,15 @@ NameTimesList ::=
655642@
656643-}
657644fnOpt :: IdrisParser FnOpt
658−fnOpt = do reservedHL "total"; return TotalFn
659− <|> PartialFn <$ reservedHL "partial"
660− <|> CoveringFn <$ reservedHL "covering"
661− <|> do P.try (lchar '%' *> reserved "export"); c <- fmap fst stringLiteral;
645+fnOpt = do keyword "total"; return TotalFn
646+ <|> PartialFn <$ keyword "partial"
647+ <|> CoveringFn <$ keyword "covering"
648+ <|> do P.try (lchar '%' *> reserved "export"); c <- stringLiteral;
662649 return $ CExport c
663650 <|> NoImplicit <$ P.try (lchar '%' *> reserved "no_implicit")
664651 <|> Inlinable <$ P.try (lchar '%' *> reserved "inline")
665652 <|> StaticFn <$ P.try (lchar '%' *> reserved "static")
666− <|> do P.try (lchar '%' *> reserved "assert_total");
667− fc <- getFC
653+ <|> do fc <- P.try $ extent $ lchar '%' *> reserved "assert_total"
668654 parserWarning fc Nothing (Msg "%assert_total is deprecated. Use the 'assert_total' function instead.")
669655 return AssertTotal
670656 <|> ErrorHandler <$ P.try (lchar '%' *> reserved "error_handler")
@@ -676,11 +662,11 @@ fnOpt = do reservedHL "total"; return TotalFn
676662 <|> do lchar '%'; reserved "specialise";
677663 lchar '['; ns <- P.sepBy nameTimes (lchar ','); lchar ']';
678664 return $ Specialise ns
679− <|> Implicit <$ reservedHL "implicit"
665+ <|> Implicit <$ keyword "implicit"
680666 <?> "function modifier"
681667 where nameTimes :: IdrisParser (Name, Maybe Int)
682− nameTimes = do n <- fst <$> fnName
683− t <- P.option Nothing (do reds <- fmap fst natural
668+ nameTimes = do n <- fnName
669+ t <- P.option Nothing (do reds <- natural
684670 return (Just (fromInteger reds)))
685671 return (n, t)
686672
@@ -700,16 +686,15 @@ postulate syn = do (doc, ext)
700686 return (doc, ext)
701687 ist <- get
702688 (opts, acc) <- fnOpts
703− (n_in, nfc) <- fnName
689+ (n_in, nfc) <- withExtent fnName
704690 let n = expandNS syn n_in
705691 lchar ':'
706692 ty <- typeExpr (allowImp syn)
707− fc <- getFC
708− terminator
693+ fc <- extent $ terminator
709694 addAcc n acc
710695 return (PPostulate ext doc syn fc nfc opts n ty)
711696 <?> "postulate"
712− where ppostDecl = do fc <- reservedHL "postulate"; return False
697+ where ppostDecl = do fc <- keyword "postulate"; return False
713698 <|> do lchar '%'; reserved "extern"; return True
714699
715700{-| Parses a using declaration
@@ -722,7 +707,7 @@ Using ::=
722707 -}
723708using_ :: SyntaxInfo -> IdrisParser [PDecl]
724709using_ syn =
725− do reservedHL "using"
710+ do keyword "using"
726711 lchar '('; ns <- usingDeclList syn; lchar ')'
727712 openBlock
728713 let uvars = using syn
@@ -741,23 +726,24 @@ Params ::=
741726-}
742727params :: SyntaxInfo -> IdrisParser [PDecl]
743728params syn =
744− do reservedHL "parameters"; lchar '('; ns <- typeDeclList syn; lchar ')'
729+ do (ns, fc) <- withExtent $ do
730+ keyword "parameters"
731+ lchar '(' *> typeDeclList syn <* lchar ')'
745732 let ns' = [(n, ty) | (_, n, _, ty) <- ns]
746733 openBlock
747734 let pvars = syn_params syn
748735 ds <- many (decl syn { syn_params = pvars ++ ns' })
749736 closeBlock
750− fc <- getFC
751737 return [PParams fc ns' (concat ds)]
752738 <?> "parameters declaration"
753739
754740-- | Parses an open block
755741openInterface :: SyntaxInfo -> IdrisParser [PDecl]
756742openInterface syn =
757− do reservedHL "using"
758− reservedHL "implementation"
759− fc <- getFC
760− ns <- P.sepBy1 fnName (lchar ',')
743+ do (ns, fc) <- withExtent $ do
744+ keyword "using"
745+ keyword "implementation"
746+ P.sepBy1 (withExtent fnName) (lchar ',')
761747
762748 openBlock
763749 ds <- many (decl syn)
@@ -779,11 +765,10 @@ Mutual ::=
779765-}
780766mutual :: SyntaxInfo -> IdrisParser [PDecl]
781767mutual syn =
782− do reservedHL "mutual"
768+ do fc <- extent $ keyword "mutual"
783769 openBlock
784770 ds <- many (decl (syn { mut_nesting = mut_nesting syn + 1 } ))
785771 closeBlock
786− fc <- getFC
787772 return [PMutual fc (concat ds)]
788773 <?> "mutual block"
789774
@@ -797,8 +782,8 @@ Namespace ::=
797782-}
798783namespace :: SyntaxInfo -> IdrisParser [PDecl]
799784namespace syn =
800− do reservedHL "namespace"
801− (n, nfc) <- identifier
785+ do keyword "namespace"
786+ (n, nfc) <- withExtent identifier
802787 openBlock
803788 ds <- some (decl syn { syn_namespace = n : syn_namespace syn })
804789 closeBlock
@@ -812,7 +797,7 @@ namespace syn =
812797@
813798-}
814799implementationBlock :: SyntaxInfo -> IdrisParser [PDecl]
815−implementationBlock syn = do reservedHL "where"
800+implementationBlock syn = do keyword "where"
816801 openBlock
817802 ds <- many (fnDecl syn)
818803 closeBlock
@@ -835,7 +820,7 @@ InterfaceBlock ::=
835820@
836821-}
837822interfaceBlock :: SyntaxInfo -> IdrisParser (Maybe (Name, FC), Docstring (Either Err PTerm), [PDecl])
838−interfaceBlock syn = do reservedHL "where"
823+interfaceBlock syn = do keyword "where"
839824 openBlock
840825 (cn, cd) <- P.option (Nothing, emptyDocstring) $
841826 P.try (do (doc, _) <- P.option noDocs docComment
@@ -844,7 +829,7 @@ interfaceBlock syn = do reservedHL "where"
844829 ist <- get
845830 let cd' = annotate syn ist cd
846831
847− ds <- many (notEndBlock >> P.try (implementation True syn)
832+ ds <- many (notEndBlock >> P.try (implementation syn)
848833 <|> do x <- data_ syn
849834 return [x]
850835 <|> fnDecl syn)
@@ -853,7 +838,7 @@ interfaceBlock syn = do reservedHL "where"
853838 <?> "interface block"
854839 where
855840 constructor :: IdrisParser (Name, FC)
856− constructor = reservedHL "constructor" *> fnName
841+ constructor = keyword "constructor" *> withExtent fnName
857842
858843 annotate :: SyntaxInfo -> IState -> Docstring () -> Docstring (Either Err PTerm)
859844 annotate syn ist = annotCode $ tryFullExpr syn ist
@@ -879,33 +864,33 @@ interface_ syn = do (doc, argDocs, acc)
879864 acc <- accessibility
880865 interfaceKeyword
881866 return (doc, argDocs, acc))
882− fc <- getFC
883− cons <- constraintList syn
884− let cons' = [(c, ty) | (_, c, _, ty) <- cons]
885− (n_in, nfc) <- fnName
886− let n = expandNS syn n_in
887− cs <- many carg
888− fds <- P.option [(cn, NoFC) | (cn, _, _) <- cs] fundeps
867+ ((cons', n, nfc, cs, fds), fc) <- withExtent $ do
868+ cons <- constraintList syn
869+ let cons' = [(c, ty) | (_, c, _, ty) <- cons]
870+ (n_in, nfc) <- withExtent fnName
871+ let n = expandNS syn n_in
872+ cs <- many carg
873+ fds <- P.option [(cn, NoFC) | (cn, _, _) <- cs] fundeps
874+ return (cons', n, nfc, cs, fds)
875+
889876 (cn, cd, ds) <- P.option (Nothing, fst noDocs, []) (interfaceBlock syn)
890877 accData acc n (concatMap declared ds)
891878 return [PInterface doc syn fc cons' n nfc cs argDocs fds ds cn cd]
892879 <?> "interface declaration"
893880 where
894881 fundeps :: IdrisParser [(Name, FC)]
895− fundeps = do lchar '|'; P.sepBy name (lchar ',')
882+ fundeps = do lchar '|'; P.sepBy (withExtent name) (lchar ',')
896883
897884 interfaceKeyword :: IdrisParser ()
898− interfaceKeyword = reservedHL "interface"
899− <|> do reservedHL "class"
900− fc <- getFC
885+ interfaceKeyword = keyword "interface"
886+ <|> do fc <- extent $ keyword "class"
901887 parserWarning fc Nothing (Msg "The 'class' keyword is deprecated. Use 'interface' instead.")
902888
903889 carg :: IdrisParser (Name, FC, PTerm)
904− carg = do lchar '('; (i, ifc) <- name; lchar ':'; ty <- expr syn; lchar ')'
890+ carg = do lchar '('; (i, ifc) <- withExtent name; lchar ':'; ty <- expr syn; lchar ')'
905891 return (i, ifc, ty)
906− <|> do (i, ifc) <- name
907− fc <- getFC
908− return (i, ifc, PType fc)
892+ <|> do (i, ifc) <- withExtent name
893+ return (i, ifc, PType ifc)
909894
910895{-| Parses an interface implementation declaration
911896
@@ -919,41 +904,39 @@ interface_ syn = do (doc, argDocs, acc)
919904ImplementationName ::= '[' Name ']';
920905@
921906-}
922−implementation :: Bool -> SyntaxInfo -> IdrisParser [PDecl]
923−implementation kwopt syn
924− = do ist <- get
925− (doc, argDocs) <- docstring syn
907+implementation :: SyntaxInfo -> IdrisParser [PDecl]
908+implementation syn = do (doc, argDocs) <- docstring syn
926909 (opts, acc) <- fnOpts
927− if kwopt then optional implementationKeyword
928− else do implementationKeyword
929− return (Just ())
930−
931− fc <- getFC
932− en <- optional implementationName
933− cs <- constraintList syn
934− let cs' = [(c, ty) | (_, c, _, ty) <- cs]
935− (cn, cnfc) <- fnName
936− args <- many (simpleExpr syn)
910+ optional implementationKeyword
911+
912+ ((en, cs, cs', cn, cnfc, args, pnames), fc) <- withExtent $ do
913+ en <- optional implementationName
914+ cs <- constraintList syn
915+ let cs' = [(c, ty) | (_, c, _, ty) <- cs]
916+ (cn, cnfc) <- withExtent fnName
917+ args <- many (simpleExpr syn)
918+ pnames <- implementationUsing
919+ return (en, cs, cs', cn, cnfc, args, pnames)
920+
937921 let sc = PApp fc (PRef cnfc [cnfc] cn) (map pexp args)
938922 let t = bindList (\r -> PPi constraint { pcount = r }) cs sc
939− pnames <- implementationUsing
923+
940924 ds <- implementationBlock syn
941925 return [PImplementation doc argDocs syn fc cs' pnames acc opts cn cnfc args [] t en ds]
942926 <?> "implementation declaration"
943927 where implementationName :: IdrisParser Name
944− implementationName = do lchar '['; n_in <- fst <$> fnName; lchar ']'
928+ implementationName = do lchar '['; n_in <- fnName; lchar ']'
945929 let n = expandNS syn n_in
946930 return n
947931 <?> "implementation name"
948932 implementationKeyword :: IdrisParser ()
949− implementationKeyword = reservedHL "implementation"
950− <|> do reservedHL "instance"
951− fc <- getFC
933+ implementationKeyword = keyword "implementation"
934+ <|> do fc <- extent $ keyword "instance"
952935 parserWarning fc Nothing (Msg "The 'instance' keyword is deprecated. Use 'implementation' (or omit it) instead.")
953936
954937 implementationUsing :: IdrisParser [Name]
955− implementationUsing = do reservedHL "using"
956− ns <- P.sepBy1 fnName (lchar ',')
938+ implementationUsing = do keyword "using"
939+ ns <- P.sepBy1 (withExtent fnName) (lchar ',')
957940 return (map fst ns)
958941 <|> return []
959942
@@ -995,7 +978,7 @@ NameList ::=
995978usingDeclList :: SyntaxInfo -> IdrisParser [Using]
996979usingDeclList syn
997980 = P.try (P.sepBy1 (usingDecl syn) (lchar ','))
998− <|> do ns <- P.sepBy1 (fst <$> name) (lchar ',')
981+ <|> do ns <- P.sepBy1 name (lchar ',')
999982 lchar ':'
1000983 t <- typeExpr (disallowImp syn)
1001984 return (map (\x -> UImplicit x t) ns)
@@ -1011,12 +994,12 @@ UsingDecl ::=
1011994@
1012995-}
1013996usingDecl :: SyntaxInfo -> IdrisParser Using
1014−usingDecl syn = P.try (do x <- fst <$> fnName
997+usingDecl syn = P.try (do x <- fnName
1015998 lchar ':'
1016999 t <- typeExpr (disallowImp syn)
10171000 return (UImplicit x t))
1018− <|> do c <- fst <$> fnName
1019− xs <- many (fst <$> fnName)
1001+ <|> do c <- fnName
1002+ xs <- many fnName
10201003 return (UConstraint c xs)
10211004 <?> "using declaration"
10221005
@@ -1027,8 +1010,7 @@ Pattern ::= Clause;
10271010@
10281011-}
10291012pattern :: SyntaxInfo -> IdrisParser PDecl
1030−pattern syn = do fc <- getFC
1031− clause <- clause syn
1013+pattern syn = do (clause, fc) <- withExtent (clause syn)
10321014 return (PClauses fc [] (sMN 2 "_") [clause]) -- collect together later
10331015 <?> "pattern"
10341016
@@ -1039,13 +1021,12 @@ CAF ::= 'let' FnName '=' Expr Terminator;
10391021@
10401022-}
10411023caf :: SyntaxInfo -> IdrisParser PDecl
1042−caf syn = do reservedHL "let"
1043− n_in <- fst <$> fnName; let n = expandNS syn n_in
1024+caf syn = do keyword "let"
1025+ (n, fc) <- withExtent (expandNS syn <$> fnName)
10441026 pushIndent
10451027 lchar '='
10461028 t <- indented $ expr syn
10471029 terminator
1048− fc <- getFC
10491030 return (PCAF fc n t)
10501031 <?> "constant applicative form declaration"
10511032
@@ -1078,9 +1059,7 @@ rhs syn n = do lchar '='
10781059 indentGt
10791060 expr syn
10801061 <|> do symbol "?=";
1081− fc <- getFC
1082− name <- P.option n' (do symbol "{"; n <- fst <$> fnName; symbol "}";
1083− return n)
1062+ (name, fc) <- withExtent $ P.option n' (symbol "{" *> fnName <* symbol "}")
10841063 r <- expr syn
10851064 return (addLet fc name r)
10861065 <|> impossible
@@ -1124,8 +1103,8 @@ WhereOrTerminator ::= WhereBlock | Terminator;
11241103-}
11251104clause :: SyntaxInfo -> IdrisParser PClause
11261105clause syn
1127− = do wargs <- P.try (do pushIndent; some (wExpr syn))
1128− fc <- getFC
1106+ = appExtent (do
1107+ wargs <- P.try (do pushIndent; some (wExpr syn))
11291108 ist <- get
11301109 n <- case lastParse ist of
11311110 Just t -> return t
@@ -1134,22 +1113,21 @@ clause syn
11341113 let wsyn = syn { syn_namespace = [], syn_toplevel = False }
11351114 (wheres, nmap) <- whereBlock n wsyn <* popIndent
11361115 <|> ([], []) <$ terminator
1137− return $ PClauseR fc wargs r wheres) <|> (do
1116+ return $ \fc -> PClauseR fc wargs r wheres) <|> (do
11381117 popIndent
1139− reservedHL "with"
1118+ keyword "with"
11401119 wval <- simpleExpr syn
11411120 pn <- optProof
11421121 openBlock
11431122 ds <- some $ fnDecl syn
11441123 let withs = concat ds
11451124 closeBlock
1146− return $ PWithR fc wargs wval pn withs)
1125+ return $ \fc -> PWithR fc wargs wval pn withs))
11471126 <|> do ty <- P.try (do pushIndent
11481127 ty <- simpleExpr syn
11491128 symbol "<=="
11501129 return ty)
1151− fc <- getFC
1152− n_in <- fst <$> fnName; let n = expandNS syn n_in
1130+ (n, fc) <- withExtent (expandNS syn <$> fnName)
11531131 r <- rhs syn n
11541132 let wsyn = syn { syn_namespace = [] }
11551133 (wheres, nmap) <- whereBlock n wsyn <* popIndent
@@ -1164,15 +1142,14 @@ clause syn
11641142 <|> do (l, op, nfc) <- P.try (do
11651143 pushIndent
11661144 l <- argExpr syn
1167− (op, nfc) <- symbolicOperatorFC
1145+ (op, nfc) <- withExtent symbolicOperator
11681146 when (op == "=" || op == "?=" ) $
11691147 fail "infix clause definition with \"=\" and \"?=\" not supported "
11701148 return (l, op, nfc))
11711149 let n = expandNS syn (sUN op)
11721150 r <- argExpr syn
1173− fc <- getFC
11741151 wargs <- many (wExpr syn)
1175− (do rs <- rhs syn n
1152+ (do (rs, fc) <- withExtent (rhs syn n)
11761153 let wsyn = syn { syn_namespace = [] }
11771154 (wheres, nmap) <- whereBlock n wsyn <* popIndent
11781155 <|> ([], []) <$ terminator
@@ -1181,9 +1158,11 @@ clause syn
11811158 put (ist { lastParse = Just n })
11821159 return $ PClause fc n capp wargs rs wheres) <|> (do
11831160 popIndent
1184− reservedHL "with"
1185− wval <- bracketed syn
1186− pn <- optProof
1161+ ((wval, pn), fc) <- withExtent $ do
1162+ keyword "with"
1163+ wval <- bracketed syn
1164+ pn <- optProof
1165+ return (wval, pn)
11871166 openBlock
11881167 ds <- some $ fnDecl syn
11891168 closeBlock
@@ -1193,35 +1172,37 @@ clause syn
11931172 put (ist { lastParse = Just n })
11941173 return $ PWith fc n capp wargs wval pn withs)
11951174 <|> do pushIndent
1196− (n_in, nfc) <- fnName; let n = expandNS syn n_in
1197− fc <- getFC
1175+ (n_in, nfc) <- withExtent fnName; let n = expandNS syn n_in
11981176 args <- many (P.try (implicitArg (syn { inPattern = True } ))
11991177 <|> P.try (constraintArg (syn { inPattern = True }))
12001178 <|> (fmap pexp (argExpr syn)))
12011179 wargs <- many (wExpr syn)
1202− let capp = PApp fc (PRef nfc [nfc] n) args
1203− (do r <- rhs syn n
1180+ (do (r, fc) <- withExtent (rhs syn n)
1181+ let capp = PApp fc (PRef nfc [nfc] n) args
12041182 let wsyn = syn { syn_namespace = [] }
12051183 (wheres, nmap) <- whereBlock n wsyn <* popIndent
12061184 <|> ([], []) <$ terminator
12071185 ist <- get
12081186 put (ist { lastParse = Just n })
12091187 return $ PClause fc n capp wargs r wheres) <|> (do
1210− reservedHL "with"
1188+ keyword "with"
12111189 ist <- get
12121190 put (ist { lastParse = Just n })
1213− wval <- bracketed syn
1214− pn <- optProof
1191+ ((wval, pn), fc) <- withExtent $ do
1192+ wval <- bracketed syn
1193+ pn <- optProof
1194+ return (wval, pn)
12151195 openBlock
12161196 ds <- some $ fnDecl syn
1217− let withs = map (fillLHSD n capp wargs) $ concat ds
12181197 closeBlock
12191198 popIndent
1199+ let capp = PApp fc (PRef nfc [nfc] n) args
1200+ let withs = map (fillLHSD n capp wargs) $ concat ds
12201201 return $ PWith fc n capp wargs wval pn withs)
12211202 <?> "function clause"
12221203 where
1223− optProof = P.option Nothing (do reservedHL "proof"
1224− n <- fnName
1204+ optProof = P.option Nothing (do keyword "proof"
1205+ n <- withExtent fnName
12251206 return (Just n))
12261207
12271208 fillLHS :: Name -> PTerm -> [PTerm] -> PClause -> PClause
@@ -1255,7 +1236,7 @@ WhereBlock ::= 'where' OpenBlock Decl+ CloseBlock;
12551236-}
12561237whereBlock :: Name -> SyntaxInfo -> IdrisParser ([PDecl], [(Name, Name)])
12571238whereBlock n syn
1258− = do reservedHL "where"
1239+ = do keyword "where"
12591240 ds <- indentedBlock1 (decl syn)
12601241 let dns = concatMap (concatMap declared) ds
12611242 return (concat ds, map (\x -> (x, decoration syn x)) dns)
@@ -1274,7 +1255,7 @@ Codegen ::= 'C'
12741255@
12751256-}
12761257codegen_ :: IdrisParser Codegen
1277−codegen_ = do n <- fst <$> identifier
1258+codegen_ = do n <- identifier
12781259 return (Via IBCFormat (map toLower n))
12791260 <|> do reserved "Bytecode"; return Bytecode
12801261 <?> "code generation language"
@@ -1315,27 +1296,27 @@ Directive' ::= 'lib' CodeGen String_t
13151296directive :: SyntaxInfo -> IdrisParser [PDecl]
13161297directive syn = do P.try (lchar '%' *> reserved "lib")
13171298 cgn <- codegen_
1318− lib <- fmap fst stringLiteral
1299+ lib <- stringLiteral
13191300 return [PDirective (DLib cgn lib)]
13201301 <|> do P.try (lchar '%' *> reserved "link")
1321− cgn <- codegen_; obj <- fst <$> stringLiteral
1302+ cgn <- codegen_; obj <- stringLiteral
13221303 return [PDirective (DLink cgn obj)]
13231304 <|> do P.try (lchar '%' *> reserved "flag")
1324− cgn <- codegen_; flag <- fst <$> stringLiteral
1305+ cgn <- codegen_; flag <- stringLiteral
13251306 return [PDirective (DFlag cgn flag)]
13261307 <|> do P.try (lchar '%' *> reserved "include")
13271308 cgn <- codegen_
1328− hdr <- fst <$> stringLiteral
1309+ hdr <- stringLiteral
13291310 return [PDirective (DInclude cgn hdr)]
1330− <|> do P.try (lchar '%' *> reserved "hide"); n <- fst <$> fnName
1311+ <|> do P.try (lchar '%' *> reserved "hide"); n <- fnName
13311312 return [PDirective (DHide n)]
1332− <|> do P.try (lchar '%' *> reserved "freeze"); n <- fst <$> iName []
1313+ <|> do P.try (lchar '%' *> reserved "freeze"); n <- iName []
13331314 return [PDirective (DFreeze n)]
1334− <|> do P.try (lchar '%' *> reserved "thaw"); n <- fst <$> iName []
1315+ <|> do P.try (lchar '%' *> reserved "thaw"); n <- iName []
13351316 return [PDirective (DThaw n)]
13361317 -- injectivity assertins are intended for debugging purposes
13371318 -- only, and won't be documented/could be removed at any point
1338− <|> do P.try (lchar '%' *> reserved "assert_injective"); n <- fst <$> fnName
1319+ <|> do P.try (lchar '%' *> reserved "assert_injective"); n <- fnName
13391320 return [PDirective (DInjective n)]
13401321 -- Assert totality of something after definition. This is
13411322 -- here as a debugging aid, so commented out...
@@ -1351,34 +1332,33 @@ directive syn = do P.try (lchar '%' *> reserved "lib")
13511332 put (i { default_total = tot } )
13521333 return [PDirective (DDefault tot)]
13531334 <|> do P.try (lchar '%' *> reserved "logging")
1354− i <- fst <$> natural
1335+ i <- natural
13551336 return [PDirective (DLogging i)]
13561337 <|> do P.try (lchar '%' *> reserved "dynamic")
1357− libs <- P.sepBy1 (fmap fst stringLiteral) (lchar ',')
1338+ libs <- P.sepBy1 stringLiteral (lchar ',')
13581339 return [PDirective (DDynamicLibs libs)]
13591340 <|> do P.try (lchar '%' *> reserved "name")
1360− (ty, tyFC) <- fnName
1361− ns <- P.sepBy1 name (lchar ',')
1341+ (ty, tyFC) <- withExtent fnName
1342+ ns <- P.sepBy1 (withExtent name) (lchar ',')
13621343 return [PDirective (DNameHint ty tyFC ns)]
13631344 <|> do P.try (lchar '%' *> reserved "error_handlers")
1364− (fn, nfc) <- fnName
1365− (arg, afc) <- fnName
1366− ns <- P.sepBy1 name (lchar ',')
1345+ (fn, nfc) <- withExtent fnName
1346+ (arg, afc) <- withExtent fnName
1347+ ns <- P.sepBy1 (withExtent name) (lchar ',')
13671348 return [PDirective (DErrorHandlers fn nfc arg afc ns) ]
13681349 <|> do P.try (lchar '%' *> reserved "language"); ext <- pLangExt;
13691350 return [PDirective (DLanguage ext)]
13701351 <|> do P.try (lchar '%' *> reserved "deprecate")
1371− n <- fst <$> fnName
1372− alt <- P.option "" (fst <$> stringLiteral)
1352+ n <- fnName
1353+ alt <- P.option "" stringLiteral
13731354 return [PDirective (DDeprecate n alt)]
13741355 <|> do P.try (lchar '%' *> reserved "fragile")
1375− n <- fst <$> fnName
1376− alt <- P.option "" (fst <$> stringLiteral)
1356+ n <- fnName
1357+ alt <- P.option "" stringLiteral
13771358 return [PDirective (DFragile n alt)]
1378− <|> do fc <- getFC
1379− P.try (lchar '%' *> reserved "used")
1380− fn <- fst <$> fnName
1381− arg <- fst <$> iName []
1359+ <|> do fc <- extent $ P.try (lchar '%' *> reserved "used")
1360+ fn <- fnName
1361+ arg <- iName []
13821362 return [PDirective (DUsed fc fn arg)]
13831363 <|> do P.try (lchar '%' *> reserved "auto_implicits")
13841364 b <- on_off
@@ -1405,9 +1385,9 @@ Totality ::= 'partial' | 'total' | 'covering'
14051385-}
14061386totality :: IdrisParser DefaultTotality
14071387totality
1408− = do reservedHL "total"; return DefaultCheckingTotal
1409− <|> do reservedHL "partial"; return DefaultCheckingPartial
1410− <|> do reservedHL "covering"; return DefaultCheckingCovering
1388+ = do keyword "total"; return DefaultCheckingTotal
1389+ <|> do keyword "partial"; return DefaultCheckingPartial
1390+ <|> do keyword "covering"; return DefaultCheckingCovering
14111391
14121392{-| Parses a type provider
14131393
@@ -1418,25 +1398,20 @@ ProviderWhat ::= 'proof' | 'term' | 'type' | 'postulate'
14181398 -}
14191399provider :: SyntaxInfo -> IdrisParser [PDecl]
14201400provider syn = do doc <- P.try (do (doc, _) <- docstring syn
1421− fc1 <- getFC
1422− lchar '%'
1423− fc2 <- reservedFC "provide"
1424− highlightP (spanFC fc1 fc2) AnnKeyword
1401+ highlight AnnKeyword $ lchar '%' *> reserved "provide"
14251402 return doc)
14261403 provideTerm doc <|> providePostulate doc
14271404 <?> "type provider"
14281405 where provideTerm doc =
1429− do lchar '('; (n, nfc) <- fnName; lchar ':'; t <- typeExpr syn; lchar ')'
1430− fc <- getFC
1431− reservedHL "with"
1432− e <- expr syn <?> "provider expression"
1406+ do lchar '('; (n, nfc) <- withExtent fnName; lchar ':'; t <- typeExpr syn; lchar ')'
1407+ keyword "with"
1408+ (e, fc) <- withExtent (expr syn) <?> "provider expression"
14331409 return [PProvider doc syn fc nfc (ProvTerm t e) n]
14341410 providePostulate doc =
1435− do reservedHL "postulate"
1436− (n, nfc) <- fnName
1437− fc <- getFC
1438− reservedHL "with"
1439− e <- expr syn <?> "provider expression"
1411+ do keyword "postulate"
1412+ (n, nfc) <- withExtent fnName
1413+ keyword "with"
1414+ (e, fc) <- withExtent (expr syn) <?> "provider expression"
14401415 return [PProvider doc syn fc nfc (ProvPostulate e) n]
14411416
14421417{-| Parses a transform
@@ -1452,8 +1427,7 @@ transform syn = do P.try (lchar '%' *> reserved "transform")
14521427-- safety <- option True (do reserved "unsafe"
14531428-- return False)
14541429 l <- expr syn
1455− fc <- getFC
1456− symbol "==>"
1430+ fc <- extent $ symbol "==>"
14571431 r <- expr syn
14581432 return [PTransform fc False l r]
14591433 <?> "transform"
@@ -1466,12 +1440,8 @@ RunElabDecl ::= '%' 'runElab' Expr
14661440-}
14671441runElabDecl :: SyntaxInfo -> IdrisParser PDecl
14681442runElabDecl syn =
1469− do kwFC <- P.try (do fc <- getFC
1470− lchar '%'
1471− fc' <- reservedFC "runElab"
1472− return (spanFC fc fc'))
1443+ do kwFC <- P.try (highlight AnnKeyword (extent $ lchar '%' *> reserved "runElab"))
14731444 script <- expr syn <?> "elaborator script"
1474− highlightP kwFC AnnKeyword
14751445 return $ PRunElabDecl kwFC script (syn_namespace syn)
14761446 <?> "top-level elaborator script"
14771447
@@ -1482,7 +1452,7 @@ parseExpr st = runparser (fullExpr defaultSyntax) st "(input)"
14821452
14831453{-| Parses a constant form input -}
14841454parseConst :: IState -> String -> Either ParseError Const
1485−parseConst st = runparser (fmap fst constant) st "(input)"
1455+parseConst st = runparser constant st "(input)"
14861456
14871457{-| Parses a tactic from input -}
14881458parseTactic :: IState -> String -> Either ParseError PTactic
@@ -1501,7 +1471,7 @@ parseElabShellStep ist = runparser (Right <$> do_ defaultSyntax <|> Left <$> ela
15011471 (expressionTactic ["t", "type"] ECheck) <|>
15021472 (expressionTactic ["search"] ESearch ) <|>
15031473 (do reserved "doc"
1504− doc <- (Right . fst <$> constant) <|> (Left . fst <$> fnName)
1474+ doc <- (Right <$> constant) <|> (Left <$> fnName)
15051475 P.eof
15061476 return (EDocStr doc))
15071477 <?> "elab command"
@@ -1513,7 +1483,7 @@ parseElabShellStep ist = runparser (Right <$> do_ defaultSyntax <|> Left <$> ela
15131483 spaced parser = indentGt *> parser
15141484
15151485-- | Parse module header and imports
1516−parseImports :: FilePath -> String -> Idris (Maybe (Docstring ()), [String], [ImportInfo], Maybe ParseState)
1486+parseImports :: FilePath -> String -> Idris (Maybe (Docstring ()), [String], [ImportInfo], Maybe Mark)
15171487parseImports fname input
15181488 = do i <- getIState
15191489 case runparser imports i fname input of
@@ -1525,13 +1495,13 @@ parseImports fname input
15251495 return x
15261496 where imports :: IdrisParser ((Maybe (Docstring ()), [String],
15271497 [ImportInfo],
1528− Maybe ParseState),
1498+ Maybe Mark),
15291499 [(FC, OutputAnnotation)], IState)
15301500 imports = do optional shebang
15311501 whiteSpace
15321502 (mdoc, mname, annots) <- moduleHeader
15331503 ps_exp <- many import_
1534− mrk <- P.getParserState
1504+ mrk <- mark
15351505 isEof <- lookAheadMatches P.eof
15361506 let mrk' = if isEof
15371507 then Nothing
@@ -1558,15 +1528,15 @@ fixColour True doc = doc
15581528
15591529-- | A program is a list of declarations, possibly with associated
15601530-- documentation strings.
1561−parseProg :: SyntaxInfo -> FilePath -> String -> Maybe ParseState -> Idris [PDecl]
1531+parseProg :: SyntaxInfo -> FilePath -> String -> Maybe Mark -> Idris [PDecl]
15621532parseProg syn fname input mrk
15631533 = do i <- getIState
15641534 case runparser mainProg i fname input of
1565− Left err -> do let fc = parseErrorFC err
1535+ Left err -> do let fc = errorExtent err
15661536 i <- getIState
15671537 case idris_outputmode i of
15681538 RawOutput h -> iputStrLn (show . fixColour (idris_colourRepl i) . parseErrorDoc $ err)
1569− IdeMode n h -> iWarn fc (Util.Pretty.text $ parseErrorMessage err)
1539+ IdeMode n h -> iWarn fc (Util.Pretty.text $ errorMessage err)
15701540 putIState (i { errSpan = Just fc })
15711541 return []
15721542 Right (x, i) -> do putIState i
@@ -1576,11 +1546,42 @@ parseProg syn fname input mrk
15761546 mainProg = case mrk of
15771547 Nothing -> do i <- get; return ([], i)
15781548 Just mrk -> do
1579− P.setParserState mrk
1549+ restore mrk
15801550 ds <- prog syn
15811551 i' <- get
15821552 return (ds, i')
15831553
1554+-- | Collect 'PClauses' with the same function name
1555+collect :: [PDecl] -> [PDecl]
1556+collect (c@(PClauses _ o _ _) : ds)
1557+ = clauses (cname c) [] (c : ds)
1558+ where clauses :: Maybe Name -> [PClause] -> [PDecl] -> [PDecl]
1559+ clauses j@(Just n) acc (PClauses fc _ _ [PClause fc' n' l ws r w] : ds)
1560+ | n == n' = clauses j (PClause fc' n' l ws r (collect w) : acc) ds
1561+ clauses j@(Just n) acc (PClauses fc _ _ [PWith fc' n' l ws r pn w] : ds)
1562+ | n == n' = clauses j (PWith fc' n' l ws r pn (collect w) : acc) ds
1563+ clauses (Just n) acc xs = PClauses (fcOf c) o n (reverse acc) : collect xs
1564+ clauses Nothing acc (x:xs) = collect xs
1565+ clauses Nothing acc [] = []
1566+
1567+ cname :: PDecl -> Maybe Name
1568+ cname (PClauses fc _ _ [PClause _ n _ _ _ _]) = Just n
1569+ cname (PClauses fc _ _ [PWith _ n _ _ _ _ _]) = Just n
1570+ cname (PClauses fc _ _ [PClauseR _ _ _ _]) = Nothing
1571+ cname (PClauses fc _ _ [PWithR _ _ _ _ _]) = Nothing
1572+ fcOf :: PDecl -> FC
1573+ fcOf (PClauses fc _ _ _) = fc
1574+collect (PParams f ns ps : ds) = PParams f ns (collect ps) : collect ds
1575+collect (POpenInterfaces f ns ps : ds) = POpenInterfaces f ns (collect ps) : collect ds
1576+collect (PMutual f ms : ds) = PMutual f (collect ms) : collect ds
1577+collect (PNamespace ns fc ps : ds) = PNamespace ns fc (collect ps) : collect ds
1578+collect (PInterface doc f s cs n nfc ps pdocs fds ds cn cd : ds')
1579+ = PInterface doc f s cs n nfc ps pdocs fds (collect ds) cn cd : collect ds'
1580+collect (PImplementation doc argDocs f s cs pnames acc opts n nfc ps pextra t en ds : ds')
1581+ = PImplementation doc argDocs f s cs pnames acc opts n nfc ps pextra t en (collect ds) : collect ds'
1582+collect (d : ds) = d : collect ds
1583+collect [] = []
1584+
15841585{-| Load idris module and show error if something wrong happens -}
15851586loadModule :: FilePath -> IBCPhase -> Idris (Maybe String)
15861587loadModule f phase
@@ -1681,8 +1682,8 @@ loadSource lidr f toline
16811682 ]
16821683 prep = map T.pack . reverse . Spl.splitOn [pathSeparator]
16831684 aliasNames = [ (alias, fc)
1684− | ImportInfo { import_rename = Just (alias, _)
1685− , import_location = fc } <- imports
1685+ | ImportInfo { import_rename = Just (alias, fc)
1686+ } <- imports
16861687 ]
16871688 histogram = groupBy ((==) `on` fst) . sortBy (comparing fst) $ aliasNames
16881689 case map head . filter ((/= 1) . length) $ histogram of