diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-09 17:08:04 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-09 17:08:04 -0600 |
| commit | 4d57be55ac5e0be8870bd7479e08448a079da2be (patch) | |
| tree | efdbf684ffa54d2ecbb76a6bff1f72d91f7d3e04 | |
| parent | d21017ac5bfade2194a50952e40257e80bd018dd (diff) | |
| download | nimterop-4d57be55ac5e0be8870bd7479e08448a079da2be.tar.gz nimterop-4d57be55ac5e0be8870bd7479e08448a079da2be.zip | |
Support for multiple functions in declarator, size_t fixes
| -rw-r--r-- | nimterop/ast.nim | 4 | ||||
| -rw-r--r-- | nimterop/getters.nim | 2 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 45 |
3 files changed, 35 insertions, 16 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index ea50b1d..5f6ad8d 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -51,8 +51,8 @@ proc saveNodeData(node: TSNode): bool = 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", "")) + elif name in ["field_declaration", "function_declarator"]: + gStateRT.data.add((name, "")) return true diff --git a/nimterop/getters.nim b/nimterop/getters.nim index 1ff4d49..db15991 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -56,6 +56,8 @@ proc getType*(str: string): string = ("unsigned ", "cu"), ("double ", "cdouble"), ("long ", "clong"), + ("ssize_t", "int"), + ("size_t", "uint") ]). replace(re"([u]?int[\d]+)_t", "$1"). replace(re"([u]?int)ptr_t", "ptr $1") diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 9abbf87..ef5cbb4 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -120,6 +120,10 @@ proc initGrammar() = i += 3 while i < gStateRT.data.len-fend: + if gStateRT.data[i].name == "function_declarator": + i += 1 + continue + if gStateRT.data[i].name == "field_declaration": break @@ -284,7 +288,7 @@ proc initGrammar() = )) let funcGrammar = &""" - (function_declarator? + (function_declarator* (identifier) {paramListGrammar} ) @@ -296,7 +300,11 @@ proc initGrammar() = (storage_class_specifier?) {typeGrammar} {funcGrammar} - (pointer_declarator? + (pointer_declarator* + {funcGrammar} + ) + {funcGrammar} + (pointer_declarator* {funcGrammar} ) ) @@ -304,27 +312,36 @@ proc initGrammar() = proc (ast: ref Ast, node: TSNode) = let ftyp = gStateRT.data[0].val.getIdentifier() - fname = gStateRT.data[1].val - fnname = fname.getIdentifier() - if fnname notin gStateRT.procs: + var + i = 1 + while i < gStateRT.data.len: + if gStateRT.data[i].name == "function_declarator": + i += 1 + continue + var + fname = gStateRT.data[i].val + fnname = fname.getIdentifier() pout, pname, ptyp = "" - i = 2 count = 1 - if gStateRT.data.len > 2: - while i < gStateRT.data.len: - funcParamCommon(pname, ptyp, pout, count, i) + i += 1 + while i < gStateRT.data.len: + if gStateRT.data[i].name == "function_declarator": + break + + funcParamCommon(pname, ptyp, pout, count, i) if pout.len != 0 and pout[^1] == ',': pout = pout[0 .. ^2] - gStateRT.procs.add(fnname) - if ftyp != "object": - gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" - else: - gStateRT.procStr &= &"proc {fnname}({pout}) {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" + if fnname notin gStateRT.procs: + gStateRT.procs.add(fnname) + if ftyp != "object": + gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" + else: + gStateRT.procStr &= &"proc {fnname}({pout}) {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" )) |
