From b8072bd22ec6e3ccf1a6be1b19fae9de3a0fdcc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 10 Dec 2013 22:56:29 +0100 Subject: [PATCH 01/27] add quoteShell to osproc.nim --- lib/pure/osproc.nim | 65 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 754e34b850..7772585a97 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -41,6 +41,58 @@ type poStdErrToStdOut, ## merge stdout and stderr to the stdout stream poParentStreams ## use the parent's streams +proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} = + ## Quote s, so it can be safely passed to Windows API. + ## Based on Python's subprocess.list2cmdline + ## See http://msdn.microsoft.com/en-us/library/17w5ykft.aspx + let needQuote = {' ', '\t'} in s or s.len == 0 + + result = "" + var backslashBuff = "" + if needQuote: + result.add("\"") + + for c in s: + if c == '\\': + backslashBuff.add(c) + elif c == '\"': + result.add(backslashBuff) + result.add(backslashBuff) + backslashBuff.setLen(0) + result.add("\\\"") + else: + if backslashBuff.len != 0: + result.add(backslashBuff) + backslashBuff.setLen(0) + result.add(c) + + if needQuote: + result.add("\"") + +proc quoteShellPosix*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} = + ## Quote s, so it can be safely passed to POSIX shell. + ## Based on Python's pipes.quote + const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@', + '0'..'9', 'A'..'Z', 'a'..'z'} + if s.len == 0: + return "''" + + let safe = s.allCharsInSet(safeUnixChars) + + if safe: + return s + else: + return "'" & s.replace("'", "'\"'\"'") & "'" + +proc quoteShell*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} = + ## Quote s, so it can be safely passed to shell. + when defined(Windows): + return quoteShellWindows(s) + elif defined(posix): + return quoteShellPosix(s) + else: + {.error:"quoteShell is not supported on your system".} + proc execProcess*(command: string, options: set[TProcessOption] = {poStdErrToStdOut, poUseShell}): TaintedString {. @@ -792,5 +844,14 @@ proc execCmdEx*(command: string, options: set[TProcessOption] = { close(p) when isMainModule: - var x = execProcess("gcc -v") - echo "ECHO ", x + assert quoteShellWindows("aaa") == "aaa" + assert quoteShellWindows("aaa\"") == "aaa\\\"" + assert quoteShellWindows("") == "\"\"" + + assert quoteShellPosix("aaa") == "aaa" + assert quoteShellPosix("aaa a") == "'aaa a'" + assert quoteShellPosix("") == "''" + assert quoteShellPosix("a'a") == "'a'\"'\"'a'" + + when defined(posix): + assert quoteShell("") == "''" From c36319727558f92a47f7aa5fed1d696d63b48d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 10 Dec 2013 23:00:01 +0100 Subject: [PATCH 02/27] Deprecate quoteIfContainsWhite in favor of osproc.quoteShell. --- lib/pure/strutils.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index a4aa815787..fe71cb77b3 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -709,9 +709,11 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 -proc quoteIfContainsWhite*(s: string): string = +proc quoteIfContainsWhite*(s: string): string {.deprecated.} = ## returns ``'"' & s & '"'`` if `s` contains a space and does not ## start with a quote, else returns `s` + ## DEPRECATED as it was confused for shell quoting function. + ## For this application use osproc.quoteShell. if find(s, {' ', '\t'}) >= 0 and s[0] != '"': result = '"' & s & '"' else: From 39cabcdd2702098825eba32fcbc6737b9aa45e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Wed, 11 Dec 2013 22:27:39 +0100 Subject: [PATCH 03/27] Use quoteShell in stdlib, where appropriate. --- compiler/extccomp.nim | 32 ++++++++++++++++---------------- compiler/nimrod.nim | 12 ++++++------ lib/pure/browsers.nim | 4 ++-- lib/pure/osproc.nim | 8 ++++---- tools/niminst/niminst.nim | 5 ++--- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim index 13eb972f6c..fe1bea3fff 100644 --- a/compiler/extccomp.nim +++ b/compiler/extccomp.nim @@ -463,9 +463,9 @@ proc getCompileOptions: string = proc getLinkOptions: string = result = linkOptions for linkedLib in items(cLinkedLibs): - result.add(cc[ccompiler].linkLibCmd % linkedLib.quoteIfContainsWhite) + result.add(cc[ccompiler].linkLibCmd % linkedLib.quoteShell) for libDir in items(cLibs): - result.add([cc[ccompiler].linkDirCmd, libDir.quoteIfContainsWhite]) + result.add([cc[ccompiler].linkDirCmd, libDir.quoteShell]) proc needsExeExt(): bool {.inline.} = result = (optGenScript in gGlobalOptions and targetOS == osWindows) or @@ -485,10 +485,10 @@ proc getCompileCFileCmd*(cfilename: string, isExternal = false): string = var includeCmd, compilePattern: string if not noAbsolutePaths(): # compute include paths: - includeCmd = cc[c].includeCmd & quoteIfContainsWhite(libpath) + includeCmd = cc[c].includeCmd & quoteShell(libpath) for includeDir in items(cIncludes): - includeCmd.add([cc[c].includeCmd, includeDir.quoteIfContainsWhite]) + includeCmd.add([cc[c].includeCmd, includeDir.quoteShell]) compilePattern = JoinPath(ccompilerpath, exe) else: @@ -501,17 +501,17 @@ proc getCompileCFileCmd*(cfilename: string, isExternal = false): string = toObjFile(cfile) else: completeCFilePath(toObjFile(cfile)) - cfile = quoteIfContainsWhite(AddFileExt(cfile, cExt)) - objfile = quoteIfContainsWhite(objfile) - result = quoteIfContainsWhite(compilePattern % [ + cfile = quoteShell(AddFileExt(cfile, cExt)) + objfile = quoteShell(objfile) + result = quoteShell(compilePattern % [ "file", cfile, "objfile", objfile, "options", options, "include", includeCmd, "nimrod", getPrefixDir(), "lib", libpath]) add(result, ' ') addf(result, cc[c].compileTmpl, [ "file", cfile, "objfile", objfile, "options", options, "include", includeCmd, - "nimrod", quoteIfContainsWhite(getPrefixDir()), - "lib", quoteIfContainsWhite(libpath)]) + "nimrod", quoteShell(getPrefixDir()), + "lib", quoteShell(libpath)]) proc footprint(filename: string): TCrc32 = result = crcFromFile(filename) >< @@ -590,7 +590,7 @@ proc CallCCompiler*(projectfile: string) = while it != nil: let objFile = if noAbsolutePaths(): it.data.extractFilename else: it.data add(objfiles, ' ') - add(objfiles, quoteIfContainsWhite( + add(objfiles, quoteShell( addFileExt(objFile, cc[ccompiler].objExt))) it = PStrEntry(it.next) @@ -602,8 +602,8 @@ proc CallCCompiler*(projectfile: string) = var linkerExe = getConfigVar(c, ".linkerexe") if len(linkerExe) == 0: linkerExe = cc[c].linkerExe if needsExeExt(): linkerExe = addFileExt(linkerExe, "exe") - if noAbsolutePaths(): linkCmd = quoteIfContainsWhite(linkerExe) - else: linkCmd = quoteIfContainsWhite(JoinPath(ccompilerpath, linkerExe)) + if noAbsolutePaths(): linkCmd = quoteShell(linkerExe) + else: linkCmd = quoteShell(JoinPath(ccompilerpath, linkerExe)) if optGenGuiApp in gGlobalOptions: buildGui = cc[c].buildGui else: buildGui = "" var exefile: string @@ -617,17 +617,17 @@ proc CallCCompiler*(projectfile: string) = exefile = options.outFile if not noAbsolutePaths(): exefile = joinPath(splitFile(projectFile).dir, exefile) - exefile = quoteIfContainsWhite(exefile) + exefile = quoteShell(exefile) let linkOptions = getLinkOptions() - linkCmd = quoteIfContainsWhite(linkCmd % ["builddll", builddll, + linkCmd = quoteShell(linkCmd % ["builddll", builddll, "buildgui", buildgui, "options", linkOptions, "objfiles", objfiles, "exefile", exefile, "nimrod", getPrefixDir(), "lib", libpath]) linkCmd.add ' ' addf(linkCmd, cc[c].linkTmpl, ["builddll", builddll, "buildgui", buildgui, "options", linkOptions, "objfiles", objfiles, "exefile", exefile, - "nimrod", quoteIfContainsWhite(getPrefixDir()), - "lib", quoteIfContainsWhite(libpath)]) + "nimrod", quoteShell(getPrefixDir()), + "lib", quoteShell(libpath)]) if optCompileOnly notin gGlobalOptions: execExternalProgram(linkCmd) else: linkCmd = "" diff --git a/compiler/nimrod.nim b/compiler/nimrod.nim index 2f10e32e36..8e3c0e61ee 100644 --- a/compiler/nimrod.nim +++ b/compiler/nimrod.nim @@ -13,9 +13,9 @@ when defined(gcc) and defined(windows): else: {.link: "icons/nimrod_icon.o".} -import - commands, lexer, condsyms, options, msgs, nversion, nimconf, ropes, - extccomp, strutils, os, platform, main, parseopt, service +import + commands, lexer, condsyms, options, msgs, nversion, nimconf, ropes, + extccomp, strutils, os, osproc, platform, main, parseopt, service when hasTinyCBackend: import tccgen @@ -23,7 +23,7 @@ when hasTinyCBackend: when defined(profiler) or defined(memProfiler): {.hint: "Profiling support is turned on!".} import nimprof - + proc prependCurDir(f: string): string = when defined(unix): if os.isAbsolute(f): result = f @@ -61,11 +61,11 @@ proc HandleCmdLine() = tccgen.run() if optRun in gGlobalOptions: if gCmd == cmdCompileToJS: - var ex = quoteIfContainsWhite( + var ex = quoteShell( completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir)) execExternalProgram("node " & ex & ' ' & service.arguments) else: - var ex = quoteIfContainsWhite( + var ex = quoteShell( changeFileExt(gProjectFull, exeExt).prependCurDir) execExternalProgram(ex & ' ' & service.arguments) diff --git a/lib/pure/browsers.nim b/lib/pure/browsers.nim index 6f5bf7ddba..b44f406c5e 100644 --- a/lib/pure/browsers.nim +++ b/lib/pure/browsers.nim @@ -33,10 +33,10 @@ proc openDefaultBrowser*(url: string) = else: discard ShellExecuteA(0'i32, "open", url, nil, nil, SW_SHOWNORMAL) elif defined(macosx): - discard execShellCmd("open " & quoteIfContainsWhite(url)) + discard execShellCmd("open " & quoteShell(url)) else: const attempts = ["gnome-open ", "kde-open ", "xdg-open "] - var u = quoteIfContainsWhite(url) + var u = quoteShell(url) for a in items(attempts): if execShellCmd(a & u) == 0: return for b in getEnv("BROWSER").string.split(PathSep): diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 7772585a97..61b940ce83 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -359,10 +359,10 @@ when defined(Windows) and not defined(useNimRtl): result.writeDataImpl = hsWriteData proc buildCommandLine(a: string, args: openarray[string]): cstring = - var res = quoteIfContainsWhite(a) + var res = quoteShell(a) for i in 0..high(args): res.add(' ') - res.add(quoteIfContainsWhite(args[i])) + res.add(quoteShell(args[i])) result = cast[cstring](alloc0(res.len+1)) copyMem(result, cstring(res), res.len) @@ -562,10 +562,10 @@ elif not defined(useNimRtl): writeIdx = 1 proc addCmdArgs(command: string, args: openarray[string]): string = - result = quoteIfContainsWhite(command) + result = quoteShell(command) for i in 0 .. high(args): add(result, " ") - add(result, quoteIfContainsWhite(args[i])) + add(result, quoteShell(args[i])) proc toCStringArray(b, a: openarray[string]): cstringArray = result = cast[cstringArray](alloc0((a.len + b.len + 1) * sizeof(cstring))) diff --git a/tools/niminst/niminst.nim b/tools/niminst/niminst.nim index 25ec0d283e..0c9717e12e 100644 --- a/tools/niminst/niminst.nim +++ b/tools/niminst/niminst.nim @@ -14,7 +14,7 @@ when haveZipLib: import zipfiles import - os, strutils, parseopt, parsecfg, strtabs, streams, debcreation + os, osproc, strutils, parseopt, parsecfg, strtabs, streams, debcreation const maxOS = 20 # max number of OSes @@ -486,7 +486,7 @@ proc setupDist(c: var TConfigData) = if c.innoSetup.path.len == 0: c.innoSetup.path = "iscc.exe" var outcmd = if c.outdir.len == 0: "build" else: c.outdir - var cmd = "$# $# /O$# $#" % [quoteIfContainsWhite(c.innoSetup.path), + var cmd = "$# $# /O$# $#" % [quoteShell(c.innoSetup.path), c.innoSetup.flags, outcmd, n] echo(cmd) if execShellCmd(cmd) == 0: @@ -587,4 +587,3 @@ if actionZip in c.actions: quit("libzip is not installed") if actionDeb in c.actions: debDist(c) - From c6c836692585b6a94f7c453bf1dff82be0107b2f Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 12 Dec 2013 00:57:31 +0100 Subject: [PATCH 04/27] Mentions static alternatives to quit(). --- lib/system.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/system.nim b/lib/system.nim index dc5a406d14..d7a9b08ab1 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -923,6 +923,10 @@ proc quit*(errorcode: int = QuitSuccess) {. ## The proc ``quit(QuitSuccess)`` is called implicitly when your nimrod ## program finishes without incident. A raised unhandled exception is ## equivalent to calling ``quit(QuitFailure)``. + ## + ## Note that this is a *runtime* call and using ``quit`` inside a macro won't + ## have any compile time effect. If you need to stop the compiler inside a + ## macro, use the ``error`` or ``fatal`` pragmas. template sysAssert(cond: bool, msg: string) = when defined(useSysAssert): From b4d6a6aafe400556d3557cb7ccf0a12a40e1b4e7 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Thu, 12 Dec 2013 01:00:23 +0100 Subject: [PATCH 05/27] Adds example to fatal pragma. --- doc/manual.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/manual.txt b/doc/manual.txt index dabff3d69d..ee283da0c0 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -4708,7 +4708,11 @@ fatal pragma ------------ The `fatal`:idx: pragma is used to make the compiler output an error message with the given content. In contrast to the ``error`` pragma, compilation -is guaranteed to be aborted by this pragma. +is guaranteed to be aborted by this pragma. Example: + +.. code-block:: nimrod + when not defined(objc): + {.fatal: "Compile this program with the objc command!".} warning pragma -------------- From aaf49947e9e259b4cd2bda6b218f9bd6abc4ca71 Mon Sep 17 00:00:00 2001 From: Erik O'Leary Date: Thu, 12 Dec 2013 10:59:30 -0600 Subject: [PATCH 06/27] Added jsondoc compiler switch Outputs top-level exported information in JSON --- compiler/docgen.nim | 284 +++++++++++++++++++++++++++---------------- compiler/main.nim | 58 +++++---- compiler/options.nim | 1 + 3 files changed, 211 insertions(+), 132 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 9929b4bd94..d44018a2ba 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -11,10 +11,10 @@ # semantic checking is done for the code. Cross-references are generated # by knowing how the anchors are going to be named. -import - ast, strutils, strtabs, options, msgs, os, ropes, idents, - wordrecg, syntaxes, renderer, lexer, rstast, rst, rstgen, times, highlite, - importer, sempass2 +import + ast, strutils, strtabs, options, msgs, os, ropes, idents, + wordrecg, syntaxes, renderer, lexer, rstast, rst, rstgen, times, highlite, + importer, sempass2, json type TSections = array[TSymKind, PRope] @@ -25,7 +25,7 @@ type indexValFilename: string PDoc* = ref TDocumentor - + proc compilerMsgHandler(filename: string, line, col: int, msgKind: rst.TMsgKind, arg: string) {.procvar.} = # translate msg kind: @@ -41,69 +41,69 @@ proc compilerMsgHandler(filename: string, line, col: int, of mwUnknownSubstitution: k = warnUnknownSubstitutionX of mwUnsupportedLanguage: k = warnLanguageXNotSupported GlobalError(newLineInfo(filename, line, col), k, arg) - + proc parseRst(text, filename: string, line, column: int, hasToc: var bool, rstOptions: TRstParseOptions): PRstNode = result = rstParse(text, filename, line, column, hasToc, rstOptions, options.FindFile, compilerMsgHandler) -proc newDocumentor*(filename: string, config: PStringTable): PDoc = +proc newDocumentor*(filename: string, config: PStringTable): PDoc = new(result) initRstGenerator(result[], (if gCmd != cmdRst2Tex: outHtml else: outLatex), options.gConfigVars, filename, {roSupportRawDirective}, options.FindFile, compilerMsgHandler) result.id = 100 -proc dispA(dest: var PRope, xml, tex: string, args: openarray[PRope]) = +proc dispA(dest: var PRope, xml, tex: string, args: openarray[PRope]) = if gCmd != cmdRst2Tex: appf(dest, xml, args) else: appf(dest, tex, args) - -proc getVarIdx(varnames: openarray[string], id: string): int = - for i in countup(0, high(varnames)): - if cmpIgnoreStyle(varnames[i], id) == 0: + +proc getVarIdx(varnames: openarray[string], id: string): int = + for i in countup(0, high(varnames)): + if cmpIgnoreStyle(varnames[i], id) == 0: return i result = -1 -proc ropeFormatNamedVars(frmt: TFormatStr, varnames: openarray[string], - varvalues: openarray[PRope]): PRope = +proc ropeFormatNamedVars(frmt: TFormatStr, varnames: openarray[string], + varvalues: openarray[PRope]): PRope = var i = 0 var L = len(frmt) result = nil var num = 0 - while i < L: - if frmt[i] == '$': + while i < L: + if frmt[i] == '$': inc(i) # skip '$' case frmt[i] - of '#': + of '#': app(result, varvalues[num]) inc(num) inc(i) - of '$': + of '$': app(result, "$") inc(i) - of '0'..'9': + of '0'..'9': var j = 0 - while true: + while true: j = (j * 10) + Ord(frmt[i]) - ord('0') inc(i) - if (i > L + 0 - 1) or not (frmt[i] in {'0'..'9'}): break + if (i > L + 0 - 1) or not (frmt[i] in {'0'..'9'}): break if j > high(varvalues) + 1: internalError("ropeFormatNamedVars") num = j app(result, varvalues[j - 1]) - of 'A'..'Z', 'a'..'z', '\x80'..'\xFF': + of 'A'..'Z', 'a'..'z', '\x80'..'\xFF': var id = "" - while true: + while true: add(id, frmt[i]) inc(i) - if not (frmt[i] in {'A'..'Z', '_', 'a'..'z', '\x80'..'\xFF'}): break + if not (frmt[i] in {'A'..'Z', '_', 'a'..'z', '\x80'..'\xFF'}): break var idx = getVarIdx(varnames, id) if idx >= 0: app(result, varvalues[idx]) else: rawMessage(errUnkownSubstitionVar, id) - of '{': + of '{': var id = "" inc(i) - while frmt[i] != '}': + while frmt[i] != '}': if frmt[i] == '\0': rawMessage(errTokenExpected, "}") add(id, frmt[i]) inc(i) @@ -124,17 +124,17 @@ proc genComment(d: PDoc, n: PNode): string = var dummyHasToc: bool if n.comment != nil and startsWith(n.comment, "##"): renderRstToOut(d[], parseRst(n.comment, toFilename(n.info), - toLineNumber(n.info), toColumn(n.info), + toLineNumber(n.info), toColumn(n.info), dummyHasToc, d.options + {roSkipPounds}), result) -proc genRecComment(d: PDoc, n: PNode): PRope = +proc genRecComment(d: PDoc, n: PNode): PRope = if n == nil: return nil result = genComment(d, n).toRope - if result == nil: + if result == nil: if n.kind notin {nkEmpty..nkNilLit}: for i in countup(0, len(n)-1): result = genRecComment(d, n.sons[i]) - if result != nil: return + if result != nil: return else: n.comment = nil @@ -158,10 +158,10 @@ proc extractDocComment*(s: PSym, d: PDoc = nil): string = else: result = n.comment.substr(2).replace("\n##", "\n").strip -proc isVisible(n: PNode): bool = +proc isVisible(n: PNode): bool = result = false - if n.kind == nkPostfix: - if n.len == 2 and n.sons[0].kind == nkIdent: + if n.kind == nkPostfix: + if n.len == 2 and n.sons[0].kind == nkIdent: var v = n.sons[0].ident result = v.id == ord(wStar) or v.id == ord(wMinus) elif n.kind == nkSym: @@ -171,36 +171,36 @@ proc isVisible(n: PNode): bool = result = {sfExported, sfFromGeneric, sfForward}*n.sym.flags == {sfExported} elif n.kind == nkPragmaExpr: result = isVisible(n.sons[0]) - -proc getName(d: PDoc, n: PNode, splitAfter = -1): string = + +proc getName(d: PDoc, n: PNode, splitAfter = -1): string = case n.kind of nkPostfix: result = getName(d, n.sons[1], splitAfter) of nkPragmaExpr: result = getName(d, n.sons[0], splitAfter) of nkSym: result = esc(d.target, n.sym.renderDefinitionName, splitAfter) of nkIdent: result = esc(d.target, n.ident.s, splitAfter) - of nkAccQuoted: - result = esc(d.target, "`") + of nkAccQuoted: + result = esc(d.target, "`") for i in 0.. $1", "\\spanComment{$1}", + of tkEof: + break + of tkComment: + dispA(result, "$1", "\\spanComment{$1}", [toRope(esc(d.target, literal))]) - of tokKeywordLow..tokKeywordHigh: - dispA(result, "$1", "\\spanKeyword{$1}", + of tokKeywordLow..tokKeywordHigh: + dispA(result, "$1", "\\spanKeyword{$1}", [toRope(literal)]) - of tkOpr: - dispA(result, "$1", "\\spanOperator{$1}", + of tkOpr: + dispA(result, "$1", "\\spanOperator{$1}", [toRope(esc(d.target, literal))]) - of tkStrLit..tkTripleStrLit: - dispA(result, "$1", + of tkStrLit..tkTripleStrLit: + dispA(result, "$1", "\\spanStringLit{$1}", [toRope(esc(d.target, literal))]) - of tkCharLit: - dispA(result, "$1", "\\spanCharLit{$1}", + of tkCharLit: + dispA(result, "$1", "\\spanCharLit{$1}", [toRope(esc(d.target, literal))]) - of tkIntLit..tkUInt64Lit: - dispA(result, "$1", + of tkIntLit..tkUInt64Lit: + dispA(result, "$1", "\\spanDecNumber{$1}", [toRope(esc(d.target, literal))]) - of tkFloatLit..tkFloat128Lit: - dispA(result, "$1", + of tkFloatLit..tkFloat128Lit: + dispA(result, "$1", "\\spanFloatNumber{$1}", [toRope(esc(d.target, literal))]) - of tkSymbol: - dispA(result, "$1", + of tkSymbol: + dispA(result, "$1", "\\spanIdentifier{$1}", [toRope(esc(d.target, literal))]) - of tkSpaces, tkInvalid: + of tkSpaces, tkInvalid: app(result, literal) - of tkParLe, tkParRi, tkBracketLe, tkBracketRi, tkCurlyLe, tkCurlyRi, - tkBracketDotLe, tkBracketDotRi, tkCurlyDotLe, tkCurlyDotRi, tkParDotLe, - tkParDotRi, tkComma, tkSemiColon, tkColon, tkEquals, tkDot, tkDotDot, - tkAccent, tkColonColon, - tkGStrLit, tkGTripleStrLit, tkInfixOpr, tkPrefixOpr, tkPostfixOpr: - dispA(result, "$1", "\\spanOther{$1}", + of tkParLe, tkParRi, tkBracketLe, tkBracketRi, tkCurlyLe, tkCurlyRi, + tkBracketDotLe, tkBracketDotRi, tkCurlyDotLe, tkCurlyDotRi, tkParDotLe, + tkParDotRi, tkComma, tkSemiColon, tkColon, tkEquals, tkDot, tkDotDot, + tkAccent, tkColonColon, + tkGStrLit, tkGTripleStrLit, tkInfixOpr, tkPrefixOpr, tkPostfixOpr: + dispA(result, "$1", "\\spanOther{$1}", [toRope(esc(d.target, literal))]) inc(d.id) - app(d.section[k], ropeFormatNamedVars(getConfigVar("doc.item"), - ["name", "header", "desc", "itemID"], + app(d.section[k], ropeFormatNamedVars(getConfigVar("doc.item"), + ["name", "header", "desc", "itemID"], [name, result, comm, toRope(d.id)])) - app(d.toc[k], ropeFormatNamedVars(getConfigVar("doc.item.toc"), + app(d.toc[k], ropeFormatNamedVars(getConfigVar("doc.item.toc"), ["name", "header", "desc", "itemID"], [ toRope(getName(d, nameNode, d.splitAfter)), result, comm, toRope(d.id)])) setIndexTerm(d[], $d.id, getName(d, nameNode)) -proc checkForFalse(n: PNode): bool = +proc genJSONItem(d: PDoc, n, nameNode: PNode, k: TSymKind): PJsonNode = + if not isVisible(nameNode): return + var + name = getName(d, nameNode) + comm = genRecComment(d, n).ropeToStr() + r: TSrcGen + + initTokRender(r, n, {renderNoBody, renderNoComments, renderDocComments}) + + result = %{ "name": %name, "type": %($k) } + + if comm != nil and comm != "": + result["description"] = %comm + if r.buf != nil: + result["code"] = %r.buf + +proc checkForFalse(n: PNode): bool = result = n.kind == nkIdent and IdentEq(n.ident, "false") - -proc traceDeps(d: PDoc, n: PNode) = + +proc traceDeps(d: PDoc, n: PNode) = const k = skModule if d.section[k] != nil: app(d.section[k], ", ") - dispA(d.section[k], - "$1", + dispA(d.section[k], + "$1", "$1", [toRope(getModuleName(n))]) -proc generateDoc*(d: PDoc, n: PNode) = +proc generateDoc*(d: PDoc, n: PNode) = case n.kind of nkCommentStmt: app(d.modDesc, genComment(d, n)) - of nkProcDef: + of nkProcDef: when useEffectSystem: documentRaises(n) genItem(d, n, n.sons[namePos], skProc) of nkMethodDef: when useEffectSystem: documentRaises(n) genItem(d, n, n.sons[namePos], skMethod) - of nkIteratorDef: + of nkIteratorDef: when useEffectSystem: documentRaises(n) genItem(d, n, n.sons[namePos], skIterator) of nkMacroDef: genItem(d, n, n.sons[namePos], skMacro) @@ -284,27 +300,69 @@ proc generateDoc*(d: PDoc, n: PNode) = genItem(d, n, n.sons[namePos], skConverter) of nkTypeSection, nkVarSection, nkLetSection, nkConstSection: for i in countup(0, sonsLen(n) - 1): - if n.sons[i].kind != nkCommentStmt: + if n.sons[i].kind != nkCommentStmt: # order is always 'type var let const': - genItem(d, n.sons[i], n.sons[i].sons[0], + genItem(d, n.sons[i], n.sons[i].sons[0], succ(skType, ord(n.kind)-ord(nkTypeSection))) - of nkStmtList: + of nkStmtList: for i in countup(0, sonsLen(n) - 1): generateDoc(d, n.sons[i]) - of nkWhenStmt: + of nkWhenStmt: # generate documentation for the first branch only: if not checkForFalse(n.sons[0].sons[0]): generateDoc(d, lastSon(n.sons[0])) of nkImportStmt: - for i in 0 .. sonsLen(n)-1: traceDeps(d, n.sons[i]) + for i in 0 .. sonsLen(n)-1: traceDeps(d, n.sons[i]) of nkFromStmt, nkImportExceptStmt: traceDeps(d, n.sons[0]) else: nil -proc genSection(d: PDoc, kind: TSymKind) = +proc generateJson(d: PDoc, n: PNode, jArray: PJsonNode = nil): PJsonNode = + case n.kind + of nkCommentStmt: + if n.comment != nil and startsWith(n.comment, "##"): + let stripped = n.comment.substr(2).strip + result = %{ "comment": %stripped } + of nkProcDef: + when useEffectSystem: documentRaises(n) + result = genJSONItem(d, n, n.sons[namePos], skProc) + of nkMethodDef: + when useEffectSystem: documentRaises(n) + result = genJSONItem(d, n, n.sons[namePos], skMethod) + of nkIteratorDef: + when useEffectSystem: documentRaises(n) + result = genJSONItem(d, n, n.sons[namePos], skIterator) + of nkMacroDef: + result = genJSONItem(d, n, n.sons[namePos], skMacro) + of nkTemplateDef: + result = genJSONItem(d, n, n.sons[namePos], skTemplate) + of nkConverterDef: + when useEffectSystem: documentRaises(n) + result = genJSONItem(d, n, n.sons[namePos], skConverter) + of nkTypeSection, nkVarSection, nkLetSection, nkConstSection: + for i in countup(0, sonsLen(n) - 1): + if n.sons[i].kind != nkCommentStmt: + # order is always 'type var let const': + result = genJSONItem(d, n.sons[i], n.sons[i].sons[0], + succ(skType, ord(n.kind)-ord(nkTypeSection))) + of nkStmtList: + var elem = jArray + if elem == nil: elem = newJArray() + for i in countup(0, sonsLen(n) - 1): + var r = generateJson(d, n.sons[i], elem) + if r != nil: + elem.add(r) + if result == nil: result = elem + of nkWhenStmt: + # generate documentation for the first branch only: + if not checkForFalse(n.sons[0].sons[0]) and jArray != nil: + discard generateJson(d, lastSon(n.sons[0]), jArray) + else: nil + +proc genSection(d: PDoc, kind: TSymKind) = const sectionNames: array[skModule..skTemplate, string] = [ - "Imports", "Types", "Vars", "Lets", "Consts", "Vars", "Procs", "Methods", + "Imports", "Types", "Vars", "Lets", "Consts", "Vars", "Procs", "Methods", "Iterators", "Converters", "Macros", "Templates" ] - if d.section[kind] == nil: return + if d.section[kind] == nil: return var title = sectionNames[kind].toRope d.section[kind] = ropeFormatNamedVars(getConfigVar("doc.section"), [ "sectionid", "sectionTitle", "sectionTitleID", "content"], [ @@ -313,7 +371,7 @@ proc genSection(d: PDoc, kind: TSymKind) = "sectionid", "sectionTitle", "sectionTitleID", "content"], [ ord(kind).toRope, title, toRope(ord(kind) + 50), d.toc[kind]]) -proc genOutFile(d: PDoc): PRope = +proc genOutFile(d: PDoc): PRope = var code, content: PRope title = "" @@ -321,7 +379,7 @@ proc genOutFile(d: PDoc): PRope = var tmp = "" renderTocEntries(d[], j, 1, tmp) var toc = tmp.toRope - for i in countup(low(TSymKind), high(TSymKind)): + for i in countup(low(TSymKind), high(TSymKind)): genSection(d, i) app(toc, d.toc[i]) if toc != nil: @@ -329,30 +387,30 @@ proc genOutFile(d: PDoc): PRope = for i in countup(low(TSymKind), high(TSymKind)): app(code, d.section[i]) if d.meta[metaTitle].len != 0: title = d.meta[metaTitle] else: title = "Module " & extractFilename(changeFileExt(d.filename, "")) - + let bodyname = if d.hasToc: "doc.body_toc" else: "doc.body_no_toc" - content = ropeFormatNamedVars(getConfigVar(bodyname), ["title", + content = ropeFormatNamedVars(getConfigVar(bodyname), ["title", "tableofcontents", "moduledesc", "date", "time", "content"], - [title.toRope, toc, d.modDesc, toRope(getDateStr()), + [title.toRope, toc, d.modDesc, toRope(getDateStr()), toRope(getClockStr()), code]) - if optCompileOnly notin gGlobalOptions: + if optCompileOnly notin gGlobalOptions: # XXX what is this hack doing here? 'optCompileOnly' means raw output!? - code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", - "tableofcontents", "moduledesc", "date", "time", - "content", "author", "version"], - [title.toRope, toc, d.modDesc, toRope(getDateStr()), - toRope(getClockStr()), content, d.meta[metaAuthor].toRope, + code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", + "tableofcontents", "moduledesc", "date", "time", + "content", "author", "version"], + [title.toRope, toc, d.modDesc, toRope(getDateStr()), + toRope(getClockStr()), content, d.meta[metaAuthor].toRope, d.meta[metaVersion].toRope]) - else: + else: code = content result = code proc generateIndex*(d: PDoc) = if optGenIndex in gGlobalOptions: - writeIndexFile(d[], splitFile(options.outFile).dir / + writeIndexFile(d[], splitFile(options.outFile).dir / splitFile(d.filename).name & indexExt) -proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) = +proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) = var content = genOutFile(d) if optStdout in gGlobalOptions: writeRope(stdout, content) @@ -361,7 +419,7 @@ proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) = proc CommandDoc*() = var ast = parseFile(gProjectMainIdx) - if ast == nil: return + if ast == nil: return var d = newDocumentor(gProjectFull, options.gConfigVars) d.hasToc = true generateDoc(d, ast) @@ -388,12 +446,26 @@ proc CommandRst2TeX*() = splitter = "\\-" CommandRstAux(gProjectFull, TexExt) +proc CommandJSON*() = + var ast = parseFile(gProjectMainIdx) + if ast == nil: return + var d = newDocumentor(gProjectFull, options.gConfigVars) + d.hasToc = true + var json = generateJson(d, ast) + var content = newRope(pretty(json)) + + if optStdout in gGlobalOptions: + writeRope(stdout, content) + else: + echo getOutFile(gProjectFull, JsonExt) + writeRope(content, getOutFile(gProjectFull, JsonExt), useWarning = false) + proc CommandBuildIndex*() = var content = mergeIndexes(gProjectFull).toRope - - let code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", - "tableofcontents", "moduledesc", "date", "time", - "content", "author", "version"], - ["Index".toRope, nil, nil, toRope(getDateStr()), + + let code = ropeFormatNamedVars(getConfigVar("doc.file"), ["title", + "tableofcontents", "moduledesc", "date", "time", + "content", "author", "version"], + ["Index".toRope, nil, nil, toRope(getDateStr()), toRope(getClockStr()), content, nil, nil]) writeRope(code, getOutFile("theindex", HtmlExt)) diff --git a/compiler/main.nim b/compiler/main.nim index 6a4ca496b9..9ffe994542 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -9,8 +9,8 @@ # implements the command dispatcher and several commands -import - llstream, strutils, ast, astalgo, lexer, syntaxes, renderer, options, msgs, +import + llstream, strutils, ast, astalgo, lexer, syntaxes, renderer, options, msgs, os, condsyms, rodread, rodwrite, times, wordrecg, sem, semdata, idents, passes, docgen, extccomp, cgen, jsgen, json, nversion, @@ -98,7 +98,7 @@ proc CommandCompileToC = # rodread.rodcompilerProcs # rodread.gTypeTable # rodread.gMods - + # !! ropes.cache # semthreads.computed? # @@ -166,7 +166,7 @@ proc commandEval(exp: string) = proc CommandPrettyOld = var projectFile = addFileExt(mainCommandArg(), NimExt) var module = parseFile(projectFile.fileInfoIdx) - if module != nil: + if module != nil: renderModule(module, getOutFile(mainCommandArg(), "pretty." & NimExt)) proc CommandPretty = @@ -175,24 +175,24 @@ proc CommandPretty = registerPass(prettyPass) compileProject() pretty.overwriteFiles() - + proc CommandScan = var f = addFileExt(mainCommandArg(), nimExt) var stream = LLStreamOpen(f, fmRead) - if stream != nil: - var + if stream != nil: + var L: TLexer tok: TToken initToken(tok) openLexer(L, f, stream) - while true: + while true: rawGetTok(L, tok) PrintTok(tok) - if tok.tokType == tkEof: break + if tok.tokType == tkEof: break CloseLexer(L) - else: + else: rawMessage(errCannotOpenFile, f) - + proc CommandSuggest = if isServing: # XXX: hacky work-around ahead @@ -246,7 +246,7 @@ proc resetMemory = for i in low(buckets)..high(buckets): buckets[i] = nil idAnon = nil - + # XXX: clean these global vars # ccgstmts.gBreakpoints # ccgthreadvars.nimtv @@ -262,7 +262,7 @@ proc resetMemory = # rodread.rodcompilerProcs # rodread.gTypeTable # rodread.gMods - + # !! ropes.cache # semthreads.computed? # @@ -289,7 +289,7 @@ const proc MainCommand* = when SimiluateCaasMemReset: gGlobalOptions.incl(optCaasEnabled) - + # In "nimrod serve" scenario, each command must reset the registered passes clearPasses() gLastCmdTime = epochTime() @@ -301,7 +301,7 @@ proc MainCommand* = passes.gIncludeFile = includeModule passes.gImportModule = importModule case command.normalize - of "c", "cc", "compile", "compiletoc": + of "c", "cc", "compile", "compiletoc": # compile means compileToC currently gCmd = cmdCompileToC wantMainModule() @@ -325,13 +325,13 @@ proc MainCommand* = when hasTinyCBackend: extccomp.setCC("tcc") CommandCompileToC() - else: + else: rawMessage(errInvalidCommandX, command) - of "js", "compiletojs": + of "js", "compiletojs": gCmd = cmdCompileToJS wantMainModule() CommandCompileToJS() - of "compiletollvm": + of "compiletollvm": gCmd = cmdCompileToLLVM wantMainModule() when has_LLVM_Backend: @@ -353,21 +353,27 @@ proc MainCommand* = wantMainModule() DefineSymbol("nimdoc") CommandDoc2() - of "rst2html": + of "rst2html": gCmd = cmdRst2html LoadConfigs(DocConfig) wantMainModule() CommandRst2Html() - of "rst2tex": + of "rst2tex": gCmd = cmdRst2tex LoadConfigs(DocTexConfig) wantMainModule() CommandRst2TeX() + of "jsondoc": + gCmd = cmdDoc + LoadConfigs(DocConfig) + wantMainModule() + DefineSymbol("nimdoc") + CommandJSON() of "buildindex": gCmd = cmdDoc LoadConfigs(DocConfig) CommandBuildIndex() - of "gendepend": + of "gendepend": gCmd = cmdGenDepend wantMainModule() CommandGenDepend() @@ -400,16 +406,16 @@ proc MainCommand* = gCmd = cmdCheck wantMainModule() CommandCheck() - of "parse": + of "parse": gCmd = cmdParse wantMainModule() discard parseFile(gProjectMainIdx) - of "scan": + of "scan": gCmd = cmdScan wantMainModule() CommandScan() MsgWriteln("Beware: Indentation tokens depend on the parser\'s state!") - of "i": + of "i": gCmd = cmdInteractive CommandInteractive() of "e": @@ -427,11 +433,11 @@ proc MainCommand* = of "serve": isServing = true gGlobalOptions.incl(optCaasEnabled) - msgs.gErrorMax = high(int) # do not stop after first error + msgs.gErrorMax = high(int) # do not stop after first error serve(MainCommand) else: rawMessage(errInvalidCommandX, command) - + if (msgs.gErrorCounter == 0 and gCmd notin {cmdInterpret, cmdRun, cmdDump} and gVerbosity > 0): diff --git a/compiler/options.nim b/compiler/options.nim index ea6b913218..d4122c7b22 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -132,6 +132,7 @@ const NimExt* = "nim" RodExt* = "rod" HtmlExt* = "html" + JsonExt* = "json" TexExt* = "tex" IniExt* = "ini" DefaultConfig* = "nimrod.cfg" From 2434324a0c43bd557fbb60716f3549077847f0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Sun, 15 Dec 2013 15:52:01 +0100 Subject: [PATCH 07/27] Expand gitignore to include all binaries built by ./koch test. --- .gitignore | 395 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 392 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d67d9f2cf6..2fc6db6678 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,7 @@ examples/cross_calculator/android/tags /examples/cairoex /examples/cgiex /examples/curlex +/examples/debugging /examples/docstrings /examples/filterex /examples/fizzbuzz @@ -89,6 +90,12 @@ examples/cross_calculator/android/tags /examples/pythonex /examples/sdlex /examples/statcsv +/examples/talk/dsl +/examples/talk/formatoptimizer +/examples/talk/hoisting +/examples/talk/lazyeval +/examples/talk/quasiquote +/examples/talk/tags /examples/tclex /examples/transff /examples/tunit @@ -96,10 +103,13 @@ examples/cross_calculator/android/tags /examples/x11ex /lib/libnimrtl.dylib /lib/libserver.dylib +/lib/packages/docutils/highlite /lib/pure/actors /lib/pure/algorithm /lib/pure/asyncio /lib/pure/base64 +/lib/pure/basic2d +/lib/pure/basic3d /lib/pure/browsers /lib/pure/cgi /lib/pure/collections/sequtils @@ -111,6 +121,7 @@ examples/cross_calculator/android/tags /lib/pure/encodings /lib/pure/endians /lib/pure/events +/lib/pure/fsmonitor /lib/pure/ftpclient /lib/pure/gentabs /lib/pure/hashes @@ -127,19 +138,23 @@ examples/cross_calculator/android/tags /lib/pure/math /lib/pure/md5 /lib/pure/memfiles +/lib/pure/mersenne /lib/pure/mimetypes /lib/pure/nimprof +/lib/pure/numeric /lib/pure/oids /lib/pure/os /lib/pure/osproc /lib/pure/parsecfg /lib/pure/parsecsv /lib/pure/parseopt +/lib/pure/parseopt2 /lib/pure/parsesql /lib/pure/parseurl /lib/pure/parseutils /lib/pure/parsexml /lib/pure/pegs +/lib/pure/poly /lib/pure/redis /lib/pure/romans /lib/pure/ropes @@ -167,9 +182,9 @@ examples/cross_calculator/android/tags /run.json /testresults.html /testresults.json -/tests/caas/SymbolProcRun.*/ /tests/caas/absurd_nesting /tests/caas/completion_dot_syntax_main +/tests/caasdriver /tests/caas/forward_declarations /tests/caas/idetools_api /tests/caas/imported @@ -178,6 +193,380 @@ examples/cross_calculator/android/tags /tests/caas/issue_477_dynamic_dispatch /tests/caas/its_full_of_procs /tests/caas/main -/tests/caasdriver -/tools/nimgrep +/tests/caas/SymbolProcRun.*/ +/tests/ccg/tmissingbracket +/tests/compile/talias +/tests/compile/tambsym2 +/tests/compile/tarrindx +/tests/compile/tassign +/tests/compile/tbindoverload +/tests/compile/tcan_alias_generic +/tests/compile/tcan_alias_specialised_generic +/tests/compile/tcan_inherit_generic +/tests/compile/tcan_specialise_generic +/tests/compile/tccgen1 +/tests/compile/tclosure4 +/tests/compile/tclosurebug2 +/tests/compile/tcmdline +/tests/compile/tcodegenbug1 +/tests/compile/tcolonisproc +/tests/compile/tcolors +/tests/compile/tcommontype +/tests/compile/tcompiles +/tests/compile/tcomputedgoto +/tests/compile/tconsteval +/tests/compile/tconstraints +/tests/compile/tconvcolors +/tests/compile/tcputime +/tests/compile/tdefaultprocparam +/tests/compile/tdictdestruct +/tests/compile/tdiscardable +/tests/compile/tdllvar +/tests/compile/tdumpast +/tests/compile/tdumpast2 +/tests/compile/techo +/tests/compile/teffects1 +/tests/compile/temptyecho +/tests/compile/tendian +/tests/compile/tenum +/tests/compile/tenum2 +/tests/compile/tenum3 +/tests/compile/teval1 +/tests/compile/texport +/tests/compile/tfib +/tests/compile/tforwardgeneric +/tests/compile/tforwty +/tests/compile/tforwty2 +/tests/compile/tgeneric +/tests/compile/tgeneric2 +/tests/compile/tgeneric3 +/tests/compile/tgeneric4 +/tests/compile/tgenericmatcher +/tests/compile/tgenericmatcher2 +/tests/compile/tgenericprocvar +/tests/compile/tgenericprop +/tests/compile/tgenericrefs +/tests/compile/tgenerictmpl +/tests/compile/tgenericvariant +/tests/compile/tgensymgeneric +/tests/compile/tgetstartmilsecs +/tests/compile/tglobalforvar +/tests/compile/thallo +/tests/compile/theaproots +/tests/compile/thexrange +/tests/compile/thygienictempl +/tests/compile/tident +/tests/compile/timplicititems +/tests/compile/timplictderef +/tests/compile/tinheritref +/tests/compile/tio +/tests/compile/tircbot +/tests/compile/titer +/tests/compile/titer2 +/tests/compile/titer_no_tuple_unpack +/tests/compile/titerovl +/tests/compile/tlastmod +/tests/compile/tlinearscanend +/tests/compile/tloops +/tests/compile/tmacro1 +/tests/compile/tmacro2 +/tests/compile/tmacroaspragma +/tests/compile/tmacrostmt +/tests/compile/tmandelbrot +/tests/compile/tmarshal +/tests/compile/tmath +/tests/compile/tmatrix1 +/tests/compile/tmatrix2 +/tests/compile/tmongo +/tests/compile/tnamedparamanonproc +/tests/compile/tnamedparams +/tests/compile/tnestedproc +/tests/compile/tnew +/tests/compile/tnewsets +/tests/compile/tnewuns +/tests/compile/tnoargopenarray +/tests/compile/tnoforward +/tests/compile/tobjconstr2 +/tests/compile/tobjcov +/tests/compile/tobject2 +/tests/compile/tobject3 +/tests/compile/tobjects +/tests/compile/toop +/tests/compile/toptions +/tests/compile/tos +/tests/compile/toverprc +/tests/compile/tparedef +/tests/compile/tparscfg +/tests/compile/tparsefloat +/tests/compile/tparsopt +/tests/compile/tposix +/tests/compile/tprep +/tests/compile/tprocvars +/tests/compile/tpush +/tests/compile/tquicksort +/tests/compile/tquit +/tests/compile/tradix +/tests/compile/treadln +/tests/compile/treadx +/tests/compile/trecmod +/tests/compile/trecmod2 +/tests/compile/trectuple +/tests/compile/trectuples +/tests/compile/tredef +/tests/compile/trepr +/tests/compile/tsecondarrayproperty +/tests/compile/tseq2 +/tests/compile/tseqcon2 +/tests/compile/tshadow_magic_type +/tests/compile/tsizeof +/tests/compile/tslurp +/tests/compile/tsockets +/tests/compile/tsortcall +/tests/compile/tspecialised_is_equivalent +/tests/compile/tstrace +/tests/compile/tstrdesc +/tests/compile/tstrdist +/tests/compile/tstreams +/tests/compile/tstrset +/tests/compile/tstrtabs +/tests/compile/ttableconstr +/tests/compile/ttempl +/tests/compile/ttempl3 +/tests/compile/ttempl4 +/tests/compile/ttempl5 +/tests/compile/ttemplreturntype +/tests/compile/tthread_generic +/tests/compile/ttime +/tests/compile/ttuple1 +/tests/compile/ttypeclasses +/tests/compile/ttypeconverter1 +/tests/compile/tuserpragma +/tests/compile/tvoid +/tests/compile/twalker +/tests/compile/typalias +/tests/dll/client +/tests/gc/closureleak +/tests/gc/cycleleak +/tests/gc/gcbench +/tests/gc/gcleak +/tests/gc/gcleak2 +/tests/gc/gcleak3 +/tests/gc/gctest +/tests/gc/weakrefs +/tests/manyloc/argument_parser/ex_wget +/tests/manyloc/nake/nakefile +/tests/manyloc/packages/noconflicts +/tests/manyloc/standalone/barebone +/tests/patterns/targlist +/tests/patterns/tcse +/tests/patterns/thoist +/tests/patterns/tmatrix +/tests/patterns/tnoalias +/tests/patterns/tnoendlessrec +/tests/patterns/tor +/tests/patterns/tpartial +/tests/patterns/tstar +/tests/patterns/tstmtlist +/tests/reject/t99bott +/tests/reject/tcheckedfield1 +/tests/reject/tdeprecated +/tests/reject/tdisallowif +/tests/reject/tuninit1 +/tests/rodfiles/aconv +/tests/rodfiles/bconv +/tests/rodfiles/bmethods +/tests/rodfiles/bmethods2 +/tests/rodfiles/deada +/tests/rodfiles/deada2 +/tests/rodfiles/hallo +/tests/rodfiles/hallo2 +/tests/rodfiles/tgeneric1 +/tests/rodfiles/tgeneric2 +/tests/run/tack +/tests/run/tactiontable +/tests/run/tambsym2 +/tests/run/tambsys +/tests/run/tanontuples +/tests/run/tarray +/tests/run/tarray2 +/tests/run/tarray3 +/tests/run/tarraycons +/tests/run/tassert +/tests/run/tastoverload1 +/tests/run/tasynciossl +/tests/run/tasyncudp +/tests/run/tbind1 +/tests/run/tbind3 +/tests/run/tbintre2 +/tests/run/tbintree +/tests/run/tborrow +/tests/run/tbug499771 +/tests/run/tbug511622 +/tests/run/tcase_setconstr +/tests/run/tcasestm +/tests/run/tcgbug +/tests/run/tclosure2 +/tests/run/tclosure3 +/tests/run/tcnstseq +/tests/run/tcnstseq2 +/tests/run/tcnstseq3 +/tests/run/tconcat +/tests/run/tconstr2 +/tests/run/tcontinue +/tests/run/tcontinuexc +/tests/run/tcopy +/tests/run/tcountup +/tests/run/tcritbits +/tests/run/tcurrncy +/tests/run/tdestructor +/tests/run/tdrdobbs_examples +/tests/run/temit +/tests/run/tenumhole +/tests/run/tenumitems +/tests/run/teventemitter +/tests/run/tevents +/tests/run/texceptions +/tests/run/texcpt1 +/tests/run/texcsub +/tests/run/texplicitgeneric1 +/tests/run/tfieldindex +/tests/run/tfielditerator +/tests/run/tfielditerator2 +/tests/run/tfilter +/tests/run/tfinally +/tests/run/tfinally2 +/tests/run/tfinally3 +/tests/run/tfinalobj +/tests/run/tfloat1 +/tests/run/tfloat2 +/tests/run/tfloat3 +/tests/run/tformat +/tests/run/tfriends +/tests/run/tgenericassign +/tests/run/tgenericassigntuples +/tests/run/tgenericconverter +/tests/run/tgenericprocvar +/tests/run/tgenerics1 +/tests/run/tgensym +/tests/run/tglobal +/tests/run/thashes +/tests/run/thexlit +/tests/run/thintoff +/tests/run/tidgen +/tests/run/tindent1 +/tests/run/tinit +/tests/run/tinterf +/tests/run/tints +/tests/run/tisopr +/tests/run/titer3 +/tests/run/titer5 +/tests/run/titer6 +/tests/run/titer7 +/tests/run/titer8 +/tests/run/titer9 +/tests/run/titerslice +/tests/run/titervaropenarray +/tests/run/tkoeniglookup +/tests/run/tlet +/tests/run/tlists +/tests/run/tlocals +/tests/run/tlowhigh +/tests/run/tmacro2 +/tests/run/tmacro3 +/tests/run/tmacro4 +/tests/run/tmacros1 +/tests/run/tmath +/tests/run/tmatrix +/tests/run/tmemoization +/tests/run/tmethods1 +/tests/run/tmixin +/tests/run/tmoditer +/tests/run/tmultim1 +/tests/run/tmultim2 +/tests/run/tmultim3 +/tests/run/tmultim4 +/tests/run/tmultim6 +/tests/run/tnamedenumfields +/tests/run/tnestif +/tests/run/tnestprc +/tests/run/tnewderef +/tests/run/tnodeadlocks +/tests/run/tobjasgn +/tests/run/tobjconstr +/tests/run/tobject +/tests/run/tofopr +/tests/run/tonraise +/tests/run/toop1 +/tests/run/topenarrayrepr +/tests/run/topenlen +/tests/run/toprprec +/tests/run/toverflw +/tests/run/toverflw2 +/tests/run/toverl2 +/tests/run/toverl3 +/tests/run/toverwr +/tests/run/tovfint +/tests/run/tpatterns +/tests/run/tpegs +/tests/run/tpos +/tests/run/tprecedence +/tests/run/tprintf +/tests/run/tprocvar +/tests/run/tquotewords +/tests/run/tregex +/tests/run/treguse +/tests/run/trepr +/tests/run/treraise +/tests/run/tromans +/tests/run/tseqcon +/tests/run/tseqtuple +/tests/run/tsequtils +/tests/run/tsets +/tests/run/tsets2 +/tests/run/tsidee2 +/tests/run/tsidee3 +/tests/run/tsimmeth +/tests/run/tsimplesort +/tests/run/tslices +/tests/run/tsortdev +/tests/run/tsplit +/tests/run/tstempl +/tests/run/tstmtexprs +/tests/run/tstrange +/tests/run/tstringinterp +/tests/run/tstrlits +/tests/run/tstrutil +/tests/run/tsubrange +/tests/run/tsubrange2 +/tests/run/ttables +/tests/run/ttables2 +/tests/run/ttoseq +/tests/run/ttypedesc1 +/tests/run/tunhandledexc +/tests/run/tunidecode +/tests/run/tunittests +/tests/run/tuserassert +/tests/run/tvarargs_vs_generic +/tests/run/tvardecl +/tests/run/tvariantasgn +/tests/run/tvariantstack +/tests/run/tvarious1 +/tests/run/tvarnums +/tests/run/tvarres1 +/tests/run/tvarres2 +/tests/run/tvartup +/tests/run/tvtable +/tests/run/twrongexc +/tests/run/txmlgen +/tests/run/txmltree +/tests/run/tzeroarray +/tests/system/helpers/readall_echo +/tests/system/io /tests/system/params +/tests/tester +/tests/threads/tactors +/tests/threads/tactors2 +/tests/threads/tthreadanalysis2 +/tests/threads/tthreadanalysis3 +/tests/threads/tthreadheapviolation1 +/tools/nimgrep From 78a48b942a6333d2844c35ec89fb108cb2931845 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Sun, 15 Dec 2013 22:57:29 +0100 Subject: [PATCH 08/27] Adds fitting quote to idetools doc. --- doc/idetools.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/idetools.txt b/doc/idetools.txt index fdc4ebde67..7e51669f27 100644 --- a/doc/idetools.txt +++ b/doc/idetools.txt @@ -8,6 +8,11 @@ .. contents:: +.. raw:: html +

