summary refs log tree commit diff stats
path: root/nimpretty/nimpretty.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-06-26 18:33:51 +0200
committerGitHub <noreply@github.com>2018-06-26 18:33:51 +0200
commit2a3a128e362929bac9c2dbf7430cbe8732840f95 (patch)
treeac794774866b8b71c5a6e271a21b42b060b57900 /nimpretty/nimpretty.nim
parente129466910b6efa730f8d5d9232efbce6dae46f0 (diff)
parentd08b9eb6731a70504be6d856723fbc94dc7bd506 (diff)
downloadNim-2a3a128e362929bac9c2dbf7430cbe8732840f95.tar.gz
Merge branch 'devel' into typedesc-reforms
Diffstat (limited to 'nimpretty/nimpretty.nim')
-rw-r--r--nimpretty/nimpretty.nim75
1 files changed, 75 insertions, 0 deletions
diff --git a/nimpretty/nimpretty.nim b/nimpretty/nimpretty.nim
new file mode 100644
index 000000000..aa9756c45
--- /dev/null
+++ b/nimpretty/nimpretty.nim
@@ -0,0 +1,75 @@
+#
+#
+#           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, options]
+
+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)
+  --output:file       set the output file (default: overwrite the .nim file)
+  --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, outfile: string) =
+  var conf = newConfigRef()
+  let fileIdx = fileInfoIdx(conf, infile)
+  conf.outFile = outfile
+  when defined(nimpretty2):
+    discard parseFile(fileIdx, newIdentCache(), conf)
+  else:
+    let tree = parseFile(fileIdx, newIdentCache(), conf)
+    renderModule(tree, infile, outfile, {}, fileIdx, conf)
+
+proc main =
+  var infile, outfile: string
+  var backup = true
+  for kind, key, val in getopt():
+    case kind
+    of cmdArgument:
+      infile = key.addFileExt(".nim")
+    of cmdLongoption, cmdShortOption:
+      case normalize(key)
+      of "help", "h": writeHelp()
+      of "version", "v": writeVersion()
+      of "backup": backup = parseBool(val)
+      of "output", "o": outfile = 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"))
+  if outfile.len == 0: outfile = infile
+  prettyPrint(infile, outfile)
+
+main()