aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2020-04-21 23:37:37 -0500
committerGanesh Viswanathan <dev@genotrance.com>2020-04-21 23:37:37 -0500
commitf2870d8ba6fe9249f26d681b3f01cfb8b121d362 (patch)
tree3327ab80438794e36237efc3ab3a6229efccb72b
parent183eabff83aa7c2cda6fcd41427ad95b3d4fd7ed (diff)
downloadnimterop-f2870d8ba6fe9249f26d681b3f01cfb8b121d362.tar.gz
nimterop-f2870d8ba6fe9249f26d681b3f01cfb8b121d362.zip
Fix multi-header processing, perf improvements
-rw-r--r--nimterop/ast.nim7
-rw-r--r--nimterop/ast2.nim24
-rw-r--r--nimterop/getters.nim23
-rw-r--r--nimterop/toast.nim23
4 files changed, 49 insertions, 28 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 6b21bc7..1f1cac6 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -202,12 +202,11 @@ proc searchAst(root: TSNode, astTable: AstTable, gState: State) =
if node == root:
break
-proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable) =
+proc parseNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable) =
+ # Generate Nim from tree-sitter AST root node
var
fp = fullpath.replace("\\", "/")
- gState.identifiers = newTable[string, string]()
-
gState.currentHeader = getCurrentHeader(fullpath)
gState.impShort = gState.currentHeader.replace("header", "imp")
gState.sourceFile = fullpath
@@ -217,6 +216,8 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
root.searchAst(astTable, gState)
+proc printNim*(gState: State) =
+ # Print Nim generated by parseNim()
if gState.enumStr.nBl:
gecho &"{gState.enumStr}\n"
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index 9e8f582..0e7a7bb 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -1777,20 +1777,13 @@ proc printNimHeader*(gState: State) =
import nimterop/types
""" % [$now(), getAppFilename(), commandLineParams().join(" ")]
-proc printNim*(gState: State, fullpath: string, root: TSNode) =
- # Generate Nim from tree-sitter AST root node
- let
- fp = fullpath.replace("\\", "/")
+proc initNim*(gState: State) =
+ # Initialize for parseNim() one time
# Track identifiers already rendered and corresponding PNodes
gState.identifiers = newTable[string, string]()
gState.identifierNodes = newTable[string, PNode]()
- # toast objects
- gState.currentHeader = getCurrentHeader(fullpath)
- gState.impShort = gState.currentHeader.replace("header", "imp")
- gState.sourceFile = fullpath
-
# Nim compiler objects
gState.identCache = newIdentCache()
gState.config = newConfigRef()
@@ -1804,12 +1797,25 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) =
gState.typeSection = newNode(nkTypeSection)
gState.varSection = newNode(nkVarSection)
+proc parseNim*(gState: State, fullpath: string, root: TSNode) =
+ # Generate Nim from tree-sitter AST root node
+ let
+ fp = fullpath.replace("\\", "/")
+
+ # toast objects
+ gState.currentHeader = getCurrentHeader(fullpath)
+ gState.impShort = gState.currentHeader.replace("header", "imp")
+ gState.sourceFile = fullpath
+
# Setup pragmas
gState.setupPragmas(root, fp)
# Search root node and render Nim
gState.searchTree(root)
+proc printNim*(gState: State) =
+ # Print Nim generated by parseNim()
+
# Add any unused cOverride symbols to output
gState.addAllOverrideFinal()
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index 1cdd2ff..88786ce 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -351,13 +351,10 @@ proc inChildren*(node: TSNode, ntype: string): bool =
proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] =
# Get line number and column info for node
- result.line = 1
- result.col = 1
- for i in 0 .. node.tsNodeStartByte().int-1:
- if gState.code[i] == '\n':
- result.col = 0
- result.line += 1
- result.col += 1
+ let
+ point = node.tsNodeStartPoint()
+ result.line = point.row.int + 1
+ result.col = point.column.int + 1
proc getTSNodeNamedChildCountSansComments*(node: TSNode): int =
for i in 0 ..< node.len:
@@ -542,6 +539,14 @@ proc getPreprocessor*(gState: State, fullpath: string): string =
for def in gState.defines:
cmd &= &"-D{def} "
+ # Remove gcc special calls
+ if defined(posix):
+ cmd &= "-D__attribute__\\(x\\)= "
+ else:
+ cmd &= "-D__attribute__(x)= "
+
+ cmd &= "-D__restrict= "
+
cmd &= &"{fullpath.sanitizePath}"
# Include content only from file
@@ -570,9 +575,7 @@ proc getPreprocessor*(gState: State, fullpath: string): string =
if "#undef" in line:
continue
rdata.add line
- return rdata.join("\n").
- replace("__restrict", "").
- replace(re"__attribute__[ ]*\(\(.*?\)\)([ ,;])", "$1")
+ return rdata.join("\n")
converter toString*(kind: Kind): string =
return case kind:
diff --git a/nimterop/toast.nim b/nimterop/toast.nim
index 3f886e1..7dad591 100644
--- a/nimterop/toast.nim
+++ b/nimterop/toast.nim
@@ -1,4 +1,4 @@
-import os, osproc, strformat, strutils, tables, times
+import os, osproc, segfaults, strformat, strutils, tables, times
import "."/treesitter/[api, c, cpp]
@@ -40,9 +40,9 @@ proc process(gState: State, path: string, astTable: AstTable) =
gecho gState.printLisp(root)
elif gState.pnim:
if Feature.ast2 in gState.feature:
- ast2.printNim(gState, path, root)
+ ast2.parseNim(gState, path, root)
else:
- ast.printNim(gState, path, root, astTable)
+ ast.parseNim(gState, path, root, astTable)
elif gState.preprocess:
gecho gState.code
@@ -137,17 +137,28 @@ proc main(
# Process grammar into AST
let
- astTable = parseGrammar()
+ astTable =
+ if Feature.ast2 notin gState.feature:
+ parseGrammar()
+ else:
+ nil
if pgrammar:
- # Print AST of grammar
- gState.printGrammar(astTable)
+ if Feature.ast2 notin gState.feature:
+ # Print AST of grammar
+ gState.printGrammar(astTable)
elif source.nBl:
# Print source after preprocess or Nim output
if gState.pnim:
gState.printNimHeader()
+ gState.initNim()
for src in source:
gState.process(src.expandSymlinkAbs(), astTable)
+ if gState.pnim:
+ if Feature.ast2 in gState.feature:
+ ast2.printNim(gState)
+ else:
+ ast.printNim(gState)
# Close outputFile
if outputFile.len != 0: