cleanup compiler/prettybase to not use redudant global variables
This commit is contained in:
parent
545b1582cd
commit
c640bd2d1b
5 changed files with 36 additions and 86 deletions
|
|
@ -714,11 +714,6 @@ proc handleCRLF(L: var TLexer, pos: int): int =
|
||||||
if col > MaxLineLength:
|
if col > MaxLineLength:
|
||||||
lexMessagePos(L, hintLineTooLong, pos)
|
lexMessagePos(L, hintLineTooLong, pos)
|
||||||
|
|
||||||
if optEmbedOrigSrc in L.config.globalOptions:
|
|
||||||
let lineStart = cast[ByteAddress](L.buf) + L.lineStart
|
|
||||||
let line = newString(cast[cstring](lineStart), col)
|
|
||||||
addSourceLine(L.config, L.fileIdx, line)
|
|
||||||
|
|
||||||
case L.buf[pos]
|
case L.buf[pos]
|
||||||
of CR:
|
of CR:
|
||||||
registerLine()
|
registerLine()
|
||||||
|
|
|
||||||
|
|
@ -194,7 +194,7 @@ type
|
||||||
quotedFullName*: Rope # cached quoted full name for codegen
|
quotedFullName*: Rope # cached quoted full name for codegen
|
||||||
# purposes
|
# purposes
|
||||||
|
|
||||||
lines*: seq[Rope] # the source code of the module
|
lines*: seq[string] # the source code of the module
|
||||||
# used for better error messages and
|
# used for better error messages and
|
||||||
# embedding the original source in the
|
# embedding the original source in the
|
||||||
# generated code
|
# generated code
|
||||||
|
|
@ -202,6 +202,7 @@ type
|
||||||
# and parsed; usually "" but is used
|
# and parsed; usually "" but is used
|
||||||
# for 'nimsuggest'
|
# for 'nimsuggest'
|
||||||
hash*: string # the checksum of the file
|
hash*: string # the checksum of the file
|
||||||
|
dirty*: bool # for 'nimfix' / 'nimpretty' like tooling
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
fullContent*: string
|
fullContent*: string
|
||||||
FileIndex* = distinct int32
|
FileIndex* = distinct int32
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,6 @@ proc newLineInfo*(conf: ConfigRef; filename: string, line, col: int): TLineInfo
|
||||||
proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} =
|
proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} =
|
||||||
raise newException(ERecoverableError, msg)
|
raise newException(ERecoverableError, msg)
|
||||||
|
|
||||||
proc sourceLine*(conf: ConfigRef; i: TLineInfo): Rope
|
|
||||||
|
|
||||||
proc concat(strings: openarray[string]): string =
|
proc concat(strings: openarray[string]): string =
|
||||||
var totalLen = 0
|
var totalLen = 0
|
||||||
|
|
@ -409,6 +408,24 @@ proc resetAttributes*(conf: ConfigRef) =
|
||||||
if {optUseColors, optStdout} * conf.globalOptions == {optUseColors}:
|
if {optUseColors, optStdout} * conf.globalOptions == {optUseColors}:
|
||||||
terminal.resetAttributes(stderr)
|
terminal.resetAttributes(stderr)
|
||||||
|
|
||||||
|
proc addSourceLine(conf: ConfigRef; fileIdx: FileIndex, line: string) =
|
||||||
|
conf.m.fileInfos[fileIdx.int32].lines.add line
|
||||||
|
|
||||||
|
proc sourceLine*(conf: ConfigRef; i: TLineInfo): string =
|
||||||
|
if i.fileIndex.int32 < 0: return ""
|
||||||
|
|
||||||
|
if not optPreserveOrigSource(conf) and conf.m.fileInfos[i.fileIndex.int32].lines.len == 0:
|
||||||
|
try:
|
||||||
|
for line in lines(toFullPath(conf, i)):
|
||||||
|
addSourceLine conf, i.fileIndex, line.string
|
||||||
|
except IOError:
|
||||||
|
discard
|
||||||
|
assert i.fileIndex.int32 < conf.m.fileInfos.len
|
||||||
|
# can happen if the error points to EOF:
|
||||||
|
if i.line.int > conf.m.fileInfos[i.fileIndex.int32].lines.len: return ""
|
||||||
|
|
||||||
|
result = conf.m.fileInfos[i.fileIndex.int32].lines[i.line.int-1]
|
||||||
|
|
||||||
proc writeSurroundingSrc(conf: ConfigRef; info: TLineInfo) =
|
proc writeSurroundingSrc(conf: ConfigRef; info: TLineInfo) =
|
||||||
const indent = " "
|
const indent = " "
|
||||||
msgWriteln(conf, indent & $sourceLine(conf, info))
|
msgWriteln(conf, indent & $sourceLine(conf, info))
|
||||||
|
|
@ -518,24 +535,6 @@ template assertNotNil*(conf: ConfigRef; e): untyped =
|
||||||
template internalAssert*(conf: ConfigRef, e: bool) =
|
template internalAssert*(conf: ConfigRef, e: bool) =
|
||||||
if not e: internalError(conf, $instantiationInfo())
|
if not e: internalError(conf, $instantiationInfo())
|
||||||
|
|
||||||
proc addSourceLine*(conf: ConfigRef; fileIdx: FileIndex, line: string) =
|
|
||||||
conf.m.fileInfos[fileIdx.int32].lines.add line.rope
|
|
||||||
|
|
||||||
proc sourceLine*(conf: ConfigRef; i: TLineInfo): Rope =
|
|
||||||
if i.fileIndex.int32 < 0: return nil
|
|
||||||
|
|
||||||
if not optPreserveOrigSource(conf) and conf.m.fileInfos[i.fileIndex.int32].lines.len == 0:
|
|
||||||
try:
|
|
||||||
for line in lines(toFullPath(conf, i)):
|
|
||||||
addSourceLine conf, i.fileIndex, line.string
|
|
||||||
except IOError:
|
|
||||||
discard
|
|
||||||
assert i.fileIndex.int32 < conf.m.fileInfos.len
|
|
||||||
# can happen if the error points to EOF:
|
|
||||||
if i.line.int > conf.m.fileInfos[i.fileIndex.int32].lines.len: return nil
|
|
||||||
|
|
||||||
result = conf.m.fileInfos[i.fileIndex.int32].lines[i.line.int-1]
|
|
||||||
|
|
||||||
proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
|
proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
|
||||||
assert i.fileIndex.int32 >= 0
|
assert i.fileIndex.int32 >= 0
|
||||||
if optExcessiveStackTrace in conf.globalOptions:
|
if optExcessiveStackTrace in conf.globalOptions:
|
||||||
|
|
|
||||||
|
|
@ -27,19 +27,19 @@ var
|
||||||
|
|
||||||
proc overwriteFiles*(conf: ConfigRef) =
|
proc overwriteFiles*(conf: ConfigRef) =
|
||||||
let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on"
|
let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on"
|
||||||
for i in 0 .. high(gSourceFiles):
|
for i in 0 .. high(conf.m.fileInfos):
|
||||||
if gSourceFiles[i].dirty and not gSourceFiles[i].isNimfixFile and
|
if conf.m.fileInfos[i].dirty and
|
||||||
(not gOnlyMainfile or gSourceFiles[i].fileIdx == conf.projectMainIdx.FileIndex):
|
(not gOnlyMainfile or FileIndex(i) == conf.projectMainIdx):
|
||||||
let newFile = if gOverWrite: gSourceFiles[i].fullpath
|
let newFile = if gOverWrite: conf.m.fileInfos[i].fullpath
|
||||||
else: gSourceFiles[i].fullpath.changeFileExt(".pretty.nim")
|
else: conf.m.fileInfos[i].fullpath.changeFileExt(".pretty.nim")
|
||||||
try:
|
try:
|
||||||
var f = open(newFile, fmWrite)
|
var f = open(newFile, fmWrite)
|
||||||
for line in gSourceFiles[i].lines:
|
for line in conf.m.fileInfos[i].lines:
|
||||||
if doStrip:
|
if doStrip:
|
||||||
f.write line.strip(leading = false, trailing = true)
|
f.write line.strip(leading = false, trailing = true)
|
||||||
else:
|
else:
|
||||||
f.write line
|
f.write line
|
||||||
f.write(gSourceFiles[i].newline)
|
f.write(conf.m.fileInfos[i], "\L")
|
||||||
f.close
|
f.close
|
||||||
except IOError:
|
except IOError:
|
||||||
rawMessage(conf, errGenerated, "cannot open file: " & newFile)
|
rawMessage(conf, errGenerated, "cannot open file: " & newFile)
|
||||||
|
|
@ -94,9 +94,7 @@ proc beautifyName(s: string, k: TSymKind): string =
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
proc replaceInFile(conf: ConfigRef; info: TLineInfo; newName: string) =
|
proc replaceInFile(conf: ConfigRef; info: TLineInfo; newName: string) =
|
||||||
loadFile(conf, info)
|
let line = conf.m.fileInfos[info.fileIndex.int].lines[info.line.int-1]
|
||||||
|
|
||||||
let line = gSourceFiles[info.fileIndex.int].lines[info.line.int-1]
|
|
||||||
var first = min(info.col.int, line.len)
|
var first = min(info.col.int, line.len)
|
||||||
if first < 0: return
|
if first < 0: return
|
||||||
#inc first, skipIgnoreCase(line, "proc ", first)
|
#inc first, skipIgnoreCase(line, "proc ", first)
|
||||||
|
|
@ -108,8 +106,8 @@ proc replaceInFile(conf: ConfigRef; info: TLineInfo; newName: string) =
|
||||||
if differ(line, first, last, newName):
|
if differ(line, first, last, newName):
|
||||||
# last-first+1 != newName.len or
|
# last-first+1 != newName.len or
|
||||||
var x = line.substr(0, first-1) & newName & line.substr(last+1)
|
var x = line.substr(0, first-1) & newName & line.substr(last+1)
|
||||||
system.shallowCopy(gSourceFiles[info.fileIndex.int].lines[info.line.int-1], x)
|
system.shallowCopy(conf.m.fileInfos[info.fileIndex.int].lines[info.line.int-1], x)
|
||||||
gSourceFiles[info.fileIndex.int].dirty = true
|
conf.m.fileInfos[info.fileIndex.int].dirty = true
|
||||||
|
|
||||||
proc checkStyle(conf: ConfigRef; cache: IdentCache; info: TLineInfo, s: string, k: TSymKind; sym: PSym) =
|
proc checkStyle(conf: ConfigRef; cache: IdentCache; info: TLineInfo, s: string, k: TSymKind; sym: PSym) =
|
||||||
let beau = beautifyName(s, k)
|
let beau = beautifyName(s, k)
|
||||||
|
|
|
||||||
|
|
@ -11,45 +11,6 @@ import strutils, lexbase, streams
|
||||||
import ".." / [ast, msgs, lineinfos, idents, options]
|
import ".." / [ast, msgs, lineinfos, idents, options]
|
||||||
from os import splitFile
|
from os import splitFile
|
||||||
|
|
||||||
type
|
|
||||||
TSourceFile* = object
|
|
||||||
lines*: seq[string]
|
|
||||||
dirty*, isNimfixFile*: bool
|
|
||||||
fullpath*, newline*: string
|
|
||||||
fileIdx*: FileIndex
|
|
||||||
|
|
||||||
var
|
|
||||||
gSourceFiles*: seq[TSourceFile] = @[]
|
|
||||||
|
|
||||||
proc loadFile*(conf: ConfigRef; info: TLineInfo) =
|
|
||||||
let i = info.fileIndex.int
|
|
||||||
if i >= gSourceFiles.len:
|
|
||||||
gSourceFiles.setLen(i+1)
|
|
||||||
if gSourceFiles[i].lines.isNil:
|
|
||||||
gSourceFiles[i].fileIdx = info.fileIndex
|
|
||||||
gSourceFiles[i].lines = @[]
|
|
||||||
let path = toFullPath(conf, info)
|
|
||||||
gSourceFiles[i].fullpath = path
|
|
||||||
gSourceFiles[i].isNimfixFile = path.splitFile.ext == ".nimfix"
|
|
||||||
# we want to die here for IOError:
|
|
||||||
for line in lines(path):
|
|
||||||
gSourceFiles[i].lines.add(line)
|
|
||||||
# extract line ending of the file:
|
|
||||||
var lex: BaseLexer
|
|
||||||
open(lex, newFileStream(path, fmRead))
|
|
||||||
var pos = lex.bufpos
|
|
||||||
while true:
|
|
||||||
case lex.buf[pos]
|
|
||||||
of '\c':
|
|
||||||
gSourceFiles[i].newline = "\c\L"
|
|
||||||
break
|
|
||||||
of '\L', '\0':
|
|
||||||
gSourceFiles[i].newline = "\L"
|
|
||||||
break
|
|
||||||
else: discard
|
|
||||||
inc pos
|
|
||||||
close(lex)
|
|
||||||
|
|
||||||
const
|
const
|
||||||
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
||||||
|
|
||||||
|
|
@ -62,9 +23,7 @@ proc differ*(line: string, a, b: int, x: string): bool =
|
||||||
result = cmpIgnoreStyle(y, x) == 0 and y != x
|
result = cmpIgnoreStyle(y, x) == 0 and y != x
|
||||||
|
|
||||||
proc replaceDeprecated*(conf: ConfigRef; info: TLineInfo; oldSym, newSym: PIdent) =
|
proc replaceDeprecated*(conf: ConfigRef; info: TLineInfo; oldSym, newSym: PIdent) =
|
||||||
loadFile(conf, info)
|
let line = sourceLine(conf, info)
|
||||||
|
|
||||||
let line = gSourceFiles[info.fileIndex.int32].lines[info.line.int-1]
|
|
||||||
var first = min(info.col.int, line.len)
|
var first = min(info.col.int, line.len)
|
||||||
if first < 0: return
|
if first < 0: return
|
||||||
#inc first, skipIgnoreCase(line, "proc ", first)
|
#inc first, skipIgnoreCase(line, "proc ", first)
|
||||||
|
|
@ -75,20 +34,18 @@ proc replaceDeprecated*(conf: ConfigRef; info: TLineInfo; oldSym, newSym: PIdent
|
||||||
let last = first+identLen(line, first)-1
|
let last = first+identLen(line, first)-1
|
||||||
if cmpIgnoreStyle(line[first..last], oldSym.s) == 0:
|
if cmpIgnoreStyle(line[first..last], oldSym.s) == 0:
|
||||||
var x = line.substr(0, first-1) & newSym.s & line.substr(last+1)
|
var x = line.substr(0, first-1) & newSym.s & line.substr(last+1)
|
||||||
system.shallowCopy(gSourceFiles[info.fileIndex.int32].lines[info.line.int-1], x)
|
system.shallowCopy(conf.m.fileInfos[info.fileIndex.int32].lines[info.line.int-1], x)
|
||||||
gSourceFiles[info.fileIndex.int32].dirty = true
|
conf.m.fileInfos[info.fileIndex.int32].dirty = true
|
||||||
#if newSym.s == "File": writeStackTrace()
|
#if newSym.s == "File": writeStackTrace()
|
||||||
|
|
||||||
proc replaceDeprecated*(conf: ConfigRef; info: TLineInfo; oldSym, newSym: PSym) =
|
proc replaceDeprecated*(conf: ConfigRef; info: TLineInfo; oldSym, newSym: PSym) =
|
||||||
replaceDeprecated(conf, info, oldSym.name, newSym.name)
|
replaceDeprecated(conf, info, oldSym.name, newSym.name)
|
||||||
|
|
||||||
proc replaceComment*(conf: ConfigRef; info: TLineInfo) =
|
proc replaceComment*(conf: ConfigRef; info: TLineInfo) =
|
||||||
loadFile(conf, info)
|
let line = sourceLine(conf, info)
|
||||||
|
|
||||||
let line = gSourceFiles[info.fileIndex.int32].lines[info.line.int-1]
|
|
||||||
var first = info.col.int
|
var first = info.col.int
|
||||||
if line[first] != '#': inc first
|
if line[first] != '#': inc first
|
||||||
|
|
||||||
var x = line.substr(0, first-1) & "discard " & line.substr(first+1).escape
|
var x = line.substr(0, first-1) & "discard " & line.substr(first+1).escape
|
||||||
system.shallowCopy(gSourceFiles[info.fileIndex.int32].lines[info.line.int-1], x)
|
system.shallowCopy(conf.m.fileInfos[info.fileIndex.int32].lines[info.line.int-1], x)
|
||||||
gSourceFiles[info.fileIndex.int32].dirty = true
|
conf.m.fileInfos[info.fileIndex.int32].dirty = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue