mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-21 16:31:39 +00:00
beginnings of the new nimpretty tool; still unusable
This commit is contained in:
67
tools/nimpretty.nim
Normal file
67
tools/nimpretty.nim
Normal file
@@ -0,0 +1,67 @@
|
||||
#
|
||||
#
|
||||
# The Nim Compiler
|
||||
# (c) Copyright 2017 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Standard tool for pretty printing.
|
||||
|
||||
when not defined(nimpretty):
|
||||
{.error: "This needs to be compiled with --define:nimPretty".}
|
||||
|
||||
import ../compiler / [idents, msgs, ast, syntaxes, renderer]
|
||||
|
||||
import parseopt, strutils, os
|
||||
|
||||
const
|
||||
Version = "0.1"
|
||||
Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
|
||||
|
||||
(c) 2017 Andreas Rumpf
|
||||
Usage:
|
||||
nimpretty [options] file.nim
|
||||
Options:
|
||||
--backup:ON|OFF create a backup file before overwritting (default: ON)
|
||||
--version show the version
|
||||
--help show this help
|
||||
"""
|
||||
|
||||
proc writeHelp() =
|
||||
stdout.write(Usage)
|
||||
stdout.flushFile()
|
||||
quit(0)
|
||||
|
||||
proc writeVersion() =
|
||||
stdout.write(Version & "\n")
|
||||
stdout.flushFile()
|
||||
quit(0)
|
||||
|
||||
proc prettyPrint(infile: string) =
|
||||
let fileIdx = fileInfoIdx(infile)
|
||||
let tree = parseFile(fileIdx, newIdentCache())
|
||||
renderModule(tree, infile, {})
|
||||
|
||||
proc main =
|
||||
var infile: string
|
||||
var backup = true
|
||||
for kind, key, val in getopt():
|
||||
case kind
|
||||
of cmdArgument:
|
||||
infile = key
|
||||
of cmdLongoption, cmdShortOption:
|
||||
case normalize(key)
|
||||
of "help", "h": writeHelp()
|
||||
of "version", "v": writeVersion()
|
||||
of "backup": backup = parseBool(val)
|
||||
else: writeHelp()
|
||||
of cmdEnd: assert(false) # cannot happen
|
||||
if infile.len == 0:
|
||||
quit "[Error] no input file."
|
||||
if backup:
|
||||
os.copyFile(source=infile, dest=changeFileExt(infile, ".nim.backup"))
|
||||
prettyPrint(infile)
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user