Get rid of nimState
This commit is contained in:
parent
01bc01a30c
commit
183eabff83
5 changed files with 595 additions and 611 deletions
156
nimterop/ast.nim
156
nimterop/ast.nim
|
|
@ -4,27 +4,27 @@ import regex
|
|||
|
||||
import "."/[getters, globals, treesitter/api]
|
||||
|
||||
proc getHeaderPragma*(nimState: NimState): string =
|
||||
proc getHeaderPragma*(gState: State): string =
|
||||
result =
|
||||
if nimState.includeHeader():
|
||||
&", header: {nimState.currentHeader}"
|
||||
if gState.isIncludeHeader():
|
||||
&", header: {gState.currentHeader}"
|
||||
else:
|
||||
""
|
||||
|
||||
proc getDynlib*(nimState: NimState): string =
|
||||
proc getDynlib*(gState: State): string =
|
||||
result =
|
||||
if nimState.gState.dynlib.nBl:
|
||||
&", dynlib: {nimState.gState.dynlib}"
|
||||
if gState.dynlib.nBl:
|
||||
&", dynlib: {gState.dynlib}"
|
||||
else:
|
||||
""
|
||||
|
||||
proc getImportC*(nimState: NimState, origName, nimName: string): string =
|
||||
proc getImportC*(gState: State, origName, nimName: string): string =
|
||||
if nimName != origName:
|
||||
result = &"importc: \"{origName}\"{nimState.getHeaderPragma()}"
|
||||
result = &"importc: \"{origName}\"{gState.getHeaderPragma()}"
|
||||
else:
|
||||
result = nimState.impShort
|
||||
result = gState.impShort
|
||||
|
||||
proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
||||
proc getPragma*(gState: State, pragmas: varargs[string]): string =
|
||||
result = ""
|
||||
for pragma in pragmas.items():
|
||||
if pragma.nBl:
|
||||
|
|
@ -32,15 +32,15 @@ proc getPragma*(nimState: NimState, pragmas: varargs[string]): string =
|
|||
if result.nBl:
|
||||
result = " {." & result[0 .. ^3] & ".}"
|
||||
|
||||
result = result.replace(nimState.impShort & ", cdecl", nimState.impShort & "C")
|
||||
result = result.replace(gState.impShort & ", cdecl", gState.impShort & "C")
|
||||
|
||||
let
|
||||
dy = nimState.getDynlib()
|
||||
dy = gState.getDynlib()
|
||||
|
||||
if ", cdecl" in result and dy.nBl:
|
||||
result = result.replace(".}", dy & ".}")
|
||||
|
||||
proc saveNodeData(node: TSNode, nimState: NimState): bool =
|
||||
proc saveNodeData(node: TSNode, gState: State): bool =
|
||||
let name = $node.tsNodeType()
|
||||
|
||||
# Atoms are nodes whose values are to be saved
|
||||
|
|
@ -52,7 +52,7 @@ proc saveNodeData(node: TSNode, nimState: NimState): bool =
|
|||
ppppname = node.getPxName(4)
|
||||
|
||||
var
|
||||
val = nimState.getNodeVal(node)
|
||||
val = gState.getNodeVal(node)
|
||||
|
||||
# Skip since value already obtained from parent atom
|
||||
if name == "primitive_type" and pname == "sized_type_specifier":
|
||||
|
|
@ -64,7 +64,7 @@ proc saveNodeData(node: TSNode, nimState: NimState): bool =
|
|||
|
||||
# Add reference point in saved data for bitfield_clause
|
||||
if name in ["number_literal"] and pname == "bitfield_clause":
|
||||
nimState.data.add(("bitfield_clause", val))
|
||||
gState.data.add(("bitfield_clause", val))
|
||||
return true
|
||||
|
||||
# Process value as a type
|
||||
|
|
@ -74,52 +74,52 @@ proc saveNodeData(node: TSNode, nimState: NimState): bool =
|
|||
if node.tsNodePrevNamedSibling().tsNodeIsNull():
|
||||
if pname == "pointer_declarator":
|
||||
if ppname notin ["function_declarator", "array_declarator"]:
|
||||
nimState.data.add(("pointer_declarator", ""))
|
||||
gState.data.add(("pointer_declarator", ""))
|
||||
elif ppname == "array_declarator":
|
||||
nimState.data.add(("array_pointer_declarator", ""))
|
||||
gState.data.add(("array_pointer_declarator", ""))
|
||||
|
||||
# Double pointer
|
||||
if ppname == "pointer_declarator":
|
||||
nimState.data.add(("pointer_declarator", ""))
|
||||
gState.data.add(("pointer_declarator", ""))
|
||||
elif pname in ["function_declarator", "array_declarator"]:
|
||||
if ppname == "pointer_declarator":
|
||||
nimState.data.add(("pointer_declarator", ""))
|
||||
gState.data.add(("pointer_declarator", ""))
|
||||
if pppname == "pointer_declarator":
|
||||
nimState.data.add(("pointer_declarator", ""))
|
||||
gState.data.add(("pointer_declarator", ""))
|
||||
|
||||
nimState.data.add((name, val))
|
||||
gState.data.add((name, val))
|
||||
|
||||
if pname == "pointer_declarator" and
|
||||
ppname == "function_declarator":
|
||||
if name == "field_identifier":
|
||||
if pppname == "pointer_declarator":
|
||||
nimState.data.insert(("pointer_declarator", ""), nimState.data.len-1)
|
||||
gState.data.insert(("pointer_declarator", ""), gState.data.len-1)
|
||||
if ppppname == "pointer_declarator":
|
||||
nimState.data.insert(("pointer_declarator", ""), nimState.data.len-1)
|
||||
nimState.data.add(("function_declarator", ""))
|
||||
gState.data.insert(("pointer_declarator", ""), gState.data.len-1)
|
||||
gState.data.add(("function_declarator", ""))
|
||||
elif name == "identifier":
|
||||
nimState.data.add(("pointer_declarator", ""))
|
||||
gState.data.add(("pointer_declarator", ""))
|
||||
|
||||
# Save node value for a top-level expression
|
||||
elif name in gExpressions and name != "escape_sequence":
|
||||
if $node.tsNodeParent.tsNodeType() notin gExpressions:
|
||||
nimState.data.add((name, nimState.getNodeVal(node)))
|
||||
gState.data.add((name, gState.getNodeVal(node)))
|
||||
|
||||
elif name in ["abstract_pointer_declarator", "enumerator", "field_declaration", "function_declarator"]:
|
||||
nimState.data.add((name.replace("abstract_", ""), ""))
|
||||
gState.data.add((name.replace("abstract_", ""), ""))
|
||||
|
||||
return true
|
||||
|
||||
proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
||||
proc searchAstForNode(ast: ref Ast, node: TSNode, gState: State): bool =
|
||||
let
|
||||
childNames = node.getTSNodeNamedChildNames().join()
|
||||
|
||||
if ast.isNil:
|
||||
return
|
||||
|
||||
if nimState.gState.debug:
|
||||
nimState.nodeBranch.add $node.tsNodeType()
|
||||
necho "#" & spaces(nimState.nodeBranch.len * 2) & nimState.nodeBranch[^1]
|
||||
if gState.debug:
|
||||
gState.nodeBranch.add $node.tsNodeType()
|
||||
gecho "#" & spaces(gState.nodeBranch.len * 2) & gState.nodeBranch[^1]
|
||||
|
||||
if ast.children.nBl:
|
||||
if childNames.contains(ast.regex) or
|
||||
|
|
@ -137,26 +137,26 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
|
|||
else:
|
||||
ast
|
||||
|
||||
if not searchAstForNode(astChild, nodeChild, nimState):
|
||||
if not searchAstForNode(astChild, nodeChild, gState):
|
||||
flag = false
|
||||
break
|
||||
|
||||
if flag:
|
||||
result = node.saveNodeData(nimState)
|
||||
result = node.saveNodeData(gState)
|
||||
else:
|
||||
result = node.saveNodeData(nimState)
|
||||
result = node.saveNodeData(gState)
|
||||
else:
|
||||
if nimState.gState.debug:
|
||||
necho "#" & spaces(nimState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}"
|
||||
if gState.debug:
|
||||
gecho "#" & spaces(gState.nodeBranch.len * 2) & &" {ast.getRegexForAstChildren()} !=~ {childNames}"
|
||||
elif node.getTSNodeNamedChildCountSansComments() == 0:
|
||||
result = node.saveNodeData(nimState)
|
||||
result = node.saveNodeData(gState)
|
||||
|
||||
if nimState.gState.debug:
|
||||
discard nimState.nodeBranch.pop()
|
||||
if nimstate.nodeBranch.Bl:
|
||||
necho ""
|
||||
if gState.debug:
|
||||
discard gState.nodeBranch.pop()
|
||||
if gState.nodeBranch.Bl:
|
||||
gecho ""
|
||||
|
||||
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
||||
proc searchAst(root: TSNode, astTable: AstTable, gState: State) =
|
||||
var
|
||||
node = root
|
||||
nextnode: TSNode
|
||||
|
|
@ -168,14 +168,14 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
|||
name = $node.tsNodeType()
|
||||
if name in astTable:
|
||||
for ast in astTable[name]:
|
||||
if nimState.gState.debug:
|
||||
necho "\n# " & nimState.getNodeVal(node).replace("\n", "\n# ") & "\n"
|
||||
if searchAstForNode(ast, node, nimState):
|
||||
ast.tonim(ast, node, nimState)
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# " & nimState.data.join("\n# ") & "\n"
|
||||
if gState.debug:
|
||||
gecho "\n# " & gState.getNodeVal(node).replace("\n", "\n# ") & "\n"
|
||||
if searchAstForNode(ast, node, gState):
|
||||
ast.tonim(ast, node, gState)
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# " & gState.data.join("\n# ") & "\n"
|
||||
break
|
||||
nimState.data = @[]
|
||||
gState.data = @[]
|
||||
else:
|
||||
break
|
||||
|
||||
|
|
@ -204,48 +204,46 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
|
|||
|
||||
proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable) =
|
||||
var
|
||||
nimState = new(NimState)
|
||||
fp = fullpath.replace("\\", "/")
|
||||
|
||||
nimState.identifiers = newTable[string, string]()
|
||||
gState.identifiers = newTable[string, string]()
|
||||
|
||||
nimState.gState = gState
|
||||
nimState.currentHeader = getCurrentHeader(fullpath)
|
||||
nimState.impShort = nimState.currentHeader.replace("header", "imp")
|
||||
nimState.sourceFile = fullpath
|
||||
gState.currentHeader = getCurrentHeader(fullpath)
|
||||
gState.impShort = gState.currentHeader.replace("header", "imp")
|
||||
gState.sourceFile = fullpath
|
||||
|
||||
if nimState.includeHeader():
|
||||
nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\""
|
||||
if gState.isIncludeHeader():
|
||||
gState.constStr &= &"\n {gState.currentHeader} {{.used.}} = \"{fp}\""
|
||||
|
||||
root.searchAst(astTable, nimState)
|
||||
root.searchAst(astTable, gState)
|
||||
|
||||
if nimState.enumStr.nBl:
|
||||
necho &"{nimState.enumStr}\n"
|
||||
if gState.enumStr.nBl:
|
||||
gecho &"{gState.enumStr}\n"
|
||||
|
||||
nimState.constStr = nimState.getOverrideFinal(nskConst) & nimState.constStr
|
||||
if nimState.constStr.nBl:
|
||||
necho &"const{nimState.constStr}\n"
|
||||
gState.constStr = gState.getOverrideFinal(nskConst) & gState.constStr
|
||||
if gState.constStr.nBl:
|
||||
gecho &"const{gState.constStr}\n"
|
||||
|
||||
necho &"""
|
||||
{{.pragma: {nimState.impShort}, importc{nimState.getHeaderPragma()}.}}
|
||||
{{.pragma: {nimState.impShort}C, {nimState.impShort}, cdecl{nimState.getDynlib()}.}}
|
||||
gecho &"""
|
||||
{{.pragma: {gState.impShort}, importc{gState.getHeaderPragma()}.}}
|
||||
{{.pragma: {gState.impShort}C, {gState.impShort}, cdecl{gState.getDynlib()}.}}
|
||||
"""
|
||||
|
||||
nimState.typeStr = nimState.getOverrideFinal(nskType) & nimState.typeStr
|
||||
if nimState.typeStr.nBl:
|
||||
necho &"type{nimState.typeStr}\n"
|
||||
gState.typeStr = gState.getOverrideFinal(nskType) & gState.typeStr
|
||||
if gState.typeStr.nBl:
|
||||
gecho &"type{gState.typeStr}\n"
|
||||
|
||||
nimState.procStr = nimState.getOverrideFinal(nskProc) & nimState.procStr
|
||||
if nimState.procStr.nBl:
|
||||
necho &"{nimState.procStr}\n"
|
||||
gState.procStr = gState.getOverrideFinal(nskProc) & gState.procStr
|
||||
if gState.procStr.nBl:
|
||||
gecho &"{gState.procStr}\n"
|
||||
|
||||
if nimState.gState.debug:
|
||||
if nimState.debugStr.nBl:
|
||||
necho nimState.debugStr
|
||||
if gState.debug:
|
||||
if gState.debugStr.nBl:
|
||||
gecho gState.debugStr
|
||||
|
||||
if nimState.skipStr.nBl:
|
||||
if gState.skipStr.nBl:
|
||||
let
|
||||
hash = nimState.skipStr.hash().abs()
|
||||
hash = gState.skipStr.hash().abs()
|
||||
sname = getTempDir() / &"nimterop_{$hash}.h"
|
||||
necho &"# Writing skipped definitions to {sname}\n"
|
||||
writeFile(sname, nimState.skipStr)
|
||||
gecho &"# Writing skipped definitions to {sname}\n"
|
||||
writeFile(sname, gState.skipStr)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -113,32 +113,32 @@ proc checkIdentifier(name, kind, parent, origName: string) =
|
|||
if parent.nBl:
|
||||
doAssert name.nBl, &"Blank identifier, originally '{parentStr}{origName}' ({kind}), cannot be empty"
|
||||
|
||||
proc getIdentifier*(nimState: NimState, name: string, kind: NimSymKind, parent=""): string =
|
||||
proc getIdentifier*(gState: State, name: string, kind: NimSymKind, parent=""): string =
|
||||
doAssert name.nBl, "Blank identifier error"
|
||||
|
||||
if name notin nimState.gState.symOverride or parent.nBl:
|
||||
if nimState.gState.onSymbol != nil:
|
||||
if name notin gState.symOverride or parent.nBl:
|
||||
if gState.onSymbol != nil:
|
||||
# Use onSymbol from plugin provided by user
|
||||
var
|
||||
sym = Symbol(name: name, parent: parent, kind: kind)
|
||||
nimState.gState.onSymbol(sym)
|
||||
gState.onSymbol(sym)
|
||||
|
||||
result = sym.name
|
||||
else:
|
||||
result = name
|
||||
|
||||
# Strip out --prefix from CLI if specified
|
||||
for str in nimState.gState.prefix:
|
||||
for str in gState.prefix:
|
||||
if result.startsWith(str):
|
||||
result = result[str.len .. ^1]
|
||||
|
||||
# Strip out --suffix from CLI if specified
|
||||
for str in nimState.gState.suffix:
|
||||
for str in gState.suffix:
|
||||
if result.endsWith(str):
|
||||
result = result[0 .. ^(str.len+1)]
|
||||
|
||||
# --replace from CLI if specified
|
||||
for name, value in nimState.gState.replace.pairs:
|
||||
for name, value in gState.replace.pairs:
|
||||
if name.len > 1 and name[0] == '@':
|
||||
result = result.replace(re(name[1 .. ^1]), value)
|
||||
else:
|
||||
|
|
@ -153,58 +153,58 @@ proc getIdentifier*(nimState: NimState, name: string, kind: NimSymKind, parent="
|
|||
# Skip identifier since in symOverride
|
||||
result = ""
|
||||
|
||||
proc getUniqueIdentifier*(nimState: NimState, prefix = ""): string =
|
||||
proc getUniqueIdentifier*(gState: State, prefix = ""): string =
|
||||
var
|
||||
name = prefix & "_" & nimState.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
|
||||
name = prefix & "_" & gState.sourceFile.extractFilename().multiReplace([(".", ""), ("-", "")])
|
||||
nimName = name[0] & name[1 .. ^1].replace("_", "").toLowerAscii
|
||||
count = 1
|
||||
|
||||
while (nimName & $count) in nimState.identifiers:
|
||||
while (nimName & $count) in gState.identifiers:
|
||||
count += 1
|
||||
|
||||
return name & $count
|
||||
|
||||
proc addNewIdentifer*(nimState: NimState, name: string, override = false): bool =
|
||||
if override or name notin nimState.gState.symOverride:
|
||||
proc addNewIdentifer*(gState: State, name: string, override = false): bool =
|
||||
if override or name notin gState.symOverride:
|
||||
let
|
||||
nimName = name[0] & name[1 .. ^1].replace("_", "").toLowerAscii
|
||||
|
||||
if nimState.identifiers.hasKey(nimName):
|
||||
doAssert name == nimState.identifiers[nimName],
|
||||
if gState.identifiers.hasKey(nimName):
|
||||
doAssert name == gState.identifiers[nimName],
|
||||
&"Identifier '{name}' is a stylistic duplicate of identifier " &
|
||||
&"'{nimState.identifiers[nimName]}', use 'cPlugin:onSymbol()' to rename"
|
||||
&"'{gState.identifiers[nimName]}', use 'cPlugin:onSymbol()' to rename"
|
||||
result = false
|
||||
else:
|
||||
nimState.identifiers[nimName] = name
|
||||
gState.identifiers[nimName] = name
|
||||
result = true
|
||||
|
||||
# Overrides related
|
||||
|
||||
proc getOverride*(nimState: NimState, name: string, kind: NimSymKind): string =
|
||||
proc getOverride*(gState: State, name: string, kind: NimSymKind): string =
|
||||
# Get cOverride for identifier `name` of `kind` if defined
|
||||
doAssert name.nBl, "Blank identifier error"
|
||||
|
||||
if nimState.gState.onSymbolOverride != nil:
|
||||
if gState.onSymbolOverride != nil:
|
||||
var
|
||||
nname = nimState.getIdentifier(name, kind, "Override")
|
||||
nname = gState.getIdentifier(name, kind, "Override")
|
||||
sym = Symbol(name: nname, kind: kind)
|
||||
if nname.nBl:
|
||||
nimState.gState.onSymbolOverride(sym)
|
||||
gState.onSymbolOverride(sym)
|
||||
|
||||
if sym.override.nBl and nimState.addNewIdentifer(nname, override = true):
|
||||
if sym.override.nBl and gState.addNewIdentifer(nname, override = true):
|
||||
result = sym.override
|
||||
|
||||
if kind != nskProc:
|
||||
result = result.replace(re"(?m)^(.*?)$", " $1")
|
||||
|
||||
proc getOverrideFinal*(nimState: NimState, kind: NimSymKind): string =
|
||||
proc getOverrideFinal*(gState: State, kind: NimSymKind): string =
|
||||
# Get all unused cOverride symbols of `kind`
|
||||
let
|
||||
typ = $kind
|
||||
|
||||
if nimState.gState.onSymbolOverrideFinal != nil:
|
||||
for i in nimState.gState.onSymbolOverrideFinal(typ):
|
||||
result &= "\n" & nimState.getOverride(i, kind)
|
||||
if gState.onSymbolOverrideFinal != nil:
|
||||
for i in gState.onSymbolOverrideFinal(typ):
|
||||
result &= "\n" & gState.getOverride(i, kind)
|
||||
|
||||
proc getKeyword*(kind: NimSymKind): string =
|
||||
# Convert `kind` into a Nim keyword
|
||||
|
|
@ -232,9 +232,6 @@ proc getNodeVal*(gState: State, node: TSNode): string =
|
|||
if not node.isNil:
|
||||
return gState.code[node.tsNodeStartByte() .. node.tsNodeEndByte()-1].strip()
|
||||
|
||||
proc getNodeVal*(nimState: NimState, node: TSNode): string =
|
||||
nimState.gState.getNodeVal(node)
|
||||
|
||||
proc getAtom*(node: TSNode): TSNode =
|
||||
if not node.isNil:
|
||||
# Get child node which is topmost atom
|
||||
|
|
@ -428,8 +425,8 @@ proc printLisp*(gState: State, root: TSNode): string =
|
|||
proc getCommented*(str: string): string =
|
||||
"\n# " & str.strip().replace("\n", "\n# ")
|
||||
|
||||
proc printTree*(nimState: NimState, pnode: PNode, offset = ""): string =
|
||||
if nimState.gState.debug and pnode.kind != nkNone:
|
||||
proc printTree*(gState: State, pnode: PNode, offset = ""): string =
|
||||
if gState.debug and pnode.kind != nkNone:
|
||||
result &= "\n# " & offset & $pnode.kind & "("
|
||||
case pnode.kind
|
||||
of nkCharLit:
|
||||
|
|
@ -447,7 +444,7 @@ proc printTree*(nimState: NimState, pnode: PNode, offset = ""): string =
|
|||
else:
|
||||
if pnode.sons.len != 0:
|
||||
for i in 0 ..< pnode.sons.len:
|
||||
result &= nimState.printTree(pnode.sons[i], offset & " ")
|
||||
result &= gState.printTree(pnode.sons[i], offset & " ")
|
||||
if i != pnode.sons.len - 1:
|
||||
result &= ","
|
||||
result &= "\n# " & offset & ")"
|
||||
|
|
@ -456,34 +453,34 @@ proc printTree*(nimState: NimState, pnode: PNode, offset = ""): string =
|
|||
if offset.len == 0:
|
||||
result &= "\n"
|
||||
|
||||
proc printDebug*(nimState: NimState, node: TSNode) =
|
||||
if nimState.gState.debug:
|
||||
necho ("Input => " & nimState.getNodeVal(node)).getCommented() & "\n" &
|
||||
nimState.gState.printLisp(node).getCommented()
|
||||
proc printDebug*(gState: State, node: TSNode) =
|
||||
if gState.debug:
|
||||
gecho ("Input => " & gState.getNodeVal(node)).getCommented() & "\n" &
|
||||
gState.printLisp(node).getCommented()
|
||||
|
||||
proc printDebug*(nimState: NimState, pnode: PNode) =
|
||||
if nimState.gState.debug:
|
||||
necho ("Output => " & $pnode).getCommented() & "\n" &
|
||||
nimState.printTree(pnode)
|
||||
proc printDebug*(gState: State, pnode: PNode) =
|
||||
if gState.debug:
|
||||
gecho ("Output => " & $pnode).getCommented() & "\n" &
|
||||
gState.printTree(pnode)
|
||||
|
||||
# Compiler shortcuts
|
||||
|
||||
proc getDefaultLineInfo*(nimState: NimState): TLineInfo =
|
||||
result = newLineInfo(nimState.config, nimState.sourceFile.AbsoluteFile, 0, 0)
|
||||
proc getDefaultLineInfo*(gState: State): TLineInfo =
|
||||
result = newLineInfo(gState.config, gState.sourceFile.AbsoluteFile, 0, 0)
|
||||
|
||||
proc getLineInfo*(nimState: NimState, node: TSNode): TLineInfo =
|
||||
proc getLineInfo*(gState: State, node: TSNode): TLineInfo =
|
||||
# Get Nim equivalent line:col info from node
|
||||
let
|
||||
(line, col) = nimState.gState.getLineCol(node)
|
||||
(line, col) = gState.getLineCol(node)
|
||||
|
||||
result = newLineInfo(nimState.config, nimState.sourceFile.AbsoluteFile, line, col)
|
||||
result = newLineInfo(gState.config, gState.sourceFile.AbsoluteFile, line, col)
|
||||
|
||||
proc getIdent*(nimState: NimState, name: string, info: TLineInfo, exported = true): PNode =
|
||||
proc getIdent*(gState: State, name: string, info: TLineInfo, exported = true): PNode =
|
||||
if name.nBl:
|
||||
# Get ident PNode for name + info
|
||||
let
|
||||
exp = getIdent(nimState.identCache, "*")
|
||||
ident = getIdent(nimState.identCache, name)
|
||||
exp = getIdent(gState.identCache, "*")
|
||||
ident = getIdent(gState.identCache, name)
|
||||
|
||||
if exported:
|
||||
result = newNode(nkPostfix)
|
||||
|
|
@ -492,8 +489,8 @@ proc getIdent*(nimState: NimState, name: string, info: TLineInfo, exported = tru
|
|||
else:
|
||||
result = newIdentNode(ident, info)
|
||||
|
||||
proc getIdent*(nimState: NimState, name: string): PNode =
|
||||
nimState.getIdent(name, nimState.getDefaultLineInfo(), exported = false)
|
||||
proc getIdent*(gState: State, name: string): PNode =
|
||||
gState.getIdent(name, gState.getDefaultLineInfo(), exported = false)
|
||||
|
||||
proc getIdentName*(node: PNode): string =
|
||||
if not node.isNil:
|
||||
|
|
@ -503,15 +500,15 @@ proc getIdentName*(node: PNode): string =
|
|||
if result.Bl and node.len > 0:
|
||||
result = node[0].getIdentName()
|
||||
|
||||
proc getNameInfo*(nimState: NimState, node: TSNode, kind: NimSymKind, parent = ""):
|
||||
proc getNameInfo*(gState: State, node: TSNode, kind: NimSymKind, parent = ""):
|
||||
tuple[name, origname: string, info: TLineInfo] =
|
||||
# Shortcut to get identifier name and info (node value and line:col)
|
||||
result.origname = nimState.getNodeVal(node)
|
||||
result.name = nimState.getIdentifier(result.origname, kind, parent)
|
||||
result.origname = gState.getNodeVal(node)
|
||||
result.name = gState.getIdentifier(result.origname, kind, parent)
|
||||
if result.name.nBl:
|
||||
if kind == nskType:
|
||||
result.name = result.name.getType()
|
||||
result.info = nimState.getLineInfo(node)
|
||||
result.info = gState.getLineInfo(node)
|
||||
|
||||
proc getCurrentHeader*(fullpath: string): string =
|
||||
("header" & fullpath.splitFile().name.multiReplace([(".", ""), ("-", "")]))
|
||||
|
|
@ -644,7 +641,7 @@ proc getAstChildByName*(ast: ref Ast, name: string): ref Ast =
|
|||
if ast.children.len == 1 and ast.children[0].name == ".":
|
||||
return ast.children[0]
|
||||
|
||||
proc getNimExpression*(nimState: NimState, expr: string, name = ""): string =
|
||||
proc getNimExpression*(gState: State, expr: string, name = ""): string =
|
||||
# Convert C/C++ expression into Nim - cast identifiers to `name` if specified
|
||||
var
|
||||
clean = expr.multiReplace([("\n", " "), ("\r", "")])
|
||||
|
|
@ -691,8 +688,8 @@ proc getNimExpression*(nimState: NimState, expr: string, name = ""): string =
|
|||
if ident.nBl:
|
||||
# Issue #178
|
||||
if ident != "_":
|
||||
ident = nimState.getIdentifier(ident, nskConst, name)
|
||||
if name.nBl and ident in nimState.constIdentifiers:
|
||||
ident = gState.getIdentifier(ident, nskConst, name)
|
||||
if name.nBl and ident in gState.constIdentifiers:
|
||||
ident = ident & "." & name
|
||||
result &= ident
|
||||
ident = ""
|
||||
|
|
@ -708,15 +705,15 @@ proc getSplitComma*(joined: seq[string]): seq[string] =
|
|||
for i in joined:
|
||||
result = result.concat(i.split(","))
|
||||
|
||||
template includeHeader*(nimState: NimState): bool =
|
||||
nimState.gState.dynlib.Bl and nimState.gState.includeHeader
|
||||
template isIncludeHeader*(gState: State): bool =
|
||||
gState.dynlib.Bl and gState.includeHeader
|
||||
|
||||
proc getComments*(nimState: NimState, strip = false): string =
|
||||
if not nimState.gState.nocomments and nimState.commentStr.nBl:
|
||||
result = "\n" & nimState.commentStr
|
||||
proc getComments*(gState: State, strip = false): string =
|
||||
if not gState.nocomments and gState.commentStr.nBl:
|
||||
result = "\n" & gState.commentStr
|
||||
if strip:
|
||||
result = result.replace("\n ", "\n")
|
||||
nimState.commentStr = ""
|
||||
gState.commentStr = ""
|
||||
|
||||
proc dll*(path: string): string =
|
||||
let
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ type
|
|||
recursive*: bool
|
||||
children*: seq[ref Ast]
|
||||
when not declared(CIMPORT):
|
||||
tonim*: proc (ast: ref Ast, node: TSNode, nimState: NimState)
|
||||
tonim*: proc (ast: ref Ast, node: TSNode, gState: State)
|
||||
regex*: Regex
|
||||
|
||||
AstTable {.used.} = TableRef[string, seq[ref Ast]]
|
||||
|
|
@ -70,7 +70,6 @@ type
|
|||
|
||||
outputHandle*: File
|
||||
|
||||
NimState {.used.} = ref object
|
||||
# All symbols that have been declared so far indexed by nimName
|
||||
identifiers*: TableRef[string, string]
|
||||
|
||||
|
|
@ -92,8 +91,6 @@ type
|
|||
# Craeted symbols to generated AST - forward declaration tracking
|
||||
identifierNodes*: TableRef[string, PNode]
|
||||
|
||||
gState*: State
|
||||
|
||||
currentHeader*, impShort*, sourceFile*: string
|
||||
|
||||
data*: seq[tuple[name, val: string]]
|
||||
|
|
@ -113,8 +110,7 @@ template Bl(s: typed): untyped {.used.} =
|
|||
(s.len == 0)
|
||||
|
||||
when not declared(CIMPORT):
|
||||
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState,
|
||||
nBl, Bl
|
||||
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, nBl, Bl
|
||||
|
||||
# Redirect output to file when required
|
||||
template gecho*(args: string) {.dirty.} =
|
||||
|
|
@ -123,11 +119,6 @@ when not declared(CIMPORT):
|
|||
else:
|
||||
gState.outputHandle.writeLine(args)
|
||||
|
||||
template necho*(args: string) {.dirty.} =
|
||||
when not declared(gState):
|
||||
let gState = nimState.gState
|
||||
gecho args
|
||||
|
||||
template decho*(str: untyped): untyped =
|
||||
if nimState.gState.debug:
|
||||
necho str.getCommented()
|
||||
if gState.debug:
|
||||
gecho str.getCommented()
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import regex
|
|||
import "."/[ast, getters, globals, lisp, treesitter/api]
|
||||
|
||||
type
|
||||
Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, nimState: NimState) {.nimcall.}]]
|
||||
Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, gState: State) {.nimcall.}]]
|
||||
|
||||
proc getPtrType(str: string): string =
|
||||
result = case str:
|
||||
|
|
@ -39,26 +39,26 @@ proc initGrammar(): Grammar =
|
|||
(preproc_arg)
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# define X Y"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# define X Y"
|
||||
|
||||
let
|
||||
name = nimState.data[0].val
|
||||
nname = nimState.getIdentifier(name, nskConst)
|
||||
val = nimState.data[1].val.getLit()
|
||||
name = gState.data[0].val
|
||||
nname = gState.getIdentifier(name, nskConst)
|
||||
val = gState.data[1].val.getLit()
|
||||
|
||||
if not nname.nBl:
|
||||
let
|
||||
override = nimState.getOverride(name, nskConst)
|
||||
override = gState.getOverride(name, nskConst)
|
||||
if override.nBl:
|
||||
nimState.constStr &= &"{nimState.getComments()}\n{override}"
|
||||
gState.constStr &= &"{gState.getComments()}\n{override}"
|
||||
else:
|
||||
nimState.constStr &= &"{nimState.getComments()}\n # Const '{name}' skipped"
|
||||
if nimState.gState.debug:
|
||||
nimState.skipStr &= &"\n{nimState.getNodeVal(node)}"
|
||||
elif val.nBl and nimState.addNewIdentifer(nname):
|
||||
nimState.constStr &= &"{nimState.getComments()}\n {nname}* = {val}"
|
||||
gState.constStr &= &"{gState.getComments()}\n # Const '{name}' skipped"
|
||||
if gState.debug:
|
||||
gState.skipStr &= &"\n{gState.getNodeVal(node)}"
|
||||
elif val.nBl and gState.addNewIdentifer(nname):
|
||||
gState.constStr &= &"{gState.getComments()}\n {nname}* = {val}"
|
||||
))
|
||||
|
||||
let
|
||||
|
|
@ -125,25 +125,25 @@ proc initGrammar(): Grammar =
|
|||
"""
|
||||
|
||||
template funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen: untyped): untyped =
|
||||
ptyp = nimState.getIdentifier(nimState.data[i].val, nskType, fname).getType()
|
||||
ptyp = gState.getIdentifier(gState.data[i].val, nskType, fname).getType()
|
||||
|
||||
pptr = ""
|
||||
while i+1 < nimState.data.len and nimState.data[i+1].name == "pointer_declarator":
|
||||
while i+1 < gState.data.len and gState.data[i+1].name == "pointer_declarator":
|
||||
pptr &= "ptr "
|
||||
i += 1
|
||||
|
||||
if i+1 < nimState.data.len and nimState.data[i+1].name == "identifier":
|
||||
pname = nimState.getIdentifier(nimState.data[i+1].val, nskParam, fname)
|
||||
if i+1 < gState.data.len and gState.data[i+1].name == "identifier":
|
||||
pname = gState.getIdentifier(gState.data[i+1].val, nskParam, fname)
|
||||
i += 2
|
||||
else:
|
||||
pname = "a" & $count
|
||||
count += 1
|
||||
i += 1
|
||||
|
||||
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
|
||||
flen = nimState.data[i].val
|
||||
if nimState.data[i].name == "identifier":
|
||||
flen = nimState.getIdentifier(flen, nskConst, fname)
|
||||
if i < gState.data.len and gState.data[i].name in ["identifier", "number_literal"]:
|
||||
flen = gState.data[i].val
|
||||
if gState.data[i].name == "identifier":
|
||||
flen = gState.getIdentifier(flen, nskConst, fname)
|
||||
|
||||
pout &= &"{pname}: array[{flen}, {getPtrType(pptr&ptyp)}], "
|
||||
i += 1
|
||||
|
|
@ -172,13 +172,13 @@ proc initGrammar(): Grammar =
|
|||
{funcGrammar}
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# typedef X Y"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# typedef X Y"
|
||||
|
||||
var
|
||||
i = 0
|
||||
typ = nimState.getIdentifier(nimState.data[i].val, nskType, "Parent").getType()
|
||||
typ = gState.getIdentifier(gState.data[i].val, nskType, "Parent").getType()
|
||||
name = ""
|
||||
nname = ""
|
||||
tptr = ""
|
||||
|
|
@ -186,8 +186,8 @@ proc initGrammar(): Grammar =
|
|||
pragmas: seq[string] = @[]
|
||||
|
||||
i += 1
|
||||
while i < nimState.data.len and "pointer" in nimState.data[i].name:
|
||||
case nimState.data[i].name:
|
||||
while i < gState.data.len and "pointer" in gState.data[i].name:
|
||||
case gState.data[i].name:
|
||||
of "pointer_declarator":
|
||||
tptr &= "ptr "
|
||||
i += 1
|
||||
|
|
@ -195,32 +195,32 @@ proc initGrammar(): Grammar =
|
|||
aptr &= "ptr "
|
||||
i += 1
|
||||
|
||||
if i < nimState.data.len:
|
||||
name = nimState.data[i].val
|
||||
nname = nimState.getIdentifier(name, nskType)
|
||||
if i < gState.data.len:
|
||||
name = gState.data[i].val
|
||||
nname = gState.getIdentifier(name, nskType)
|
||||
i += 1
|
||||
|
||||
if nimState.includeHeader():
|
||||
pragmas.add nimState.getImportC(name, nname)
|
||||
if gState.isIncludeHeader():
|
||||
pragmas.add gState.getImportC(name, nname)
|
||||
|
||||
let
|
||||
pragma = nimState.getPragma(pragmas)
|
||||
pragma = gState.getPragma(pragmas)
|
||||
|
||||
if not nname.nBl:
|
||||
let
|
||||
override = nimState.getOverride(name, nskType)
|
||||
override = gState.getOverride(name, nskType)
|
||||
if override.nBl:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||
elif nname notin gTypeMap and typ.nBl and nimState.addNewIdentifer(nname):
|
||||
if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
|
||||
gState.typeStr &= &"{gState.getComments()}\n{override}"
|
||||
elif nname notin gTypeMap and typ.nBl and gState.addNewIdentifer(nname):
|
||||
if i < gState.data.len and gState.data[^1].name == "function_declarator":
|
||||
var
|
||||
fname = nname
|
||||
pout, pname, ptyp, pptr = ""
|
||||
count = 1
|
||||
flen = ""
|
||||
|
||||
while i < nimState.data.len:
|
||||
if nimState.data[i].name == "function_declarator":
|
||||
while i < gState.data.len:
|
||||
if gState.data[i].name == "function_declarator":
|
||||
break
|
||||
|
||||
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
||||
|
|
@ -229,34 +229,34 @@ proc initGrammar(): Grammar =
|
|||
pout = pout[0 .. ^3]
|
||||
|
||||
if tptr.nBl or typ != "object":
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.cdecl.}}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.cdecl.}}"
|
||||
else:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = proc({pout}) {{.cdecl.}}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = proc({pout}) {{.cdecl.}}"
|
||||
else:
|
||||
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
|
||||
if i < gState.data.len and gState.data[i].name in ["identifier", "number_literal"]:
|
||||
var
|
||||
flen = nimState.data[i].val
|
||||
if nimState.data[i].name == "identifier":
|
||||
flen = nimState.getIdentifier(flen, nskConst, nname)
|
||||
flen = gState.data[i].val
|
||||
if gState.data[i].name == "identifier":
|
||||
flen = gState.getIdentifier(flen, nskConst, nname)
|
||||
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
|
||||
else:
|
||||
if nname == typ:
|
||||
pragmas.add "incompleteStruct"
|
||||
let
|
||||
pragma = nimState.getPragma(pragmas)
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = object"
|
||||
pragma = gState.getPragma(pragmas)
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = object"
|
||||
else:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = {getPtrType(tptr&typ)}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = {getPtrType(tptr&typ)}"
|
||||
))
|
||||
|
||||
proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# pDupTypeCommon()"
|
||||
proc pDupTypeCommon(nname: string, fend: int, gState: State, isEnum=false) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# pDupTypeCommon()"
|
||||
|
||||
var
|
||||
dname = nimState.data[^1].val
|
||||
ndname = nimState.getIdentifier(dname, nskType)
|
||||
dname = gState.data[^1].val
|
||||
ndname = gState.getIdentifier(dname, nskType)
|
||||
dptr =
|
||||
if fend == 2:
|
||||
"ptr "
|
||||
|
|
@ -265,21 +265,21 @@ proc initGrammar(): Grammar =
|
|||
|
||||
if ndname.nBl and ndname != nname:
|
||||
if isEnum:
|
||||
if nimState.addNewIdentifer(ndname):
|
||||
nimState.enumStr &= &"{nimState.getComments(true)}\ntype {ndname}* = {dptr}{nname}"
|
||||
if gState.addNewIdentifer(ndname):
|
||||
gState.enumStr &= &"{gState.getComments(true)}\ntype {ndname}* = {dptr}{nname}"
|
||||
else:
|
||||
if nimState.addNewIdentifer(ndname):
|
||||
if gState.addNewIdentifer(ndname):
|
||||
let
|
||||
pragma = nimState.getPragma(nimState.getImportc(dname, ndname), "bycopy")
|
||||
nimState.typeStr &=
|
||||
&"{nimState.getComments()}\n {ndname}*{pragma} = {dptr}{nname}"
|
||||
pragma = gState.getPragma(gState.getImportc(dname, ndname), "bycopy")
|
||||
gState.typeStr &=
|
||||
&"{gState.getComments()}\n {ndname}*{pragma} = {dptr}{nname}"
|
||||
|
||||
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# pStructCommon"
|
||||
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# pStructCommon"
|
||||
|
||||
var
|
||||
nname = nimState.getIdentifier(name, nskType)
|
||||
nname = gState.getIdentifier(name, nskType)
|
||||
prefix = ""
|
||||
union = ""
|
||||
|
||||
|
|
@ -307,25 +307,25 @@ proc initGrammar(): Grammar =
|
|||
|
||||
if not nname.nBl:
|
||||
let
|
||||
override = nimState.getOverride(name, nskType)
|
||||
override = gState.getOverride(name, nskType)
|
||||
if override.nBl:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||
elif nimState.addNewIdentifer(nname):
|
||||
if nimState.data.len == 1:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}* {{.bycopy{union}.}} = object"
|
||||
gState.typeStr &= &"{gState.getComments()}\n{override}"
|
||||
elif gState.addNewIdentifer(nname):
|
||||
if gState.data.len == 1:
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}* {{.bycopy{union}.}} = object"
|
||||
else:
|
||||
var
|
||||
pragmas: seq[string] = @[]
|
||||
if nimState.includeHeader():
|
||||
pragmas.add nimState.getImportC(prefix & name, nname)
|
||||
if gState.isIncludeHeader():
|
||||
pragmas.add gState.getImportC(prefix & name, nname)
|
||||
pragmas.add "bycopy"
|
||||
if union.nBl:
|
||||
pragmas.add "union"
|
||||
|
||||
let
|
||||
pragma = nimState.getPragma(pragmas)
|
||||
pragma = gState.getPragma(pragmas)
|
||||
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = object"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {nname}*{pragma} = object"
|
||||
|
||||
var
|
||||
i = fstart
|
||||
|
|
@ -333,19 +333,19 @@ proc initGrammar(): Grammar =
|
|||
fptr = ""
|
||||
aptr = ""
|
||||
flen = ""
|
||||
while i < nimState.data.len-fend:
|
||||
while i < gState.data.len-fend:
|
||||
fptr = ""
|
||||
aptr = ""
|
||||
if nimState.data[i].name == "field_declaration":
|
||||
if gState.data[i].name == "field_declaration":
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if nimState.data[i].name notin ["field_identifier", "pointer_declarator", "array_pointer_declarator"]:
|
||||
ftyp = nimState.getIdentifier(nimState.data[i].val, nskType, nname).getType()
|
||||
if gState.data[i].name notin ["field_identifier", "pointer_declarator", "array_pointer_declarator"]:
|
||||
ftyp = gState.getIdentifier(gState.data[i].val, nskType, nname).getType()
|
||||
i += 1
|
||||
|
||||
while i < nimState.data.len-fend and "pointer" in nimState.data[i].name:
|
||||
case nimState.data[i].name:
|
||||
while i < gState.data.len-fend and "pointer" in gState.data[i].name:
|
||||
case gState.data[i].name:
|
||||
of "pointer_declarator":
|
||||
fptr &= "ptr "
|
||||
i += 1
|
||||
|
|
@ -353,33 +353,33 @@ proc initGrammar(): Grammar =
|
|||
aptr &= "ptr "
|
||||
i += 1
|
||||
|
||||
fname = nimState.getIdentifier(nimState.data[i].val, nskField, nname)
|
||||
fname = gState.getIdentifier(gState.data[i].val, nskField, nname)
|
||||
|
||||
if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals:
|
||||
if i+1 < gState.data.len-fend and gState.data[i+1].name in gEnumVals:
|
||||
# Struct field is an array where size is an expression
|
||||
var
|
||||
flen = nimState.getNimExpression(nimState.data[i+1].val)
|
||||
flen = gState.getNimExpression(gState.data[i+1].val)
|
||||
if "/" in flen:
|
||||
flen = &"({flen}).int"
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]"
|
||||
i += 2
|
||||
elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "bitfield_clause":
|
||||
elif i+1 < gState.data.len-fend and gState.data[i+1].name == "bitfield_clause":
|
||||
let
|
||||
size = nimState.data[i+1].val
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}* {{.bitsize: {size}.}} : {getPtrType(fptr&ftyp)} "
|
||||
size = gState.data[i+1].val
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}* {{.bitsize: {size}.}} : {getPtrType(fptr&ftyp)} "
|
||||
i += 2
|
||||
elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "function_declarator":
|
||||
elif i+1 < gState.data.len-fend and gState.data[i+1].name == "function_declarator":
|
||||
var
|
||||
pout, pname, ptyp, pptr = ""
|
||||
count = 1
|
||||
|
||||
i += 2
|
||||
while i < nimState.data.len-fend:
|
||||
if nimState.data[i].name == "function_declarator":
|
||||
while i < gState.data.len-fend:
|
||||
if gState.data[i].name == "function_declarator":
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if nimState.data[i].name == "field_declaration":
|
||||
if gState.data[i].name == "field_declaration":
|
||||
break
|
||||
|
||||
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
|
||||
|
|
@ -387,20 +387,20 @@ proc initGrammar(): Grammar =
|
|||
if pout.nBl and pout[^2 .. ^1] == ", ":
|
||||
pout = pout[0 .. ^3]
|
||||
if fptr.nBl or ftyp != "object":
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.cdecl.}}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.cdecl.}}"
|
||||
else:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: proc({pout}) {{.cdecl.}}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}*: proc({pout}) {{.cdecl.}}"
|
||||
i += 1
|
||||
else:
|
||||
if ftyp == "object":
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: pointer"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}*: pointer"
|
||||
else:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: {getPtrType(fptr&ftyp)}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n {fname}*: {getPtrType(fptr&ftyp)}"
|
||||
i += 1
|
||||
|
||||
if node.tsNodeType() == "type_definition" and
|
||||
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.nBl:
|
||||
pDupTypeCommon(nname, fend, nimState, false)
|
||||
gState.data[^1].name == "type_identifier" and gState.data[^1].val.nBl:
|
||||
pDupTypeCommon(nname, fend, gState, false)
|
||||
|
||||
let
|
||||
fieldGrammar = &"""
|
||||
|
|
@ -451,11 +451,11 @@ proc initGrammar(): Grammar =
|
|||
{fieldListGrammar}
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# struct X {}"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# struct X {}"
|
||||
|
||||
pStructCommon(ast, node, nimState.data[0].val, 1, 1, nimState)
|
||||
pStructCommon(ast, node, gState.data[0].val, 1, 1, gState)
|
||||
))
|
||||
|
||||
# typedef struct X {}
|
||||
|
|
@ -474,69 +474,69 @@ proc initGrammar(): Grammar =
|
|||
)
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# typedef struct X {}"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# typedef struct X {}"
|
||||
|
||||
var
|
||||
fstart = 0
|
||||
fend = 1
|
||||
|
||||
if nimState.data[^2].name == "pointer_declarator":
|
||||
if gState.data[^2].name == "pointer_declarator":
|
||||
fend = 2
|
||||
|
||||
if nimState.data.len > 1 and
|
||||
nimState.data[0].name == "type_identifier" and
|
||||
nimState.data[1].name notin ["field_identifier", "pointer_declarator"]:
|
||||
if gState.data.len > 1 and
|
||||
gState.data[0].name == "type_identifier" and
|
||||
gState.data[1].name notin ["field_identifier", "pointer_declarator"]:
|
||||
|
||||
fstart = 1
|
||||
pStructCommon(ast, node, nimState.data[0].val, fstart, fend, nimState)
|
||||
pStructCommon(ast, node, gState.data[0].val, fstart, fend, gState)
|
||||
else:
|
||||
pStructCommon(ast, node, nimState.data[^1].val, fstart, fend, nimState)
|
||||
pStructCommon(ast, node, gState.data[^1].val, fstart, fend, gState)
|
||||
))
|
||||
|
||||
proc pEnumCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# pEnumCommon()"
|
||||
proc pEnumCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# pEnumCommon()"
|
||||
|
||||
let nname =
|
||||
if name.Bl:
|
||||
getUniqueIdentifier(nimState, "Enum")
|
||||
getUniqueIdentifier(gState, "Enum")
|
||||
else:
|
||||
nimState.getIdentifier(name, nskType)
|
||||
gState.getIdentifier(name, nskType)
|
||||
|
||||
if nname.nBl and nimState.addNewIdentifer(nname):
|
||||
nimState.enumStr &= &"{nimState.getComments(true)}\ndefineEnum({nname})"
|
||||
if nname.nBl and gState.addNewIdentifer(nname):
|
||||
gState.enumStr &= &"{gState.getComments(true)}\ndefineEnum({nname})"
|
||||
|
||||
var
|
||||
i = fstart
|
||||
count = 0
|
||||
while i < nimState.data.len-fend:
|
||||
if nimState.data[i].name == "enumerator":
|
||||
while i < gState.data.len-fend:
|
||||
if gState.data[i].name == "enumerator":
|
||||
i += 1
|
||||
continue
|
||||
|
||||
let
|
||||
fname = nimState.getIdentifier(nimState.data[i].val, nskEnumField)
|
||||
fname = gState.getIdentifier(gState.data[i].val, nskEnumField)
|
||||
|
||||
if i+1 < nimState.data.len-fend and
|
||||
nimState.data[i+1].name in gEnumVals:
|
||||
if fname.nBl and nimState.addNewIdentifer(fname):
|
||||
nimState.constStr &= &"{nimState.getComments()}\n {fname}* = ({nimState.getNimExpression(nimState.data[i+1].val)}).{nname}"
|
||||
if i+1 < gState.data.len-fend and
|
||||
gState.data[i+1].name in gEnumVals:
|
||||
if fname.nBl and gState.addNewIdentifer(fname):
|
||||
gState.constStr &= &"{gState.getComments()}\n {fname}* = ({gState.getNimExpression(gState.data[i+1].val)}).{nname}"
|
||||
try:
|
||||
count = nimState.data[i+1].val.parseInt() + 1
|
||||
count = gState.data[i+1].val.parseInt() + 1
|
||||
except:
|
||||
count += 1
|
||||
i += 2
|
||||
else:
|
||||
if fname.nBl and nimState.addNewIdentifer(fname):
|
||||
nimState.constStr &= &"{nimState.getComments()}\n {fname}* = {count}.{nname}"
|
||||
if fname.nBl and gState.addNewIdentifer(fname):
|
||||
gState.constStr &= &"{gState.getComments()}\n {fname}* = {count}.{nname}"
|
||||
i += 1
|
||||
count += 1
|
||||
|
||||
if node.tsNodeType() == "type_definition" and
|
||||
nimState.data[^1].name == "type_identifier" and nimState.data[^1].val.nBl:
|
||||
pDupTypeCommon(nname, fend, nimState, true)
|
||||
gState.data[^1].name == "type_identifier" and gState.data[^1].val.nBl:
|
||||
pDupTypeCommon(nname, fend, gState, true)
|
||||
|
||||
# enum X {}
|
||||
result.add(("""
|
||||
|
|
@ -550,19 +550,19 @@ proc initGrammar(): Grammar =
|
|||
)
|
||||
)
|
||||
""" % gEnumVals.join("|"),
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# enum X {}"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# enum X {}"
|
||||
|
||||
var
|
||||
name = ""
|
||||
offset = 0
|
||||
|
||||
if nimState.data[0].name == "type_identifier":
|
||||
name = nimState.data[0].val
|
||||
if gState.data[0].name == "type_identifier":
|
||||
name = gState.data[0].val
|
||||
offset = 1
|
||||
|
||||
pEnumCommon(ast, node, name, offset, 0, nimState)
|
||||
pEnumCommon(ast, node, name, offset, 0, gState)
|
||||
))
|
||||
|
||||
# typedef enum {} X
|
||||
|
|
@ -578,23 +578,23 @@ proc initGrammar(): Grammar =
|
|||
)
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# typedef enum {}"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# typedef enum {}"
|
||||
|
||||
var
|
||||
fstart = 0
|
||||
fend = 1
|
||||
|
||||
if nimState.data[^2].name == "pointer_declarator":
|
||||
if gState.data[^2].name == "pointer_declarator":
|
||||
fend = 2
|
||||
|
||||
if nimState.data[0].name == "type_identifier":
|
||||
if gState.data[0].name == "type_identifier":
|
||||
fstart = 1
|
||||
|
||||
pEnumCommon(ast, node, nimState.data[0].val, fstart, fend, nimState)
|
||||
pEnumCommon(ast, node, gState.data[0].val, fstart, fend, gState)
|
||||
else:
|
||||
pEnumCommon(ast, node, nimState.data[^1].val, fstart, fend, nimState)
|
||||
pEnumCommon(ast, node, gState.data[^1].val, fstart, fend, gState)
|
||||
))
|
||||
|
||||
# typ function(typ param1, ...)
|
||||
|
|
@ -611,39 +611,39 @@ proc initGrammar(): Grammar =
|
|||
{funcGrammar}
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
if nimState.gState.debug:
|
||||
nimState.debugStr &= "\n# typ function"
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
if gState.debug:
|
||||
gState.debugStr &= "\n# typ function"
|
||||
|
||||
var
|
||||
fptr = ""
|
||||
i = 1
|
||||
|
||||
while i < nimState.data.len:
|
||||
if nimState.data[i].name == "function_declarator":
|
||||
while i < gState.data.len:
|
||||
if gState.data[i].name == "function_declarator":
|
||||
i += 1
|
||||
continue
|
||||
|
||||
fptr = ""
|
||||
while i < nimState.data.len and nimState.data[i].name == "pointer_declarator":
|
||||
while i < gState.data.len and gState.data[i].name == "pointer_declarator":
|
||||
fptr &= "ptr "
|
||||
i += 1
|
||||
|
||||
var
|
||||
fname = nimState.data[i].val
|
||||
fnname = nimState.getIdentifier(fname, nskProc)
|
||||
fname = gState.data[i].val
|
||||
fnname = gState.getIdentifier(fname, nskProc)
|
||||
pout, pname, ptyp, pptr = ""
|
||||
count = 1
|
||||
flen = ""
|
||||
fVar = false
|
||||
|
||||
i += 1
|
||||
if i < nimState.data.len and nimState.data[i].name == "pointer_declarator":
|
||||
if i < gState.data.len and gState.data[i].name == "pointer_declarator":
|
||||
fVar = true
|
||||
i += 1
|
||||
|
||||
while i < nimState.data.len:
|
||||
if nimState.data[i].name == "function_declarator":
|
||||
while i < gState.data.len:
|
||||
if gState.data[i].name == "function_declarator":
|
||||
break
|
||||
|
||||
funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i, flen)
|
||||
|
|
@ -653,24 +653,24 @@ proc initGrammar(): Grammar =
|
|||
|
||||
if not fnname.nBl:
|
||||
let
|
||||
override = nimState.getOverride(fname, nskProc)
|
||||
override = gState.getOverride(fname, nskProc)
|
||||
if override.nBl:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||
elif nimState.addNewIdentifer(fnname):
|
||||
gState.typeStr &= &"{gState.getComments()}\n{override}"
|
||||
elif gState.addNewIdentifer(fnname):
|
||||
let
|
||||
ftyp = nimState.getIdentifier(nimState.data[0].val, nskType, fnname).getType()
|
||||
pragma = nimState.getPragma(nimState.getImportC(fname, fnname), "cdecl")
|
||||
ftyp = gState.getIdentifier(gState.data[0].val, nskType, fnname).getType()
|
||||
pragma = gState.getPragma(gState.getImportC(fname, fnname), "cdecl")
|
||||
|
||||
if fptr.nBl or ftyp != "object":
|
||||
if fVar:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nvar {fnname}*: proc ({pout}): {getPtrType(fptr&ftyp)}{{.cdecl.}}"
|
||||
gState.procStr &= &"{gState.getComments(true)}\nvar {fnname}*: proc ({pout}): {getPtrType(fptr&ftyp)}{{.cdecl.}}"
|
||||
else:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
|
||||
gState.procStr &= &"{gState.getComments(true)}\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
|
||||
else:
|
||||
if fVar:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nvar {fnname}*: proc ({pout}){{.cdecl.}}"
|
||||
gState.procStr &= &"{gState.getComments(true)}\nvar {fnname}*: proc ({pout}){{.cdecl.}}"
|
||||
else:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\nproc {fnname}*({pout}){pragma}"
|
||||
gState.procStr &= &"{gState.getComments(true)}\nproc {fnname}*({pout}){pragma}"
|
||||
))
|
||||
|
||||
# // comment
|
||||
|
|
@ -678,15 +678,15 @@ proc initGrammar(): Grammar =
|
|||
(comment
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
let
|
||||
cmt = $nimState.getNodeVal(node)
|
||||
cmt = $gState.getNodeVal(node)
|
||||
|
||||
for line in cmt.splitLines():
|
||||
let
|
||||
line = line.multiReplace([("//", ""), ("/*", ""), ("*/", "")])
|
||||
|
||||
nimState.commentStr &= &"\n # {line.strip(leading=false)}"
|
||||
gState.commentStr &= &"\n # {line.strip(leading=false)}"
|
||||
))
|
||||
|
||||
# // unknown
|
||||
|
|
@ -695,37 +695,37 @@ proc initGrammar(): Grammar =
|
|||
(^.*)
|
||||
)
|
||||
""",
|
||||
proc (ast: ref Ast, node: TSNode, nimState: NimState) =
|
||||
proc (ast: ref Ast, node: TSNode, gState: State) =
|
||||
var
|
||||
done = false
|
||||
for i in nimState.data:
|
||||
for i in gState.data:
|
||||
case $node.tsNodeType()
|
||||
of "declaration":
|
||||
if i.name == "identifier":
|
||||
let
|
||||
override = nimState.getOverride(i.val, nskProc)
|
||||
override = gState.getOverride(i.val, nskProc)
|
||||
|
||||
if override.nBl:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\n{override}"
|
||||
gState.procStr &= &"{gState.getComments(true)}\n{override}"
|
||||
done = true
|
||||
break
|
||||
else:
|
||||
nimState.procStr &= &"{nimState.getComments(true)}\n# Declaration '{i.val}' skipped"
|
||||
gState.procStr &= &"{gState.getComments(true)}\n# Declaration '{i.val}' skipped"
|
||||
|
||||
else:
|
||||
if i.name == "type_identifier":
|
||||
let
|
||||
override = nimState.getOverride(i.val, nskType)
|
||||
override = gState.getOverride(i.val, nskType)
|
||||
|
||||
if override.nBl:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n{override}"
|
||||
gState.typeStr &= &"{gState.getComments()}\n{override}"
|
||||
done = true
|
||||
break
|
||||
else:
|
||||
nimState.typeStr &= &"{nimState.getComments()}\n # Type '{i.val}' skipped"
|
||||
gState.typeStr &= &"{gState.getComments()}\n # Type '{i.val}' skipped"
|
||||
|
||||
if nimState.gState.debug and not done:
|
||||
nimState.skipStr &= &"\n{nimState.getNodeVal(node)}"
|
||||
if gState.debug and not done:
|
||||
gState.skipStr &= &"\n{gState.getNodeVal(node)}"
|
||||
))
|
||||
|
||||
proc initRegex(ast: ref Ast) =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue