Fix #160 - toast should check wrapper
This commit is contained in:
parent
cf372eb061
commit
ea6785500a
4 changed files with 47 additions and 13 deletions
|
|
@ -96,6 +96,7 @@ Usage:
|
||||||
Options(opt-arg sep :|=|spc):
|
Options(opt-arg sep :|=|spc):
|
||||||
-h, --help print this cligen-erated help
|
-h, --help print this cligen-erated help
|
||||||
--help-syntax advanced: prepend,plurals,..
|
--help-syntax advanced: prepend,plurals,..
|
||||||
|
-k, --check bool false check generated wrapper with compiler
|
||||||
-d, --debug bool false enable debug output
|
-d, --debug bool false enable debug output
|
||||||
-D=, --defines= strings {} definitions to pass to preprocessor
|
-D=, --defines= strings {} definitions to pass to preprocessor
|
||||||
-l=, --dynlib= string "" Import symbols from library in specified Nim string
|
-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
|
-m=, --mode= string "cpp" language parser: c or cpp
|
||||||
--nim= string "nim" use a particular Nim executable (default: $PATH/nim)
|
--nim= string "nim" use a particular Nim executable (default: $PATH/nim)
|
||||||
-c, --nocomments bool false exclude top-level comments from output
|
-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
|
-a, --past bool false print AST output
|
||||||
-g, --pgrammar bool false print grammar
|
-g, --pgrammar bool false print grammar
|
||||||
--pluginSourcePath= string "" Nim file to build and load as a plugin
|
--pluginSourcePath= string "" Nim file to build and load as a plugin
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ const CIMPORT {.used.} = 1
|
||||||
|
|
||||||
include "."/globals
|
include "."/globals
|
||||||
|
|
||||||
import "."/[build, paths, types]
|
import "."/[build, compat, paths, types]
|
||||||
export types
|
export types
|
||||||
|
|
||||||
proc interpPath(dir: string): string=
|
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
|
doAssert fileExists(result.tmpFile), "Failed to write to cache dir: " & result.tmpFile
|
||||||
|
|
||||||
let
|
let
|
||||||
nim =
|
(check, _) = gorgeEx(&"{getCurrentCompilerExe()} check {result.tmpFile.sanitizePath}")
|
||||||
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
|
|
||||||
getCurrentCompilerExe()
|
|
||||||
else:
|
|
||||||
"nim"
|
|
||||||
(check, _) = gorgeEx(&"{nim} check {result.tmpFile.sanitizePath}")
|
|
||||||
|
|
||||||
result.errors = "\n\n" & check
|
result.errors = "\n\n" & check
|
||||||
|
|
||||||
|
|
@ -157,7 +152,6 @@ proc getToast(fullpath: string, recurse: bool = false, dynlib: string = "",
|
||||||
if gStateCT.symOverride.nBl:
|
if gStateCT.symOverride.nBl:
|
||||||
cmd.add &" --symOverride={gStateCT.symOverride.join(\",\")}"
|
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:
|
if gStateCT.pluginSourcePath.nBl:
|
||||||
|
|
|
||||||
|
|
@ -30,3 +30,5 @@ else:
|
||||||
if not base.endsWith DirSep: base.add DirSep
|
if not base.endsWith DirSep: base.add DirSep
|
||||||
doAssert file.startsWith base
|
doAssert file.startsWith base
|
||||||
result = file[base.len .. ^1]
|
result = file[base.len .. ^1]
|
||||||
|
|
||||||
|
proc getCurrentCompilerExe*(): string = "nim"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import os, strformat, strutils
|
import os, strformat, strutils, times
|
||||||
|
|
||||||
import "."/treesitter/[api, c, cpp]
|
import "."/treesitter/[api, c, cpp]
|
||||||
|
|
||||||
import "."/[ast, globals, getters, grammar]
|
import "."/[ast, compat, globals, getters, grammar]
|
||||||
|
|
||||||
proc printLisp(gState: State, root: TSNode) =
|
proc printLisp(gState: State, root: TSNode) =
|
||||||
var
|
var
|
||||||
|
|
@ -99,6 +99,7 @@ proc process(gState: State, path: string, astTable: AstTable) =
|
||||||
|
|
||||||
# CLI processing with default values
|
# CLI processing with default values
|
||||||
proc main(
|
proc main(
|
||||||
|
check = false,
|
||||||
debug = false,
|
debug = false,
|
||||||
defines: seq[string] = @[],
|
defines: seq[string] = @[],
|
||||||
dynlib: string = "",
|
dynlib: string = "",
|
||||||
|
|
@ -147,23 +148,57 @@ proc main(
|
||||||
if pluginSourcePath.nBl:
|
if pluginSourcePath.nBl:
|
||||||
gState.loadPlugin(pluginSourcePath)
|
gState.loadPlugin(pluginSourcePath)
|
||||||
|
|
||||||
if output.len != 0:
|
# Backup stdout
|
||||||
doAssert reopen(stdout, output, fmWrite), "Failed to write to " & output
|
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
|
let
|
||||||
astTable = parseGrammar()
|
astTable = parseGrammar()
|
||||||
|
|
||||||
if pgrammar:
|
if pgrammar:
|
||||||
|
# Print AST of grammar
|
||||||
astTable.printGrammar()
|
astTable.printGrammar()
|
||||||
elif source.nBl:
|
elif source.nBl:
|
||||||
|
# Print source after preprocess or Nim output
|
||||||
if gState.pnim:
|
if gState.pnim:
|
||||||
printNimHeader()
|
printNimHeader()
|
||||||
for src in source:
|
for src in source:
|
||||||
gState.process(src.expandSymlinkAbs(), astTable)
|
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:
|
when isMainModule:
|
||||||
# Setup cligen command line help and short flags
|
# Setup cligen command line help and short flags
|
||||||
import cligen
|
import cligen
|
||||||
dispatch(main, help = {
|
dispatch(main, help = {
|
||||||
|
"check": "check generated wrapper with compiler",
|
||||||
"debug": "enable debug output",
|
"debug": "enable debug output",
|
||||||
"defines": "definitions to pass to preprocessor",
|
"defines": "definitions to pass to preprocessor",
|
||||||
"dynlib": "Import symbols from library in specified Nim string",
|
"dynlib": "Import symbols from library in specified Nim string",
|
||||||
|
|
@ -183,6 +218,7 @@ when isMainModule:
|
||||||
"suffix": "Strip suffix from identifiers",
|
"suffix": "Strip suffix from identifiers",
|
||||||
"symOverride": "skip generating specified symbols"
|
"symOverride": "skip generating specified symbols"
|
||||||
}, short = {
|
}, short = {
|
||||||
|
"check": 'k',
|
||||||
"debug": 'd',
|
"debug": 'd',
|
||||||
"defines": 'D',
|
"defines": 'D',
|
||||||
"dynlib": 'l',
|
"dynlib": 'l',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue