nimpretty.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, llstream, ast, msgs, syntaxes, options, pathutils, layouter]
  13. import parseopt, strutils, os, sequtils
  14. import std/tempfiles
  15. const
  16. Version = "0.2"
  17. Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
  18. (c) 2017 Andreas Rumpf
  19. Usage:
  20. nimpretty [options] nimfiles...
  21. Options:
  22. --out:file set the output file (default: overwrite the input file)
  23. --outDir:dir set the output dir (default: overwrite the input files)
  24. --stdin read input from stdin and write output to stdout
  25. --indent:N[=0] set the number of spaces that is used for indentation
  26. --indent:0 means autodetection (default behaviour)
  27. --maxLineLen:N set the desired maximum line length (default: 80)
  28. --version show the version
  29. --help show this help
  30. """
  31. proc writeHelp() =
  32. stdout.write(Usage)
  33. stdout.flushFile()
  34. quit(0)
  35. proc writeVersion() =
  36. stdout.write(Version & "\n")
  37. stdout.flushFile()
  38. quit(0)
  39. type
  40. PrettyOptions* = object
  41. indWidth*: Natural
  42. maxLineLen*: Positive
  43. proc goodEnough(a, b: PNode): bool =
  44. if a.kind == b.kind and a.safeLen == b.safeLen:
  45. case a.kind
  46. of nkNone, nkEmpty, nkNilLit: result = true
  47. of nkIdent: result = a.ident.id == b.ident.id
  48. of nkSym: result = a.sym == b.sym
  49. of nkType: result = true
  50. of nkCharLit, nkIntLit..nkInt64Lit, nkUIntLit..nkUInt64Lit:
  51. result = a.intVal == b.intVal
  52. of nkFloatLit..nkFloat128Lit:
  53. result = a.floatVal == b.floatVal
  54. of nkStrLit, nkRStrLit, nkTripleStrLit:
  55. result = a.strVal == b.strVal
  56. else:
  57. for i in 0 ..< a.len:
  58. if not goodEnough(a[i], b[i]): return false
  59. return true
  60. elif a.kind == nkStmtList and a.len == 1:
  61. result = goodEnough(a[0], b)
  62. elif b.kind == nkStmtList and b.len == 1:
  63. result = goodEnough(a, b[0])
  64. else:
  65. result = false
  66. proc finalCheck(content: string; origAst: PNode): bool {.nimcall.} =
  67. var conf = newConfigRef()
  68. let oldErrors = conf.errorCounter
  69. var parser: Parser
  70. parser.em.indWidth = 2
  71. let fileIdx = fileInfoIdx(conf, AbsoluteFile "nimpretty_bug.nim")
  72. openParser(parser, fileIdx, llStreamOpen(content), newIdentCache(), conf)
  73. let newAst = parseAll(parser)
  74. closeParser(parser)
  75. result = conf.errorCounter == oldErrors # and goodEnough(newAst, origAst)
  76. proc prettyPrint*(infile, outfile: string; opt: PrettyOptions) =
  77. var conf = newConfigRef()
  78. let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
  79. let f = splitFile(outfile.expandTilde)
  80. conf.outFile = RelativeFile f.name & f.ext
  81. conf.outDir = toAbsoluteDir f.dir
  82. var parser: Parser
  83. parser.em.indWidth = opt.indWidth
  84. if setupParser(parser, fileIdx, newIdentCache(), conf):
  85. parser.em.maxLineLen = opt.maxLineLen
  86. let fullAst = parseAll(parser)
  87. closeParser(parser)
  88. when defined(nimpretty):
  89. closeEmitter(parser.em, fullAst, finalCheck)
  90. proc handleStdinInput(opt: PrettyOptions) =
  91. var content = readAll(stdin)
  92. var (cfile, path) = createTempFile("nimpretty_", ".nim")
  93. writeFile(path, content)
  94. prettyPrint(path, path, opt)
  95. echo(readAll(cfile))
  96. close(cfile)
  97. removeFile(path)
  98. proc main =
  99. var outfile, outdir: string
  100. var infiles = newSeq[string]()
  101. var outfiles = newSeq[string]()
  102. var isStdin = false
  103. var backup = false
  104. # when `on`, create a backup file of input in case
  105. # `prettyPrint` could overwrite it (note that the backup may happen even
  106. # if input is not actually overwritten, when nimpretty is a noop).
  107. # --backup was un-documented (rely on git instead).
  108. var opt = PrettyOptions(indWidth: 0, maxLineLen: 80)
  109. for kind, key, val in getopt():
  110. case kind
  111. of cmdArgument:
  112. if dirExists(key):
  113. for file in walkDirRec(key, skipSpecial = true):
  114. if file.endsWith(".nim") or file.endsWith(".nimble"):
  115. infiles.add(file)
  116. else:
  117. infiles.add(key.addFileExt(".nim"))
  118. of cmdLongOption, cmdShortOption:
  119. case normalize(key)
  120. of "help", "h": writeHelp()
  121. of "version", "v": writeVersion()
  122. of "backup": backup = parseBool(val)
  123. of "output", "o", "out": outfile = val
  124. of "outDir", "outdir": outdir = val
  125. of "indent": opt.indWidth = parseInt(val)
  126. of "maxlinelen": opt.maxLineLen = parseInt(val)
  127. # "" is equal to '-' as input
  128. of "stdin", "": isStdin = true
  129. else: writeHelp()
  130. of cmdEnd: assert(false) # cannot happen
  131. if isStdin:
  132. handleStdinInput(opt)
  133. return
  134. if infiles.len == 0:
  135. quit "[Error] no input file."
  136. if outfile.len != 0 and outdir.len != 0:
  137. quit "[Error] out and outDir cannot both be specified"
  138. if outfile.len == 0 and outdir.len == 0:
  139. outfiles = infiles
  140. elif outfile.len != 0 and infiles.len > 1:
  141. # Take the last file to maintain backwards compatibility
  142. let infile = infiles[^1]
  143. infiles = @[infile]
  144. outfiles = @[outfile]
  145. elif outfile.len != 0:
  146. outfiles = @[outfile]
  147. elif outdir.len != 0:
  148. outfiles = infiles.mapIt($(joinPath(outdir, it)))
  149. for (infile, outfile) in zip(infiles, outfiles):
  150. let (dir, _, _) = splitFile(outfile)
  151. createDir(dir)
  152. if not fileExists(outfile) or not sameFile(infile, outfile):
  153. backup = false # no backup needed since won't be over-written
  154. if backup:
  155. let infileBackup = infile & ".backup" # works with .nim or .nims
  156. echo "writing backup " & infile & " > " & infileBackup
  157. os.copyFile(source = infile, dest = infileBackup)
  158. prettyPrint(infile, outfile, opt)
  159. when isMainModule:
  160. main()