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:
|
if node == root:
|
||||||
break
|
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
|
var
|
||||||
fp = fullpath.replace("\\", "/")
|
fp = fullpath.replace("\\", "/")
|
||||||
|
|
||||||
gState.identifiers = newTable[string, string]()
|
|
||||||
|
|
||||||
gState.currentHeader = getCurrentHeader(fullpath)
|
gState.currentHeader = getCurrentHeader(fullpath)
|
||||||
gState.impShort = gState.currentHeader.replace("header", "imp")
|
gState.impShort = gState.currentHeader.replace("header", "imp")
|
||||||
gState.sourceFile = fullpath
|
gState.sourceFile = fullpath
|
||||||
|
|
@ -217,6 +216,8 @@ proc printNim*(gState: State, fullpath: string, root: TSNode, astTable: AstTable
|
||||||
|
|
||||||
root.searchAst(astTable, gState)
|
root.searchAst(astTable, gState)
|
||||||
|
|
||||||
|
proc printNim*(gState: State) =
|
||||||
|
# Print Nim generated by parseNim()
|
||||||
if gState.enumStr.nBl:
|
if gState.enumStr.nBl:
|
||||||
gecho &"{gState.enumStr}\n"
|
gecho &"{gState.enumStr}\n"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1777,20 +1777,13 @@ proc printNimHeader*(gState: State) =
|
||||||
import nimterop/types
|
import nimterop/types
|
||||||
""" % [$now(), getAppFilename(), commandLineParams().join(" ")]
|
""" % [$now(), getAppFilename(), commandLineParams().join(" ")]
|
||||||
|
|
||||||
proc printNim*(gState: State, fullpath: string, root: TSNode) =
|
proc initNim*(gState: State) =
|
||||||
# Generate Nim from tree-sitter AST root node
|
# Initialize for parseNim() one time
|
||||||
let
|
|
||||||
fp = fullpath.replace("\\", "/")
|
|
||||||
|
|
||||||
# Track identifiers already rendered and corresponding PNodes
|
# Track identifiers already rendered and corresponding PNodes
|
||||||
gState.identifiers = newTable[string, string]()
|
gState.identifiers = newTable[string, string]()
|
||||||
gState.identifierNodes = newTable[string, PNode]()
|
gState.identifierNodes = newTable[string, PNode]()
|
||||||
|
|
||||||
# toast objects
|
|
||||||
gState.currentHeader = getCurrentHeader(fullpath)
|
|
||||||
gState.impShort = gState.currentHeader.replace("header", "imp")
|
|
||||||
gState.sourceFile = fullpath
|
|
||||||
|
|
||||||
# Nim compiler objects
|
# Nim compiler objects
|
||||||
gState.identCache = newIdentCache()
|
gState.identCache = newIdentCache()
|
||||||
gState.config = newConfigRef()
|
gState.config = newConfigRef()
|
||||||
|
|
@ -1804,12 +1797,25 @@ proc printNim*(gState: State, fullpath: string, root: TSNode) =
|
||||||
gState.typeSection = newNode(nkTypeSection)
|
gState.typeSection = newNode(nkTypeSection)
|
||||||
gState.varSection = newNode(nkVarSection)
|
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
|
# Setup pragmas
|
||||||
gState.setupPragmas(root, fp)
|
gState.setupPragmas(root, fp)
|
||||||
|
|
||||||
# Search root node and render Nim
|
# Search root node and render Nim
|
||||||
gState.searchTree(root)
|
gState.searchTree(root)
|
||||||
|
|
||||||
|
proc printNim*(gState: State) =
|
||||||
|
# Print Nim generated by parseNim()
|
||||||
|
|
||||||
# Add any unused cOverride symbols to output
|
# Add any unused cOverride symbols to output
|
||||||
gState.addAllOverrideFinal()
|
gState.addAllOverrideFinal()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -351,13 +351,10 @@ proc inChildren*(node: TSNode, ntype: string): bool =
|
||||||
|
|
||||||
proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] =
|
proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] =
|
||||||
# Get line number and column info for node
|
# Get line number and column info for node
|
||||||
result.line = 1
|
let
|
||||||
result.col = 1
|
point = node.tsNodeStartPoint()
|
||||||
for i in 0 .. node.tsNodeStartByte().int-1:
|
result.line = point.row.int + 1
|
||||||
if gState.code[i] == '\n':
|
result.col = point.column.int + 1
|
||||||
result.col = 0
|
|
||||||
result.line += 1
|
|
||||||
result.col += 1
|
|
||||||
|
|
||||||
proc getTSNodeNamedChildCountSansComments*(node: TSNode): int =
|
proc getTSNodeNamedChildCountSansComments*(node: TSNode): int =
|
||||||
for i in 0 ..< node.len:
|
for i in 0 ..< node.len:
|
||||||
|
|
@ -542,6 +539,14 @@ proc getPreprocessor*(gState: State, fullpath: string): string =
|
||||||
for def in gState.defines:
|
for def in gState.defines:
|
||||||
cmd &= &"-D{def} "
|
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}"
|
cmd &= &"{fullpath.sanitizePath}"
|
||||||
|
|
||||||
# Include content only from file
|
# Include content only from file
|
||||||
|
|
@ -570,9 +575,7 @@ proc getPreprocessor*(gState: State, fullpath: string): string =
|
||||||
if "#undef" in line:
|
if "#undef" in line:
|
||||||
continue
|
continue
|
||||||
rdata.add line
|
rdata.add line
|
||||||
return rdata.join("\n").
|
return rdata.join("\n")
|
||||||
replace("__restrict", "").
|
|
||||||
replace(re"__attribute__[ ]*\(\(.*?\)\)([ ,;])", "$1")
|
|
||||||
|
|
||||||
converter toString*(kind: Kind): string =
|
converter toString*(kind: Kind): string =
|
||||||
return case kind:
|
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]
|
import "."/treesitter/[api, c, cpp]
|
||||||
|
|
||||||
|
|
@ -40,9 +40,9 @@ proc process(gState: State, path: string, astTable: AstTable) =
|
||||||
gecho gState.printLisp(root)
|
gecho gState.printLisp(root)
|
||||||
elif gState.pnim:
|
elif gState.pnim:
|
||||||
if Feature.ast2 in gState.feature:
|
if Feature.ast2 in gState.feature:
|
||||||
ast2.printNim(gState, path, root)
|
ast2.parseNim(gState, path, root)
|
||||||
else:
|
else:
|
||||||
ast.printNim(gState, path, root, astTable)
|
ast.parseNim(gState, path, root, astTable)
|
||||||
elif gState.preprocess:
|
elif gState.preprocess:
|
||||||
gecho gState.code
|
gecho gState.code
|
||||||
|
|
||||||
|
|
@ -137,17 +137,28 @@ proc main(
|
||||||
|
|
||||||
# Process grammar into AST
|
# Process grammar into AST
|
||||||
let
|
let
|
||||||
astTable = parseGrammar()
|
astTable =
|
||||||
|
if Feature.ast2 notin gState.feature:
|
||||||
|
parseGrammar()
|
||||||
|
else:
|
||||||
|
nil
|
||||||
|
|
||||||
if pgrammar:
|
if pgrammar:
|
||||||
# Print AST of grammar
|
if Feature.ast2 notin gState.feature:
|
||||||
gState.printGrammar(astTable)
|
# Print AST of grammar
|
||||||
|
gState.printGrammar(astTable)
|
||||||
elif source.nBl:
|
elif source.nBl:
|
||||||
# Print source after preprocess or Nim output
|
# Print source after preprocess or Nim output
|
||||||
if gState.pnim:
|
if gState.pnim:
|
||||||
gState.printNimHeader()
|
gState.printNimHeader()
|
||||||
|
gState.initNim()
|
||||||
for src in source:
|
for src in source:
|
||||||
gState.process(src.expandSymlinkAbs(), astTable)
|
gState.process(src.expandSymlinkAbs(), astTable)
|
||||||
|
if gState.pnim:
|
||||||
|
if Feature.ast2 in gState.feature:
|
||||||
|
ast2.printNim(gState)
|
||||||
|
else:
|
||||||
|
ast.printNim(gState)
|
||||||
|
|
||||||
# Close outputFile
|
# Close outputFile
|
||||||
if outputFile.len != 0:
|
if outputFile.len != 0:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue