diff --git a/src/Lean/Elab/Frontend.lean b/src/Lean/Elab/Frontend.lean index aec916031d..2c5edcb5ae 100644 --- a/src/Lean/Elab/Frontend.lean +++ b/src/Lean/Elab/Frontend.lean @@ -82,6 +82,12 @@ def process (input : String) (env : Environment) (opts : Options) (fileName : Op let s ← IO.processCommands inputCtx { : Parser.ModuleParserState } (Command.mkState env {} opts) pure (s.commandState.env, s.commandState.messages) +builtin_initialize + registerOption `printMessageEndPos { defValue := false, descr := "print end position of each message in addition to start position" } + +def getPrintMessageEndPos (opts : Options) : Bool := + opts.getBool `printMessageEndPos false + @[export lean_run_frontend] def runFrontend (input : String) (opts : Options) (fileName : String) (mainModuleName : Name) : IO (Environment × Bool) := do let inputCtx := Parser.mkInputContext input fileName @@ -90,7 +96,7 @@ def runFrontend (input : String) (opts : Options) (fileName : String) (mainModul let env := env.setMainModule mainModuleName let s ← IO.processCommands inputCtx parserState (Command.mkState env messages opts) for msg in s.commandState.messages.toList do - IO.print (← msg.toString) + IO.print (← msg.toString (includeEndPos := getPrintMessageEndPos opts)) pure (s.commandState.env, !s.commandState.messages.hasErrors) end Lean.Elab diff --git a/src/Lean/Message.lean b/src/Lean/Message.lean index b970181955..67a96aec62 100644 --- a/src/Lean/Message.lean +++ b/src/Lean/Message.lean @@ -14,8 +14,11 @@ import Lean.Util.PPExt namespace Lean -def mkErrorStringWithPos (fileName : String) (line col : Nat) (msg : String) : String := - fileName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ toString msg +def mkErrorStringWithPos (fileName : String) (pos : Position) (msg : String) (endPos : Option Position := none) : String := + let endPos := match endPos with + | some endPos => s!"-{endPos.line}:{endPos.column}" + | none => "" + s!"{fileName}:{pos.line}:{pos.column}{endPos}: {msg}" inductive MessageSeverity where | information | warning | error @@ -149,14 +152,15 @@ def mkMessageEx (fileName : String) (pos : Position) (endPos : Option Position) { fileName := fileName, pos := pos, endPos := endPos, severity := severity, caption := caption, data := text } namespace Message -protected def toString (msg : Message) : IO String := do +protected def toString (msg : Message) (includeEndPos := false) : IO String := do let mut str ← msg.data.toString + let endPos := if includeEndPos then msg.endPos else none unless msg.caption == "" do str := msg.caption ++ ":\n" ++ str match msg.severity with | MessageSeverity.information => pure () - | MessageSeverity.warning => str := mkErrorStringWithPos msg.fileName msg.pos.line msg.pos.column "warning: " ++ str - | MessageSeverity.error => str := mkErrorStringWithPos msg.fileName msg.pos.line msg.pos.column "error: " ++ str + | MessageSeverity.warning => str := mkErrorStringWithPos msg.fileName msg.pos (endPos := endPos) "warning: " ++ str + | MessageSeverity.error => str := mkErrorStringWithPos msg.fileName msg.pos (endPos := endPos) "error: " ++ str if str.isEmpty || str.back != '\n' then str := str ++ "\n" return str diff --git a/src/Lean/Parser/Basic.lean b/src/Lean/Parser/Basic.lean index 791ed6a18c..e4cfa0ff1c 100644 --- a/src/Lean/Parser/Basic.lean +++ b/src/Lean/Parser/Basic.lean @@ -208,7 +208,7 @@ def toErrorMsg (ctx : ParserContext) (s : ParserState) : String := | none => "" | some msg => let pos := ctx.fileMap.toPosition s.pos - mkErrorStringWithPos ctx.fileName pos.line pos.column (toString msg) + mkErrorStringWithPos ctx.fileName pos (toString msg) def mkNode (s : ParserState) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserState := match s with