use cligen; fix bugs; some cleanups
This commit is contained in:
parent
691e7d80e5
commit
4a6973c14d
6 changed files with 62 additions and 54 deletions
|
|
@ -10,7 +10,7 @@ installDirs = @["nimterop"]
|
|||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 0.19.0", "treesitter >= 0.1.0", "treesitter_c >= 0.1.0", "treesitter_cpp >= 0.1.0", "regex >= 0.10.0"
|
||||
requires "nim >= 0.19.0", "treesitter >= 0.1.0", "treesitter_c >= 0.1.0", "treesitter_cpp >= 0.1.0", "regex >= 0.10.0", "cligen >= 0.9.17"
|
||||
|
||||
proc execCmd(cmd:string)=
|
||||
echo cmd
|
||||
|
|
|
|||
|
|
@ -26,15 +26,19 @@ proc findPath(path: string, fail = true): string =
|
|||
|
||||
proc getToast(fullpath: string): string =
|
||||
var
|
||||
cmd = "toast -n -p "
|
||||
cmd = "toast --pnim --preprocess "
|
||||
|
||||
for i in gStateCT.defines:
|
||||
cmd &= &"-D\"{i}\" "
|
||||
cmd.add &"--defines+={i.quoteShell} "
|
||||
|
||||
for i in gStateCT.includeDirs:
|
||||
cmd &= &"-I\"{i}\" "
|
||||
cmd.add &"--includeDirs+={i.quoteShell} "
|
||||
|
||||
result = staticExec(cmd & fullpath)
|
||||
cmd.add &"--source:{fullpath.quoteShell}"
|
||||
echo cmd
|
||||
var (output, exitCode) = gorgeEx(cmd)
|
||||
doAssert exitCode == 0, $exitCode
|
||||
result = output
|
||||
|
||||
proc cSearchPath*(path: string): string {.compileTime.}=
|
||||
result = findPath(path, fail = false)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import macros, ospaths, strformat, strutils
|
||||
import macros, os, strformat, strutils
|
||||
|
||||
import regex
|
||||
|
||||
|
|
|
|||
|
|
@ -12,5 +12,13 @@ var
|
|||
gStateCT* {.compiletime.}: State
|
||||
gStateRT*: State
|
||||
|
||||
template nBl*(s: untyped): untyped =
|
||||
(s.len != 0)
|
||||
template nBl*(s: typed): untyped =
|
||||
(s.len != 0)
|
||||
|
||||
|
||||
type CompileMode* = enum
|
||||
c,
|
||||
cpp,
|
||||
|
||||
# TODO: can cligen accept enum instead of string?
|
||||
const modeDefault* = $cpp # TODO: USE this everywhere relevant
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import strutils
|
||||
import strformat
|
||||
|
||||
import globals
|
||||
|
||||
|
|
@ -11,7 +12,11 @@ proc tokenize(fullpath: string) =
|
|||
|
||||
gTokens = @[]
|
||||
idx = 0
|
||||
for i in staticExec("toast -m " & fullpath):
|
||||
# TODO: consider calling API directly
|
||||
const cmd = &"toast --past --pretty:false --source:{fullpath.quoteShell}"
|
||||
var (output, exitCode) = gorgeEx cmd
|
||||
doAssert exitCode == 0, $exitCode
|
||||
for i in output:
|
||||
case i:
|
||||
of ' ', '\n', '\r', '(', ')':
|
||||
if collect.nBl:
|
||||
|
|
|
|||
81
toast.nim
81
toast.nim
|
|
@ -4,17 +4,6 @@ import treesitter/[runtime, c, cpp]
|
|||
|
||||
import nimterop/[ast, globals, getters]
|
||||
|
||||
const HELP = """
|
||||
> toast header.h
|
||||
-a print AST output
|
||||
-m print minimized AST output - non-pretty (implies -a)
|
||||
-n print Nim output
|
||||
|
||||
-c C mode - CPP is default
|
||||
-p run preprocessor on header
|
||||
-D definitions to pass to preprocessor
|
||||
-I include directory to pass to preprocessor"""
|
||||
|
||||
proc printLisp(root: TSNode) =
|
||||
var
|
||||
node = root
|
||||
|
|
@ -74,8 +63,8 @@ proc process(path: string) =
|
|||
defer:
|
||||
parser.tsParserDelete()
|
||||
|
||||
if gStateRT.mode.len != 0:
|
||||
gStateRT.mode = "cpp"
|
||||
if gStateRT.mode.len == 0:
|
||||
gStateRT.mode = modeDefault
|
||||
elif ext in [".h", ".c"]:
|
||||
gStateRT.mode = "c"
|
||||
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
|
||||
|
|
@ -110,37 +99,39 @@ proc process(path: string) =
|
|||
elif gStateRT.pnim:
|
||||
printNim(path, root)
|
||||
|
||||
proc parseCli() =
|
||||
var params = commandLineParams()
|
||||
proc main(
|
||||
mode = modeDefault,
|
||||
past = false,
|
||||
pnim = false,
|
||||
pretty = true,
|
||||
preprocess = false,
|
||||
defines: seq[string] = @[],
|
||||
includeDirs: seq[string] = @[],
|
||||
# defines.add(param[2..^1].strip(chars={'"'}))
|
||||
source: string,
|
||||
) =
|
||||
# TODO: should we add back `-m` param? meaning was: print minimized AST output - non-pretty (implies -a)
|
||||
|
||||
gStateRT.mode = "cpp"
|
||||
gStateRT.past = false
|
||||
gStateRT.pnim = false
|
||||
gStateRT.pretty = true
|
||||
gStateRT.preprocess = false
|
||||
gStateRT = State(
|
||||
mode: mode,
|
||||
past: past,
|
||||
pnim: pnim,
|
||||
pretty: pretty,
|
||||
preprocess: preprocess,
|
||||
# Note: was: strip(chars={'"'} but that seemed buggy (the shell should remove these already)
|
||||
defines: defines,
|
||||
includeDirs: includeDirs,
|
||||
)
|
||||
process(source)
|
||||
|
||||
for param in params:
|
||||
let flag = if param.len() <= 2: param else: param[0..<2]
|
||||
|
||||
if flag in ["-h", "-?"]:
|
||||
echo HELP
|
||||
quit()
|
||||
elif flag == "-a":
|
||||
gStateRT.past = true
|
||||
elif flag == "-c":
|
||||
gStateRT.mode = "c"
|
||||
elif flag == "-m":
|
||||
gStateRT.past = true
|
||||
gStateRT.pretty = false
|
||||
elif flag == "-n":
|
||||
gStateRT.pnim = true
|
||||
elif flag == "-p":
|
||||
gStateRT.preprocess = true
|
||||
elif flag == "-D":
|
||||
gStateRT.defines.add(param[2..^1].strip(chars={'"'}))
|
||||
elif flag == "-I":
|
||||
gStateRT.includeDirs.add(param[2..^1].strip(chars={'"'}))
|
||||
else:
|
||||
process(param)
|
||||
|
||||
parseCli()
|
||||
when isMainModule:
|
||||
import cligen
|
||||
dispatch(main, help = {
|
||||
"past": "print AST output",
|
||||
"mode": "language; see CompileMode", # TODO: auto-generate valid choices
|
||||
"pnim": "run preprocessor on header",
|
||||
"defines": "definitions to pass to preprocessor",
|
||||
"includeDirs": "include directory to pass to preprocessor",
|
||||
"preprocess": "print Nim output",
|
||||
"source" : "C/C++ source/header",
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue