aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-01-19 15:02:49 -0600
committerGanesh Viswanathan <dev@genotrance.com>2019-01-19 15:02:49 -0600
commit3d7682c61130101270828582e60ff87a858b3c82 (patch)
tree82a57878fa7ffa9f74b2befe60722af001c15ca6
parent1748279553e7a693e6bcf3aa5857cf7d65e7fc30 (diff)
downloadnimterop-3d7682c61130101270828582e60ff87a858b3c82.tar.gz
nimterop-3d7682c61130101270828582e60ff87a858b3c82.zip
Better error handling - fix #39, #47
-rw-r--r--nimterop/cimport.nim25
-rw-r--r--toast.nim13
2 files changed, 21 insertions, 17 deletions
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim
index 3d337c7..2bad012 100644
--- a/nimterop/cimport.nim
+++ b/nimterop/cimport.nim
@@ -18,10 +18,8 @@ proc findPath(path: string, fail = true): string =
# Relative to project path
result = joinPathIfRel(getProjectPath(), path).replace("\\", "/")
if not fileExists(result) and not dirExists(result):
- if fail:
- doAssert false, "File or directory not found: " & path
- else:
- return ""
+ doAssert not fail, "File or directory not found: " & path
+ result = ""
proc walkDirImpl(indir, inext: string, file=true): seq[string] =
let
@@ -66,11 +64,21 @@ proc getFileDate(fullpath: string): string =
(result, ret) = gorgeEx(cmd)
- if ret != 0:
- doAssert false, "File date error: " & fullpath & "\n" & result
+ doAssert ret == 0, "File date error: " & fullpath & "\n" & result
+
+proc getToastError(output: string): string =
+ # Filter out preprocessor errors
+ for line in output.splitLines():
+ if "fatal error:" in line.toLowerAscii:
+ result &= &"\nERROR: {line.split(\"fatal error\")[1]}\n"
+
+ # Toast error
+ if result.len == 0:
+ result = output
proc getToast(fullpath: string, recurse: bool = false): string =
var
+ ret = 0
cmd = when defined(Windows): "cmd /c " else: ""
cmd &= "toast --pnim --preprocess "
@@ -86,9 +94,8 @@ proc getToast(fullpath: string, recurse: bool = false): string =
cmd.add &"{fullpath.quoteShell}"
echo cmd
- var (output, exitCode) = gorgeEx(cmd, cache=getFileDate(fullpath))
- doAssert exitCode == 0, $exitCode
- result = output
+ (result, ret) = gorgeEx(cmd, cache=getFileDate(fullpath))
+ doAssert ret == 0, getToastError(result)
proc getGccPaths*(mode = "c"): string =
var
diff --git a/toast.nim b/toast.nim
index 712d1a7..7781369 100644
--- a/toast.nim
+++ b/toast.nim
@@ -78,17 +78,14 @@ proc process(path: string) =
else:
gStateRT.code = readFile(path)
+ doAssert gStateRT.code.len != 0, "Empty file or preprocessor error"
+
if gStateRT.mode == "c":
- if not parser.tsParserSetLanguage(treeSitterC()):
- echo "Failed to load C parser"
- quit()
+ doAssert parser.tsParserSetLanguage(treeSitterC()), "Failed to load C parser"
elif gStateRT.mode == "cpp":
- if not parser.tsParserSetLanguage(treeSitterCpp()):
- echo "Failed to load C++ parser"
- quit()
+ doAssert parser.tsParserSetLanguage(treeSitterCpp()), "Failed to load C++ parser"
else:
- echo "Invalid parser " & gStateRT.mode
- quit()
+ doAssert false, "Invalid parser " & gStateRT.mode
var
tree = parser.tsParserParseString(nil, gStateRT.code.cstring, gStateRT.code.len.uint32)