Skip to content

Commit

Permalink
fixes #537; of all the fixes I could think of this one seems to be th…
Browse files Browse the repository at this point in the history
…e least annoying (#557)
  • Loading branch information
Araq authored Feb 14, 2025
1 parent c97fc6e commit 48bb5e1
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 37 deletions.
23 changes: 12 additions & 11 deletions src/hexer/nifcgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ proc writeOutput(e: var EContext, rootInfo: PackedLineInfo) =
var stack: seq[PackedLineInfo] = @[]
if rootInfo.isValid:
stack.add rootInfo
var (file, line, col) = unpack(pool.man, rootInfo)
let (file, line, col) = unpack(pool.man, rootInfo)
b.addLineInfo(col, line, pool.files[file])
b.addTree "stmts"
for h in e.headers:
Expand All @@ -1326,16 +1326,17 @@ proc writeOutput(e: var EContext, rootInfo: PackedLineInfo) =
let info = c.info
if info.isValid:
var (file, line, col) = unpack(pool.man, info)
var fileAsStr = ""
if stack.len > 0:
let (pfile, pline, pcol) = unpack(pool.man, stack[^1])
if file != pfile: fileAsStr = pool.files[file]
if fileAsStr.len == 0:
line = line - pline
col = col - pcol
else:
fileAsStr = pool.files[file]
b.addLineInfo(col, line, fileAsStr)
if file.isValid:
var fileAsStr = ""
if stack.len > 0:
let (pfile, pline, pcol) = unpack(pool.man, stack[^1])
if file != pfile: fileAsStr = pool.files[file]
if fileAsStr.len == 0:
line = line - pline
col = col - pcol
else:
fileAsStr = pool.files[file]
b.addLineInfo(col, line, fileAsStr)

case c.kind
of DotToken:
Expand Down
2 changes: 2 additions & 0 deletions src/lib/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ type

const
NoLineInfo* = PackedLineInfo(0'u32)
NoFile* = FileId(0'u32)

proc isValid*(x: PackedLineInfo): bool {.inline.} = uint32(x) != uint32(NoLineInfo)
proc isValid*(x: FileId): bool {.inline.} = x != NoFile

proc pack*(m: var LineInfoManager; file: FileId; line, col: int32): PackedLineInfo =
if file.uint32 <= FileMax.uint32 and line <= LineMax and col <= ColMax:
Expand Down
21 changes: 11 additions & 10 deletions src/lib/nifstreams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,17 @@ proc toString*(tree: openArray[PackedToken]; produceLineInfo = true): string =
let k = tree[n].kind
if produceLineInfo and info.isValid and k != ParRi:
var (file, line, col) = unpack(pool.man, info)
var fileAsStr = ""
if stack.len > 0:
let (pfile, pline, pcol) = unpack(pool.man, stack[^1])
if file != pfile: fileAsStr = pool.files[file]
if fileAsStr.len == 0:
line = line - pline
col = col - pcol
else:
fileAsStr = pool.files[file]
b.addLineInfo(col, line, fileAsStr)
if file.isValid:
var fileAsStr = ""
if stack.len > 0:
let (pfile, pline, pcol) = unpack(pool.man, stack[^1])
if file != pfile: fileAsStr = pool.files[file]
if fileAsStr.len == 0:
line = line - pline
col = col - pcol
else:
fileAsStr = pool.files[file]
b.addLineInfo(col, line, fileAsStr)

case k
of DotToken:
Expand Down
5 changes: 4 additions & 1 deletion src/nifc/amd64/emitter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ proc isTag(c: var Context; tag: TagId): bool =
proc error(c: var Context; msg: string) {.noreturn.} =
if c.current.info.isValid:
let (f, line, col) = unpack(pool.man, c.current.info)
echo "[Error] ", pool.files[f] & "(" & $line & ", " & $col & "): " & msg
if f.isValid:
echo "[Error] ", pool.files[f] & "(" & $line & ", " & $col & "): " & msg
else:
echo "[Error] ???: " & msg
else:
echo "[Error] ???: " & msg
when defined(debug):
Expand Down
9 changes: 5 additions & 4 deletions src/nifc/codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ proc error(m: Module; msg: string; n: Cursor) {.noreturn.} =
let info = n.info
if info.isValid:
let (file, line, col) = unpack(pool.man, info)
write stdout, pool.files[file]
write stdout, "(" & $line & ", " & $(col+1) & ") "
if file.isValid:
write stdout, pool.files[file]
write stdout, "(" & $line & ", " & $(col+1) & ") "
write stdout, "[Error] "
write stdout, msg
writeLine stdout, toString(n, false)
Expand Down Expand Up @@ -336,8 +337,8 @@ proc genCLineDir(c: var GeneratedCode; info: PackedLineInfo) =
c.add Space
c.add name
c.add NewLine

c.fileIds.incl id
if id.isValid:
c.fileIds.incl id

template moveToDataSection(body: untyped) =
let oldLen = c.code.len
Expand Down
3 changes: 2 additions & 1 deletion src/nifc/nifc_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ proc parse*(r: var Reader; m: var Module; parentInfo: PackedLineInfo): bool =
# relative file position
if t.pos.line != 0 or t.pos.col != 0:
let (file, line, col) = unpack(pool.man, parentInfo)
currentInfo = pack(pool.man, file, line+t.pos.line, col+t.pos.col)
if file.isValid:
currentInfo = pack(pool.man, file, line+t.pos.line, col+t.pos.col)
else:
# absolute file position:
let fileId = pool.files.getOrIncl(decodeFilename t)
Expand Down
17 changes: 9 additions & 8 deletions src/nimony/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ proc asNimCode*(n: Cursor): string =
inc n2

when false: #if m0.isValid:
let (_, line0, col0) = unpack(pool.man, m0)
if m1.isValid:
let (_, line1, col1) = unpack(pool.man, m1)
result = extract(pool.files[file0],
FilePosition(line: line0, col: col0),
FilePosition(line: line1, col: col1))
else:
result = extract(pool.files[file0], FilePosition(line: line0, col: col0))
if file0.isValid:
let (_, line0, col0) = unpack(pool.man, m0)
if m1.isValid:
let (_, line1, col1) = unpack(pool.man, m1)
result = extract(pool.files[file0],
FilePosition(line: line0, col: col0),
FilePosition(line: line1, col: col1))
else:
result = extract(pool.files[file0], FilePosition(line: line0, col: col0))
var visible = false
for i in 0..<result.len:
if result[i] > ' ':
Expand Down
2 changes: 1 addition & 1 deletion src/nimony/reporters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ proc shortenDir*(x: string): string =

proc infoToStr*(info: PackedLineInfo): string =
let (file, line, col) = unpack(pool.man, info)
if not info.isValid:
if not info.isValid or not file.isValid:
result = "???"
else:
result = pool.files[file].shortenDir()
Expand Down
5 changes: 4 additions & 1 deletion src/nimony/semos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ proc parseFile*(nimFile: string; paths: openArray[string]): TokenBuf =

proc getFile*(info: PackedLineInfo): string =
let fid = unpack(pool.man, info)[0]
result = pool.files[fid]
if fid.isValid:
result = pool.files[fid]
else:
result = ""

proc selfExec*(c: var SemContext; file: string; moreArgs: string) =
exec os.getAppFilename() & c.commandLineArgs & moreArgs & " --ischild m " & quoteShell(file)
Expand Down

0 comments on commit 48bb5e1

Please sign in to comment.