Fix #149 - cOverride injecting multiple times
This commit is contained in:
parent
12cc08900d
commit
36726e2442
8 changed files with 79 additions and 69 deletions
|
|
@ -78,9 +78,9 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
||||||
nimState.nodeBranch.add $node.tsNodeType()
|
nimState.nodeBranch.add $node.tsNodeType()
|
||||||
echo "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1]
|
echo "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1]
|
||||||
|
|
||||||
if ast.children.len != 0:
|
if ast.children.nBl:
|
||||||
if childNames.contains(ast.regex) or
|
if childNames.contains(ast.regex) or
|
||||||
(childNames.len == 0 and ast.recursive):
|
(childNames.Bl and ast.recursive):
|
||||||
if node.getTSNodeNamedChildCountSansComments() != 0:
|
if node.getTSNodeNamedChildCountSansComments() != 0:
|
||||||
var flag = true
|
var flag = true
|
||||||
|
|
||||||
|
|
@ -110,7 +110,7 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
||||||
|
|
||||||
if nimState.gState.debug:
|
if nimState.gState.debug:
|
||||||
discard nimState.nodeBranch.pop()
|
discard nimState.nodeBranch.pop()
|
||||||
if nimstate.nodeBranch.len == 0:
|
if nimstate.nodeBranch.Bl:
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
||||||
|
|
@ -179,7 +179,7 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
|
||||||
nimState.impShort = nimState.currentHeader.replace("header", "imp")
|
nimState.impShort = nimState.currentHeader.replace("header", "imp")
|
||||||
nimState.sourceFile = fullpath
|
nimState.sourceFile = fullpath
|
||||||
|
|
||||||
if nimState.gState.dynlib.len == 0:
|
if nimState.gState.dynlib.Bl:
|
||||||
nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\""
|
nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\""
|
||||||
|
|
||||||
root.searchAst(astTable, nimState)
|
root.searchAst(astTable, nimState)
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ proc execAction*(cmd: string, retry = 0, nostderr = false): string =
|
||||||
var
|
var
|
||||||
colonIndex = cmd.find(":")
|
colonIndex = cmd.find(":")
|
||||||
driveLetter = cmd.substr(colonIndex-1, colonIndex)
|
driveLetter = cmd.substr(colonIndex-1, colonIndex)
|
||||||
if (driveLetter[0].isAlphaAscii() and
|
if (driveLetter[0].isAlphaAscii() and
|
||||||
driveLetter[1] == ':' and
|
driveLetter[1] == ':' and
|
||||||
colonIndex == 4):
|
colonIndex == 4):
|
||||||
filteredCmd = &"{driveLetter} && {cmd}"
|
filteredCmd = &"{driveLetter} && {cmd}"
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ proc walkDirImpl(indir, inext: string, file=true): seq[string] =
|
||||||
let
|
let
|
||||||
dir = joinPathIfRel(getProjectPath(), indir)
|
dir = joinPathIfRel(getProjectPath(), indir)
|
||||||
ext =
|
ext =
|
||||||
if inext.len != 0:
|
if inext.nBl:
|
||||||
when not defined(Windows):
|
when not defined(Windows):
|
||||||
"-name " & inext
|
"-name " & inext
|
||||||
else:
|
else:
|
||||||
|
|
@ -100,7 +100,7 @@ proc getToastError(output: string): string =
|
||||||
result &= "\n\nERROR:$1\n" % line.split("fatal error:")[1]
|
result &= "\n\nERROR:$1\n" % line.split("fatal error:")[1]
|
||||||
|
|
||||||
# Toast error
|
# Toast error
|
||||||
if result.len == 0:
|
if result.Bl:
|
||||||
result = "\n\n" & output
|
result = "\n\n" & output
|
||||||
|
|
||||||
proc getNimCheckError(output: string): tuple[tmpFile, errors: string] =
|
proc getNimCheckError(output: string): tuple[tmpFile, errors: string] =
|
||||||
|
|
@ -139,7 +139,7 @@ proc getToast(fullpath: string, recurse: bool = false, dynlib: string = "",
|
||||||
if recurse:
|
if recurse:
|
||||||
cmd.add " --recurse"
|
cmd.add " --recurse"
|
||||||
|
|
||||||
if flags.len != 0:
|
if flags.nBl:
|
||||||
cmd.add flags
|
cmd.add flags
|
||||||
|
|
||||||
for i in gStateCT.defines:
|
for i in gStateCT.defines:
|
||||||
|
|
@ -151,10 +151,10 @@ proc getToast(fullpath: string, recurse: bool = false, dynlib: string = "",
|
||||||
if not noNimout:
|
if not noNimout:
|
||||||
cmd.add &" --pnim"
|
cmd.add &" --pnim"
|
||||||
|
|
||||||
if dynlib.len != 0:
|
if dynlib.nBl:
|
||||||
cmd.add &" --dynlib={dynlib}"
|
cmd.add &" --dynlib={dynlib}"
|
||||||
|
|
||||||
if gStateCT.symOverride.len != 0:
|
if gStateCT.symOverride.nBl:
|
||||||
cmd.add &" --symOverride={gStateCT.symOverride.join(\",\")}"
|
cmd.add &" --symOverride={gStateCT.symOverride.join(\",\")}"
|
||||||
|
|
||||||
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
|
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
|
||||||
|
|
@ -222,7 +222,7 @@ macro cOverride*(body): untyped =
|
||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
if gStateCT.overrides.len == 0:
|
if gStateCT.overrides.Bl:
|
||||||
gStateCT.overrides = """
|
gStateCT.overrides = """
|
||||||
import sets, tables
|
import sets, tables
|
||||||
|
|
||||||
|
|
@ -230,7 +230,7 @@ proc onSymbolOverride*(sym: var Symbol) {.exportc, dynlib.} =
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# If cPlugin called before cOverride
|
# If cPlugin called before cOverride
|
||||||
if gStateCT.pluginSourcePath.len != 0:
|
if gStateCT.pluginSourcePath.nBl:
|
||||||
gStateCT.pluginSourcePath = ""
|
gStateCT.pluginSourcePath = ""
|
||||||
|
|
||||||
var
|
var
|
||||||
|
|
@ -257,7 +257,7 @@ proc onSymbolOverride*(sym: var Symbol) {.exportc, dynlib.} =
|
||||||
|
|
||||||
gStateCT.symOverride.add name
|
gStateCT.symOverride.add name
|
||||||
|
|
||||||
if gStateCT.debug and names.len != 0:
|
if gStateCT.debug and names.nBl:
|
||||||
echo "# Overriding " & names.join(" ")
|
echo "# Overriding " & names.join(" ")
|
||||||
|
|
||||||
proc cSkipSymbol*(skips: seq[string]) {.compileTime.} =
|
proc cSkipSymbol*(skips: seq[string]) {.compileTime.} =
|
||||||
|
|
@ -273,18 +273,19 @@ proc cSkipSymbol*(skips: seq[string]) {.compileTime.} =
|
||||||
proc cPluginHelper(body: string) =
|
proc cPluginHelper(body: string) =
|
||||||
gStateCT.pluginSource = body
|
gStateCT.pluginSource = body
|
||||||
|
|
||||||
let
|
if gStateCT.pluginSource.nBl or gStateCT.overrides.nBl:
|
||||||
data = "import macros, nimterop/plugin\n\n" & body & gStateCT.overrides
|
let
|
||||||
hash = data.hash().abs()
|
data = "import macros, nimterop/plugin\n\n" & body & gStateCT.overrides
|
||||||
path = getProjectCacheDir("cPlugins", forceClean = false) / "nimterop_" & $hash & ".nim"
|
hash = data.hash().abs()
|
||||||
|
path = getProjectCacheDir("cPlugins", forceClean = false) / "nimterop_" & $hash & ".nim"
|
||||||
|
|
||||||
if not fileExists(path) or gStateCT.nocache or compileOption("forceBuild"):
|
if not fileExists(path) or gStateCT.nocache or compileOption("forceBuild"):
|
||||||
mkDir(path.parentDir())
|
mkDir(path.parentDir())
|
||||||
writeFile(path, data)
|
writeFile(path, data)
|
||||||
|
|
||||||
doAssert fileExists(path), "Unable to write plugin file: " & path
|
doAssert fileExists(path), "Unable to write plugin file: " & path
|
||||||
|
|
||||||
gStateCT.pluginSourcePath = path
|
gStateCT.pluginSourcePath = path
|
||||||
|
|
||||||
macro cPlugin*(body): untyped =
|
macro cPlugin*(body): untyped =
|
||||||
## When `cOverride() <cimport.html#cOverride.m>`_ and
|
## When `cOverride() <cimport.html#cOverride.m>`_ and
|
||||||
|
|
@ -347,11 +348,11 @@ proc cSearchPath*(path: string): string {.compileTime.}=
|
||||||
## `cImport() <cimport.html#cImport.m%2C%2Cstring%2Cstring%2Cstring>`_.
|
## `cImport() <cimport.html#cImport.m%2C%2Cstring%2Cstring%2Cstring>`_.
|
||||||
|
|
||||||
result = findPath(path, fail = false)
|
result = findPath(path, fail = false)
|
||||||
if result.len == 0:
|
if result.Bl:
|
||||||
var found = false
|
var found = false
|
||||||
for inc in gStateCT.searchDirs:
|
for inc in gStateCT.searchDirs:
|
||||||
result = findPath(inc / path, fail = false)
|
result = findPath(inc / path, fail = false)
|
||||||
if result.len != 0:
|
if result.nBl:
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
doAssert found, "File or directory not found: " & path &
|
doAssert found, "File or directory not found: " & path &
|
||||||
|
|
@ -498,7 +499,7 @@ macro cCompile*(path: static string, mode = "c", exclude = ""): untyped =
|
||||||
result = true
|
result = true
|
||||||
if "_nimterop_" in file:
|
if "_nimterop_" in file:
|
||||||
result = false
|
result = false
|
||||||
elif exclude.len != 0:
|
elif exclude.nBl:
|
||||||
for excl in exclude.split(","):
|
for excl in exclude.split(","):
|
||||||
if excl in file:
|
if excl in file:
|
||||||
result = false
|
result = false
|
||||||
|
|
@ -508,7 +509,7 @@ macro cCompile*(path: static string, mode = "c", exclude = ""): untyped =
|
||||||
files = walkDirImpl(dir, ext)
|
files = walkDirImpl(dir, ext)
|
||||||
|
|
||||||
for f in files:
|
for f in files:
|
||||||
if f.len != 0 and f.notExcluded(exclude):
|
if f.nBl and f.notExcluded(exclude):
|
||||||
result &= fcompile(f)
|
result &= fcompile(f)
|
||||||
|
|
||||||
if path.contains("*") or path.contains("?"):
|
if path.contains("*") or path.contains("?"):
|
||||||
|
|
@ -576,7 +577,8 @@ macro cImport*(filename: static string, recurse: static bool = false, dynlib: st
|
||||||
let
|
let
|
||||||
fullpath = findPath(filename)
|
fullpath = findPath(filename)
|
||||||
|
|
||||||
if gStateCT.overrides.len != 0 and gStateCT.pluginSourcePath.len == 0:
|
# In case cOverride called after cPlugin
|
||||||
|
if gStateCT.pluginSourcePath.Bl:
|
||||||
cPluginHelper(gStateCT.pluginSource)
|
cPluginHelper(gStateCT.pluginSource)
|
||||||
|
|
||||||
echo "# Importing " & fullpath.sanitizePath
|
echo "# Importing " & fullpath.sanitizePath
|
||||||
|
|
@ -584,6 +586,10 @@ macro cImport*(filename: static string, recurse: static bool = false, dynlib: st
|
||||||
let
|
let
|
||||||
output = getToast(fullpath, recurse, dynlib, mode, flags)
|
output = getToast(fullpath, recurse, dynlib, mode, flags)
|
||||||
|
|
||||||
|
# Reset plugin and overrides for next cImport
|
||||||
|
gStateCT.pluginSourcePath = ""
|
||||||
|
gStateCT.overrides = ""
|
||||||
|
|
||||||
if gStateCT.debug:
|
if gStateCT.debug:
|
||||||
echo output
|
echo output
|
||||||
|
|
||||||
|
|
@ -641,11 +647,11 @@ macro c2nImport*(filename: static string, recurse: static bool = false, dynlib:
|
||||||
cmd = when defined(Windows): "cmd /c " else: ""
|
cmd = when defined(Windows): "cmd /c " else: ""
|
||||||
cmd &= &"c2nim {hpath} --header:{header}"
|
cmd &= &"c2nim {hpath} --header:{header}"
|
||||||
|
|
||||||
if dynlib.len != 0:
|
if dynlib.nBl:
|
||||||
cmd.add &" --dynlib:{dynlib}"
|
cmd.add &" --dynlib:{dynlib}"
|
||||||
if mode.contains("cpp"):
|
if mode.contains("cpp"):
|
||||||
cmd.add " --cpp"
|
cmd.add " --cpp"
|
||||||
if flags.len != 0:
|
if flags.nBl:
|
||||||
cmd.add &" {flags}"
|
cmd.add &" {flags}"
|
||||||
|
|
||||||
for i in gStateCT.defines:
|
for i in gStateCT.defines:
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ proc checkIdentifier(name, kind, parent, origName: string) =
|
||||||
let
|
let
|
||||||
parentStr = if parent.nBl: parent & ":" else: ""
|
parentStr = if parent.nBl: parent & ":" else: ""
|
||||||
|
|
||||||
if name.len != 0:
|
if name.nBl:
|
||||||
let
|
let
|
||||||
origStr = if name != origName: &", originally '{origName}' before 'cPlugin:onSymbol()', still" else: ""
|
origStr = if name != origName: &", originally '{origName}' before 'cPlugin:onSymbol()', still" else: ""
|
||||||
errmsg = &"Identifier '{parentStr}{name}' ({kind}){origStr} contains"
|
errmsg = &"Identifier '{parentStr}{name}' ({kind}){origStr} contains"
|
||||||
|
|
@ -108,7 +108,7 @@ proc checkIdentifier(name, kind, parent, origName: string) =
|
||||||
doAssert name.nBl, &"Blank identifier, originally '{parentStr}{name}' ({kind}), cannot be empty"
|
doAssert name.nBl, &"Blank identifier, originally '{parentStr}{name}' ({kind}), cannot be empty"
|
||||||
|
|
||||||
proc getIdentifier*(nimState: NimState, name: string, kind: NimSymKind, parent=""): string =
|
proc getIdentifier*(nimState: NimState, name: string, kind: NimSymKind, parent=""): string =
|
||||||
doAssert name.len != 0, "Blank identifier error"
|
doAssert name.nBl, "Blank identifier error"
|
||||||
|
|
||||||
if name notin nimState.gState.symOverride or parent.nBl:
|
if name notin nimState.gState.symOverride or parent.nBl:
|
||||||
if nimState.gState.onSymbol != nil:
|
if nimState.gState.onSymbol != nil:
|
||||||
|
|
@ -153,7 +153,7 @@ proc addNewIdentifer*(nimState: NimState, name: string, override = false): bool
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc getOverride*(nimState: NimState, name: string, kind: NimSymKind): string =
|
proc getOverride*(nimState: NimState, name: string, kind: NimSymKind): string =
|
||||||
doAssert name.len != 0, "Blank identifier error"
|
doAssert name.nBl, "Blank identifier error"
|
||||||
|
|
||||||
if nimState.gState.onSymbolOverride != nil:
|
if nimState.gState.onSymbolOverride != nil:
|
||||||
var
|
var
|
||||||
|
|
@ -162,7 +162,7 @@ proc getOverride*(nimState: NimState, name: string, kind: NimSymKind): string =
|
||||||
if nname.nBl:
|
if nname.nBl:
|
||||||
nimState.gState.onSymbolOverride(sym)
|
nimState.gState.onSymbolOverride(sym)
|
||||||
|
|
||||||
if sym.override.len != 0 and nimState.addNewIdentifer(nname, override = true):
|
if sym.override.nBl and nimState.addNewIdentifer(nname, override = true):
|
||||||
result = sym.override
|
result = sym.override
|
||||||
|
|
||||||
if kind != nskProc:
|
if kind != nskProc:
|
||||||
|
|
@ -257,7 +257,7 @@ proc getPreprocessor*(gState: State, fullpath: string, mode = "cpp"): string =
|
||||||
elif gState.recurse:
|
elif gState.recurse:
|
||||||
let
|
let
|
||||||
pDir = sfile.expandFilename().parentDir().sanitizePath(noQuote = true)
|
pDir = sfile.expandFilename().parentDir().sanitizePath(noQuote = true)
|
||||||
if pDir.len == 0 or pDir in saniLine:
|
if pDir.Bl or pDir in saniLine:
|
||||||
start = true
|
start = true
|
||||||
else:
|
else:
|
||||||
for inc in gState.includeDirs:
|
for inc in gState.includeDirs:
|
||||||
|
|
@ -368,10 +368,10 @@ proc getNimExpression*(nimState: NimState, expr: string): string =
|
||||||
|
|
||||||
for i in 0 .. clean.len:
|
for i in 0 .. clean.len:
|
||||||
if i != clean.len:
|
if i != clean.len:
|
||||||
if clean[i] == '_' and ident.len == 0:
|
if clean[i] == '_' and ident.Bl:
|
||||||
gen = $clean[i]
|
gen = $clean[i]
|
||||||
elif clean[i] in IdentChars:
|
elif clean[i] in IdentChars:
|
||||||
if clean[i] in Digits and ident.len == 0:
|
if clean[i] in Digits and ident.Bl:
|
||||||
gen = $clean[i]
|
gen = $clean[i]
|
||||||
elif clean[i] in HexDigits and hex == true:
|
elif clean[i] in HexDigits and hex == true:
|
||||||
gen = $clean[i]
|
gen = $clean[i]
|
||||||
|
|
@ -397,8 +397,8 @@ proc getNimExpression*(nimState: NimState, expr: string): string =
|
||||||
)
|
)
|
||||||
hex = false
|
hex = false
|
||||||
|
|
||||||
if i == clean.len or gen.len != 0:
|
if i == clean.len or gen.nBl:
|
||||||
if ident.len != 0:
|
if ident.nBl:
|
||||||
ident = nimState.getIdentifier(ident, nskConst)
|
ident = nimState.getIdentifier(ident, nskConst)
|
||||||
result &= ident
|
result &= ident
|
||||||
ident = ""
|
ident = ""
|
||||||
|
|
@ -415,14 +415,14 @@ proc getSplitComma*(joined: seq[string]): seq[string] =
|
||||||
|
|
||||||
proc getHeader*(nimState: NimState): string =
|
proc getHeader*(nimState: NimState): string =
|
||||||
result =
|
result =
|
||||||
if nimState.gState.dynlib.len == 0:
|
if nimState.gState.dynlib.Bl:
|
||||||
&", header: {nimState.currentHeader}"
|
&", header: {nimState.currentHeader}"
|
||||||
else:
|
else:
|
||||||
""
|
""
|
||||||
|
|
||||||
proc getDynlib*(nimState: NimState): string =
|
proc getDynlib*(nimState: NimState): string =
|
||||||
result =
|
result =
|
||||||
if nimState.gState.dynlib.len != 0:
|
if nimState.gState.dynlib.nBl:
|
||||||
&", dynlib: {nimState.gState.dynlib}"
|
&", dynlib: {nimState.gState.dynlib}"
|
||||||
else:
|
else:
|
||||||
""
|
""
|
||||||
|
|
@ -436,9 +436,9 @@ proc getImportC*(nimState: NimState, origName, nimName: string): string =
|
||||||
proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
||||||
result = ""
|
result = ""
|
||||||
for pragma in pragmas.items():
|
for pragma in pragmas.items():
|
||||||
if pragma.len != 0:
|
if pragma.nBl:
|
||||||
result &= pragma & ", "
|
result &= pragma & ", "
|
||||||
if result.len != 0:
|
if result.nBl:
|
||||||
result = " {." & result[0 .. ^3] & ".}"
|
result = " {." & result[0 .. ^3] & ".}"
|
||||||
|
|
||||||
result = result.replace(nimState.impShort & ", cdecl", nimState.impShort & "C")
|
result = result.replace(nimState.impShort & ", cdecl", nimState.impShort & "C")
|
||||||
|
|
@ -446,11 +446,11 @@ proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
||||||
let
|
let
|
||||||
dy = nimState.getDynlib()
|
dy = nimState.getDynlib()
|
||||||
|
|
||||||
if ", cdecl" in result and dy.len != 0:
|
if ", cdecl" in result and dy.nBl:
|
||||||
result = result.replace(".}", dy & ".}")
|
result = result.replace(".}", dy & ".}")
|
||||||
|
|
||||||
proc getComments*(nimState: NimState, strip = false): string =
|
proc getComments*(nimState: NimState, strip = false): string =
|
||||||
if not nimState.gState.nocomments and nimState.commentStr.len != 0:
|
if not nimState.gState.nocomments and nimState.commentStr.nBl:
|
||||||
result = "\n" & nimState.commentStr
|
result = "\n" & nimState.commentStr
|
||||||
if strip:
|
if strip:
|
||||||
result = result.replace("\n ", "\n")
|
result = result.replace("\n ", "\n")
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,9 @@ var
|
||||||
template nBl(s: typed): untyped {.used.} =
|
template nBl(s: typed): untyped {.used.} =
|
||||||
(s.len != 0)
|
(s.len != 0)
|
||||||
|
|
||||||
|
template Bl(s: typed): untyped {.used.} =
|
||||||
|
(s.len == 0)
|
||||||
|
|
||||||
type CompileMode = enum
|
type CompileMode = enum
|
||||||
c,
|
c,
|
||||||
cpp,
|
cpp,
|
||||||
|
|
@ -89,4 +92,5 @@ type CompileMode = enum
|
||||||
const modeDefault {.used.} = $cpp # TODO: USE this everywhere relevant
|
const modeDefault {.used.} = $cpp # TODO: USE this everywhere relevant
|
||||||
|
|
||||||
when not declared(CIMPORT):
|
when not declared(CIMPORT):
|
||||||
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, nBl, CompileMode, modeDefault
|
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState,
|
||||||
|
nBl, Bl, CompileMode, modeDefault
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ proc initGrammar(): Grammar =
|
||||||
if not nname.nBl:
|
if not nname.nBl:
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(name, nskConst)
|
override = nimState.getOverride(name, nskConst)
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.constStr &= &"{nimState.getComments()}\n{override}"
|
nimState.constStr &= &"{nimState.getComments()}\n{override}"
|
||||||
else:
|
else:
|
||||||
nimState.constStr &= &"{nimState.getComments()}\n # Const '{name}' skipped"
|
nimState.constStr &= &"{nimState.getComments()}\n # Const '{name}' skipped"
|
||||||
|
|
@ -123,7 +123,7 @@ proc initGrammar(): Grammar =
|
||||||
|
|
||||||
pout &= &"{pname}: array[{flen}, {getPtrType(pptr&ptyp)}], "
|
pout &= &"{pname}: array[{flen}, {getPtrType(pptr&ptyp)}], "
|
||||||
i += 1
|
i += 1
|
||||||
elif pptr.len != 0 or ptyp != "object":
|
elif pptr.nBl or ptyp != "object":
|
||||||
pout &= &"{pname}: {getPtrType(pptr&ptyp)}, "
|
pout &= &"{pname}: {getPtrType(pptr&ptyp)}, "
|
||||||
|
|
||||||
# typedef int X
|
# typedef int X
|
||||||
|
|
@ -176,7 +176,7 @@ proc initGrammar(): Grammar =
|
||||||
nname = nimState.getIdentifier(name, nskType)
|
nname = nimState.getIdentifier(name, nskType)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if nimState.gState.dynlib.len == 0:
|
if nimState.gState.dynlib.Bl:
|
||||||
pragmas.add nimState.getImportC(name, nname)
|
pragmas.add nimState.getImportC(name, nname)
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
@ -185,7 +185,7 @@ proc initGrammar(): Grammar =
|
||||||
if not nname.nBl:
|
if not nname.nBl:
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(name, nskType)
|
override = nimState.getOverride(name, nskType)
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||||
elif nname notin gTypeMap and typ.nBl and nimState.addNewIdentifer(nname):
|
elif nname notin gTypeMap and typ.nBl and nimState.addNewIdentifer(nname):
|
||||||
if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
|
if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
|
||||||
|
|
@ -201,10 +201,10 @@ proc initGrammar(): Grammar =
|
||||||
|
|
||||||
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
||||||
|
|
||||||
if pout.len != 0 and pout[^2 .. ^1] == ", ":
|
if pout.nBl and pout[^2 .. ^1] == ", ":
|
||||||
pout = pout[0 .. ^3]
|
pout = pout[0 .. ^3]
|
||||||
|
|
||||||
if tptr.len != 0 or typ != "object":
|
if tptr.nBl or typ != "object":
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.cdecl.}}"
|
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.cdecl.}}"
|
||||||
else:
|
else:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}) {{.cdecl.}}"
|
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}) {{.cdecl.}}"
|
||||||
|
|
@ -284,7 +284,7 @@ proc initGrammar(): Grammar =
|
||||||
if not nname.nBl:
|
if not nname.nBl:
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(name, nskType)
|
override = nimState.getOverride(name, nskType)
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||||
elif nimState.addNewIdentifer(nname):
|
elif nimState.addNewIdentifer(nname):
|
||||||
if nimState.data.len == 1:
|
if nimState.data.len == 1:
|
||||||
|
|
@ -292,10 +292,10 @@ proc initGrammar(): Grammar =
|
||||||
else:
|
else:
|
||||||
var
|
var
|
||||||
pragmas: seq[string] = @[]
|
pragmas: seq[string] = @[]
|
||||||
if nimState.gState.dynlib.len == 0:
|
if nimState.gState.dynlib.Bl:
|
||||||
pragmas.add nimState.getImportC(prefix & name, nname)
|
pragmas.add nimState.getImportC(prefix & name, nname)
|
||||||
pragmas.add "bycopy"
|
pragmas.add "bycopy"
|
||||||
if union.len != 0:
|
if union.nBl:
|
||||||
pragmas.add "union"
|
pragmas.add "union"
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
@ -357,9 +357,9 @@ proc initGrammar(): Grammar =
|
||||||
|
|
||||||
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
||||||
|
|
||||||
if pout.len != 0 and pout[^2 .. ^1] == ", ":
|
if pout.nBl and pout[^2 .. ^1] == ", ":
|
||||||
pout = pout[0 .. ^3]
|
pout = pout[0 .. ^3]
|
||||||
if fptr.len != 0 or ftyp != "object":
|
if fptr.nBl or ftyp != "object":
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.cdecl.}}"
|
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.cdecl.}}"
|
||||||
else:
|
else:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}) {{.cdecl.}}"
|
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}) {{.cdecl.}}"
|
||||||
|
|
@ -372,7 +372,7 @@ proc initGrammar(): Grammar =
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if node.tsNodeType() == "type_definition" and
|
if node.tsNodeType() == "type_definition" and
|
||||||
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.len != 0:
|
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.nBl:
|
||||||
pDupTypeCommon(nname, fend, nimState, false)
|
pDupTypeCommon(nname, fend, nimState, false)
|
||||||
|
|
||||||
let
|
let
|
||||||
|
|
@ -473,7 +473,7 @@ proc initGrammar(): Grammar =
|
||||||
nimState.debugStr &= "\n# pEnumCommon()"
|
nimState.debugStr &= "\n# pEnumCommon()"
|
||||||
|
|
||||||
let nname =
|
let nname =
|
||||||
if name.len == 0:
|
if name.Bl:
|
||||||
getUniqueIdentifier(nimState, "Enum")
|
getUniqueIdentifier(nimState, "Enum")
|
||||||
else:
|
else:
|
||||||
nimState.getIdentifier(name, nskType)
|
nimState.getIdentifier(name, nskType)
|
||||||
|
|
@ -508,7 +508,7 @@ proc initGrammar(): Grammar =
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
if node.tsNodeType() == "type_definition" and
|
if node.tsNodeType() == "type_definition" and
|
||||||
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.len != 0:
|
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.nBl:
|
||||||
pDupTypeCommon(nname, fend, nimState, true)
|
pDupTypeCommon(nname, fend, nimState, true)
|
||||||
|
|
||||||
# enum X {}
|
# enum X {}
|
||||||
|
|
@ -621,20 +621,20 @@ proc initGrammar(): Grammar =
|
||||||
|
|
||||||
funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i, flen)
|
funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i, flen)
|
||||||
|
|
||||||
if pout.len != 0 and pout[^2 .. ^1] == ", ":
|
if pout.nBl and pout[^2 .. ^1] == ", ":
|
||||||
pout = pout[0 .. ^3]
|
pout = pout[0 .. ^3]
|
||||||
|
|
||||||
if not fnname.nBl:
|
if not fnname.nBl:
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(fname, nskProc)
|
override = nimState.getOverride(fname, nskProc)
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||||
elif nimState.addNewIdentifer(fnname):
|
elif nimState.addNewIdentifer(fnname):
|
||||||
let
|
let
|
||||||
ftyp = nimState.getIdentifier(nimState.data[0].val, nskType, fnname).getType()
|
ftyp = nimState.getIdentifier(nimState.data[0].val, nskType, fnname).getType()
|
||||||
pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl")
|
pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl")
|
||||||
|
|
||||||
if fptr.len != 0 or ftyp != "object":
|
if fptr.nBl or ftyp != "object":
|
||||||
if fVar:
|
if fVar:
|
||||||
nimState.procStr &= &"{nimState.getComments(true)}\nvar {fnname}*: proc ({pout}): {getPtrType(fptr&ftyp)}{{.cdecl.}}"
|
nimState.procStr &= &"{nimState.getComments(true)}\nvar {fnname}*: proc ({pout}): {getPtrType(fptr&ftyp)}{{.cdecl.}}"
|
||||||
else:
|
else:
|
||||||
|
|
@ -678,7 +678,7 @@ proc initGrammar(): Grammar =
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(i.val, nskProc)
|
override = nimState.getOverride(i.val, nskProc)
|
||||||
|
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.procStr &= &"{nimState.getComments(true)}\n{override}"
|
nimState.procStr &= &"{nimState.getComments(true)}\n{override}"
|
||||||
done = true
|
done = true
|
||||||
break
|
break
|
||||||
|
|
@ -690,7 +690,7 @@ proc initGrammar(): Grammar =
|
||||||
let
|
let
|
||||||
override = nimState.getOverride(i.val, nskType)
|
override = nimState.getOverride(i.val, nskType)
|
||||||
|
|
||||||
if override.len != 0:
|
if override.nBl:
|
||||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||||
done = true
|
done = true
|
||||||
break
|
break
|
||||||
|
|
@ -702,7 +702,7 @@ proc initGrammar(): Grammar =
|
||||||
))
|
))
|
||||||
|
|
||||||
proc initRegex(ast: ref Ast) =
|
proc initRegex(ast: ref Ast) =
|
||||||
if ast.children.len != 0:
|
if ast.children.nBl:
|
||||||
if not ast.recursive:
|
if not ast.recursive:
|
||||||
for child in ast.children:
|
for child in ast.children:
|
||||||
child.initRegex()
|
child.initRegex()
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ proc readFromTokens(): ref Ast =
|
||||||
proc printAst*(node: ref Ast, offset=""): string =
|
proc printAst*(node: ref Ast, offset=""): string =
|
||||||
result = offset & "(" & (if node.recursive: "^" else: "") & node.name & node.kind.toString()
|
result = offset & "(" & (if node.recursive: "^" else: "") & node.name & node.kind.toString()
|
||||||
|
|
||||||
if node.children.len != 0 and not node.recursive:
|
if node.children.nBl and not node.recursive:
|
||||||
result &= "\n"
|
result &= "\n"
|
||||||
for child in node.children:
|
for child in node.children:
|
||||||
result &= printAst(child, offset & " ")
|
result &= printAst(child, offset & " ")
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ proc process(gState: State, path: string, astTable: AstTable) =
|
||||||
defer:
|
defer:
|
||||||
parser.tsParserDelete()
|
parser.tsParserDelete()
|
||||||
|
|
||||||
if gState.mode.len == 0:
|
if gState.mode.Bl:
|
||||||
if ext in [".h", ".c"]:
|
if ext in [".h", ".c"]:
|
||||||
gState.mode = "c"
|
gState.mode = "c"
|
||||||
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
|
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
|
||||||
|
|
@ -74,7 +74,7 @@ proc process(gState: State, path: string, astTable: AstTable) =
|
||||||
else:
|
else:
|
||||||
gState.code = readFile(path)
|
gState.code = readFile(path)
|
||||||
|
|
||||||
doAssert gState.code.len != 0, "Empty file or preprocessor error"
|
doAssert gState.code.nBl, "Empty file or preprocessor error"
|
||||||
|
|
||||||
if gState.mode == "c":
|
if gState.mode == "c":
|
||||||
doAssert parser.tsParserSetLanguage(treeSitterC()), "Failed to load C parser"
|
doAssert parser.tsParserSetLanguage(treeSitterC()), "Failed to load C parser"
|
||||||
|
|
@ -141,7 +141,7 @@ proc main(
|
||||||
astTable = parseGrammar()
|
astTable = parseGrammar()
|
||||||
if pgrammar:
|
if pgrammar:
|
||||||
astTable.printGrammar()
|
astTable.printGrammar()
|
||||||
elif source.len != 0:
|
elif source.nBl:
|
||||||
if gState.pnim:
|
if gState.pnim:
|
||||||
printNimHeader()
|
printNimHeader()
|
||||||
for src in source:
|
for src in source:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue