compiler/ropes.nim has no global error handler anymore
This commit is contained in:
parent
bf6c2c5ccf
commit
b2c7910fb6
6 changed files with 48 additions and 42 deletions
|
|
@ -19,7 +19,8 @@ import
|
|||
import strutils except `%` # collides with ropes.`%`
|
||||
|
||||
from modulegraphs import ModuleGraph
|
||||
from configuration import warnGcMem, errXMustBeCompileTime, hintDependency, errGenerated
|
||||
from configuration import
|
||||
warnGcMem, errXMustBeCompileTime, hintDependency, errGenerated, errCannotOpenFile
|
||||
import dynlib
|
||||
|
||||
when not declared(dynlib.libCandidates):
|
||||
|
|
@ -1347,7 +1348,8 @@ proc writeHeader(m: BModule) =
|
|||
result.addf("N_CDECL(void, NimMain)(void);$n", [])
|
||||
if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim()
|
||||
result.addf("#endif /* $1 */$n", [guard])
|
||||
writeRope(result, m.filename)
|
||||
if not writeRope(result, m.filename):
|
||||
rawMessage(m.config, errCannotOpenFile, m.filename)
|
||||
|
||||
proc getCFile(m: BModule): string =
|
||||
let ext =
|
||||
|
|
@ -1395,12 +1397,14 @@ proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
|
|||
echo "diff ", cfile.cname, ".backup ", cfile.cname
|
||||
else:
|
||||
echo "new file ", cfile.cname
|
||||
writeRope(code, cfile.cname)
|
||||
if not writeRope(code, cfile.cname):
|
||||
rawMessage(m.config, errCannotOpenFile, cfile.cname)
|
||||
return
|
||||
if existsFile(cfile.obj) and os.fileNewer(cfile.obj, cfile.cname):
|
||||
result = false
|
||||
else:
|
||||
writeRope(code, cfile.cname)
|
||||
if not writeRope(code, cfile.cname):
|
||||
rawMessage(m.config, errCannotOpenFile, cfile.cname)
|
||||
|
||||
# We need 2 different logics here: pending modules (including
|
||||
# 'nim__dat') may require file merging for the combination of dead code
|
||||
|
|
@ -1435,7 +1439,8 @@ proc writeModule(m: BModule, pending: bool) =
|
|||
genInitCode(m)
|
||||
finishTypeDescriptions(m)
|
||||
var code = genModule(m, cf)
|
||||
writeRope(code, cfile)
|
||||
if not writeRope(code, cfile):
|
||||
rawMessage(m.config, errCannotOpenFile, cfile)
|
||||
addFileToCompile(m.config, cf)
|
||||
else:
|
||||
# Consider: first compilation compiles ``system.nim`` and produces
|
||||
|
|
@ -1456,7 +1461,8 @@ proc updateCachedModule(m: BModule) =
|
|||
finishTypeDescriptions(m)
|
||||
|
||||
var code = genModule(m, cf)
|
||||
writeRope(code, cfile)
|
||||
if not writeRope(code, cfile):
|
||||
rawMessage(m.config, errCannotOpenFile, cfile)
|
||||
else:
|
||||
cf.flags = {CfileFlag.Cached}
|
||||
addFileToCompile(m.config, cf)
|
||||
|
|
|
|||
|
|
@ -14,8 +14,6 @@ import
|
|||
|
||||
from modulegraphs import ModuleGraph
|
||||
|
||||
proc generateDot*(project: string)
|
||||
|
||||
type
|
||||
TGen = object of TPassContext
|
||||
module*: PSym
|
||||
|
|
@ -44,8 +42,8 @@ proc addDotDependency(c: PPassContext, n: PNode): PNode =
|
|||
else:
|
||||
discard
|
||||
|
||||
proc generateDot(project: string) =
|
||||
writeRope("digraph $1 {$n$2}$n" % [
|
||||
proc generateDot*(project: string) =
|
||||
discard writeRope("digraph $1 {$n$2}$n" % [
|
||||
rope(changeFileExt(extractFilename(project), "")), gDotGraph],
|
||||
changeFileExt(project, "dot"))
|
||||
|
||||
|
|
|
|||
|
|
@ -777,10 +777,16 @@ proc getOutFile2(conf: ConfigRef; filename, ext, dir: string): string =
|
|||
|
||||
proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) =
|
||||
var content = genOutFile(d)
|
||||
var success = true
|
||||
var filename: string
|
||||
if optStdout in d.conf.globalOptions:
|
||||
writeRope(stdout, content)
|
||||
filename = "<stdout>"
|
||||
else:
|
||||
writeRope(content, getOutFile2(d.conf, filename, outExt, "htmldocs"), useWarning)
|
||||
filename = getOutFile2(d.conf, filename, outExt, "htmldocs")
|
||||
success = writeRope(content, filename)
|
||||
if not success:
|
||||
rawMessage(d.conf, if useWarning: warnCannotOpenFile else: errCannotOpenFile, filename)
|
||||
|
||||
proc writeOutputJson*(d: PDoc, filename, outExt: string,
|
||||
useWarning = false) =
|
||||
|
|
@ -861,7 +867,9 @@ proc commandJson*(conf: ConfigRef) =
|
|||
writeRope(stdout, content)
|
||||
else:
|
||||
#echo getOutFile(gProjectFull, JsonExt)
|
||||
writeRope(content, getOutFile(conf, conf.projectFull, JsonExt), useWarning = false)
|
||||
let filename = getOutFile(conf, conf.projectFull, JsonExt)
|
||||
if not writeRope(content, filename):
|
||||
rawMessage(conf, errCannotOpenFile, filename)
|
||||
|
||||
proc commandTags*(conf: ConfigRef) =
|
||||
var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf)
|
||||
|
|
@ -876,7 +884,9 @@ proc commandTags*(conf: ConfigRef) =
|
|||
writeRope(stdout, content)
|
||||
else:
|
||||
#echo getOutFile(gProjectFull, TagsExt)
|
||||
writeRope(content, getOutFile(conf, conf.projectFull, TagsExt), useWarning = false)
|
||||
let filename = getOutFile(conf, conf.projectFull, TagsExt)
|
||||
if not writeRope(content, filename):
|
||||
rawMessage(conf, errCannotOpenFile, filename)
|
||||
|
||||
proc commandBuildIndex*(conf: ConfigRef) =
|
||||
var content = mergeIndexes(conf.projectFull).rope
|
||||
|
|
@ -887,4 +897,6 @@ proc commandBuildIndex*(conf: ConfigRef) =
|
|||
["Index".rope, nil, nil, rope(getDateStr()),
|
||||
rope(getClockStr()), content, nil, nil, nil])
|
||||
# no analytics because context is not available
|
||||
writeRope(code, getOutFile(conf, "theindex", HtmlExt))
|
||||
let filename = getOutFile(conf, "theindex", HtmlExt)
|
||||
if not writeRope(code, filename):
|
||||
rawMessage(conf, errCannotOpenFile, filename)
|
||||
|
|
|
|||
|
|
@ -438,9 +438,12 @@ proc execExternalProgram*(conf: ConfigRef; cmd: string, msg = hintExecuting) =
|
|||
|
||||
proc generateScript(conf: ConfigRef; projectFile: string, script: Rope) =
|
||||
let (dir, name, ext) = splitFile(projectFile)
|
||||
writeRope(script, getNimcacheDir(conf) / addFileExt("compile_" & name,
|
||||
platform.OS[targetOS].scriptExt))
|
||||
copyFile(conf.libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h")
|
||||
let filename = getNimcacheDir(conf) / addFileExt("compile_" & name,
|
||||
platform.OS[targetOS].scriptExt)
|
||||
if writeRope(script, filename):
|
||||
copyFile(conf.libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h")
|
||||
else:
|
||||
rawMessage(conf, errGenerated, "could not write to file: " & filename)
|
||||
|
||||
proc getOptSpeed(conf: ConfigRef; c: TSystemCC): string =
|
||||
result = getConfigVar(conf, c, ".options.speed")
|
||||
|
|
@ -872,4 +875,6 @@ proc writeMapping*(conf: ConfigRef; symbolMapping: Rope) =
|
|||
add(code, strutils.escape(conf.libpath))
|
||||
|
||||
addf(code, "\n[Symbols]$n$1", [symbolMapping])
|
||||
writeRope(code, joinPath(conf.projectPath, "mapping.txt"))
|
||||
let filename = joinPath(conf.projectPath, "mapping.txt")
|
||||
if not writeRope(code, filename):
|
||||
rawMessage(conf, errGenerated, "could not write to file: " & filename)
|
||||
|
|
|
|||
|
|
@ -618,13 +618,6 @@ proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
|
|||
else:
|
||||
result = fileInfos[i.fileIndex.int32].quotedName
|
||||
|
||||
ropes.errorHandler = proc (err: RopesError, msg: string, useWarning: bool) =
|
||||
case err
|
||||
of rInvalidFormatStr:
|
||||
internalError(newPartialConfigRef(), "ropes: invalid format string: " & msg)
|
||||
of rCannotOpenFile:
|
||||
rawMessage(newPartialConfigRef(), if useWarning: warnCannotOpenFile else: errCannotOpenFile, msg)
|
||||
|
||||
proc listWarnings*(conf: ConfigRef) =
|
||||
msgWriteln(conf, "Warnings:")
|
||||
for warn in warnMin..warnMax:
|
||||
|
|
|
|||
|
|
@ -72,15 +72,6 @@ type
|
|||
|
||||
RopeSeq* = seq[Rope]
|
||||
|
||||
RopesError* = enum
|
||||
rCannotOpenFile
|
||||
rInvalidFormatStr
|
||||
|
||||
# implementation
|
||||
|
||||
var errorHandler*: proc(err: RopesError, msg: string, useWarning = false)
|
||||
# avoid dependency on msgs.nim
|
||||
|
||||
proc len*(a: Rope): int =
|
||||
## the rope's length
|
||||
if a == nil: result = 0
|
||||
|
|
@ -204,13 +195,14 @@ proc writeRope*(f: File, r: Rope) =
|
|||
## writes a rope to a file.
|
||||
for s in leaves(r): write(f, s)
|
||||
|
||||
proc writeRope*(head: Rope, filename: string, useWarning = false) =
|
||||
proc writeRope*(head: Rope, filename: string): bool =
|
||||
var f: File
|
||||
if open(f, filename, fmWrite):
|
||||
if head != nil: writeRope(f, head)
|
||||
close(f)
|
||||
result = true
|
||||
else:
|
||||
errorHandler(rCannotOpenFile, filename, useWarning)
|
||||
result = false
|
||||
|
||||
proc `$`*(r: Rope): string =
|
||||
## converts a rope back to a string.
|
||||
|
|
@ -254,7 +246,7 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
|
|||
if i >= frmt.len or frmt[i] notin {'0'..'9'}: break
|
||||
num = j
|
||||
if j > high(args) + 1:
|
||||
errorHandler(rInvalidFormatStr, $(j))
|
||||
doAssert false, "invalid format string: " & frmt
|
||||
else:
|
||||
add(result, args[j-1])
|
||||
of '{':
|
||||
|
|
@ -265,10 +257,11 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
|
|||
inc(i)
|
||||
num = j
|
||||
if frmt[i] == '}': inc(i)
|
||||
else: errorHandler(rInvalidFormatStr, $(frmt[i]))
|
||||
else:
|
||||
doAssert false, "invalid format string: " & frmt
|
||||
|
||||
if j > high(args) + 1:
|
||||
errorHandler(rInvalidFormatStr, $(j))
|
||||
doAssert false, "invalid format string: " & frmt
|
||||
else:
|
||||
add(result, args[j-1])
|
||||
of 'n':
|
||||
|
|
@ -278,7 +271,7 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
|
|||
add(result, rnl)
|
||||
inc(i)
|
||||
else:
|
||||
errorHandler(rInvalidFormatStr, $(frmt[i]))
|
||||
doAssert false, "invalid format string: " & frmt
|
||||
var start = i
|
||||
while i < length:
|
||||
if frmt[i] != '$': inc(i)
|
||||
|
|
@ -350,7 +343,6 @@ proc equalsFile*(r: Rope, filename: string): bool =
|
|||
proc writeRopeIfNotEqual*(r: Rope, filename: string): bool =
|
||||
# returns true if overwritten
|
||||
if not equalsFile(r, filename):
|
||||
writeRope(r, filename)
|
||||
result = true
|
||||
result = writeRope(r, filename)
|
||||
else:
|
||||
result = false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue