Question

For a college project i have to create a tool that check the validity of the c# code. Validity means checking whether all the variables are following camel case or checking whether the class names are start with a upper case letter and so on. The rules that must follow can be varied. The biggest problem i have is to retrieve details like class names, method names, their properties and their line numbers from the source code. So I came up with following solutions,

  1. Using a language parser(like COCO/R or ANTLR)
  2. Using regular expressions
  3. By coding according to C# grammer file... Just if else and so on...
  4. Reflection API - Hope this won't work

What is the best method to do this.. or are there any alternative?

Thank You..

Was it helpful?

Solution

I personally would go with ANTLR for the following reasons:

  1. It is simpler to use than regexs and it is more powerfull
  2. It is easier to implement complex logic according to your rules using ANTLR
  3. The result code will be easier to change
  4. You can track current line number
  5. Since you do not need to compile C# code you do not need to implement whole grammar in ANTL. You can assume that the code is valid and skip some parts of it like variables initializations, conditionals, LINQ etc. and only leave those parts of grammar that you really need for verification.

Of course ANTLR will require some learning and you may need to spend a day or two playing with it. If you don't know ANTLR and you don't have much time you could try to hack the task using some regexs and C#/Java/etc. code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top