+ "yes, I'm the creator" -- Araq, 2013-07-26 19:28:32. +

+ Nimrod differs from many other compilers in that it is really fast, and being so fast makes it suited to provide external queries for text editors about the source code being written. Through the From 132b6b3efee8d1be7f94545ed5fefaf64de741dc Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 16 Dec 2013 22:26:17 +0100 Subject: [PATCH 09/27] implemented 'injectStmt'; more debug support --- compiler/ccgcalls.nim | 2 ++ compiler/ccgstmts.nim | 7 ++++++- compiler/cgen.nim | 3 +++ compiler/cgendata.nim | 1 + compiler/pragmas.nim | 8 +++++++- compiler/semthreads.nim | 3 ++- compiler/wordrecg.nim | 5 +++-- doc/manual.txt | 12 ++++++++++++ doc/nimrodc.txt | 13 +++++++++++++ lib/system/alloc.nim | 2 +- lib/system/gc.nim | 13 ++++++++----- lib/system/mmdisp.nim | 3 ++- 12 files changed, 60 insertions(+), 12 deletions(-) diff --git a/compiler/ccgcalls.nim b/compiler/ccgcalls.nim index 1d6df3c15a..07fba95a3d 100644 --- a/compiler/ccgcalls.nim +++ b/compiler/ccgcalls.nim @@ -290,6 +290,7 @@ proc genCall(p: BProc, e: PNode, d: var TLoc) = genNamedParamCall(p, e, d) else: genPrefixCall(p, nil, e, d) + postStmtActions(p) when false: if d.s == onStack and containsGarbageCollectedRef(d.t): keepAlive(p, d) @@ -303,6 +304,7 @@ proc genAsgnCall(p: BProc, le, ri: PNode, d: var TLoc) = genNamedParamCall(p, ri, d) else: genPrefixCall(p, le, ri, d) + postStmtActions(p) when false: if d.s == onStack and containsGarbageCollectedRef(d.t): keepAlive(p, d) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 75cabf4143..ac4bbb79fc 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -906,7 +906,12 @@ proc genPragma(p: BProc, n: PNode) = of wEmit: genEmit(p, it) of wBreakpoint: genBreakPoint(p, it) of wWatchpoint: genWatchpoint(p, it) - else: nil + of wInjectStmt: + var p = newProc(nil, p.module) + p.options = p.options - {optLineTrace, optStackTrace} + genStmts(p, it.sons[1]) + p.module.injectStmt = p.s(cpsStmts) + else: discard proc FieldDiscriminantCheckNeeded(p: BProc, asgn: PNode): bool = if optFieldCheck in p.options: diff --git a/compiler/cgen.nim b/compiler/cgen.nim index b0c90de766..c143a5d6a5 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -289,6 +289,9 @@ proc genLineDir(p: BProc, t: PNode) = linefmt(p, cpsStmts, "nimln($1, $2);$n", line.toRope, t.info.quotedFilename) +proc postStmtActions(p: BProc) {.inline.} = + app(p.s(cpsStmts), p.module.injectStmt) + proc accessThreadLocalVar(p: BProc, s: PSym) proc emulatedThreadVars(): bool {.inline.} diff --git a/compiler/cgendata.nim b/compiler/cgendata.nim index c156c40fe5..a803c0ba1e 100644 --- a/compiler/cgendata.nim +++ b/compiler/cgendata.nim @@ -111,6 +111,7 @@ type labels*: natural # for generating unique module-scope names extensionLoaders*: array['0'..'9', PRope] # special procs for the # OpenGL wrapper + injectStmt*: PRope var mainModProcs*, mainModInit*, mainDatInit*: PRope # parts of the main module diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 8c2425de3c..6f1e7af25e 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -43,7 +43,8 @@ const wFatal, wDefine, wUndef, wCompile, wLink, wLinkSys, wPure, wPush, wPop, wBreakpoint, wWatchpoint, wPassL, wPassC, wDeadCodeElim, wDeprecated, wFloatChecks, wInfChecks, wNanChecks, wPragma, wEmit, wUnroll, - wLinearScanEnd, wPatterns, wEffects, wNoForward, wComputedGoto} + wLinearScanEnd, wPatterns, wEffects, wNoForward, wComputedGoto, + wInjectStmt} lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, wNosideEffect, wSideEffect, wNoreturn, wDynLib, wHeader, wDeprecated, wExtern, wThread, wImportcpp, wImportobjc, wNoStackFrame, @@ -722,6 +723,11 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int, of wOperator: if sym == nil: invalidPragma(it) else: sym.position = expectIntLit(c, it) + of wInjectStmt: + if it.kind != nkExprColonExpr: + localError(it.info, errExprExpected) + else: + it.sons[1] = c.semExpr(c, it.sons[1]) else: invalidPragma(it) else: invalidPragma(it) else: processNote(c, it) diff --git a/compiler/semthreads.nim b/compiler/semthreads.nim index 595ab0454d..eded993254 100644 --- a/compiler/semthreads.nim +++ b/compiler/semthreads.nim @@ -380,7 +380,8 @@ proc analyseThreadProc*(prc: PSym) = var formals = skipTypes(prc.typ, abstractInst).n for i in 1 .. formals.len-1: var formal = formals.sons[i].sym - c.mapping[formal.id] = toTheirs # thread receives foreign data! + # the input is copied and belongs to the thread: + c.mapping[formal.id] = toMine discard analyse(c, prc.getBody) proc needsGlobalAnalysis*: bool = diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim index b37a7bb4f9..39b19646e8 100644 --- a/compiler/wordrecg.nim +++ b/compiler/wordrecg.nim @@ -60,7 +60,7 @@ type wPassc, wPassl, wBorrow, wDiscardable, wFieldChecks, wWatchPoint, wSubsChar, - wAcyclic, wShallow, wUnroll, wLinearScanEnd, wComputedGoto, + wAcyclic, wShallow, wUnroll, wLinearScanEnd, wComputedGoto, wInjectStmt, wWrite, wGensym, wInject, wDirty, wInheritable, wThreadVar, wEmit, wNoStackFrame, wImplicitStatic, wGlobal, wCodegenDecl, @@ -142,7 +142,8 @@ const "compiletime", "noinit", "passc", "passl", "borrow", "discardable", "fieldchecks", "watchpoint", - "subschar", "acyclic", "shallow", "unroll", "linearscanend", "computedgoto", + "subschar", "acyclic", "shallow", "unroll", "linearscanend", + "computedgoto", "injectstmt", "write", "gensym", "inject", "dirty", "inheritable", "threadvar", "emit", "nostackframe", "implicitstatic", "global", "codegendecl", diff --git a/doc/manual.txt b/doc/manual.txt index dabff3d69d..371f2b9bfc 100644 --- a/doc/manual.txt +++ b/doc/manual.txt @@ -5053,6 +5053,18 @@ Note that this pragma is somewhat of a misnomer: Other backends will provide the same feature under the same name. +Extern pragma +------------- +Like ``exportc`` or ``importc`` the `extern`:idx: pragma affects name +mangling. The string literal passed to ``extern`` can be a format string: + +.. code-block:: Nimrod + proc p(s: string) {.extern: "prefix$1".} = + echo s + +In the example the external name of ``p`` is set to ``prefixp``. + + Bycopy pragma ------------- diff --git a/doc/nimrodc.txt b/doc/nimrodc.txt index d494a0922f..f5fbf3ebbe 100644 --- a/doc/nimrodc.txt +++ b/doc/nimrodc.txt @@ -468,6 +468,19 @@ proc is declared in the generated code: proc myinterrupt() {.codegenDecl: "__interrupt $# $#$#".} = echo "realistic interrupt handler" + + +InjectStmt pragma +----------------- + +The `injectStmt`:idx: pragma can be used to inject a statement before every +other statement in the current module. It is only supposed to be used for +debugging: + +.. code-block:: nimrod + {.injectStmt: gcInvariants().} + + # ... complex code here that produces crashes ... LineDir option diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 2bab792126..17258cf68a 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -760,7 +760,7 @@ proc getOccupiedMem(a: TMemRegion): int {.inline.} = # ---------------------- thread memory region ------------------------------- template InstantiateForRegion(allocator: expr) = - when false: + when defined(fulldebug): proc interiorAllocatedPtr*(p: pointer): pointer = result = interiorAllocatedPtr(allocator, p) diff --git a/lib/system/gc.nim b/lib/system/gc.nim index d2b065d6bd..68e8b423db 100644 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -345,8 +345,9 @@ proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) = proc forAllChildren(cell: PCell, op: TWalkOp) = gcAssert(cell != nil, "forAllChildren: 1") - gcAssert(cell.typ != nil, "forAllChildren: 2") - gcAssert cell.typ.kind in {tyRef, tySequence, tyString}, "forAllChildren: 3" + gcAssert(isAllocatedPtr(gch.region, cell), "forAllChildren: 2") + gcAssert(cell.typ != nil, "forAllChildren: 3") + gcAssert cell.typ.kind in {tyRef, tySequence, tyString}, "forAllChildren: 4" let marker = cell.typ.marker if marker != nil: marker(cellToUsr(cell), op.int) @@ -361,7 +362,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) = for i in 0..s.len-1: forAllChildrenAux(cast[pointer](d +% i *% cell.typ.base.size +% GenericSeqSize), cell.typ.base, op) - else: nil + else: discard proc addNewObjToZCT(res: PCell, gch: var TGcHeap) {.inline.} = # we check the last 8 entries (cache line) for a slot that could be reused. @@ -408,8 +409,10 @@ proc addNewObjToZCT(res: PCell, gch: var TGcHeap) {.inline.} = add(gch.zct, res) {.push stackTrace: off, profiler:off.} -proc gcInvariant*(msg: string) = - sysAssert(allocInv(gch.region), msg) +proc gcInvariant*() = + sysAssert(allocInv(gch.region), "injected") + when defined(markForDebug): + markForDebug(gch) {.pop.} proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap): pointer = diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 118272ee31..942b6778ed 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -18,7 +18,8 @@ const logGC = false traceGC = false # extensive debugging alwaysCycleGC = false - alwaysGC = false # collect after every memory allocation (for debugging) + alwaysGC = defined(fulldebug) # collect after every memory + # allocation (for debugging) leakDetector = false overwriteFree = false trackAllocationSource = leakDetector From db73f3af2027a68e7f9d311a8096c677b6ef4523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 17 Dec 2013 20:36:53 +0100 Subject: [PATCH 10/27] Add stdmsg (an alias to either stdout or stderr). --- lib/system.nim | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index dc5a406d14..3fccb4e8bb 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1947,15 +1947,13 @@ when not defined(JS): #and not defined(NimrodVM): ## The standard output stream. stderr* {.importc: "stderr", header: "".}: TFile ## The standard error stream. - ## - ## Note: In my opinion, this should not be used -- the concept of a - ## separate error stream is a design flaw of UNIX. A separate *message - ## stream* is a good idea, but since it is named ``stderr`` there are few - ## programs out there that distinguish properly between ``stdout`` and - ## ``stderr``. So, that's what you get if you don't name your variables - ## appropriately. It also annoys people if redirection - ## via ``>output.txt`` does not work because the program writes - ## to ``stderr``. + + when defined(useStdoutAsStdmsg): + template stdmsg*: TFile = stdout + else: + template stdmsg*: TFile = stderr + ## Template which expands to either stdout or stderr depending on + ## `useStdoutAsStdmsg` compile-time switch. proc Open*(f: var TFile, filename: string, mode: TFileMode = fmRead, bufSize: int = -1): Bool {.tags: [].} From 646458d328f7b4cdda518a1b2595962bea775309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Tue, 17 Dec 2013 20:42:02 +0100 Subject: [PATCH 11/27] write exception tracebacks to stdmsg, instead of stdout --- lib/system/excpt.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index 7937d9738e..45ad42a606 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -18,7 +18,7 @@ template stackTraceNL: expr = (if IsNil(stackTraceNewLine): "\n" else: stackTraceNewLine) when not defined(windows) or not defined(guiapp): - proc writeToStdErr(msg: CString) = write(stdout, msg) + proc writeToStdErr(msg: CString) = write(stdmsg, msg) else: proc MessageBoxA(hWnd: cint, lpText, lpCaption: cstring, uType: int): int32 {. From ab2eb884a0d440796b0c712346a17bbe1ff54e94 Mon Sep 17 00:00:00 2001 From: Clay Sweetser Date: Tue, 17 Dec 2013 18:03:08 -0500 Subject: [PATCH 12/27] Modified os.removeFile to act correctly when deleting a file that doesn't exist. --- lib/pure/os.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 71639d821c..ecab692cf0 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -972,15 +972,21 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", when not defined(ENOENT): var ENOENT {.importc, header: "".}: cint +when not defined(EACCES): + var EACCES {.importc, header: "".}: cint proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## Removes the `file`. If this fails, `EOS` is raised. This does not fail ## if the file never existed in the first place. ## On Windows, ignores the read-only attribute. - when defined(Windows): - setFilePermissions(file, {fpUserWrite}) if cremove(file) != 0'i32 and errno != ENOENT: - raise newException(EOS, $strerror(errno)) + when defined(Windows): + if errno == EACCES: # Turn this into a case stmt? + setFilePermissions(file, {fpUserWrite}) # Use lower level code? + if cremove(file) != 0'i32 and errno != ENOENT: + raise newException(EOS, $strerror(errno)) + else: + raise newException(EOS, $strerror(errno)) proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", tags: [FExecIO].} = From 74a63c004fe188bc710d6867e451f894c70381be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Wed, 18 Dec 2013 18:15:39 +0100 Subject: [PATCH 13/27] Rework CGI error reporting. - remove 'undocumented' stackTraceNL - add 'undocumented' errorMessageWriter - make printing errors for browser optional (for security) --- lib/pure/cgi.nim | 36 +++++++++++++++++++++++++++--------- lib/system/excpt.nim | 42 ++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/pure/cgi.nim b/lib/pure/cgi.nim index c499abdc09..29c686fd7f 100644 --- a/lib/pure/cgi.nim +++ b/lib/pure/cgi.nim @@ -342,16 +342,35 @@ proc writeContentType*() = ## ## .. code-block:: Nimrod ## write(stdout, "Content-type: text/html\n\n") - ## - ## It also modifies the debug stack traces so that they contain - ## ``
`` and are easily readable in a browser. write(stdout, "Content-type: text/html\n\n") - system.stackTraceNewLine = "
\n" -proc setStackTraceNewLine*() = - ## Modifies the debug stack traces so that they contain - ## ``
`` and are easily readable in a browser. - system.stackTraceNewLine = "
\n" +proc resetForStacktrace() = + stdout.write """ + --> --> + + +""" + +proc writeErrorMessage*(data: string) = + ## Tries to reset browser state and writes `data` to stdout in + ## tag. + resetForStacktrace() + # We use <plaintext> here, instead of escaping, so stacktrace can + # be understood by human looking at source. + stdout.write("<plaintext>\n") + stdout.write(data) + +proc setStackTraceStdout*() = + ## Makes Nimrod output stacktraces to stdout, instead of server log. + errorMessageWriter = writeErrorMessage + +proc setStackTraceNewLine*() {.deprecated.} = + ## Makes Nimrod output stacktraces to stdout, instead of server log. + ## Depracated alias for setStackTraceStdout. + setStackTraceStdout() proc setCookie*(name, value: string) = ## Sets a cookie. @@ -374,4 +393,3 @@ when isMainModule: const test1 = "abc\L+def xyz" assert UrlEncode(test1) == "abc%0A%2Bdef+xyz" assert UrlDecode(UrlEncode(test1)) == test1 - diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index 45ad42a606..9b6a64fb05 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -11,11 +11,10 @@ # use the heap (and nor exceptions) do not include the GC or memory allocator. var - stackTraceNewLine*: string ## undocumented feature; it is replaced by ``<br>`` - ## for CGI applications - -template stackTraceNL: expr = - (if IsNil(stackTraceNewLine): "\n" else: stackTraceNewLine) + errorMessageWriter*: (proc(msg: string): void {.tags: [FWriteIO].}) + ## Function that will be called + ## instead of stdmsg.write when printing stacktrace. + ## Unstable API. when not defined(windows) or not defined(guiapp): proc writeToStdErr(msg: CString) = write(stdmsg, msg) @@ -27,6 +26,12 @@ else: proc writeToStdErr(msg: CString) = discard MessageBoxA(0, msg, nil, 0) +proc showErrorMessage(data: cstring) = + if errorMessageWriter != nil: + errorMessageWriter($data) + else: + writeToStdErr(data) + proc chckIndx(i, a, b: int): int {.inline, compilerproc.} proc chckRange(i, a, b: int): int {.inline, compilerproc.} proc chckRangeF(x, a, b: float): float {.inline, compilerproc.} @@ -111,7 +116,7 @@ when defined(nativeStacktrace) and nativeStackTraceSupported: add(s, tempDlInfo.dli_sname) else: add(s, '?') - add(s, stackTraceNL) + add(s, "\n") else: if dlresult != 0 and tempDlInfo.dli_sname != nil and c_strcmp(tempDlInfo.dli_sname, "signalHandler") == 0'i32: @@ -172,21 +177,18 @@ proc auxWriteStackTrace(f: PFrame, s: var string) = add(s, ')') for k in 1..max(1, 25-(s.len-oldLen)): add(s, ' ') add(s, tempFrames[j].procname) - add(s, stackTraceNL) + add(s, "\n") when hasSomeStackTrace: proc rawWriteStackTrace(s: var string) = when nimrodStackTrace: if framePtr == nil: - add(s, "No stack traceback available") - add(s, stackTraceNL) + add(s, "No stack traceback available\n") else: - add(s, "Traceback (most recent call last)") - add(s, stackTraceNL) + add(s, "Traceback (most recent call last)\n") auxWriteStackTrace(framePtr, s) elif defined(nativeStackTrace) and nativeStackTraceSupported: - add(s, "Traceback from system (most recent call last)") - add(s, stackTraceNL) + add(s, "Traceback from system (most recent call last)\n") auxWriteStackTraceWithBacktrace(s) else: add(s, "No stack traceback available\n") @@ -207,7 +209,7 @@ proc raiseExceptionAux(e: ref E_Base) = pushCurrentException(e) c_longjmp(excHandler.context, 1) elif e[] of EOutOfMemory: - writeToStdErr(e.name) + showErrorMessage(e.name) quitOrDebug() else: when hasSomeStackTrace: @@ -219,7 +221,7 @@ proc raiseExceptionAux(e: ref E_Base) = add(buf, " [") add(buf, $e.name) add(buf, "]\n") - writeToStdErr(buf) + showErrorMessage(buf) else: # ugly, but avoids heap allocations :-) template xadd(buf, s, slen: expr) = @@ -235,7 +237,7 @@ proc raiseExceptionAux(e: ref E_Base) = add(buf, " [") xadd(buf, e.name, c_strlen(e.name)) add(buf, "]\n") - writeToStdErr(buf) + showErrorMessage(buf) quitOrDebug() proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} = @@ -255,9 +257,9 @@ proc WriteStackTrace() = when hasSomeStackTrace: var s = "" rawWriteStackTrace(s) - writeToStdErr(s) + showErrorMessage(s) else: - writeToStdErr("No stack traceback available\n") + showErrorMessage("No stack traceback available\n") proc getStackTrace(): string = when hasSomeStackTrace: @@ -298,13 +300,13 @@ when not defined(noSignalHandler): var buf = newStringOfCap(2000) rawWriteStackTrace(buf) processSignal(sig, buf.add) # nice hu? currying a la nimrod :-) - writeToStdErr(buf) + showErrorMessage(buf) GC_enable() else: var msg: cstring template asgn(y: expr) = msg = y processSignal(sig, asgn) - writeToStdErr(msg) + showErrorMessage(msg) when defined(endb): dbgAborting = True quit(1) # always quit when SIGABRT From 101d8bc9c333546b970e670be116e45788a069d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= <michal@zielinscy.org.pl> Date: Wed, 18 Dec 2013 18:19:32 +0100 Subject: [PATCH 14/27] Add two CGI example and basic Python CGI server. --- .gitignore | 2 ++ examples/cgi/cgi_server.py | 11 +++++++++++ examples/cgi/cgi_stacktrace.nim | 5 +++++ examples/cgi/example.nim | 7 +++++++ 4 files changed, 25 insertions(+) create mode 100644 examples/cgi/cgi_server.py create mode 100644 examples/cgi/cgi_stacktrace.nim create mode 100644 examples/cgi/example.nim diff --git a/.gitignore b/.gitignore index d67d9f2cf6..f576baf17e 100644 --- a/.gitignore +++ b/.gitignore @@ -63,6 +63,8 @@ examples/cross_calculator/android/tags /examples/allany /examples/cairoex /examples/cgiex +/examples/cgi/cgi_stacktrace +/examples/cgi/example /examples/curlex /examples/docstrings /examples/filterex diff --git a/examples/cgi/cgi_server.py b/examples/cgi/cgi_server.py new file mode 100644 index 0000000000..1907515e80 --- /dev/null +++ b/examples/cgi/cgi_server.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +import BaseHTTPServer +import CGIHTTPServer + +server = BaseHTTPServer.HTTPServer +handler = CGIHTTPServer.CGIHTTPRequestHandler +server_address = ('localhost', 8008) +handler.cgi_directories = ['/'] + +httpd = server(server_address, handler) +httpd.serve_forever() diff --git a/examples/cgi/cgi_stacktrace.nim b/examples/cgi/cgi_stacktrace.nim new file mode 100644 index 0000000000..e9f2f567c2 --- /dev/null +++ b/examples/cgi/cgi_stacktrace.nim @@ -0,0 +1,5 @@ +import cgi +cgi.setStackTraceStdout() + +var a: string = nil +a.add "foobar" diff --git a/examples/cgi/example.nim b/examples/cgi/example.nim new file mode 100644 index 0000000000..17629982ab --- /dev/null +++ b/examples/cgi/example.nim @@ -0,0 +1,7 @@ +import cgi + +write(stdout, "Content-type: text/html\n\n") +write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n") +write(stdout, "<html><head><title>Test</title></head><body>\n") +write(stdout, "Hello!") +writeln(stdout, "</body></html>") From 7d5fa3f52d23c4f238c1664fc568eb7f3c6a34b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= <michal@zielinscy.org.pl> Date: Wed, 18 Dec 2013 18:21:55 +0100 Subject: [PATCH 15/27] add info about CGI error reporing changes to web/news.txt --- web/news.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/news.txt b/web/news.txt index 1b492fa971..3d1546fb75 100644 --- a/web/news.txt +++ b/web/news.txt @@ -29,7 +29,6 @@ Changes affecting backwards compatibility using the new ``OSLastError`` proc. - ``os.parentDir`` now returns "" if there is no parent dir. - Compiler Additions ------------------ @@ -41,7 +40,8 @@ Compiler Additions over the generated code. - The compiler now supports a ``computedGoto`` pragma to support very fast dispatching for interpreters and the like. - +- In CGI scripts stacktraces are shown user only if cgi.setStackTraceStdout + is used. Language Additions ------------------ From 543d6b3209b4f106e3ead63d5d88fda87db64de6 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Tue, 17 Dec 2013 12:23:02 +0100 Subject: [PATCH 16/27] Replaces PDoc with var TRstGenerator for documentation. The PDoc type is a convenience internal alias. Where procs are exported only documented types should be used to avoid confusing users. --- lib/packages/docutils/rstgen.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index 364f847ccf..b9637baa02 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -40,7 +40,7 @@ type filename*: string meta*: array[TMetaEnum, string] - PDoc = var TRstGenerator + PDoc = var TRstGenerator ## Alias to type less. proc initRstGenerator*(g: var TRstGenerator, target: TOutputTarget, config: PStringTable, filename: string, @@ -147,7 +147,7 @@ proc dispA(target: TOutputTarget, dest: var string, if target != outLatex: addf(dest, xml, args) else: addf(dest, tex, args) -proc renderRstToOut*(d: PDoc, n: PRstNode, result: var string) +proc renderRstToOut*(d: var TRstGenerator, n: PRstNode, result: var string) proc renderAux(d: PDoc, n: PRstNode, result: var string) = for i in countup(0, len(n)-1): renderRstToOut(d, n.sons[i], result) @@ -162,7 +162,7 @@ proc renderAux(d: PDoc, n: PRstNode, frmtA, frmtB: string, result: var string) = # ---------------- index handling -------------------------------------------- -proc setIndexTerm*(d: PDoc, id, term: string) = +proc setIndexTerm*(d: var TRstGenerator, id, term: string) = d.theIndex.add(term) d.theIndex.add('\t') let htmlFile = changeFileExt(extractFilename(d.filename), HtmlExt) @@ -295,7 +295,7 @@ proc renderTocEntry(d: PDoc, e: TTocEntry, result: var string) = "<li><a class=\"reference\" id=\"$1_toc\" href=\"#$1\">$2</a></li>\n", "\\item\\label{$1_toc} $2\\ref{$1}\n", [e.refname, e.header]) -proc renderTocEntries*(d: PDoc, j: var int, lvl: int, result: var string) = +proc renderTocEntries*(d: var TRstGenerator, j: var int, lvl: int, result: var string) = var tmp = "" while j <= high(d.tocPart): var a = abs(d.tocPart[j].n.level) From f46336ae8bdddb7f3a6da4904aae18f99bc34f6f Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Wed, 18 Dec 2013 02:20:35 +0100 Subject: [PATCH 17/27] Adds missing ``code-block`` to macros' docstrings. --- lib/core/macros.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index d01d4ebee4..5f0d60e5c5 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -268,6 +268,8 @@ proc quote*(bl: stmt, op = "``"): PNimrodNode {.magic: "QuoteAst".} ## ## Example: ## + ## .. code-block:: nimrod + ## ## macro check(ex: expr): stmt = ## # this is a simplified version of the check macro from the ## # unittest module. @@ -290,6 +292,8 @@ template emit*(e: expr[string]): stmt = ## that should be inserted verbatim in the program ## Example: ## + ## .. code-block:: nimrod + ## ## emit("echo " & '"' & "hello world".toUpper & '"') ## eval: result = e.parseStmt From e0d6a1cec18698652f1b6a4a55a749fbeb9689d1 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Wed, 18 Dec 2013 21:21:16 +0100 Subject: [PATCH 18/27] Adds some docstrings to rstgen. --- lib/packages/docutils/rstgen.nim | 91 ++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index b9637baa02..09c6ba8b8c 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -7,7 +7,18 @@ # distribution, for details about the copyright. # -## This module implements a generator of HTML/Latex from `reStructuredText`:idx:. +## This module implements a generator of HTML/Latex from +## `reStructuredText`:idx: (see http://docutils.sourceforge.net/rst.html for +## information on this markup syntax). You can generate HTML output through the +## convenience proc ``rstToHtml``, which provided an input string with rst +## markup returns a string with the generated HTML. The final output is meant +## to be embedded inside a full document you provide yourself, so it won't +## contain the usual ``<header>`` or ``<body>`` parts. +## +## You can also create a ``TRstGenerator`` structure and populate it with the +## other lower level methods to finally build complete documents. This requires +## many options and tweaking, but you are not limited to snippets and can +## generate `LaTeX documents <https://en.wikipedia.org/wiki/LaTeX>`_ too. import strutils, os, hashes, strtabs, rstast, rst, highlite @@ -47,6 +58,44 @@ proc initRstGenerator*(g: var TRstGenerator, target: TOutputTarget, options: TRstParseOptions, findFile: TFindFileHandler, msgHandler: TMsgHandler) = + ## Initializes a ``TRstGenerator``. + ## + ## You need to call this before using a ``TRstGenerator`` with any other + ## procs in this module. Pass a non ``nil`` ``PStringTable`` value as + ## ``config`` with parameters used by the HTML output generator. If you + ## don't know what to use, pass the results of the ``defaultConfig()`` proc. + ## The ``filename`` is symbolic and used only for error reporting, you can + ## pass any non ``nil`` string here. + ## + ## The ``TRstParseOptions``, ``TFindFileHandler`` and ``TMsgHandler`` types + ## are defined in the the `packages/docutils/rst module <rst.html>`_. + ## ``options`` selects the behaviour of the rst parser. + ## + ## ``findFile`` is a proc used by the rst ``include`` directive among others. + ## The purpose of this proc is to mangle or filter paths. It receives paths + ## specified in the rst document and has to return a valid path to existing + ## files or the empty string otherwise. If you pass ``nil``, a default proc + ## will be used which given a path returns the input path only if the file + ## exists. One use for this proc is to transform relative paths found in the + ## document to absolute path, useful if the rst file and the resources it + ## references are not in the same directory as the current working directory. + ## + ## The ``msgHandler`` is a proc used for user error reporting. It will be + ## called with the filename, line, col, and type of any error found during + ## parsing. If you pass ``nil``, a default message handler will be used which + ## writes the messages to the standard output. + ## + ## Example: + ## + ## .. code-block:: nimrod + ## + ## import packages/docutils/rstgen + ## + ## var gen: TRstGenerator + ## + ## gen.initRstGenerator(outHtml, defaultConfig(), + ## "filename", {}, nil, nil) + g.config = config g.target = target g.tocPart = @[] @@ -148,6 +197,18 @@ proc dispA(target: TOutputTarget, dest: var string, else: addf(dest, tex, args) proc renderRstToOut*(d: var TRstGenerator, n: PRstNode, result: var string) + ## Writes into ``result`` the rst ast ``n`` using the ``d`` configuration. + ## + ## Before using this proc you need to initialise a ``TRstGenerator`` with + ## ``initRstGenerator`` and parse a rst file with ``rstParse`` from the + ## `packages/docutils/rst module <rst.html>`_. Example: + ## + ## .. code-block:: nimrod + ## + ## # ...configure gen and rst vars... + ## var generatedHTML = "" + ## renderRstToOut(gen, rst, generatedHTML) + ## echo generatedHTML proc renderAux(d: PDoc, n: PRstNode, result: var string) = for i in countup(0, len(n)-1): renderRstToOut(d, n.sons[i], result) @@ -678,8 +739,26 @@ $content proc rstToHtml*(s: string, options: TRstParseOptions, config: PStringTable): string = - ## exported for *nimforum*. - + ## Converts an input rst string into embeddable HTML. + ## + ## This convenience proc parses any input string using rst markup (it doesn't + ## have to be a full document!) and returns an embeddable piece of HTML. The + ## proc is meant to be used in *online* environments without access to a + ## meaningful filesystem, and therefore rst ``include`` like directives won't + ## work. For an explanation of the ``config`` parameter see the + ## ``initRstGenerator`` proc. Example: + ## + ## .. code-block:: nimrod + ## import packages/docutils/rstgen, strtabs + ## + ## echo rstToHtml("*Hello* **world**!", {}, + ## newStringTable(modeStyleInsensitive)) + ## # --> <em>Hello</em> <strong>world</strong>! + ## + ## If you need to allow the rst ``include`` directive or tweak the generated + ## output you have to create your own ``TRstGenerator`` with + ## ``initRstGenerator`` and related procs. + proc myFindFile(filename: string): string = # we don't find any files in online mode: result = "" @@ -692,4 +771,8 @@ proc rstToHtml*(s: string, options: TRstParseOptions, var rst = rstParse(s, filen, 0, 1, dummyHasToc, options) result = "" renderRstToOut(d, rst, result) - + + +when isMainModule: + echo rstToHtml("*Hello* **world**!", {}, + newStringTable(modeStyleInsensitive)) From 9f8db3ae542ab3dc8d18e5859b06bfa95b4e12ce Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Wed, 18 Dec 2013 21:25:49 +0100 Subject: [PATCH 19/27] Updates idetools docs after changes by #743. --- doc/idetools.txt | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/doc/idetools.txt b/doc/idetools.txt index fdc4ebde67..f2e690b0f8 100644 --- a/doc/idetools.txt +++ b/doc/idetools.txt @@ -522,21 +522,6 @@ At the moment idetools support is still in development so the test suite is not integrated with the main test suite and you have to run it manually. First you have to compile the tester:: - $ cd my/nimrod/checkout - $ nimrod c tests/tester.nim - -Running the tester without parameters will display some options. -To run the caas test suite (and other special tests) you need to -use the `special` command. You need to run this command from the -root of the checkout or it won't be able to open the required files:: - - $ ./tests/tester special - -However this is a roundabout way of running the test suite. You can -also compile and run ``tests/caasdriver.nim`` manually. In fact, -running it manually will allow you to specify special parameters -too. Example:: - $ cd my/nimrod/checkout/tests $ nimrod c caasdriver.nim From c26e787e5dab153b35470ea88f71dbf373de51f7 Mon Sep 17 00:00:00 2001 From: Clay Sweetser <clay.sweetser@gmail.com> Date: Wed, 18 Dec 2013 15:32:26 -0500 Subject: [PATCH 20/27] os.nim - Modify removeFile to use native Windows API calls --- lib/pure/os.nim | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index ecab692cf0..a366a7965a 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -972,8 +972,16 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", when not defined(ENOENT): var ENOENT {.importc, header: "<errno.h>".}: cint -when not defined(EACCES): - var EACCES {.importc, header: "<errno.h>".}: cint + +when defined(Windows): + when useWinUnicode: + template DeleteFile(file: expr): expr {.immediate.} = DeleteFileW(file) + template SetFileAttributes(file, attrs: expr): expr {.immediate.} = + SetFileAttributesW(file, attrs) + else: + template DeleteFile(file: expr): expr {.immediate.} = DeleteFileA(file) + template SetFileAttributes(file, attrs: expr): expr {.immediate.} = + SetFileAttributesA(file, attrs) proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## Removes the `file`. If this fails, `EOS` is raised. This does not fail @@ -981,11 +989,18 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## On Windows, ignores the read-only attribute. if cremove(file) != 0'i32 and errno != ENOENT: when defined(Windows): - if errno == EACCES: # Turn this into a case stmt? - setFilePermissions(file, {fpUserWrite}) # Use lower level code? - if cremove(file) != 0'i32 and errno != ENOENT: - raise newException(EOS, $strerror(errno)) + when useWinUnicode: + let f = newWideCString(file) else: + let f = file + if DeleteFile(f) == 0: + if GetLastError() == ERROR_ACCESS_DENIED: + if SetFileAttributes(f, FILE_ATTRIBUTE_NORMAL) == 0: + OSError(OSLastError()) + if DeleteFile(f) == 0: + OSError(OSLastError()) + else: + if cremove(file) != 0'i32 and errno != ENOENT: raise newException(EOS, $strerror(errno)) proc execShellCmd*(command: string): int {.rtl, extern: "nos$1", From 9de92522a6418b3460cea786dc363177288e02e1 Mon Sep 17 00:00:00 2001 From: Clay Sweetser <clay.sweetser@gmail.com> Date: Wed, 18 Dec 2013 16:04:43 -0500 Subject: [PATCH 21/27] Further fixes (stupid kdiff automerge) --- lib/pure/os.nim | 5 ++--- lib/windows/winlean.nim | 30 +++++++++++++++++++----------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index a366a7965a..d74cb1fb94 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -970,7 +970,7 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1", if crename(source, dest) != 0'i32: raise newException(EOS, $strerror(errno)) -when not defined(ENOENT): +when not defined(ENOENT) and not defined(Windows): var ENOENT {.importc, header: "<errno.h>".}: cint when defined(Windows): @@ -987,8 +987,7 @@ proc removeFile*(file: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = ## Removes the `file`. If this fails, `EOS` is raised. This does not fail ## if the file never existed in the first place. ## On Windows, ignores the read-only attribute. - if cremove(file) != 0'i32 and errno != ENOENT: - when defined(Windows): + when defined(Windows): when useWinUnicode: let f = newWideCString(file) else: diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim index 56d279db6b..264285d095 100644 --- a/lib/windows/winlean.nim +++ b/lib/windows/winlean.nim @@ -416,17 +416,17 @@ var SOMAXCONN* {.importc, header: "Winsock2.h".}: cint INVALID_SOCKET* {.importc, header: "Winsock2.h".}: TSocketHandle SOL_SOCKET* {.importc, header: "Winsock2.h".}: cint - SO_DEBUG* {.importc, header: "Winsock2.h".}: cint ## turn on debugging info recording - SO_ACCEPTCONN* {.importc, header: "Winsock2.h".}: cint # socket has had listen() - SO_REUSEADDR* {.importc, header: "Winsock2.h".}: cint # allow local address reuse - SO_KEEPALIVE* {.importc, header: "Winsock2.h".}: cint # keep connections alive - SO_DONTROUTE* {.importc, header: "Winsock2.h".}: cint # just use interface addresses - SO_BROADCAST* {.importc, header: "Winsock2.h".}: cint # permit sending of broadcast msgs - SO_USELOOPBACK* {.importc, header: "Winsock2.h".}: cint # bypass hardware when possible - SO_LINGER* {.importc, header: "Winsock2.h".}: cint # linger on close if data present - SO_OOBINLINE* {.importc, header: "Winsock2.h".}: cint # leave received OOB data in line - - SO_DONTLINGER* {.importc, header: "Winsock2.h".}: cint + SO_DEBUG* {.importc, header: "Winsock2.h".}: cint ## turn on debugging info recording + SO_ACCEPTCONN* {.importc, header: "Winsock2.h".}: cint # socket has had listen() + SO_REUSEADDR* {.importc, header: "Winsock2.h".}: cint # allow local address reuse + SO_KEEPALIVE* {.importc, header: "Winsock2.h".}: cint # keep connections alive + SO_DONTROUTE* {.importc, header: "Winsock2.h".}: cint # just use interface addresses + SO_BROADCAST* {.importc, header: "Winsock2.h".}: cint # permit sending of broadcast msgs + SO_USELOOPBACK* {.importc, header: "Winsock2.h".}: cint # bypass hardware when possible + SO_LINGER* {.importc, header: "Winsock2.h".}: cint # linger on close if data present + SO_OOBINLINE* {.importc, header: "Winsock2.h".}: cint # leave received OOB data in line + + SO_DONTLINGER* {.importc, header: "Winsock2.h".}: cint SO_EXCLUSIVEADDRUSE* {.importc, header: "Winsock2.h".}: cint # disallow local address reuse proc `==`*(x, y: TSocketHandle): bool {.borrow.} @@ -553,18 +553,26 @@ const FILE_FLAG_BACKUP_SEMANTICS* = 33554432'i32 +# Error Constants +const + ERROR_ACCESS_DENIED* = 5 + when useWinUnicode: proc CreateFileW*(lpFileName: widecstring, dwDesiredAccess, dwShareMode: DWORD, lpSecurityAttributes: pointer, dwCreationDisposition, dwFlagsAndAttributes: DWORD, hTemplateFile: THANDLE): THANDLE {. stdcall, dynlib: "kernel32", importc: "CreateFileW".} + proc DeleteFileW*(pathName: widecstring): int32 {. + importc: "DeleteFileW", dynlib: "kernel32", stdcall.} else: proc CreateFileA*(lpFileName: cstring, dwDesiredAccess, dwShareMode: DWORD, lpSecurityAttributes: pointer, dwCreationDisposition, dwFlagsAndAttributes: DWORD, hTemplateFile: THANDLE): THANDLE {. stdcall, dynlib: "kernel32", importc: "CreateFileA".} + proc DeleteFileA*(pathName: cstring): int32 {. + importc: "DeleteFileA", dynlib: "kernel32", stdcall.} proc SetEndOfFile*(hFile: THANDLE): WINBOOL {.stdcall, dynlib: "kernel32", importc: "SetEndOfFile".} From 4b0e391bb83ade40a049288d33bfff7aba2b0124 Mon Sep 17 00:00:00 2001 From: Araq <rumpf_a@web.de> Date: Fri, 20 Dec 2013 00:02:04 +0100 Subject: [PATCH 22/27] no stack tracing for the system module; fixes stack bottom detection --- compiler/cgen.nim | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index c143a5d6a5..6ccef5fde4 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1122,6 +1122,9 @@ proc newPostInitProc(m: BModule): BProc = # little hack so that unique temporaries are generated: result.labels = 200_000 +proc initProcOptions(m: BModule): TOptions = + if sfSystemModule in m.module.flags: gOptions-{optStackTrace} else: gOptions + proc rawNewModule(module: PSym, filename: string): BModule = new(result) InitLinkedList(result.headerFiles) @@ -1134,7 +1137,7 @@ proc rawNewModule(module: PSym, filename: string): BModule = result.module = module result.typeInfoMarker = initIntSet() result.initProc = newProc(nil, result) - result.initProc.options = gOptions + result.initProc.options = initProcOptions(result) result.preInitProc = newPreInitProc(result) result.postInitProc = newPostInitProc(result) initNodeTable(result.dataCache) @@ -1142,7 +1145,12 @@ proc rawNewModule(module: PSym, filename: string): BModule = result.forwardedProcs = @[] result.typeNodesName = getTempName() result.nimTypesName = getTempName() - result.PreventStackTrace = sfSystemModule in module.flags + # no line tracing for the init sections of the system module so that we + # don't generate a TFrame which can confuse the stack botton initialization: + if sfSystemModule in module.flags: + result.PreventStackTrace = true + excl(result.preInitProc.options, optStackTrace) + excl(result.postInitProc.options, optStackTrace) proc nullify[T](arr: var T) = for i in low(arr)..high(arr): @@ -1155,7 +1163,7 @@ proc resetModule*(m: var BModule) = m.declaredProtos = initIntSet() initIdTable(m.forwTypeCache) m.initProc = newProc(nil, m) - m.initProc.options = gOptions + m.initProc.options = initProcOptions(m) m.preInitProc = newPreInitProc(m) m.postInitProc = newPostInitProc(m) initNodeTable(m.dataCache) @@ -1245,7 +1253,7 @@ proc myProcess(b: PPassContext, n: PNode): PNode = result = n if b == nil or passes.skipCodegen(n): return var m = BModule(b) - m.initProc.options = gOptions + m.initProc.options = initProcOptions(m) genStmts(m.initProc, n) proc finishModule(m: BModule) = @@ -1332,7 +1340,7 @@ proc myClose(b: PPassContext, n: PNode): PNode = if b == nil or passes.skipCodegen(n): return var m = BModule(b) if n != nil: - m.initProc.options = gOptions + m.initProc.options = initProcOptions(m) genStmts(m.initProc, n) # cached modules need to registered too: registerModuleToMain(m.module) From 3b38ceac2e12c2b1bbe3876eb634000ddb2a9b68 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Fri, 20 Dec 2013 13:13:09 +0100 Subject: [PATCH 23/27] Runs user specified outFile if available. Refs #738. --- compiler/nimrod.nim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/nimrod.nim b/compiler/nimrod.nim index 8e3c0e61ee..2bc94e3f87 100644 --- a/compiler/nimrod.nim +++ b/compiler/nimrod.nim @@ -65,8 +65,14 @@ proc HandleCmdLine() = completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir)) execExternalProgram("node " & ex & ' ' & service.arguments) else: - var ex = quoteShell( - changeFileExt(gProjectFull, exeExt).prependCurDir) + var binPath: string + if options.outFile.len > 0: + # If the user specified an outFile path, use that directly. + binPath = options.outFile.prependCurDir + else: + # Figure out ourselves a valid binary name. + binPath = changeFileExt(gProjectFull, exeExt).prependCurDir + var ex = quoteShell(binPath) execExternalProgram(ex & ' ' & service.arguments) when defined(GC_setMaxPause): From c0e2cb29195c9d84781609689697a26892ea943c Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Fri, 20 Dec 2013 13:33:43 +0100 Subject: [PATCH 24/27] Allows passing absolute paths for output. Refs #738. Previous to this the project base dir was added always, preventing user specified absolute paths to work. --- compiler/extccomp.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim index fe1bea3fff..d3b3cee757 100644 --- a/compiler/extccomp.nim +++ b/compiler/extccomp.nim @@ -616,7 +616,8 @@ proc CallCCompiler*(projectfile: string) = if options.outFile.len > 0: exefile = options.outFile if not noAbsolutePaths(): - exefile = joinPath(splitFile(projectFile).dir, exefile) + if not exeFile.isAbsolute(): + exefile = joinPath(splitFile(projectFile).dir, exefile) exefile = quoteShell(exefile) let linkOptions = getLinkOptions() linkCmd = quoteShell(linkCmd % ["builddll", builddll, From f06c9a77b50ab4abfe57d9d9e679817acbea128b Mon Sep 17 00:00:00 2001 From: Billingsly Wetherfordshire <phowl.mouth@gmail.com> Date: Fri, 20 Dec 2013 14:23:13 -0600 Subject: [PATCH 25/27] Update macros.nim --- lib/core/macros.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index d01d4ebee4..d288260717 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -497,11 +497,11 @@ from strutils import cmpIgnoreStyle, format proc expectKind*(n: PNimrodNode; k: set[TNimrodNodeKind]) {.compileTime.} = assert n.kind in k, "Expected one of $1, got $2".format(k, n.kind) -proc newProc*(name = newEmptyNode(); params: openarray[PNimrodNode] = []; +proc newProc*(name = newEmptyNode(); params: openarray[PNimrodNode] = [newEmptyNode()]; body: PNimrodNode = newStmtList(), procType = nnkProcDef): PNimrodNode {.compileTime.} = ## shortcut for creating a new proc ## - ## The ``params`` array should start with the return type of the proc, + ## The ``params`` array must start with the return type of the proc, ## followed by a list of IdentDefs which specify the params. assert procType in RoutineNodes result = newNimNode(procType).add( From 7177e0f69877d9cde61a14e4df6a2478d35503e4 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Wed, 18 Dec 2013 23:32:39 +0100 Subject: [PATCH 26/27] Adds docstring to macros.newIdentDefs. --- lib/core/macros.nim | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 5f0d60e5c5..7caf4c5537 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -478,6 +478,34 @@ proc newDotExpr*(a, b: PNimrodNode): PNimrodNode {.compileTime.} = proc newIdentDefs*(name, kind: PNimrodNode; default = newEmptyNode()): PNimrodNode {.compileTime.} = + ## Creates a new ``nnkIdentDefs`` node of a specific kind and value. + ## + ## ``nnkIdentDefs`` need to have at least three children, but they can have + ## more: first comes a list of identifiers followed by a type and value + ## nodes. This helper proc creates a three node subtree, the first subnode + ## being a single identifier name. Both the ``kind`` node and ``default`` + ## (value) nodes may be empty depending on where the ``nnkIdentDefs`` + ## appears: tuple or object definitions will have an empty ``default`` node, + ## ``let`` or ``var`` blocks may have an empty ``kind`` node if the + ## identifier is being assigned a value. Example: + ## + ## .. code-block:: nimrod + ## + ## var varSection = newNimNode(nnkVarSection).add( + ## newIdentDefs(ident("a"), ident("string")), + ## newIdentDefs(ident("b"), newEmptyNode(), newLit(3))) + ## # --> var + ## # a: string + ## # b = 3 + ## + ## If you need to create multiple identifiers you need to use the lower level + ## ``newNimNode``: + ## + ## .. code-block:: nimrod + ## + ## result = newNimNode(nnkIdentDefs).add( + ## ident("a"), ident("b"), ident("c"), ident("string"), + ## newStrLitNode("Hello")) newNimNode(nnkIdentDefs).add(name, kind, default) proc newNilLit*(): PNimrodNode {.compileTime.} = From 7a05b108d52d7d4ea2c57daf57b82b9a5cb9c37b Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz <gradha@imap.cc> Date: Sat, 21 Dec 2013 11:44:17 +0100 Subject: [PATCH 27/27] Renames configuration file to new pattern. Refs #720. --- lib/pure/{actors.cfg => actors.nimrod.cfg} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/pure/{actors.cfg => actors.nimrod.cfg} (100%) diff --git a/lib/pure/actors.cfg b/lib/pure/actors.nimrod.cfg similarity index 100% rename from lib/pure/actors.cfg rename to lib/pure/actors.nimrod.cfg