Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code parsing C#

Tags:

c#

parsing

I am researching ways, tools and techniques to parse code files in order to support syntax highlighting and intellisence in an editor written in c#.

Does anyone have any ideas/patterns & practices/tools/techiques for that.

EDIT: A nice source of info for anyone interested:

Parsing beyond Context-free grammars ISBN 978-3-642-14845-3

like image 667
sTodorov Avatar asked Oct 24 '10 15:10

sTodorov


3 Answers

My favourite parser for C# is Irony: http://irony.codeplex.com/ - i have used it a couple of times with great success

Here is a wikipedia page listing many more: http://en.wikipedia.org/wiki/Compiler-compiler

like image 132
Rob Fonseca-Ensor Avatar answered Nov 09 '22 17:11

Rob Fonseca-Ensor


There are two basic aproaches:
1) Parse the entire solution and everything it references so you understand all the types involved in the code
2) Parse locally and do your best to guess what types etc are.

The trouble with (2) is that you have to guess, and in some circumstances you just can't tell from a code snippet exactly what everything is. But if you're happy with the sort oif syntax highlighting shown on (e.g.) Stack Overflow, then this approach is easy and quite effective.

To do (1) then you need to do one of (in decreasing order of difficulty):

  • Parse all the source code. Not possible if you reference 3rd party assemblies.
  • Use reflection on the compiled code to garner type information you can use when parsing the source.
  • Use the host IDE's (if avaiable - so not applicable in your case!) code element interfaces to provide the information you need
like image 43
Jason Williams Avatar answered Nov 09 '22 16:11

Jason Williams


You could take a look at how http://www.icsharpcode.net/ did it. They wrote a book doing just that, Dissecting a C# Application: Inside SharpDevelop, it even has a chapter called

Implement a parser to provide syntax highlighting and auto-completion as users type

like image 34
Jonas Elfström Avatar answered Nov 09 '22 18:11

Jonas Elfström