Fix multi-header processing, perf improvements
This commit is contained in:
parent
183eabff83
commit
f2870d8ba6
4 changed files with 49 additions and 28 deletions
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue