www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ parser

reply "IgorStepanov" <wazar mail.ru> writes:
Is there someone free C++ parser? I want to create simple util to 
converting C++ headers to D like htod, but I want, if it 
possible, to dont write C++ parser. Any ideas?
May 22 2013
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-22 10:05, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util to
 converting C++ headers to D like htod, but I want, if it possible, to
 dont write C++ parser. Any ideas?

There's libclang. I already have a tool that handles C and Objective-C files. Pull request are welcome :) https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
May 22 2013
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-22 16:55, IgorStepanov wrote:

 Seems pretty. What I need to do first to start work over C++ to D
 handle? As I think, I'll need to generate to one C++ header one .cpp
 glue file (with some hacking over header) and one .di file.

I'm not sure on how to best bind C++ code to D. D can handle some parts of the C++ ABI, like classes and virtual functions. dlang.org contains some information: http://dlang.org/cpp_interface.html
 First goal
 is a parse and syntax analyse of this header. Do libclang provide me
 this functional?

-- /Jacob Carlborg
May 22 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-05-22 20:31, Jacob Carlborg wrote:

I accidentally sent the message too soon.

 On 2013-05-22 16:55, IgorStepanov wrote:

 Seems pretty. What I need to do first to start work over C++ to D
 handle? As I think, I'll need to generate to one C++ header one .cpp
 glue file (with some hacking over header) and one .di file.

I'm not sure on how to best bind C++ code to D. D can handle some parts of the C++ ABI, like classes and virtual functions. dlang.org contains some information: http://dlang.org/cpp_interface.html

There was a talk on the recent D conference about binding C++ code: Reddit: http://www.reddit.com/r/programming/comments/1eiku4/dconf_2013_day_1_talk_5_using_d_alongside_a_game/ Youtube: http://www.youtube.com/watch?v=FKceA691Wcg
 First goal
 is a parse and syntax analyse of this header. Do libclang provide me
 this functional?


Yes, libclang will lex, parse and do semantic analysis. You can then walk the AST and do want you want with it. You need to indicate it's C++ you want to parse using the -x flag. You need to add C++ in the "setupArguments" and "handleLanguage" functions. You can see that the "handleLanguage" function already contains a "case" for C++. After that you can put the C++ specific code in a new package: translator/cpp. -- /Jacob Carlborg
May 22 2013
prev sibling next sibling parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 05/22/2013 03:17 PM, nazriel wrote:
 On Wednesday, 22 May 2013 at 08:10:31 UTC, Jacob Carlborg wrote:
 On 2013-05-22 10:05, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util to
 converting C++ headers to D like htod, but I want, if it possible, to=



 dont write C++ parser. Any ideas?

There's libclang. I already have a tool that handles C and Objective-C=


 files. Pull request are welcome :)

 https://github.com/jacob-carlborg/dstep

I am afraid it doesn't build anymore with DMD git-head. It isn't dstep itself but one of its dependencies.

Yeah, I was afraid of that... So, Now the next easiest thing is probably SWIG.
=20
 I wanted to use it in order to create bindings for XCB but couldn't
 built it.
 Also AFAIK SiegeLord went with flow to Rust, so probably Tango-D2
 dependency may start to be a problem soon.
=20
 Just my 5 cents.

Well, I don't think he'll have it any easier there... I've tried Binding rust, I sucked worse than binding D.
May 22 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-05-22 22:33, 1100110 wrote:

 Yeah, I was afraid of that...

 So, Now the next easiest thing is probably SWIG.

Hey, at least give me a change to fix the problem. -- /Jacob Carlborg
May 22 2013
prev sibling next sibling parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hmmm... Or you could try using pegged (or CTPG or goldie or dparser).

I haven't played with it too much, but there's a C grammar, and it
mentions the ability to specify semantic actions, so.... That's like 1/3
of the way to something usable.

https://github.com/PhilippeSigaud/Pegged/wiki/Semantic-Actions
May 22 2013
parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 05/22/2013 04:07 PM, Dicebot wrote:
 On Wednesday, 22 May 2013 at 20:39:08 UTC, 1100110 wrote:
 Hmmm... Or you could try using pegged (or CTPG or goldie or dparser).

 I haven't played with it too much, but there's a C grammar, and it
 mentions the ability to specify semantic actions, so.... That's like 1=


 of the way to something usable.

 https://github.com/PhilippeSigaud/Pegged/wiki/Semantic-Actions

