Which parsers are available for parsing C# code? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Which parsers are available for parsing C# code?
I'm looking for a C# parser that can be used in C# and give me access to line and file informations about each artefact of the analysed code.

Works on source code:
CSParser:
From C# 1.0 to 2.0, open-source
Metaspec C# Parser:
From C# 1.0 to 3.0, commercial product (about 5000$)
#recognize!:
From C# 1.0 to 3.0, commercial product (about 900€) (answer by SharpRecognize)
SharpDevelop Parser (answer by Akselsson)
NRefactory:
From C# 1.0 to 4.0 (+async), open-source, parser used in SharpDevelop. Includes semantic analysis.
C# Parser and CodeDOM:
A complete C# 4.0 Parser, already support the C# 5.0 async feature. Commercial product (49$ to 299$) (answer by Ken Beckett)
Microsoft Roslyn CTP:
Compiler as a service.
Works on assembly:
System.Reflection
Microsoft Common Compiler Infrastructure:
From C# 1.0 to 3.0, Microsoft Public License. Used by Fxcop and Spec#
Mono.Cecil:
From C# 1.0 to 3.0, open-source
The problem with assembly "parsing" is that we have less informations about line and file (the informations is based on .pdb file, and Pdb contains lines informations only for methods)
I personnaly recommend Mono.Cecil and NRefactory.

Mono (open source) includes C# compiler (and of course parser)

If you are going to compile C# v3.5 to .net assemblies:
var cp = new Microsoft.CSharp.CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

If you're familiar with ANTLR, you can use Antlr C# grammar.

