Skip to content

Commit

Permalink
docstring: use cursor as input
Browse files Browse the repository at this point in the history
Finally, we get to make the parser completely oblivious to the rendering
layer! Hopefully docstring and doccursor can now evolve to something
better.

Closes #180. Though a lot of the potential is still to be realized.
  • Loading branch information
BrunoMSantos committed Nov 18, 2023
1 parent 34282b3 commit 0cd1f59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 66 deletions.
40 changes: 25 additions & 15 deletions src/hawkmoth/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,27 @@ class Docstring():
_indent = 0
_fmt = ''

def __init__(self, domain='c', text=None, name=None,
decl_name=None, ttype=None, args=None,
quals=None, meta=None, nest=0):
self._text = text
self._name = name
self._decl_name = decl_name
self._ttype = ttype
self._args = args
self._quals = quals
self._meta = meta
def __init__(self, cursor=None, text=None, meta=None, nest=0):
if cursor:
self._args = cursor.args
self._decl_name = cursor.decl_name
self._domain = cursor.domain
self._meta = cursor.meta
self._name = cursor.name
self._quals = cursor.quals
self._text = cursor.comment
self._ttype = cursor.type
else:
self._args = None
self._decl_name = None
self._domain = None
self._meta = meta
self._name = None
self._quals = None
self._text = text
self._ttype = None

self._nest = nest
self._domain = domain
self._children = []

def _match(self, filter_types=None, filter_names=None):
Expand Down Expand Up @@ -265,8 +274,9 @@ def walk(self, recurse=True, filter_types=None, filter_names=None):

class RootDocstring(_CompoundDocstring):
def __init__(self, filename=None, domain='c', clang_args=None):
super().__init__(domain=domain)
super().__init__()
self._filename = filename
self._domain = domain
self._clang_args = clang_args

def get_filename(self):
Expand Down Expand Up @@ -294,9 +304,9 @@ class EnumeratorDocstring(Docstring):
_indent = 1
_fmt = '.. {domain}:enumerator:: {name}{value}'

def __init__(self, domain, name, value, text, meta, nest):
self._value = value
super().__init__(domain=domain, name=name, text=text, meta=meta, nest=nest)
def __init__(self, cursor, nest=0):
self._value = cursor.value
super().__init__(cursor=cursor, nest=nest)

def _get_header_lines(self):
value = f' = {self._value}' if self._value is not None else ''
Expand Down
66 changes: 15 additions & 51 deletions src/hawkmoth/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,47 +193,29 @@ def _comment_extract(tu):
return top_level_comments, comments

def _recursive_parse(errors, cursor, nest):
domain = cursor.domain
name = cursor.name
decl_name = cursor.decl_name
ttype = cursor.type
text = cursor.comment
meta = cursor.meta
args = cursor.args
quals = cursor.quals

if cursor.kind == CursorKind.MACRO_DEFINITION:
# FIXME: check args against comment
args = cursor.args

if args is None:
ds = docstring.MacroDocstring(domain=domain, text=text,
nest=nest, name=name, meta=meta)
if cursor.args is None:
ds = docstring.MacroDocstring(cursor=cursor, nest=nest)
else:
ds = docstring.MacroFunctionDocstring(domain=domain, text=text,
nest=nest, name=name,
args=args, meta=meta)
ds = docstring.MacroFunctionDocstring(cursor=cursor, nest=nest)

return [ds]

elif cursor.kind in [CursorKind.VAR_DECL, CursorKind.FIELD_DECL]:

if cursor.kind == CursorKind.VAR_DECL:
ds = docstring.VarDocstring(domain=domain, text=text, nest=nest,
name=name, decl_name=decl_name,
ttype=ttype, meta=meta)
ds = docstring.VarDocstring(cursor=cursor, nest=nest)
else:
ds = docstring.MemberDocstring(domain=domain, text=text, nest=nest,
name=name, decl_name=decl_name,
ttype=ttype, meta=meta)
ds = docstring.MemberDocstring(cursor=cursor, nest=nest)

return [ds]

elif cursor.kind == CursorKind.TYPEDEF_DECL:
# FIXME: function pointers typedefs.

ds = docstring.TypeDocstring(domain=domain, text=text,
nest=nest, name=ttype, meta=meta)
ds = docstring.TypeDocstring(cursor=cursor, nest=nest)

return [ds]

Expand All @@ -244,26 +226,16 @@ def _recursive_parse(errors, cursor, nest):
CursorKind.CLASS_TEMPLATE]:

if cursor.kind == CursorKind.STRUCT_DECL:
ds = docstring.StructDocstring(domain=domain, text=text,
nest=nest, name=name,
decl_name=decl_name, meta=meta)
ds = docstring.StructDocstring(cursor=cursor, nest=nest)
elif cursor.kind == CursorKind.UNION_DECL:
ds = docstring.UnionDocstring(domain=domain, text=text,
nest=nest, name=name,
decl_name=decl_name, meta=meta)
ds = docstring.UnionDocstring(cursor=cursor, nest=nest)
elif cursor.kind == CursorKind.ENUM_DECL:
if cursor.is_scoped_enum:
ds = docstring.EnumClassDocstring(domain=domain, text=text,
nest=nest, name=name,
decl_name=decl_name, meta=meta)
ds = docstring.EnumClassDocstring(cursor=cursor, nest=nest)
else:
ds = docstring.EnumDocstring(domain=domain, text=text,
nest=nest, name=name,
decl_name=decl_name, meta=meta)
ds = docstring.EnumDocstring(cursor=cursor, nest=nest)
elif cursor.kind in [CursorKind.CLASS_DECL, CursorKind.CLASS_TEMPLATE]:
ds = docstring.ClassDocstring(domain=domain, text=text,
nest=nest, name=name,
decl_name=decl_name, meta=meta)
ds = docstring.ClassDocstring(cursor=cursor, nest=nest)

for c in cursor.get_children():
if c.comment:
Expand All @@ -272,27 +244,19 @@ def _recursive_parse(errors, cursor, nest):
return [ds]

elif cursor.kind == CursorKind.ENUM_CONSTANT_DECL:
ds = docstring.EnumeratorDocstring(domain=domain, name=name,
value=cursor.value, text=text,
meta=meta, nest=nest)
ds = docstring.EnumeratorDocstring(cursor=cursor, nest=nest)

return [ds]

elif cursor.kind == CursorKind.FUNCTION_DECL:
ds = docstring.FunctionDocstring(domain=domain, text=text,
nest=nest, name=name,
ttype=ttype, args=args,
quals='', meta=meta)
ds = docstring.FunctionDocstring(cursor=cursor, nest=nest)
return [ds]

elif cursor.kind in [CursorKind.CONSTRUCTOR,
CursorKind.DESTRUCTOR,
CursorKind.CXX_METHOD,
CursorKind.FUNCTION_TEMPLATE]:
ds = docstring.FunctionDocstring(domain=domain, text=text,
nest=nest, name=name,
ttype=ttype, args=args,
quals=quals, meta=meta)
ds = docstring.FunctionDocstring(cursor=cursor, nest=nest)
return [ds]

# If we reach here, nothing matched i.e. there's a documentation comment
Expand All @@ -301,7 +265,7 @@ def _recursive_parse(errors, cursor, nest):
errors.append(ParserError(ErrorLevel.WARNING, cursor.location.file.name,
cursor.location.line, message))

ds = docstring.TextDocstring(text=text, meta=meta)
ds = docstring.TextDocstring(text=cursor.comment, meta=cursor.meta)

return [ds]

Expand Down

0 comments on commit 0cd1f59

Please sign in to comment.