Add comments for enums and procs

This commit is contained in:
Joey Yakimowich-Payne 2020-04-24 20:42:30 -06:00 • committed by genotrance
commit dc1c4bc177
2 changed files with 45 additions and 12 deletions

View file

@ -1393,14 +1393,18 @@ proc addEnum(gState: State, node: TSNode) =
fnames: HashSet[string] fnames: HashSet[string]
# Hold all of field information so that we can add all of them # Hold all of field information so that we can add all of them
# after the const identifiers has been updated # after the const identifiers has been updated
fieldDeclarations: seq[tuple[fname: string, fval: string, cexpr: Option[TSNode]]] fieldDeclarations: seq[tuple[fname: string, fval: string, cexpr: Option[TSNode], comment: Option[TSNode]]]
for i in 0 .. enumlist.len - 1: for i in 0 .. enumlist.len - 1:
let let
en = enumlist[i] en = enumlist[i]
if en.getName() == "comment": if en.getName() == "comment":
continue continue
let let
fname = gState.getIdentifier(gState.getNodeVal(en.getAtom()), nskEnumField) atom = en.getAtom()
commentNode = en.getInlineCommentNode()
fname = gState.getIdentifier(gState.getNodeVal(atom), nskEnumField)
if fname.nBl and gState.addNewIdentifer(fname): if fname.nBl and gState.addNewIdentifer(fname):
var var
fval = "" fval = ""
@ -1412,9 +1416,9 @@ proc addEnum(gState: State, node: TSNode) =
fval = &"({prev} + 1).{name}" fval = &"({prev} + 1).{name}"
if en.len > 1 and en[1].getName() in gEnumVals: if en.len > 1 and en[1].getName() in gEnumVals:
fieldDeclarations.add((fname, "", some(en[1]))) fieldDeclarations.add((fname, "", some(en[1]), commentNode))
else: else:
fieldDeclarations.add((fname, fval, none(TSNode))) fieldDeclarations.add((fname, fval, none(TSNode), commentNode))
fnames.incl fname fnames.incl fname
prev = fname prev = fname
@ -1424,18 +1428,20 @@ proc addEnum(gState: State, node: TSNode) =
gState.constIdentifiers.incl fnames gState.constIdentifiers.incl fnames
# parseCExpression requires all const identifiers to be present for the enum # parseCExpression requires all const identifiers to be present for the enum
for (fname, fval, cexprNode) in fieldDeclarations: for (fname, fval, cexprNode, commentNode) in fieldDeclarations:
var fval = fval var fval = fval
if cexprNode.isSome: if cexprNode.isSome:
fval = "(" & $gState.parseCExpression(gState.getNodeVal(cexprNode.get()), name) & ")." & name fval = "(" & $gState.parseCExpression(gState.getNodeVal(cexprNode.get()), name) & ")." & name
# Cannot use newConstDef() since parseString(fval) adds backticks to and/or # Cannot use newConstDef() since parseString(fval) adds backticks to and/or
gState.constSection.add gState.parseString(&"const {fname}* = {fval}")[0][0] let constNode = gState.parseString(&"const {fname}* = {fval}")[0][0]
constNode.comment = gState.getCommentVal(commentNode)
gState.constSection.add constNode
# Add other names # Add other names
if node.getName() == "type_definition" and node.len > 1: if node.getName() == "type_definition" and node.len > 1:
gState.addTypeTyped(node, ftname = name, offset = offset) gState.addTypeTyped(node, ftname = name, offset = offset)
proc addProcVar(gState: State, node, rnode: TSNode) = proc addProcVar(gState: State, node, rnode: TSNode, commentNode: Option[TSNode]) =
# Add a proc variable # Add a proc variable
decho("addProcVar()") decho("addProcVar()")
let let
@ -1488,12 +1494,13 @@ proc addProcVar(gState: State, node, rnode: TSNode) =
# nkEmpty() # nkEmpty()
# ) # )
identDefs.comment = gState.getCommentVal(commentNode)
# nkVarSection.add # nkVarSection.add
gState.varSection.add identDefs gState.varSection.add identDefs
gState.printDebug(identDefs) gState.printDebug(identDefs)
proc addProc(gState: State, node, rnode: TSNode) = proc addProc(gState: State, node, rnode: TSNode, commentNode: Option[TSNode]) =
# Add a proc # Add a proc
# #
# `node` is the `nth` child of (declaration) # `node` is the `nth` child of (declaration)
@ -1599,6 +1606,8 @@ proc addProc(gState: State, node, rnode: TSNode) =
procDef.add newNode(nkEmpty) procDef.add newNode(nkEmpty)
procDef.add newNode(nkEmpty) procDef.add newNode(nkEmpty)
procDef.comment = gState.getCommentVal(commentNode)
# nkProcSection.add # nkProcSection.add
gState.procSection.add procDef gState.procSection.add procDef
@ -1609,18 +1618,20 @@ proc addDecl(gState: State, node: TSNode) =
decho("addDecl()") decho("addDecl()")
gState.printDebug(node) gState.printDebug(node)
let let
start = getStartAtom(node) start = getStartAtom(node)
commentNode = node.getCommentNode()
for i in start+1 ..< node.len: for i in start+1 ..< node.len:
if not node[i].firstChildInTree("function_declarator").isNil: if not node[i].firstChildInTree("function_declarator").isNil:
# Proc declaration - var or actual proc # Proc declaration - var or actual proc
if node[i].getAtom().getPxName(1) == "pointer_declarator": if node[i].getAtom().getPxName(1) == "pointer_declarator":
# proc var # proc var
gState.addProcVar(node[i], node[start]) gState.addProcVar(node[i], node[start], commentNode)
else: else:
# proc # proc
gState.addProc(node[i], node[start]) gState.addProc(node[i], node[start], commentNode)
else: else:
# Regular var # Regular var
discard discard
@ -1632,11 +1643,14 @@ proc addDef(gState: State, node: TSNode) =
# and will fail at link time # and will fail at link time
decho("addDef()") decho("addDef()")
gState.printDebug(node) gState.printDebug(node)
let let
start = getStartAtom(node) start = getStartAtom(node)
commentNode = node.getCommentNode()
if node[start+1].getName() == "function_declarator": if node[start+1].getName() == "function_declarator":
if gState.isIncludeHeader(): if gState.isIncludeHeader():
gState.addProc(node[start+1], node[start]) gState.addProc(node[start+1], node[start], commentNode)
else: else:
gecho &"\n# proc '$1' skipped - static inline procs require 'includeHeader'" % gecho &"\n# proc '$1' skipped - static inline procs require 'includeHeader'" %
gState.getNodeVal(node[start+1].getAtom()) gState.getNodeVal(node[start+1].getAtom())

