Closed Dmunch04 closed 3 years ago
A struct declaration contains a struct body (careful: might be null, in case your code is a forward declaration like struct Foo;
)
which contains a list of declarations
where you either have nested structs, classes, etc or variable declarations in there. With those you know what members you have.
and also how would i check what kind of top level the the variable is under
I'm not sure what you mean by that, can you explain further what you want to do? If you mean you want to know which struct the variable is in in a nested struct, if you go through the declarations and you find another StructDeclaration, it will be a member of the struct body of that declaration.
Yes I saw that the struct body had the declarations
field, but i can't seem to access the data that i want (the variables comment), or any other useful info tbh.
And what I mean with
and also how would i check what kind of top level the the variable is under
Is that fx here
void foo()
{
int bar = 5;
}
How would I know the "top-level" of the variable bar
? (Which in the above example would be the function foo
)
Also how would I get the string representation of IdType
field? Like fx. the id 130
would be int
right? But how would I figure that out by code?
And btw how would I be able to determine the comment type of the StructDeclaration
's comment
field?
you need to keep track of the parent yourself when visiting your module. For example you could have a member string parent;
and on every visit on types which interest you you do
override void visit(const FunctionDeclaration f)
{
string previous = parent; // save last parent on the stack
parent = f.name.text; // set parent for all child AST nodes
scope (exit) parent = previous; // restore parent when exiting this node
f.accept(this);
}
IdType
is basically an enum of the different token types as defined here: https://github.com/dlang-community/libdparse/blob/6f0893f6b07ec561d382f26c81e74f7788056828/src/dparse/lexer.d#L93
To get the name of an IdType, use str(c)
. Note that you want to use token.text
instead of str(token.type)
for all tokens which are in the "dynamicTokens" array. For example code which does this see https://github.com/Pure-D/workspace-d/blob/a2af7c65c81e9523de208e3a82cb5b300ce53cc1/source/workspaced/dparseext.d#L52-L72 (mirrors the dynamicTokens array because it is private in libdparse)
The comment field is only very basic and contains ddoc. If you want full control over the comments, use the tokens
member of any AST node and check the attached comment tokens (before/after) on each token you want to analyze. For a struct this would basically mean to check the first token (which is the struct
token) and check the before-comments which are the raw comment texts before any ddoc processing.
Oh I see. How would I go about getting the string represntation of something like a FunctionDeclaration
's returnType
? So far I've got this, although isn't there a better way?
str(decl.returnType.type2.builtinType); // builtin types
decl.returnType.tokens[0].text; // other types such as string
there is a formatter in libdparse (https://github.com/dlang-community/libdparse/blob/6f0893f6b07ec561d382f26c81e74f7788056828/src/dparse/formatter.d#L58) which you can use to format most AST nodes you have back to a string, representing what is written in code.
You would use it with an appender!string
as the first argument and decl.returnType
as second argument.
Yes that works. Thank you. I think that answered all my questions, but if I come across more I'll open another issue. Thanks for the help!
how would i access the variables inside a struct? currently they're only accessible with the variable declaration function. and also how would i check what kind of top level the the variable is under