diff options
| author | Joey Yakimowich-Payne <jyapayne@gmail.com> | 2020-04-14 20:38:35 -0600 |
|---|---|---|
| committer | Joey Yakimowich-Payne <jyapayne@gmail.com> | 2020-04-23 17:30:28 -0600 |
| commit | 80018f43cf35214e95c4fa777f65bb756be1db24 (patch) | |
| tree | b08ad0cfb824cfce8592cfe8235615021e8732aa | |
| parent | 43dd43e3183178e71abd3319290c566cb4dd80a9 (diff) | |
| download | nimterop-80018f43cf35214e95c4fa777f65bb756be1db24.tar.gz nimterop-80018f43cf35214e95c4fa777f65bb756be1db24.zip | |
WIP Ast printing
| -rw-r--r-- | nimterop/ast2.nim | 30 | ||||
| -rw-r--r-- | nimterop/getters.nim | 21 |
2 files changed, 44 insertions, 7 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim index ce45f1d..43b6b35 100644 --- a/nimterop/ast2.nim +++ b/nimterop/ast2.nim @@ -4,10 +4,35 @@ import regex import compiler/[ast, idents, lineinfos, modulegraphs, msgs, options, parser, renderer] -import "."/treesitter/api +import "."/treesitter/[api, c, cpp] import "."/[globals, getters] +proc getCCodeAst*(gState: State, code: string): string = + var parser = tsParserNew() + var code = code + + defer: + parser.tsParserDelete() + + + doAssert code.nBl, "Empty code" + if gState.mode == "c": + doAssert parser.tsParserSetLanguage(treeSitterC()), "Failed to load C parser" + elif gState.mode == "cpp": + doAssert parser.tsParserSetLanguage(treeSitterCpp()), "Failed to load C++ parser" + else: + doAssert false, &"Invalid parser {gState.mode}" + + var + tree = parser.tsParserParseString(nil, code.cstring, code.len.uint32) + root = tree.tsTreeRootNode() + + defer: + tree.tsTreeDelete() + + return code.printLisp(root) + proc getPtrType*(str: string): string = result = case str: of "cchar": @@ -59,6 +84,9 @@ proc getLit*(gState: State, str: string, expression = false): PNode = result = newStrNode(nkStrLit, str[1 .. ^2]) else: + decho "Macro AST:" + decho str + decho nimState.gState.getCCodeAst(str) let str = if expression: gState.getNimExpression(str) diff --git a/nimterop/getters.nim b/nimterop/getters.nim index 2d8d9bb..4ffb5d7 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -228,9 +228,12 @@ proc getName*(node: TSNode): string {.inline.} = if not node.isNil: return $node.tsNodeType() -proc getNodeVal*(gState: State, node: TSNode): string = +proc getNodeVal*(code: var string, node: TSNode): string = if not node.isNil: - return gState.code[node.tsNodeStartByte() .. node.tsNodeEndByte()-1].strip() + return code[node.tsNodeStartByte() .. node.tsNodeEndByte()-1].strip() + +proc getNodeVal*(gState: State, node: TSNode): string = + gState.code.getNodeVal(node) proc getAtom*(node: TSNode): TSNode = if not node.isNil: @@ -349,13 +352,16 @@ proc inChildren*(node: TSNode, ntype: string): bool = result = true break -proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] = +proc getLineCol*(code: var string, node: TSNode): tuple[line, col: int] = # Get line number and column info for node let point = node.tsNodeStartPoint() result.line = point.row.int + 1 result.col = point.column.int + 1 +proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] = + getLineCol(gState.code, node) + proc getTSNodeNamedChildCountSansComments*(node: TSNode): int = for i in 0 ..< node.len: if node.getName() != "comment": @@ -374,7 +380,7 @@ proc getPxName*(node: TSNode, offset: int): string = if count == offset and not np.isNil: return np.getName() -proc printLisp*(gState: State, root: TSNode): string = +proc printLisp*(code: var string, root: TSNode): string = var node = root nextnode: TSNode @@ -384,10 +390,10 @@ proc printLisp*(gState: State, root: TSNode): string = if not node.isNil and depth > -1: result &= spaces(depth) let - (line, col) = gState.getLineCol(node) + (line, col) = code.getLineCol(node) result &= &"({$node.tsNodeType()} {line} {col} {node.tsNodeEndByte() - node.tsNodeStartByte()}" let - val = gState.getNodeVal(node) + val = code.getNodeVal(node) if "\n" notin val and " " notin val: result &= &" \"{val}\"" else: @@ -419,6 +425,9 @@ proc printLisp*(gState: State, root: TSNode): string = if node == root: break +proc printLisp*(gState: State, root: TSNode): string = + printLisp(gState.code, root) + proc getCommented*(str: string): string = "\n# " & str.strip().replace("\n", "\n# ") |
