This commit is contained in:
parent
532647171e
commit
d1fe195dcc
4 changed files with 33 additions and 16 deletions
|
|
@ -30,7 +30,7 @@ type
|
||||||
lastTok: TTokType
|
lastTok: TTokType
|
||||||
inquote, lastTokWasTerse: bool
|
inquote, lastTokWasTerse: bool
|
||||||
semicolons: SemicolonKind
|
semicolons: SemicolonKind
|
||||||
col, lastLineNumber, lineSpan, indentLevel, indWidth: int
|
col, lastLineNumber, lineSpan, indentLevel, indWidth*: int
|
||||||
keepIndents*: int
|
keepIndents*: int
|
||||||
doIndentMore*: int
|
doIndentMore*: int
|
||||||
content: string
|
content: string
|
||||||
|
|
@ -41,6 +41,7 @@ type
|
||||||
proc openEmitter*(em: var Emitter, cache: IdentCache;
|
proc openEmitter*(em: var Emitter, cache: IdentCache;
|
||||||
config: ConfigRef, fileIdx: FileIndex) =
|
config: ConfigRef, fileIdx: FileIndex) =
|
||||||
let fullPath = Absolutefile config.toFullPath(fileIdx)
|
let fullPath = Absolutefile config.toFullPath(fileIdx)
|
||||||
|
if em.indWidth == 0:
|
||||||
em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead),
|
em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead),
|
||||||
cache, config)
|
cache, config)
|
||||||
if em.indWidth == 0: em.indWidth = 2
|
if em.indWidth == 0: em.indWidth = 2
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ type
|
||||||
inSemiStmtList*: int
|
inSemiStmtList*: int
|
||||||
emptyNode: PNode
|
emptyNode: PNode
|
||||||
when defined(nimpretty2):
|
when defined(nimpretty2):
|
||||||
em: Emitter
|
em*: Emitter
|
||||||
|
|
||||||
SymbolMode = enum
|
SymbolMode = enum
|
||||||
smNormal, smAllowNil, smAfterDot
|
smNormal, smAllowNil, smAfterDot
|
||||||
|
|
|
||||||
|
|
@ -158,14 +158,18 @@ proc openParsers*(p: var TParsers, fileIdx: FileIndex, inputstream: PLLStream;
|
||||||
proc closeParsers*(p: var TParsers) =
|
proc closeParsers*(p: var TParsers) =
|
||||||
parser.closeParser(p.parser)
|
parser.closeParser(p.parser)
|
||||||
|
|
||||||
proc parseFile*(fileIdx: FileIndex; cache: IdentCache; config: ConfigRef): PNode {.procvar.} =
|
proc setupParsers*(p: var TParsers; fileIdx: FileIndex; cache: IdentCache;
|
||||||
var
|
config: ConfigRef): bool =
|
||||||
p: TParsers
|
var f: File
|
||||||
f: File
|
|
||||||
let filename = toFullPathConsiderDirty(config, fileIdx)
|
let filename = toFullPathConsiderDirty(config, fileIdx)
|
||||||
if not open(f, filename.string):
|
if not open(f, filename.string):
|
||||||
rawMessage(config, errGenerated, "cannot open file: " & filename.string)
|
rawMessage(config, errGenerated, "cannot open file: " & filename.string)
|
||||||
return
|
return false
|
||||||
openParsers(p, fileIdx, llStreamOpen(f), cache, config)
|
openParsers(p, fileIdx, llStreamOpen(f), cache, config)
|
||||||
|
result = true
|
||||||
|
|
||||||
|
proc parseFile*(fileIdx: FileIndex; cache: IdentCache; config: ConfigRef): PNode {.procvar.} =
|
||||||
|
var p: TParsers
|
||||||
|
if setupParsers(p, fileIdx, cache, config):
|
||||||
result = parseAll(p)
|
result = parseAll(p)
|
||||||
closeParsers(p)
|
closeParsers(p)
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@
|
||||||
when not defined(nimpretty):
|
when not defined(nimpretty):
|
||||||
{.error: "This needs to be compiled with --define:nimPretty".}
|
{.error: "This needs to be compiled with --define:nimPretty".}
|
||||||
|
|
||||||
import ../compiler / [idents, msgs, ast, syntaxes, renderer, options, pathutils]
|
import ../compiler / [idents, msgs, ast, syntaxes, renderer, options,
|
||||||
|
pathutils, layouter]
|
||||||
|
|
||||||
import parseopt, strutils, os
|
import parseopt, strutils, os
|
||||||
|
|
||||||
|
|
@ -26,6 +27,7 @@ Usage:
|
||||||
Options:
|
Options:
|
||||||
--backup:on|off create a backup file before overwritting (default: ON)
|
--backup:on|off create a backup file before overwritting (default: ON)
|
||||||
--output:file set the output file (default: overwrite the .nim file)
|
--output:file set the output file (default: overwrite the .nim file)
|
||||||
|
--indent:N set the number of spaces that is used for indentation
|
||||||
--version show the version
|
--version show the version
|
||||||
--help show this help
|
--help show this help
|
||||||
"""
|
"""
|
||||||
|
|
@ -40,12 +42,20 @@ proc writeVersion() =
|
||||||
stdout.flushFile()
|
stdout.flushFile()
|
||||||
quit(0)
|
quit(0)
|
||||||
|
|
||||||
proc prettyPrint(infile, outfile: string) =
|
type
|
||||||
|
PrettyOptions = object
|
||||||
|
indWidth: int
|
||||||
|
|
||||||
|
proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
|
||||||
var conf = newConfigRef()
|
var conf = newConfigRef()
|
||||||
let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
|
let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
|
||||||
conf.outFile = AbsoluteFile outfile
|
conf.outFile = AbsoluteFile outfile
|
||||||
when defined(nimpretty2):
|
when defined(nimpretty2):
|
||||||
discard parseFile(fileIdx, newIdentCache(), conf)
|
var p: TParsers
|
||||||
|
p.parser.em.indWidth = opt.indWidth
|
||||||
|
if setupParsers(p, fileIdx, newIdentCache(), conf):
|
||||||
|
discard parseAll(p)
|
||||||
|
closeParsers(p)
|
||||||
else:
|
else:
|
||||||
let tree = parseFile(fileIdx, newIdentCache(), conf)
|
let tree = parseFile(fileIdx, newIdentCache(), conf)
|
||||||
renderModule(tree, infile, outfile, {}, fileIdx, conf)
|
renderModule(tree, infile, outfile, {}, fileIdx, conf)
|
||||||
|
|
@ -53,6 +63,7 @@ proc prettyPrint(infile, outfile: string) =
|
||||||
proc main =
|
proc main =
|
||||||
var infile, outfile: string
|
var infile, outfile: string
|
||||||
var backup = true
|
var backup = true
|
||||||
|
var opt: PrettyOptions
|
||||||
for kind, key, val in getopt():
|
for kind, key, val in getopt():
|
||||||
case kind
|
case kind
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
|
|
@ -63,6 +74,7 @@ proc main =
|
||||||
of "version", "v": writeVersion()
|
of "version", "v": writeVersion()
|
||||||
of "backup": backup = parseBool(val)
|
of "backup": backup = parseBool(val)
|
||||||
of "output", "o": outfile = val
|
of "output", "o": outfile = val
|
||||||
|
of "indent": opt.indWidth = parseInt(val)
|
||||||
else: writeHelp()
|
else: writeHelp()
|
||||||
of cmdEnd: assert(false) # cannot happen
|
of cmdEnd: assert(false) # cannot happen
|
||||||
if infile.len == 0:
|
if infile.len == 0:
|
||||||
|
|
@ -70,6 +82,6 @@ proc main =
|
||||||
if backup:
|
if backup:
|
||||||
os.copyFile(source=infile, dest=changeFileExt(infile, ".nim.backup"))
|
os.copyFile(source=infile, dest=changeFileExt(infile, ".nim.backup"))
|
||||||
if outfile.len == 0: outfile = infile
|
if outfile.len == 0: outfile = infile
|
||||||
prettyPrint(infile, outfile)
|
prettyPrint(infile, outfile, opt)
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue