nimpretty.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Standard tool for pretty printing.
  10. when not defined(nimpretty):
  11. {.error: "This needs to be compiled with --define:nimPretty".}
  12. import ../compiler / [idents, msgs, ast, syntaxes, renderer, options,
  13. pathutils, layouter]
  14. import parseopt, strutils, os
  15. const
  16. Version = "0.1"
  17. Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
  18. (c) 2017 Andreas Rumpf
  19. Usage:
  20. nimpretty [options] file.nim
  21. Options:
  22. --output:file set the output file (default: overwrite the input file)
  23. --indent:N[=2] set the number of spaces that is used for indentation
  24. --version show the version
  25. --help show this help
  26. """
  27. proc writeHelp() =
  28. stdout.write(Usage)
  29. stdout.flushFile()
  30. quit(0)
  31. proc writeVersion() =
  32. stdout.write(Version & "\n")
  33. stdout.flushFile()
  34. quit(0)
  35. type
  36. PrettyOptions = object
  37. indWidth: int
  38. proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
  39. var conf = newConfigRef()
  40. let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
  41. conf.outFile = AbsoluteFile outfile
  42. when defined(nimpretty2):
  43. var p: TParsers
  44. p.parser.em.indWidth = opt.indWidth
  45. if setupParsers(p, fileIdx, newIdentCache(), conf):
  46. discard parseAll(p)
  47. closeParsers(p)
  48. else:
  49. let tree = parseFile(fileIdx, newIdentCache(), conf)
  50. renderModule(tree, infile, outfile, {}, fileIdx, conf)
  51. proc main =
  52. var infile, outfile: string
  53. var backup = false
  54. # when `on`, create a backup file of input in case
  55. # `prettyPrint` could over-write it (note that the backup may happen even
  56. # if input is not actually over-written, when nimpretty is a noop).
  57. # --backup was un-documented (rely on git instead).
  58. var opt: PrettyOptions
  59. for kind, key, val in getopt():
  60. case kind
  61. of cmdArgument:
  62. infile = key.addFileExt(".nim")
  63. of cmdLongoption, cmdShortOption:
  64. case normalize(key)
  65. of "help", "h": writeHelp()
  66. of "version", "v": writeVersion()
  67. of "backup": backup = parseBool(val)
  68. of "output", "o": outfile = val
  69. of "indent": opt.indWidth = parseInt(val)
  70. else: writeHelp()
  71. of cmdEnd: assert(false) # cannot happen
  72. if infile.len == 0:
  73. quit "[Error] no input file."
  74. if outfile.len == 0:
  75. outfile = infile
  76. if not existsFile(outfile) or not sameFile(infile, outfile):
  77. backup = false # no backup needed since won't be over-written
  78. if backup:
  79. let infileBackup = infile & ".backup" # works with .nim or .nims
  80. echo "writing backup " & infile & " > " & infileBackup
  81. os.copyFile(source = infile, dest = infileBackup)
  82. prettyPrint(infile, outfile, opt)
  83. main()