February 22, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On Thu, 22 Feb 2018 10:41:48 +0000, psychoticRabbit wrote:
> On Tuesday, 20 February 2018 at 15:26:12 UTC, Adam D. Ruppe wrote:
>> dmd -X spits out the json file with a list of functions and classes and other stuff. Then you can just filter that.
>
> do you know why the first and last character of the output from "dmd -o- -X somefile.d" are [ and ] with all the json inbetween.
>
> I'm don't really know json (never had a need to know) but as I try different json parsers, the first thing I have to do (before giving it to the json parser), is strip off the first and last character of that output.
>
> so why put them there in the first place, is my question.
They form an array.
`[1, 2, 3]` is an array of numbers, and `[{"a":1}, {"b":2}, {"c":3}]` is an array of objects.
|
February 22, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to rjframe | On Thursday, 22 February 2018 at 11:32:59 UTC, rjframe wrote:
> On Thu, 22 Feb 2018 10:41:48 +0000, psychoticRabbit wrote:
>
>> On Tuesday, 20 February 2018 at 15:26:12 UTC, Adam D. Ruppe wrote:
>>> dmd -X spits out the json file with a list of functions and classes and other stuff. Then you can just filter that.
>>
>> do you know why the first and last character of the output from "dmd -o- -X somefile.d" are [ and ] with all the json inbetween.
>>
>> I'm don't really know json (never had a need to know) but as I try different json parsers, the first thing I have to do (before giving it to the json parser), is strip off the first and last character of that output.
>>
>> so why put them there in the first place, is my question.
>
> They form an array.
>
> `[1, 2, 3]` is an array of numbers, and `[{"a":1}, {"b":2}, {"c":3}]` is an array of objects.
here is my point though:
=============
module test;
import std.stdio, std.file, std.json;
void main()
{
string myFile= "source.json"; // a file produced by: dmd -o- -X source.d
string js = readText(myFile);
JSONValue j = parseJSON( js[1..$-1] ); // why do I have to do this??
writefln("%s = %s", j["kind"].str, j["name"].str);
writefln("file = %s", j["file"].str);
}
==============
|
February 22, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On 02/22/2018 12:54 PM, psychoticRabbit wrote:
> module test;
>
> import std.stdio, std.file, std.json;
>
> void main()
> {
> string myFile= "source.json"; // a file produced by: dmd -o- -X source.d
>
> string js = readText(myFile);
>
> JSONValue j = parseJSON( js[1..$-1] ); // why do I have to do this??
>
> writefln("%s = %s", j["kind"].str, j["name"].str);
> writefln("file = %s", j["file"].str);
>
> }
You don't have to remove the brackets. You just have to process the result correctly. It's not an object but an array with an object as its first element.
If you're only interested in the first module (e.g. because you know that there is exactly one), you can use `j[0]["kind"]`, `j[0]["name"]`, `j[0]["file"]`.
If you want to handle multiple modules, you can loop over the array:
----
foreach (size_t i, item; j)
/* It's a bit silly that `foreach (item; j)` doesn't work. */
{
/* ... use `item["kind"]` etc. here ... */
}
----
|
February 22, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Thursday, 22 February 2018 at 13:17:42 UTC, ag0aep6g wrote:
>
> You don't have to remove the brackets. You just have to process the result correctly. It's not an object but an array with an object as its first element.
>
ok. I think I demonstrated that I don't know what I'm doing with the json ;-)
thanks for the tips (I tried it and it worked).
Now..I better go off and learn more about parsing json...
|
February 22, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to psychoticRabbit | On Wednesday, 21 February 2018 at 01:58:17 UTC, psychoticRabbit wrote: > On Tuesday, 20 February 2018 at 15:26:12 UTC, Adam D. Ruppe wrote: >> >> dmd -X spits out the json file with a list of functions and classes and other stuff. Then you can just filter that. >> > > 'dmd -X' looks like the perfect solution for my need. thanks. I don't know what exactly you plan to do, but the AST dump from DScanner is usually also quite handy: https://github.com/dlang-community/D-Scanner#ast-dump In fact, with libdparse, you don't get it as XML, but as AST: https://github.com/dlang-community/libdparse Example: https://run.dlang.io/is/qZsGDD |
February 23, 2018 Re: Annotation of functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Thursday, 22 February 2018 at 14:50:37 UTC, Seb wrote:
> On Wednesday, 21 February 2018 at 01:58:17 UTC, psychoticRabbit wrote:
>> On Tuesday, 20 February 2018 at 15:26:12 UTC, Adam D. Ruppe wrote:
>>>
>>> dmd -X spits out the json file with a list of functions and classes and other stuff. Then you can just filter that.
>>>
>>
>> 'dmd -X' looks like the perfect solution for my need. thanks.
>
> I don't know what exactly you plan to do, but the AST dump from DScanner is usually also quite handy:
>
> https://github.com/dlang-community/D-Scanner#ast-dump
>
> In fact, with libdparse, you don't get it as XML, but as AST:
>
> https://github.com/dlang-community/libdparse
>
> Example:
>
> https://run.dlang.io/is/qZsGDD
I want to do some cyber-security related analysis of D code bases - so looking for tools that might assist.
I'll definately go have a look at dscanner and libdparse too.
Thanks for the tip.
|
Copyright © 1999-2021 by the D Language Foundation