Writing C++ grammar parser in Pegged/Goldie from scratch so that it can=

 produce usable AST... Well, good luck! :)

I know right? (I figured there's probably a half-baked one floating around somewhere...) But we kinda need something good like http://luajit.org/ext_ffi.html...
May 22 2013
parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 05/22/2013 04:40 PM, Dicebot wrote:
 On Wednesday, 22 May 2013 at 21:38:49 UTC, 1100110 wrote:
 ...

I simply don't understand why do you find libclang unsuitable. =20

hmmm? I never said it was unsuitable, I think you mistaking me for someone else. (I also have no experience with libclang, and I'm tired of manually translating these things... So, am I looking for a way to not translate this? Yes.) Also I'm pretty sure I'd need help with something like this...
May 22 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-23 00:17, 1100110 wrote:

 hmmm?  I never said it was unsuitable, I think you mistaking me for
 someone else.
 (I also have no experience with libclang, and I'm tired of manually
 translating these things...  So, am I looking for a way to not translate
 this?  Yes.)


 Also I'm pretty sure I'd need help with something like this...

I'm here to help, DStep is also here to help. I'll see if I can get it up an running again. -- /Jacob Carlborg
May 22 2013
parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 05/23/2013 01:25 AM, Jacob Carlborg wrote:
 On 2013-05-23 00:17, 1100110 wrote:
=20
 hmmm?  I never said it was unsuitable, I think you mistaking me for
 someone else.
 (I also have no experience with libclang, and I'm tired of manually
 translating these things...  So, am I looking for a way to not transla=


 this?  Yes.)


 Also I'm pretty sure I'd need help with something like this...

I'm here to help, DStep is also here to help. I'll see if I can get it up an running again. =20

Hell yeah! I didn't realize dstep was yours. If I can do anything to help, let me know.
May 23 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-23 11:44, 1100110 wrote:

 Hell yeah!
 I didn't realize dstep was yours.


 If I can do anything to help, let me know.

It works with DMD 2.062. To get it to work with DMD 2.063 there are is regression that needs to be fixed for find a workaround for. Tango fails to compile because of this. http://d.puremagic.com/issues/show_bug.cgi?id=10142 -- /Jacob Carlborg
May 23 2013
parent reply 1100110 <0b1100110 gmail.com> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On 05/23/2013 07:33 AM, Jacob Carlborg wrote:
 On 2013-05-23 11:44, 1100110 wrote:
=20
 Hell yeah!
 I didn't realize dstep was yours.


 If I can do anything to help, let me know.

It works with DMD 2.062. To get it to work with DMD 2.063 there are is regression that needs to be fixed for find a workaround for. Tango fail=

 to compile because of this.
=20
 http://d.puremagic.com/issues/show_bug.cgi?id=3D10142
=20

That was fast. I'll check it out. Thank you.
May 23 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-05-23 16:46, 1100110 wrote:

 That was fast.

 I'll check it out.  Thank you.

Note, if you're using DVM you need to change the build.sh file to use DMD 2.062. Also, I don't know if there are other hidden compiler errors in Tango using DMD 2.063. -- /Jacob Carlborg
May 23 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-05-22 22:17, nazriel wrote:

 I am afraid it doesn't build anymore with DMD git-head.
 It isn't dstep itself but one of its dependencies.

 I wanted to use it in order to create bindings for XCB but couldn't
 built it.

Could you use the pre compiled binary in the mean time: https://github.com/jacob-carlborg/dstep/downloads I forgot to put a link to the downloads on the front page. -- /Jacob Carlborg
May 22 2013
prev sibling next sibling parent "Dicebot" <m.strashun gmail.com> writes:
On Wednesday, 22 May 2013 at 08:05:06 UTC, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util 
 to converting C++ headers to D like htod, but I want, if it 
 possible, to dont write C++ parser. Any ideas?

Using libclang is probably most convenient and robust approach nowadays. You can look at Jacob's DStep for inspiration : https://github.com/jacob-carlborg/dstep Actually, I would even recommend to work in improving dstep instead of wasting efforts on new tool as it does exactly what you want :)
May 22 2013
prev sibling next sibling parent "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Wednesday, 22 May 2013 at 08:05:06 UTC, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util 
 to converting C++ headers to D like htod, but I want, if it 
 possible, to dont write C++ parser. Any ideas?

The easiest thing for you would be to use the GCCXML - http://gccxml.github.io/HTML/Index.html
May 22 2013
prev sibling next sibling parent "IgorStepanov" <wazar mail.ru> writes:
On Wednesday, 22 May 2013 at 08:10:31 UTC, Jacob Carlborg wrote:
 On 2013-05-22 10:05, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util 
 to
 converting C++ headers to D like htod, but I want, if it 
 possible, to
 dont write C++ parser. Any ideas?

There's libclang. I already have a tool that handles C and Objective-C files. Pull request are welcome :) https://github.com/jacob-carlborg/dstep

Seems pretty. What I need to do first to start work over C++ to D handle? As I think, I'll need to generate to one C++ header one .cpp glue file (with some hacking over header) and one .di file. First goal is a parse and syntax analyse of this header. Do libclang provide me this functional?
May 22 2013
prev sibling next sibling parent "nazriel" <spam dzfl.pl> writes:
On Wednesday, 22 May 2013 at 08:10:31 UTC, Jacob Carlborg wrote:
 On 2013-05-22 10:05, IgorStepanov wrote:
 Is there someone free C++ parser? I want to create simple util 
 to
 converting C++ headers to D like htod, but I want, if it 
 possible, to
 dont write C++ parser. Any ideas?

There's libclang. I already have a tool that handles C and Objective-C files. Pull request are welcome :) https://github.com/jacob-carlborg/dstep

I am afraid it doesn't build anymore with DMD git-head. It isn't dstep itself but one of its dependencies. I wanted to use it in order to create bindings for XCB but couldn't built it. Also AFAIK SiegeLord went with flow to Rust, so probably Tango-D2 dependency may start to be a problem soon. Just my 5 cents.
May 22 2013
prev sibling next sibling parent "Igor Stepanov" <wazar.leollone yahoo.com> writes:
I'm not sure on how to best bind C++ code to D. D can handle 
some parts of the C++ ABI, like classes and virtual functions. 
dlang.org contains some information:

non-virtual (with final annotation) methods, static methods. After my pull for posix and fix windows mangling (in work) static variables also be allowed. I've idea, how we can get access to fields: //C++ class Foo { public: int a; int b; //virtual methods and other stuff }; //Glue C++ (automatically generated) int& __accessor_Foo_a(Foo* f){return f->a;} int& __accessor_Foo_b(Foo* f){return f->b;} //also we can get access to private/protected methods with some header hack if in needed //D header extern(C++) { interface Foo { //we can implement final methods in interface property final ref int a() {return __accessor_Foo_a(this);} property final ref int b() {return __accessor_Foo_b(this);} //other stuff } ref int __accessor_Foo_a(Foo); ref int __accessor_Foo_b(Foo); } for get access to different special functions (like constructors, destructors, operators) we can use special methods + pragma(mangle) interface Foo { pragma(mangle, getCPPOperatorMangle!(Foo.__operator_idx, "[]")) int& __operator_idx(size_t); //get access to int& Foo::operator[](size_t) } There is small trouble with inlined functions: //C++ class Foo { protected: int foo(){return 5;} //this function is inline and there's a possibility that this function won't be written into object file. }; In this case, we must to do some actions to force write foo into object file. This is my ideas about binding C++ to D :) If I'll do all as I want, we'll get a simple interface to access to most of C++ code (except templates).
May 22 2013
prev sibling next sibling parent "Dicebot" <m.strashun gmail.com> writes:
On Wednesday, 22 May 2013 at 20:39:08 UTC, 1100110 wrote:
 Hmmm... Or you could try using pegged (or CTPG or goldie or 
 dparser).

 I haven't played with it too much, but there's a C grammar, and 
 it
 mentions the ability to specify semantic actions, so.... That's 
 like 1/3
 of the way to something usable.

 https://github.com/PhilippeSigaud/Pegged/wiki/Semantic-Actions

Writing C++ grammar parser in Pegged/Goldie from scratch so that it can produce usable AST... Well, good luck! :)
May 22 2013
prev sibling parent "Dicebot" <m.strashun gmail.com> writes:
On Wednesday, 22 May 2013 at 21:38:49 UTC, 1100110 wrote:
 ...

I simply don't understand why do you find libclang unsuitable.
May 22 2013