From ea6785500ad697bb13a546ecf29ecf52421bc633 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Thu, 19 Dec 2019 21:20:27 -0600 Subject: Fix #160 - toast should check wrapper --- README.md | 2 ++ nimterop/cimport.nim | 12 +++--------- nimterop/compat.nim | 2 ++ nimterop/toast.nim | 44 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 502688e..67689b3 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ Usage: Options(opt-arg sep :|=|spc): -h, --help print this cligen-erated help --help-syntax advanced: prepend,plurals,.. + -k, --check bool false check generated wrapper with compiler -d, --debug bool false enable debug output -D=, --defines= strings {} definitions to pass to preprocessor -l=, --dynlib= string "" Import symbols from library in specified Nim string @@ -103,6 +104,7 @@ Options(opt-arg sep :|=|spc): -m=, --mode= string "cpp" language parser: c or cpp --nim= string "nim" use a particular Nim executable (default: $PATH/nim) -c, --nocomments bool false exclude top-level comments from output + -o=, --output= string "" file to output content - default stdout -a, --past bool false print AST output -g, --pgrammar bool false print grammar --pluginSourcePath= string "" Nim file to build and load as a plugin diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index bebcf3d..c13ce1f 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -21,7 +21,7 @@ const CIMPORT {.used.} = 1 include "."/globals -import "."/[build, paths, types] +import "."/[build, compat, paths, types] export types proc interpPath(dir: string): string= @@ -116,12 +116,7 @@ proc getNimCheckError(output: string): tuple[tmpFile, errors: string] = doAssert fileExists(result.tmpFile), "Failed to write to cache dir: " & result.tmpFile let - nim = - when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9): - getCurrentCompilerExe() - else: - "nim" - (check, _) = gorgeEx(&"{nim} check {result.tmpFile.sanitizePath}") + (check, _) = gorgeEx(&"{getCurrentCompilerExe()} check {result.tmpFile.sanitizePath}") result.errors = "\n\n" & check @@ -157,8 +152,7 @@ proc getToast(fullpath: string, recurse: bool = false, dynlib: string = "", if gStateCT.symOverride.nBl: cmd.add &" --symOverride={gStateCT.symOverride.join(\",\")}" - when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9): - cmd.add &" --nim:{getCurrentCompilerExe().sanitizePath}" + cmd.add &" --nim:{getCurrentCompilerExe().sanitizePath}" if gStateCT.pluginSourcePath.nBl: cmd.add &" --pluginSourcePath={gStateCT.pluginSourcePath.sanitizePath}" diff --git a/nimterop/compat.nim b/nimterop/compat.nim index 1115252..7e2bc98 100644 --- a/nimterop/compat.nim +++ b/nimterop/compat.nim @@ -30,3 +30,5 @@ else: if not base.endsWith DirSep: base.add DirSep doAssert file.startsWith base result = file[base.len .. ^1] + + proc getCurrentCompilerExe*(): string = "nim" diff --git a/nimterop/toast.nim b/nimterop/toast.nim index d155263..614da83 100644 --- a/nimterop/toast.nim +++ b/nimterop/toast.nim @@ -1,8 +1,8 @@ -import os, strformat, strutils +import os, strformat, strutils, times import "."/treesitter/[api, c, cpp] -import "."/[ast, globals, getters, grammar] +import "."/[ast, compat, globals, getters, grammar] proc printLisp(gState: State, root: TSNode) = var @@ -99,6 +99,7 @@ proc process(gState: State, path: string, astTable: AstTable) = # CLI processing with default values proc main( + check = false, debug = false, defines: seq[string] = @[], dynlib: string = "", @@ -147,23 +148,57 @@ proc main( if pluginSourcePath.nBl: gState.loadPlugin(pluginSourcePath) - if output.len != 0: - doAssert reopen(stdout, output, fmWrite), "Failed to write to " & output + # Backup stdout + var + outputFile = output + outputHandle: File + stdoutBackup = stdout + + # Check needs a file + if check and outputFile.len == 0: + outputFile = getTempDir() / "toast_" & ($getTime().toUnix()).addFileExt("nim") + # Redirect output to file + if outputFile.len != 0: + doAssert outputHandle.open(outputFile, fmWrite), "Failed to write to " & outputFile + stdout = outputHandle + + # Process grammar into AST let astTable = parseGrammar() + if pgrammar: + # Print AST of grammar astTable.printGrammar() elif source.nBl: + # Print source after preprocess or Nim output if gState.pnim: printNimHeader() for src in source: gState.process(src.expandSymlinkAbs(), astTable) + # Restore stdout + stdout = stdoutBackup + + # Print wrapper if temporarily redirected to file + if check and output.len == 0: + stdout.write outputFile.readFile() + discard outputFile.tryRemoveFile() + + # Check Nim output + if gState.pnim and check: + var + (check, err) = gorgeEx(&"{getCurrentCompilerExe()} check {outputFile}") + if err == 0: + echo "# Checked wrapper successfully" + else: + doAssert err == 0, "# Nim check failed:\n\n" & check + when isMainModule: # Setup cligen command line help and short flags import cligen dispatch(main, help = { + "check": "check generated wrapper with compiler", "debug": "enable debug output", "defines": "definitions to pass to preprocessor", "dynlib": "Import symbols from library in specified Nim string", @@ -183,6 +218,7 @@ when isMainModule: "suffix": "Strip suffix from identifiers", "symOverride": "skip generating specified symbols" }, short = { + "check": 'k', "debug": 'd', "defines": 'D', "dynlib": 'l', -- cgit v1.2.3