diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-06-09 15:58:01 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-06-09 15:58:01 -0500 |
| commit | 68ab2dbec18d9edc3392bdb56f818db6830fb52c (patch) | |
| tree | ad9c1b6ce5ac0226c6697d07db187436a3a4bf16 | |
| parent | 9387de8638d7805130c2c9dc53e83541eb399f6b (diff) | |
| download | nimterop-68ab2dbec18d9edc3392bdb56f818db6830fb52c.tar.gz nimterop-68ab2dbec18d9edc3392bdb56f818db6830fb52c.zip | |
Bitfield support, space after comments
| -rw-r--r-- | nimterop/ast.nim | 22 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 10 | ||||
| -rw-r--r-- | tests/include/test.h | 1 | ||||
| -rw-r--r-- | tests/tnimterop_c.nim | 4 |
4 files changed, 27 insertions, 10 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index 8efdf6f..f858043 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -7,24 +7,28 @@ import "."/[getters, globals, grammar, treesitter/api] proc saveNodeData(node: TSNode, nimState: NimState): bool = let name = $node.tsNodeType() if name in gAtoms: + let + pname = node.getPxName(1) + ppname = node.getPxName(2) + pppname = node.getPxName(3) + ppppname = node.getPxName(4) + var val = nimState.getNodeVal(node) - if name == "primitive_type" and node.tsNodeParent.tsNodeType() == "sized_type_specifier": + if name == "primitive_type" and pname == "sized_type_specifier": + return true + + if name in ["number_literal", "identifier"] and pname in gExpressions: return true - if name in ["number_literal", "identifier"] and $node.tsNodeParent.tsNodeType() in gExpressions: + if name in ["number_literal"] and pname == "bitfield_clause": + nimState.data.add(("bitfield_clause", val)) return true if name in ["primitive_type", "sized_type_specifier"]: val = val.getType() - let - pname = node.getPxName(1) - ppname = node.getPxName(2) - pppname = node.getPxName(3) - ppppname = node.getPxName(4) - if node.tsNodePrevNamedSibling().tsNodeIsNull(): if pname == "pointer_declarator": if ppname notin ["function_declarator", "array_declarator"]: @@ -43,7 +47,7 @@ proc saveNodeData(node: TSNode, nimState: NimState): bool = nimState.data.add((name, val)) - if node.tsNodeType() == "field_identifier" and + if name == "field_identifier" and pname == "pointer_declarator" and ppname == "function_declarator": if pppname == "pointer_declarator": diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 043099c..e40a4dc 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -290,6 +290,11 @@ proc initGrammar(): Grammar = flen = nimState.data[i+1].val.getNimExpression() nimState.typeStr &= &"{nimState.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": + let + size = nimState.data[i+1].val + nimState.typeStr &= &"{nimState.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": var pout, pname, ptyp, pptr = "" @@ -327,6 +332,9 @@ proc initGrammar(): Grammar = let fieldGrammar = &""" (field_identifier!) + (bitfield_clause! + (number_literal) + ) (array_declarator! (field_identifier!) (pointer_declarator @@ -588,7 +596,7 @@ proc initGrammar(): Grammar = let line = line.multiReplace([("//", ""), ("/*", ""), ("*/", "")]) - nimState.commentStr &= &"\n#{line.strip(leading=false)}" + nimState.commentStr &= &"\n# {line.strip(leading=false)}" )) proc initRegex(ast: ref Ast) = diff --git a/tests/include/test.h b/tests/include/test.h index 9a28dc0..91bfd75 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -88,6 +88,7 @@ typedef struct { enum ENUM field3[TEST_INT]; int *field4[TEST_INT]; ENUM4 *field5[TEST_INT+TEST_INT]; + int field6 : 1; } STRUCT4; typedef struct struct5 { diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim index ddfa43e..9f37141 100644 --- a/tests/tnimterop_c.nim +++ b/tests/tnimterop_c.nim @@ -78,6 +78,10 @@ when defined(cpp): else: s4.field3[3] = enum1 +s4.field6 = 1 +s4.field6 += 1 +check s4.field6 == 0 + s5.tci = test_call_int s5.tcp = test_call_param s5.tcp8 = test_call_param8 |