I've implemented just what you are asking (AST Parsing of C# code) at the OWASP O2 Platform project using SharpDevelop AST APIs.
In order to make it easier to consume I wrote a quick API that exposes a number of key source code elements (using statements, types, methods, properties, fields, comments) and is able to rewrite the original C# code into C# and into VBNET.
You can see this API in action on this O2 XRule script file: ascx_View_SourceCode_AST.cs.o2 .
For example this is how you process a C# source code text and populate a number of TreeViews & TextBoxes:
public void updateView(string sourceCode)
{
var ast = new Ast_CSharp(sourceCode);
ast_TreeView.show_Ast(ast);
types_TreeView.show_List(ast.astDetails.Types, "Text");
usingDeclarations_TreeView.show_List(ast.astDetails.UsingDeclarations,"Text");
methods_TreeView.show_List(ast.astDetails.Methods,"Text");
fields_TreeView.show_List(ast.astDetails.Fields,"Text");
properties_TreeView.show_List(ast.astDetails.Properties,"Text");
comments_TreeView.show_List(ast.astDetails.Comments,"Text");
rewritenCSharpCode_SourceCodeEditor.setDocumentContents(ast.astDetails.CSharpCode, ".cs");
rewritenVBNet_SourceCodeEditor.setDocumentContents(ast.astDetails.VBNetCode, ".vb");
}
The example on ascx_View_SourceCode_AST.cs.o2 also shows how you can then use the information gathered from the AST to select on the source code a type, method, comment, etc..
For reference here is the API code that wrote (note that this is my first pass at using SharpDevelop's C# AST parser, and I am still getting my head around how it works):
AstDetails.cs
AstTreeView.cs
AstValue.cs
Ast_CSharp.cs

We have recently released a C# parser that handles all C# 4.0 features plus the new async feature: C# Parser and CodeDOM
This library generates a semantic object model which retains comments and formatting information and can be modified and saved. It also supports the use of LINQ queries to analyze source code.

You should definitely check out Roslyn since MS just opened (or will soon open) the code with an Apache 2 license here. You can also check out a way to parse this info with this code from GitHub.

http://www.codeplex.com/csparser

SharpDevelop, an open source IDE, comes with a visitor-based code parser which works really well. It can be used independently of the IDE.

Consider to use reflection on a built binary instead of parsing the C# code directly. The reflection API is really easy to use and perhaps you can get all the information you need?

Have a look at Gold Parser. It has a very intuitive IU that lets you interactively test your grammar and generate C# code. There are plenty of examples available with it and it is completely free.

Maybe you could try with Irony on irony.codeplex.com.
It's very fast and a c# grammar already exists.
The grammar itself is written directly in c# in a BNF like way (acheived with some operators overloads)
The best thing with it is that the "grammar" produces the AST directly.

Something that is gaining momentum and very appropriate for the job is Nemerle
you can see how it could solve it in these videos from NDC :
Igor Tkachev - Metaprogramming with Nemerle
Igor Tkachev - Nemerle Programming Language

Not in C#, but a full C# 2/3/4 parser that builds full ASTs is available with our DMS Software Reengineering Toolkit.
DMS provides a vast infrastructure for parsing, tree building, construction of symbol tables and flow analyses, source-to-source transformation, and regeneration of source code from the (modified) ASTs. (It also handles many other languages than just C#.)
EDIT (September) 2013: This answer hasn't been updated recently. DMS has long handled C# 5.0

GPPG might be of use, if you are willing to write your own parser (which is fun).

Related

CppCodeGenerator parse managed C++

I am looking to parse managed C++ files into a CodeDOM tree (or any other c# representation, for that matter). I see that CppCodeGenerator has been removed in .NET4, and it does not provide a Parse(string) implementation. Any ideas?
Thanks
Can't help with CodeDom; according to other posters there's no help for it anyway.
If you want robust, accurate parsers for C#, VB.net, VC++ and managed VC++ (and many other languages) you might consider our DMS Software Reengineering Toolkit and its family of language front-ends.
Using a front-end, DMS parses source into a detailed AST enabling further analysis, transformation, and generation of valid source code from modified ASTs. No, you can't manipulate these trees using C# calls; you have to do that from inside DMS, which offers a complete ecosystem for these tasks. But you can manipulate them from inside DMS in virtually arbitary ways.

C# code builder/formatter

Does anyone know of a library which helps with generating C# code? For example, if I need to generate a *.cs file containing the definition of a class, I'd like to be able to specify the class and method bodies using an object tree (similar to expression trees) and then tell the library to give me well the formatted C# code as a string.
Thanks.
Have you looked at Microsoft.CSharp.CSharpCodeProvider?
Provides access to instances of the C# code generator and code compiler.
There are many tools for code generation and they are listed below:
Code Smith
codesmithtools dot com
codegeneratorpro dot com
etc
Code generators may sound like saving a lot of time, in reality it depends on the kind of project your working on.
Look into System.CodeDom and CSharpCodeProvider.
I'm rather fond of Terance Parr's StringTemplate. It's at the core of the compiler building tool ANTLR — StringTemplate is responsible for for code generation, allowing ANTLR to target just about any language for its compilers.
You can download the latest C#/.Net port from http://www.stringtemplate.org/download.html
You can read about string template in these papers by Dr. Parr:
[DRAFT] A Functional Language For Generating Structured Text
Enforcing Strict Model-View Separation in Template Engines
More at http://www.stringtemplate.org/article/list
CodeProject has an article on code generation with StringTemplate as well: http://www.codeproject.com/KB/codegen/DotNetCodeGeneration.aspx

Looking for a C# code parser [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm looking for a set of classes (preferably in the .net framework) that will parse C# code and return a list of functions with parameters, classes with their methods, properties etc. Ideally it would provide all that's needed to build my own intellisense.
I have a feeling something like this should be in the .net framework, given all the reflection stuff they offer, but if not then an open source alternative is good enough.
What I'm trying to build is basically something like Snippet Compiler, but with a twist. I'm trying to figure out how to get the code dom first.
I tried googling for this but I'm not sure what the correct term for this is so I came up empty.
Edit: Since I'm looking to use this for intellisense-like processing, actually compiling the code won't work since it will most likely be incomplete. Sorry I should have mentioned that first.
While .NET's CodeDom namespace provides the basic API for code language parsers, they are not implemented. Visual Studio does this through its own language services. These are not available in the redistributable framework.
You could either...
Compile the code then use reflection on the resulting assembly
Look at something like the Mono C# compiler which creates these syntax trees. It won't be a high-level API like CodeDom but maybe you can work with it.
There may be something on CodePlex or a similar site.
UPDATE
See this related post. Parser for C#
If you need it to work on incomplete code, or code with errors in it, then I believe you're pretty much on your own (that is, you won't be able to use the CSharpCodeCompiler class or anything like that).
There's tools like ReSharper which does its own parsing, but that's prorietary. You might be able to start with the Mono compiler, but in my experience, writing a parser that works on incomplete code is a whole different ballgame to writing one that's just supposed to spit out errors on incomplete code.
If you just need the names of classes and methods (metadata, basically) then you might be able to do the parsing "by hand", but I guess it depends on how accurate you need the results to be.
Mono project GMCS compiler contains a pretty reusable parser for C#4.0. And, it is relatively easy to write your own parser which will suite your specific needs. For example, you can reuse this: http://antlrcsharp.codeplex.com/
Have a look at CSharpCodeCompiler in Microsoft.CSharp namespace. You can compile using CSharpCodeCompiler and access the result assembly using CompilerResults.CompiledAssembly. Off that assembly you will be able to get the types and off the type you can get all property and method information using reflection.
The performance will be pretty average as you will need to compile all the source code whenever something changes. I am not aware of any methods that will let you incrementatlly compile snippets of code.
Have you tried using the Microsoft.CSharp.CSharpCodeProvider class? This is a full C# code provider that supports CodeDom. You would simply need to call .Parse() on a text stream, and you get a CodeCompileUnit back.
var codeStream = new StringReader(code);
var codeProvider = new CSharpCodeProvider();
var compileUnit = codeProvider.Parse(codeStream);
// compileUnit contains your code dom
Well, seeing as the above does not work (I just tested it), the following article might be of interest. I bookmarked it a good long time ago, so I believe it only supports C# 2.0, but it might still be worth it:
Generate Code-DOMs directly from C# or VB.NET
It might be a bit late for Blindy, but I recently released a C# parser that would be perfect for this sort of thing, as it's designed to handle code fragments and retains comments:
C# Parser and CodeDOM
It handles C# 4.0 and also the new 'async' feature. It's commercial, but is a small fraction of the cost of other commercial compilers.
I really think few people realize just how difficult parsing C# has become, especially if you need to resolve symbolic references properly (which is usually required, unless maybe you're just doing formatting). Just try to read and fully understand the Type Inference section of the 500+ page language specification. Then, meditate on the fact that the spec is not actually fully correct (as mentioned by Eric Lippert himself).

Format C# Source Code with Hyperlinks to Reference Library Documentation

I'm wondering if anyone has done this already.
I want to format C# source code in HTML. But with a twist! I want to turn the names of all types and methods that appear in the code into hyperlinks to the MSDN Library documentation of the types and methods.
To do a good job, the data types of variables and expressions needs to be known, just like how the C# compiler does it. So it's a tall order. If something like this is not available, please point me to any free libraries that can generate a parsed tree of the C# source code in sufficient detail to do this task. (In fact, I'd like to know about such a standalone parser library even if the full solution I am asking for already exists.)
This kind of utility might benefit blogs and forums -- maybe even Stack Overflow!
Have you checked out Docu? It's an open source library that converts .net documentation into HTML documents.
I'd suggest using the Visual Studio SDK.

JSON decoding in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how to decode a json response in c#?
Check out the DataContractJsonSerializer. You'll have to target .NET 3.5, which means Visual Studio 2008 is pretty much required. Here's a good blog post about using the Json data contract serializer.
See here for info on the DataContractJsonSerializer
In addition to the 3.5 methods above, if you install the ASP.NET 2.0 AJAX Extensions 1.0 (2.0 is the framework version), you will gain the System.Web.Script.Serialization.JavaScriptSerializer class, which can encode/decode json.
The .NET integrated classes have their merits.
But they have their shortcomings.
For example, DataContractJsonSerializer is not available in .NET 2.0, System.Web.Extensions needs admin rights to install it (in NET 2.0 - you can localcopy it, if you don't have a WebSite project) plus it doesn't work in SilverLight and WindowsPhone.
If you have a WebSite project, you need to copy the System.Web.Extensions assemblies to your project, and remove them from GAC afterwards, else VisualStudio doesn't understand it has to localcopy them.
But more importantly, if you work with pretty much any JavaScript library, e.g. SlickGrid (AJAX grid), you will stumble upon this valid JavaScript object (but it's invalid JSON, because fnFormatDate_DE is a function call and not text, it lacks the quotation marks):
FormatterCallback :
{
name : "DateFormatter_DE"
func: fnFormatDate_DE(val)
}
No chance to serialize this with any of the .NET integrated classes (because it's invalid JSON). Also, they fall short in terms of performance, availability in SilverLight, Windows Phone and WindowsRT. They are neither OpenSource nor MIT license. They have no support for indentation (human readable JSON), and they can't serialize DataTables, and they have problems with circular references. You can't handle serialization errors with them, can't serialize enums to their names, and you can't switch the date format (OK, this is not really a problem, because the MS date format is the only date format the safari crap understands [it doesn't undestand ISO]), and they don't serialize neither nHibernate nor Entity...
But most importantly, you won't want to switch your library or adjust project references if you go from .NET 2.0 to 4.0, you don't want to rewrite your code if you want to use some code in SilverLight/Windows Phone, and you don't want to be write a function to beautify JSON if you want to look whether you got the class right, and you won't want to write your own method to strip out quotation marks just because Microsoft's libraries can't handle invalid JSON.
Also, Microsoft's libraries have a low performance, and they can't serialize to BSON (for use with NoSQL databases like MongoDB).
So for all these reasons, you better choose NewtonSoft JSON (JSON.NET).
It's free and OpenSource (MIT license, not GPL).
There is a nice comparison matrix here:
http://james.newtonking.com/pages/json-net.aspx

Categories

Resources