experimental support for outputting code snippets in error messages; implements #301

see #301 for description.
currently, the feature is activated by setting the verbosity level to 2 or more
This commit is contained in:
Zahary Karadjov 2013-04-09 00:15:43 +03:00
commit e7581e7b9b
2 changed files with 21 additions and 0 deletions

View file

@ -506,6 +506,8 @@ var gCodegenLineInfo* = newLineInfo(int32(1), 1, 1)
proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} =
raise newException(ERecoverableError, msg)
proc sourceLine*(i: TLineInfo): PRope
var
gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} - {warnShadowIdent}
gErrorCounter*: int = 0 # counts the number of errors
@ -707,6 +709,11 @@ proc rawMessage*(msg: TMsgKind, arg: string) =
var
lastError = UnknownLineInfo()
proc writeSurroundingSrc(info: TLineInfo) =
const indent = " "
MsgWriteln(indent & info.sourceLine.data)
MsgWriteln(indent & repeatChar(info.col, ' ') & '^')
proc liMessage(info: TLineInfo, msg: TMsgKind, arg: string,
eh: TErrorHandling) =
var frmt: string
@ -732,6 +739,8 @@ proc liMessage(info: TLineInfo, msg: TMsgKind, arg: string,
coordToStr(info.col), getMessageStr(msg, arg)]
if not ignoreMsg:
MsgWriteln(s)
if optPrintSurroundingSrc and msg in errMin..errMax:
info.writeSurroundingSrc
handleError(msg, eh, s)
proc Fatal*(info: TLineInfo, msg: TMsgKind, arg = "") =
@ -771,6 +780,12 @@ proc addSourceLine*(fileIdx: int32, line: string) =
proc sourceLine*(i: TLineInfo): PRope =
if i.fileIndex < 0: return nil
if not optPreserveOrigSource and
fileInfos[i.fileIndex].lines.len == 0:
for line in lines(i.toFullPath):
addSourceLine i.fileIndex, line.string
InternalAssert i.fileIndex < fileInfos.len and
i.line <= fileInfos[i.fileIndex].lines.len