开发者

Adding Annotation Syntax to C++ Source

I want to create my own custom annotation (just like the Java style annotation)开发者_Python百科 tags in my C++ source. Since standard C++ syntax doesn't allow annotation, I'd like to modify/enhance the compiler for my own needs.

But does Visual Studio expose its compiler internals for users to modify (e.g. output of its lexer, abstract syntax tree, etc)? If not, are there any third party tools for parsing c++ syntax and allow me to emit my own customized c++ source base on my own annotation?


Does Visual Studio expose its compiler internals for users to modify (e.g. output of its lexer, abstract syntax tree, etc.)?

No

Are there any third party tools for parsing C++ syntax and allow me to emit my own customized C++ source base on my own annotation?

Clang


The standard solution to a problem like this, if it's faced in a programming language that is not C++, is to write a custom preprocessor that understands some subset of the language in question and your annotation, rewrites the code with annotations removed, which is then passed to the actual language compiler.

This is the standard method when it comes to extending languages: Adding an preprocessing step before the compiler.

Unfortunately it's next to impossible to just parse feature complete C++; processing C++ source code almost always ends up in creating a full AST generator, due to some of it's tedious to analyze language features.

Look at these declarations:

template<bool b, class T> void foo(T &t){if(b) T.do_this(); else T.do_that();}
class foobar { public: virtual void do_this(); virtual void do_that(); };
class barfoo : public foobar { public: virtual void do_this(); virtual void do_that(); };

and now this function

void bar(float l){ barfoo t; foo<fsqrt(l) > 1, foobar>(t); }

The problem here is, that the brackets have double meaning, for the template and the comparision for a template parameter. The only way to decide to which each bracket belongs requires a complete syntax tree.

I know of only one such tool like you describe: The MOC of the Qt toolkit, does what you suggest: C++ code annotated with keywords signal and slot. Qt is open source so it may be a good idea to look into MOC's sources.

Writing and reading C++ is very hard indeed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