Minor fixes, improvements

This commit is contained in:
Ganesh Viswanathan 2020-07-02 00:10:16 -05:00
commit a63f67f685
5 changed files with 48 additions and 55 deletions

View file

@ -129,7 +129,7 @@ when defined(TOAST):
Status* = enum Status* = enum
success, unknown, error success, unknown, error
proc getCommented*(str: string): string = template getCommented*(str: string): string =
"\n# " & str.strip().replace("\n", "\n# ") "\n# " & str.strip().replace("\n", "\n# ")
# Redirect output to file when required # Redirect output to file when required
@ -147,13 +147,13 @@ template gecho*(args: string) =
template decho*(args: varargs[string, `$`]): untyped = template decho*(args: varargs[string, `$`]): untyped =
let let
str = join(args, "").getCommented() str = join(args, "")
when defined(TOAST): when defined(TOAST):
if gState.debug: if gState.debug:
gecho str gecho str.getCommented()
else: else:
if gStateCT.debug: if gStateCT.debug:
echo str echo str.getCommented()
template nBl*(s: typed): untyped {.used.} = template nBl*(s: typed): untyped {.used.} =
(s.len != 0) (s.len != 0)

View file

@ -34,10 +34,6 @@ src/*.cc
src/tree_sitter/parser.h src/tree_sitter/parser.h
""", "0.16.1") """, "0.16.1")
writeFile(cacheDir / "treesitter_c" / "src" / "api.h", """
const TSLanguage *tree_sitter_c();
""")
proc treesitterCppSetup*() = proc treesitterCppSetup*() =
gitPull("https://github.com/tree-sitter/tree-sitter-cpp", cacheDir / "treesitter_cpp", """ gitPull("https://github.com/tree-sitter/tree-sitter-cpp", cacheDir / "treesitter_cpp", """
src/*.h src/*.h
@ -45,7 +41,3 @@ src/*.c
src/*.cc src/*.cc
src/tree_sitter/parser.h src/tree_sitter/parser.h
""", "v0.16.0") """, "v0.16.0")
writeFile(cacheDir / "treesitter_cpp" / "src" / "api.h", """
const TSLanguage *tree_sitter_cpp();
""")

View file

@ -1,4 +1,4 @@
import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times import dynlib, macros, os, osproc, sequtils, sets, streams, strformat, strutils, tables, times
import regex import regex
@ -124,12 +124,12 @@ var
gTypeImport* = { gTypeImport* = {
"time_t": """ "time_t": """
import std/time_t as std_time_t import std/time_t as std_time_t
type time_t* = Time type time_t* = std_time_t.Time
""", """,
"time64_t": """ "time64_t": """
import std/time_t as std_time64_t import std/time_t as std_time64_t
type time64_t* = Time type time64_t* = std_time_t.Time
""", """,
"wchar_t": """ "wchar_t": """
@ -286,10 +286,7 @@ proc getCurrentHeader*(fullpath: string): string =
proc getPreprocessor*(gState: State, fullpath: string) = proc getPreprocessor*(gState: State, fullpath: string) =
var var
cmts = if gState.noComments: "" else: "-CC" args: seq[string]
cmd = &"""{getCompiler()} -E {cmts} -dD {getGccModeArg(gState.mode)} -w """
rdata: seq[string]
start = false start = false
sfile = fullpath.sanitizePath(noQuote = true) sfile = fullpath.sanitizePath(noQuote = true)
@ -297,50 +294,54 @@ proc getPreprocessor*(gState: State, fullpath: string) =
pDir = sfile.expandFilename().parentDir() pDir = sfile.expandFilename().parentDir()
includeDirs: seq[string] includeDirs: seq[string]
args.add @["-E", "-dD", getGccModeArg(gState.mode), "-w"]
if not gState.noComments:
args.add "-CC"
for inc in gState.includeDirs: for inc in gState.includeDirs:
cmd &= &"-I{inc.sanitizePath} " args.add &"-I{inc.sanitizePath}"
includeDirs.add inc.absolutePath().sanitizePath(noQuote = true) includeDirs.add inc.absolutePath().sanitizePath(noQuote = true)
for def in gState.defines: for def in gState.defines:
cmd &= &"-D{def} " args.add &"-D{def}"
# Remove gcc special calls # Remove gcc special calls
if defined(posix):
cmd &= "-D__attribute__\\(x\\)= "
else:
cmd &= "-D__attribute__(x)= "
cmd &= "-D__restrict= -D__extension__= -D__inline__=inline -D__inline=inline "
# https://github.com/tree-sitter/tree-sitter-c/issues/43 # https://github.com/tree-sitter/tree-sitter-c/issues/43
cmd &= "-D_Noreturn= " args.add @["-D__attribute__(x)=", "-D__restrict=", "-D__extension__=", "-D__inline__=inline",
"-D__inline=inline", "-D_Noreturn=", &"{fullpath.sanitizePath}"]
cmd &= &"{fullpath.sanitizePath}"
# Include content only from file # Include content only from file
for line in execAction(cmd).output.splitLines(): var
# We want to keep blank lines here for comment processing p = startProcess(getCompiler(), args = args, options = {poStdErrToStdOut, poUsePath})
if line.len > 1 and line[0 .. 1] == "# ": outp = p.outputStream()
start = false line = ""
let
saniLine = line.sanitizePath(noQuote = true) # Include content only from file
if sfile in saniLine or gState.code = ""
(DirSep notin saniLine and sfileName in saniLine): while true:
start = true if outp.readLine(line):
elif gState.recurse: # We want to keep blank lines here for comment processing
if pDir.Bl or pDir in saniLine: if line.len > 1 and line[0] == '#' and line[1] == ' ':
start = false
line = line.sanitizePath(noQuote = true)
if sfile in line or
(DirSep notin line and sfileName in line):
start = true start = true
else: elif gState.recurse:
for inc in includeDirs: if pDir.Bl or pDir in line:
if inc in saniLine: start = true
start = true else:
break for inc in includeDirs:
else: if inc in line:
if start: start = true
if "#undef" in line: break
continue else:
rdata.add line if start:
gState.code = rdata.join("\n") if "#undef" in line:
continue
gState.code.add line & "\n"
elif not p.running(): break
p.close()
# Plugin related # Plugin related

View file

@ -13,4 +13,4 @@ import "."/api
{.compile: srcDir / "parser.c".} {.compile: srcDir / "parser.c".}
proc treeSitterC*(): ptr TSLanguage {.importc: "tree_sitter_c", header: srcDir / "api.h".} proc treeSitterC*(): ptr TSLanguage {.importc: "tree_sitter_c".}

View file

@ -18,4 +18,4 @@ static:
{.compile: srcDir / "parser_cpp.c".} {.compile: srcDir / "parser_cpp.c".}
{.compile: srcDir / "scanner.cc".} {.compile: srcDir / "scanner.cc".}
proc treeSitterCpp*(): ptr TSLanguage {.importc: "tree_sitter_cpp", header: srcDir / "api.h".} proc treeSitterCpp*(): ptr TSLanguage {.importc: "tree_sitter_cpp".}