View file

@ -1,4 +1,4 @@
import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times import dynlib, macros, os, sequtils, sets, strformat, strutils, tables, times, options
import regex import regex
@ -456,6 +456,9 @@ proc printLisp*(gState: State, root: TSNode): string =
proc getCommented*(str: string): string = proc getCommented*(str: string): string =
"\n# " & str.strip().replace("\n", "\n# ") "\n# " & str.strip().replace("\n", "\n# ")
proc getDocStrCommented*(str: string): string =
"\n## " & str.strip().replace("\n", "\n## ")
proc printTree*(gState: State, pnode: PNode, offset = ""): string = proc printTree*(gState: State, pnode: PNode, offset = ""): string =
if not pnode.isNil and gState.debug and pnode.kind != nkNone: if not pnode.isNil and gState.debug and pnode.kind != nkNone:
result &= "\n# " & offset & $pnode.kind & "(" result &= "\n# " & offset & $pnode.kind & "("
@ -634,6 +637,22 @@ proc getNameKind*(name: string): tuple[name: string, kind: Kind, recursive: bool
if result.kind != exactlyOne: if result.kind != exactlyOne:
result.name = result.name[0 .. ^2] result.name = result.name[0 .. ^2]
proc getCommentVal*(gState: State, commentNode: Option[TSNode]): string =
if commentNode.isSome():
result = gState.getNodeVal(commentNode.get()).replace(re"( *(/\*\*|\*\*/|\*/|\*))", "").strip()
proc getCommentNode*(node: TSNode): Option[TSNode] =
result = none(TSNode)
let prevSibling = node.tsNodePrevNamedSibling()
if not prevSibling.isNil and prevSibling.getName() == "comment":
result = some(prevSibling)
proc getInlineCommentNode*(node: TSNode): Option[TSNode] =
result = none(TSNode)
let nextSibling = node.tsNodeNextNamedSibling()
if not nextSibling.isNil and nextSibling.getName() == "comment":
result = some(nextSibling)
proc getTSNodeNamedChildNames*(node: TSNode): seq[string] = proc getTSNodeNamedChildNames*(node: TSNode): seq[string] =
if node.tsNodeNamedChildCount() != 0: if node.tsNodeNamedChildCount() != 0:
for i in 0 .. node.tsNodeNamedChildCount()-1: for i in 0 .. node.tsNodeNamedChildCount()-1: