Partial fix for #139

This commit is contained in:
Ganesh Viswanathan 2019-10-16 17:13:27 -05:00
commit 48955b3aa1
6 changed files with 58 additions and 18 deletions

View file

@ -86,15 +86,30 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
ast.getAstChildByName($nodeChild.tsNodeType())
else:
ast
if nimState.gState.debug:
nimState.nodeBranch.add $node.tsNodeType()
echo "#" & spaces(nimState.nodeBranch.len) & nimState.nodeBranch[^1]
if not searchAstForNode(astChild, nodeChild, nimState):
if nimState.gState.debug:
echo "#" & spaces(nimState.nodeBranch.len) & &" {$nodeChild.tsNodeType()} unexpected"
discard nimState.nodeBranch.pop()
flag = false
break
if nimState.gState.debug:
discard nimState.nodeBranch.pop()
if nimState.nodeBranch.len == 0:
echo ""
if flag:
return node.saveNodeData(nimState)
else:
echo "#" & spaces(nimState.nodeBranch.len+1) & $node.tsNodeType()
return node.saveNodeData(nimState)
elif node.getTSNodeNamedChildCountSansComments() == 0:
echo "#" & spaces(nimState.nodeBranch.len+1) & $node.tsNodeType()
return node.saveNodeData(nimState)
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =

View file

@ -72,6 +72,8 @@ type
data*: seq[tuple[name, val: string]]
nodeBranch*: seq[string]
var
gStateCT {.compiletime, used.} = new(State)

View file

@ -43,6 +43,19 @@ proc initGrammar(): Grammar =
)
"""
arrGrammar = &"""
(array_declarator!
(pointer_declarator!
(pointer_declarator!
(type_identifier)
)
(type_identifier)
)
(type_identifier|identifier)
(identifier|number_literal)
)
"""
paramListGrammar = &"""
(parameter_list
(parameter_declaration*
@ -52,10 +65,13 @@ proc initGrammar(): Grammar =
(type_qualifier?)
(pointer_declarator!
(type_qualifier?)
{arrGrammar}
(identifier|type_identifier)
)
{arrGrammar}
(identifier|type_identifier)
)
{arrGrammar}
(abstract_pointer_declarator?
(abstract_pointer_declarator?)
)
@ -77,20 +93,7 @@ proc initGrammar(): Grammar =
)
"""
arrGrammar = &"""
(array_declarator!
(pointer_declarator!
(pointer_declarator!
(type_identifier)
)
(type_identifier)
)
(type_identifier)
(identifier|number_literal)
)
"""
template funcParamCommon(fname, pname, ptyp, pptr, pout, count, i: untyped): untyped =
template funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen: untyped): untyped =
ptyp = nimState.getIdentifier(nimState.data[i].val, nskType, fname).getType()
pptr = ""
@ -106,7 +109,14 @@ proc initGrammar(): Grammar =
count += 1
i += 1
if pptr.len != 0 or ptyp != "object":
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)
pout &= &"{pname}: array[{flen}, {getPtrType(pptr&ptyp)}], "
i += 1
elif pptr.len != 0 or ptyp != "object":
pout &= &"{pname}: {getPtrType(pptr&ptyp)}, "
# typedef int X
@ -171,12 +181,13 @@ proc initGrammar(): Grammar =
fname = nname
pout, pname, ptyp, pptr = ""
count = 1
flen = ""
while i < nimState.data.len:
if nimState.data[i].name == "function_declarator":
break
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i)
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]
@ -277,6 +288,7 @@ proc initGrammar(): Grammar =
ftyp, fname: string
fptr = ""
aptr = ""
flen = ""
while i < nimState.data.len-fend:
fptr = ""
aptr = ""
@ -323,7 +335,7 @@ proc initGrammar(): Grammar =
if nimState.data[i].name == "field_declaration":
break
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i)
funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]
@ -575,13 +587,14 @@ proc initGrammar(): Grammar =
fnname = nimState.getIdentifier(fname, nskProc)
pout, pname, ptyp, pptr = ""
count = 1
flen = ""
i += 1
while i < nimState.data.len:
if nimState.data[i].name == "function_declarator":
break
funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i)
funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]

View file

@ -64,6 +64,10 @@ void **test_call10(int **param1) {
return NULL;
}
char *test_array_param(int arr[5]) {
return NULL;
}
void multiline1(void) {}
void *multiline2(void) {

View file

@ -119,6 +119,7 @@ int test_call_param7(union UNION1 param1);
float test_call_param8(int *param1);
void *test_call9();
void **test_call10(int **param1);
char *test_array_param(int arr[5]);
// Issue #58
void

View file

@ -178,3 +178,8 @@ check TDEFL_OUT_BUF_SIZE == 85196
check TDEFL_BOGUS_1 == 2
check TDEFL_BOGUS_2 == 1024
check TDEFL_BOGUS_3 == (85196 / 2).int
var
arr: array[5, cint]
check test_array_param(arr) == nil