开发者

How to create a formating and indentaion code in C#

How to start with creating a program to format C and their derived code int开发者_开发知识库o .net style formatted code so that if I input any program. This program can recognize and reformat by properly adding indentation and other things.


This is an extremely complex task because programming languages have complex grammar. If you want to format not just C#, but C and C++ too, it’s even more complex — possibly impossible because there may be syntax in one language that is not valid (or means something different) in another.

If you want to do it just for C#, you need a C# parser. There are a few free-software C# parsers available:

  • C# parsers

Once you have the parse tree, you will have to walk the tree and progressively output it properly formatted. Some of the C# parsers may already have this functionality.

Regarding the grammar ambiguity: Consider the following line of code:

Method(a<b,c>(d+1));

In C# the correct formatting would be:

Method(a<b, c>(d + 1));  // “a<T1, T2>” is generic; Method has one argument

In C and C++ I believe the correct formatting would be:

Method(a < b, c > (d + 1));  // Method has two arguments with binary operators
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