Struct field function pointers, cstring, show toast error in cimport
This commit is contained in:
parent
c7a3c7b7d6
commit
21bd2d4112
7 changed files with 121 additions and 28 deletions
|
|
@ -32,23 +32,28 @@ proc saveNodeData(node: TSNode): bool =
|
|||
if name in ["math_expression", "primitive_type", "sized_type_specifier"]:
|
||||
val = val.getType()
|
||||
|
||||
let
|
||||
nparent = node.tsNodeParent()
|
||||
if not nparent.tsNodeIsNull() and node.tsNodePrevNamedSibling().tsNodeIsNull():
|
||||
let
|
||||
npname = nparent.tsNodeType()
|
||||
npparent = nparent.tsNodeParent()
|
||||
if npname == "pointer_declarator" or
|
||||
($npname in ["function_declarator", "array_declarator"] and
|
||||
not npparent.tsNodeIsNull() and npparent.tsNodeType() == "pointer_declarator"):
|
||||
|
||||
if gStateRT.data[^1].val != "object":
|
||||
gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val.getIdentifier()
|
||||
else:
|
||||
gStateRT.data[^1].val = "pointer"
|
||||
if node.tsNodePrevNamedSibling().tsNodeIsNull() and
|
||||
((node.isPName("pointer_declarator") and not node.isPPName("function_declarator")) or
|
||||
(node.getPName() in ["function_declarator", "array_declarator"] and node.isPPName("pointer_declarator"))):
|
||||
if gStateRT.data[^1].val != "object":
|
||||
gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val.getIdentifier()
|
||||
if gStateRT.data[^1].val == "ptr char":
|
||||
gStateRT.data[^1].val = "cstring"
|
||||
else:
|
||||
gStateRT.data[^1].val = "pointer"
|
||||
|
||||
gStateRT.data.add((name, val))
|
||||
|
||||
if node.isPName("pointer_declarator") and node.isPPName("function_declarator"):
|
||||
gStateRT.data.add(("function_declarator", ""))
|
||||
|
||||
elif name in ["abstract_pointer_declarator"]:
|
||||
gStateRT.data[^1].val = "ptr " & gStateRT.data[^1].val.getIdentifier()
|
||||
if gStateRT.data[^1].val == "ptr char":
|
||||
gStateRT.data[^1].val = "cstring"
|
||||
elif name == "field_declaration":
|
||||
gStateRT.data.add(("field_declaration", ""))
|
||||
|
||||
return true
|
||||
|
||||
proc searchAstForNode(ast: ref Ast, node: TSNode): bool =
|
||||
|
|
|
|||
|
|
@ -164,7 +164,15 @@ macro cImport*(filename: static string): untyped =
|
|||
|
||||
echo "Importing " & fullpath
|
||||
|
||||
result.add parseStmt(getToast(fullpath))
|
||||
let
|
||||
output = getToast(fullpath)
|
||||
|
||||
try:
|
||||
result.add parseStmt(output)
|
||||
except:
|
||||
echo output
|
||||
echo "Failed to import generated nim"
|
||||
result.add parseStmt(output)
|
||||
|
||||
if gStateCT.debug:
|
||||
echo result.repr
|
||||
|
|
|
|||
|
|
@ -191,3 +191,17 @@ proc getAstChildByName*(ast: ref Ast, name: string): ref Ast =
|
|||
for i in 0 .. ast.children.len-1:
|
||||
if name in ast.children[i].name.split("|"):
|
||||
return ast.children[i]
|
||||
|
||||
proc getPName*(node: TSNode): string =
|
||||
if not node.tsNodeIsNull():
|
||||
let
|
||||
nparent = node.tsNodeParent()
|
||||
if not nparent.tsNodeIsNull():
|
||||
return $nparent.tsNodeType()
|
||||
|
||||
proc isPName*(node: TSNode, name: string): bool =
|
||||
return node.getPName() == name
|
||||
|
||||
proc isPPName*(node: TSNode, name: string): bool =
|
||||
if node.getPName().len != 0:
|
||||
return node.tsNodeParent().isPName(name)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ proc initGrammar() =
|
|||
))
|
||||
|
||||
let typeGrammar = """
|
||||
(type_qualifier?)
|
||||
(primitive_type|type_identifier?)
|
||||
(sized_type_specifier?
|
||||
(primitive_type?)
|
||||
|
|
@ -88,6 +89,10 @@ proc initGrammar() =
|
|||
var
|
||||
i = fstart
|
||||
while i < gStateRT.data.len-fend:
|
||||
if gStateRT.data[i].name == "field_declaration":
|
||||
i += 1
|
||||
continue
|
||||
|
||||
let
|
||||
ftyp = gStateRT.data[i].val.getIdentifier()
|
||||
fname = gStateRT.data[i+1].val.getIdentifier()
|
||||
|
|
@ -96,17 +101,64 @@ proc initGrammar() =
|
|||
flen = gStateRT.data[i+2].val.getIdentifier()
|
||||
gStateRT.typeStr &= &" {fname}*: array[{flen}, {ftyp}]\n"
|
||||
i += 3
|
||||
elif i+2 < gStateRT.data.len-fend and gStateRT.data[i+2].name == "function_declarator":
|
||||
var
|
||||
pout, pname, ptyp = ""
|
||||
count = 1
|
||||
|
||||
i += 3
|
||||
while i < gStateRT.data.len-fend:
|
||||
if gStateRT.data[i].name == "field_declaration":
|
||||
break
|
||||
|
||||
ptyp = gStateRT.data[i].val.getIdentifier()
|
||||
if gStateRT.data[i+1].name == "identifier":
|
||||
pname = gStateRT.data[i+1].val.getIdentifier()
|
||||
i += 2
|
||||
else:
|
||||
pname = "a" & $count
|
||||
count += 1
|
||||
i += 1
|
||||
if ptyp != "object":
|
||||
pout &= &"{pname}: {ptyp},"
|
||||
|
||||
if pout.len != 0 and pout[^1] == ',':
|
||||
pout = pout[0 .. ^2]
|
||||
if ftyp != "object":
|
||||
gStateRT.typeStr &= &" {fname}*: proc({pout}): {ftyp} {{.nimcall.}}\n"
|
||||
else:
|
||||
gStateRT.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n"
|
||||
i += 1
|
||||
else:
|
||||
gStateRT.typeStr &= &" {fname}*: {ftyp}\n"
|
||||
i += 2
|
||||
|
||||
let
|
||||
fieldGrammar = """
|
||||
paramListGrammar = &"""
|
||||
(parameter_list
|
||||
(parameter_declaration*
|
||||
{typeGrammar}
|
||||
(identifier?)
|
||||
(pointer_declarator?
|
||||
(identifier)
|
||||
)
|
||||
(abstract_pointer_declarator?)
|
||||
)
|
||||
)
|
||||
"""
|
||||
|
||||
fieldGrammar = &"""
|
||||
(field_identifier?)
|
||||
(array_declarator?
|
||||
(field_identifier)
|
||||
(identifier|number_literal)
|
||||
)
|
||||
(function_declarator?
|
||||
(pointer_declarator
|
||||
(field_identifier)
|
||||
)
|
||||
{paramListGrammar}
|
||||
)
|
||||
"""
|
||||
|
||||
fieldListGrammar = &"""
|
||||
|
|
@ -231,16 +283,7 @@ proc initGrammar() =
|
|||
let funcGrammar = &"""
|
||||
(function_declarator?
|
||||
(identifier)
|
||||
(parameter_list
|
||||
(parameter_declaration*
|
||||
(type_qualifier?)
|
||||
{typeGrammar}
|
||||
(identifier?)
|
||||
(pointer_declarator?
|
||||
(identifier)
|
||||
)
|
||||
)
|
||||
)
|
||||
{paramListGrammar}
|
||||
)
|
||||
"""
|
||||
|
||||
|
|
@ -248,7 +291,6 @@ proc initGrammar() =
|
|||
gStateRT.grammar.add((&"""
|
||||
(declaration
|
||||
(storage_class_specifier?)
|
||||
(type_qualifier?)
|
||||
{typeGrammar}
|
||||
{funcGrammar}
|
||||
(pointer_declarator?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue