Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Documentation support to Enum AST macro nodes #16616

Open
awr1 opened this issue Jan 6, 2021 · 1 comment
Open

Feature: Documentation support to Enum AST macro nodes #16616

awr1 opened this issue Jan 6, 2021 · 1 comment

Comments

@awr1
Copy link
Contributor

awr1 commented Jan 6, 2021

Potentially related to #12353.

proc thing() =
  ## This doc comment will get dumped
  discard
type Foo = enum
  ## Top-level description for Foo
  bar ## Field comment
  baz ## Another field comment
  qux

Appropriate documentation comments will get generated for both the procedure and the enum, however when we use dumpTree:

StmtList
  ProcDef
    Ident "thing"
    Empty
    Empty
    FormalParams
      Empty
    Empty
    Empty
    StmtList
      CommentStmt "This doc comment will get dumped"
      DiscardStmt
        Empty
  TypeSection
    TypeDef
      Ident "Foo"
      Empty
      EnumTy
        Empty
        Ident "bar"
        Ident "baz"
        Ident "qux"

There appears to be no available way for a macro (if there is, it is undocumented and doesn't work with dumpTree) to be able to generate doc comments for enums, either in terms of the top-level description of the enum or comments on each of the enumerator values.

Adding doc comment support to enum nodes will give users fuller controller over the generated documentation that comes out of code produced by a marco.

@blackmius
Copy link

blackmius commented May 7, 2023

I found that comments can be accessed from global comment store

import compiler/[ast, parser, idents, options]
import deques

var stream = """
## dsfsdfdsf
type
  ## dsgsdgsdg
  B = enum
    ## enum doc
    B1 ## first kind
    B2 ## second kind
    B3 = 5 ## third kind
  A = object
    ## object doc
    ## sdfsdf
    ## dfsdfdf
    ## dsfsdfds
    field1: int ## documentation comment
    field2: string ## multi
                ## line
                ## doc
                ## string
    when defined(something):
    field3: int ## when doc
    case kind: B
    of B1: field4: int ## bla-bla
    of B2: field5: string ## ble-ble
"""

var cache = newIdentCache()
var config = newConfigRef()

let a = parseString(stream, cache, config, "test.nim", 0)

const untraversable = { nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit, nkSym, nkIdent }

proc findComments(node: PNode) =
  var queue = initDeque[PNode]()
  queue.addLast(node)
  while queue.len != 0:
    let node = queue.popFirst()
    if node.comment != "":
      echo node.kind, ' ', node.comment
    if node.kind in untraversable:
      continue
    for i in 0..<node.len:
      queue.addLast(node[i])

findComments(a)

they are assigned every time the parser thinks there will be a comment
https://github.com/nim-lang/Nim/blob/devel/compiler/parser.nim#L203
and stored in https://github.com/nim-lang/Nim/blob/devel/compiler/ast.nim#L1025
which can accessed later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants