diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim index 2ae4a70..474ec69 100644 --- a/nimterop/ast2.nim +++ b/nimterop/ast2.nim @@ -4,7 +4,7 @@ import options as opts import compiler/[ast, idents, lineinfos, modulegraphs, msgs, options, renderer] -import "."/treesitter/[api, c, cpp] +import "."/treesitter/api import "."/[globals, getters, exprparser, comphelp, tshelp] @@ -1391,7 +1391,9 @@ proc addEnum(gState: State, node: TSNode) = # Create const for fields var fnames: HashSet[string] - fvalSections: seq[tuple[fname: string, fval: string, cexpr: Option[TSNode]]] + # Hold all of field information so that we can add all of them + # after the const identifiers has been updated + fieldDeclarations: seq[tuple[fname: string, fval: string, cexpr: Option[TSNode]]] for i in 0 .. enumlist.len - 1: let en = enumlist[i] @@ -1410,9 +1412,9 @@ proc addEnum(gState: State, node: TSNode) = fval = &"({prev} + 1).{name}" if en.len > 1 and en[1].getName() in gEnumVals: - fvalSections.add((fname, "", some(en[1]))) + fieldDeclarations.add((fname, "", some(en[1]))) else: - fvalSections.add((fname, fval, none(TSNode))) + fieldDeclarations.add((fname, fval, none(TSNode))) fnames.incl fname prev = fname @@ -1422,7 +1424,7 @@ proc addEnum(gState: State, node: TSNode) = gState.constIdentifiers.incl fnames # parseCExpression requires all const identifiers to be present for the enum - for (fname, fval, cexprNode) in fvalSections: + for (fname, fval, cexprNode) in fieldDeclarations: var fval = fval if cexprNode.isSome: fval = "(" & $gState.parseCExpression(gState.getNodeVal(cexprNode.get()), name) & ")." & name diff --git a/nimterop/globals.nim b/nimterop/globals.nim index d433ab5..5db17a3 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -76,7 +76,9 @@ type # All const names for enum casting constIdentifiers*: HashSet[string] - # All symbols that have been skipped + # All symbols that have been skipped due to + # being unwrappable or the user provided + # override is blank skippedSyms*: HashSet[string] # Legacy ast fields, remove when ast2 becomes default diff --git a/nimterop/tshelp.nim b/nimterop/tshelp.nim index f234bc0..109321c 100644 --- a/nimterop/tshelp.nim +++ b/nimterop/tshelp.nim @@ -1,11 +1,9 @@ -template withCodeAst*(inputCode: string, inputMode: string, body: untyped): untyped = - ## A simple template to inject the TSNode into a body of code +import "."/treesitter/[c, cpp] - # This section is needed to be able to reference - # mode in strformat calls - let - code = inputCode - mode {.inject.} = inputMode +template withCodeAst*(code: string, mode: string, body: untyped): untyped = + ## A simple template to inject the TSNode into a body of code + mixin treeSitterC + mixin treeSitterCpp var parser = tsParserNew() defer: @@ -18,7 +16,7 @@ template withCodeAst*(inputCode: string, inputMode: string, body: untyped): unty elif mode == "cpp": doAssert parser.tsParserSetLanguage(treeSitterCpp()), "Failed to load C++ parser" else: - doAssert false, &"Invalid parser {mode}" + doAssert false, "Invalid parser " & mode var tree = parser.tsParserParseString(nil, code.cstring, code.len.uint32)