summary refs log tree commit diff stats
path: root/tools
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-11-02 10:46:30 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-11-02 10:46:30 +0100
commit1eaeccc15d15d15d2f62ea1648f7dd64722dbd37 (patch)
treeb922cdabc780fa3a8837a6804d2df31793d9e2ca /tools
parente9243a16167b24899d4fcf051f3252b3a5804811 (diff)
parentbd19b5f4d36bb40b4af93d7e15fdfa582e9fe3b7 (diff)
downloadNim-1eaeccc15d15d15d2f62ea1648f7dd64722dbd37.tar.gz
Merge branch 'devel' into araq
Diffstat (limited to 'tools')
-rw-r--r--tools/nim.zsh-completion2
-rw-r--r--tools/nimpretty.nim68
-rw-r--r--tools/nimresolve.nim158
3 files changed, 70 insertions, 158 deletions
diff --git a/tools/nim.zsh-completion b/tools/nim.zsh-completion
index 3d6e0d5f6..135649c90 100644
--- a/tools/nim.zsh-completion
+++ b/tools/nim.zsh-completion
@@ -60,6 +60,8 @@ _nim() {
     '*--nanChecks=off[turn NaN checks off]' \
     '*--infChecks=on[turn Inf checks on]' \
     '*--infChecks=off[turn Inf checks off]' \
+    '*--nilChecks=on[turn nil checks on]' \
+    '*--nilChecks=off[turn nil checks off]' \
     '*--deadCodeElim=on[whole program dead code elimination on]' \
     '*--deadCodeElim=off[whole program dead code elimination off]' \
     '*--opt=none[do not optimize]' \
diff --git a/tools/nimpretty.nim b/tools/nimpretty.nim
new file mode 100644
index 000000000..36d1382cf
--- /dev/null
+++ b/tools/nimpretty.nim
@@ -0,0 +1,68 @@
+#
+#
+#           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())
+  let outfile = changeFileExt(infile, ".pretty.nim")
+  renderModule(tree, infile, outfile, {})
+
+proc main =
+  var infile: 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)
+      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()
diff --git a/tools/nimresolve.nim b/tools/nimresolve.nim
deleted file mode 100644
index 9af24df5a..000000000
--- a/tools/nimresolve.nim
+++ /dev/null
@@ -1,158 +0,0 @@
-#
-#
-#           The Nim Compiler
-#        (c) Copyright 2017 Andreas Rumpf
-#
-#    See the file "copying.txt", included in this
-#    distribution, for details about the copyright.
-#
-
-## Standard tool that resolves import paths.
-
-import
-  os, strutils, parseopt
-
-import "../compiler/nimblecmd"
-
-# You can change these constants to build you own adapted resolver.
-const
-  considerParentDirs = not defined(noParentProjects)
-  considerNimbleDirs = not defined(noNimbleDirs)
-
-const
-  Version = "1.0"
-  Usage = "nimresolve - Nim Resolve Package Path Version " & Version & """
-
-  (c) 2017 Andreas Rumpf
-Usage:
-  nimresolve [options] package
-Options:
-  --source:FILE       the file that requests to resolve 'package'
-  --stdlib:PATH       the path to use for the standard library
-  --project:FILE      the main '.nim' file that was passed to the Nim compiler
-  --subdir:EXPR       the subdir part in: 'import $pkg / subdir'
-  --noNimblePath      do not search the Nimble path to resolve the package
-"""
-
-proc writeHelp() =
-  stdout.write(Usage)
-  stdout.flushFile()
-  quit(0)
-
-proc writeVersion() =
-  stdout.write(Version & "\n")
-  stdout.flushFile()
-  quit(0)
-
-type
-  Task = object
-    source, stdlib, subdir, project, pkg: string
-    noNimblePath: bool
-
-proc findInNimbleDir(t: Task; dir: string): bool =
-  var best = ""
-  var bestv = ""
-  for k, p in os.walkDir(dir, relative=true):
-    if k == pcDir and p.len > t.pkg.len+1 and
-        p[t.pkg.len] == '-' and p.startsWith(t.pkg):
-      let (_, a) = getPathVersion(p)
-      if bestv.len == 0 or bestv < a:
-        bestv = a
-        best = dir / p
-
-  if best.len > 0:
-    var f: File
-    if open(f, best / changeFileExt(t.pkg, ".nimble-link")):
-      # the second line contains what we're interested in, see:
-      # https://github.com/nim-lang/nimble#nimble-link
-      var override = ""
-      discard readLine(f, override)
-      discard readLine(f, override)
-      close(f)
-      if not override.isAbsolute():
-        best = best / override
-      else:
-        best = override
-  let f = if t.subdir.len == 0: t.pkg else: t.subdir
-  let res = addFileExt(best / f, "nim")
-  if best.len > 0 and fileExists(res):
-    echo res
-    result = true
-
-const stdlibDirs = [
-  "pure", "core", "arch",
-  "pure/collections",
-  "pure/concurrency", "impure",
-  "wrappers", "wrappers/linenoise",
-  "windows", "posix", "js"]
-
-proc resolve(t: Task) =
-  template attempt(a) =
-    let x = addFileExt(a, "nim")
-    if fileExists(x):
-      echo x
-      return
-
-  case t.pkg
-  of "stdlib":
-    if t.subdir.len == 0:
-      echo t.stdlib
-      return
-    else:
-      for candidate in stdlibDirs:
-        attempt(t.stdlib / candidate / t.subdir)
-  of "root":
-    let root = t.project.splitFile.dir
-    if t.subdir.len == 0:
-      echo root
-      return
-    else:
-      attempt(root / t.subdir)
-  else:
-    when considerParentDirs:
-      var p = parentDir(t.source.splitFile.dir)
-      # support 'import $karax':
-      let f = if t.subdir.len == 0: t.pkg else: t.subdir
-
-      while p.len > 0:
-        let dir = p / t.pkg
-        if dirExists(dir):
-          attempt(dir / f)
-          # 2nd attempt: try to use 'karax/karax'
-          attempt(dir / t.pkg / f)
-          # 3rd attempt: try to use 'karax/src/karax'
-          attempt(dir / "src" / f)
-          attempt(dir / "src" / t.pkg / f)
-        p = parentDir(p)
-
-    when considerNimbleDirs:
-      if not t.noNimblePath:
-        if findInNimbleDir(t, getHomeDir() / ".nimble" / "pkgs"): return
-        when not defined(windows):
-          if findInNimbleDir(t, "/opt/nimble/pkgs"): return
-
-  quit "cannot resolve: " & (t.pkg / t.subdir)
-
-proc main =
-  var t: Task
-  t.subdir = ""
-  for kind, key, val in getopt():
-    case kind
-    of cmdArgument:
-      t.pkg = key
-    of cmdLongoption, cmdShortOption:
-      case normalize(key)
-      of "source": t.source = val
-      of "stdlib": t.stdlib = val
-      of "project": t.project = val
-      of "subdir": t.subdir = val
-      of "nonimblepath": t.noNimblePath = true
-      of "help", "h": writeHelp()
-      of "version", "v": writeVersion()
-      else: writeHelp()
-    of cmdEnd: assert(false) # cannot happen
-  if t.pkg.len == 0:
-    quit "[Error] no package to resolve."
-  resolve(t)
-
-main()