Is there a good way to parse C# 3.0 code? - c#

I've been looking for a free (as in "free beer") library that can parse C# 3.0 code (actually, C# 2.0 with lambdas would do it) and return some sort of AST/code DOM for a personal project. However, most alternatives I've seen up to now are inadequate:
Microsoft.CSharp.CSharpCodeProvider.Parse throws a NotImpementedException;
Visual Studio's "exposed" code parser sucks;
The ways of Mono's C# compiler are impenetrable (and next to not documented);
The C# Parser project at CodePlex only parses C# 2.0 code and thus we forget lambdas;
Another project on SourceForge claims to parse C# to CodeDOM, but it's 2000 days old so I guess it's not C# 3.0;
... and it goes on for several pages of Google results.
So... is there a free, working C# 3.0 parser out there, that can be plugged into another project?

Take a look at http://antlrcsharp.codeplex.com/
It is a C# 4 grammar for the Antlr parser generator.

I've had good luck with NRefactory, written in C# and used for the SharpDevelop IDE written for Mono:
http://wiki.sharpdevelop.net/NRefactory.ashx

Related

Stuck with Delphi.NET and Delphi 2007

My company has a major problem. We developed an application consisting of more than 1.000.000 lines of code in Delphi.NET. Because of this we are stuck with Delphi 2007 and .NET 2.0.
As technology and usecases are moving on we need to migrate to another development platform. So far we tried several tools which promised to convert Delphi.NET to C# code - each of this tools had several problems like wrong indexing of strings (Delphi 1 C# 0) or when types were used to declare array boundaries.
After that approach we tried to decompile the Delphi.NET assembly - the code that comes back from that is hardly readable and has hundreds of helper functions which call into Borland specific assemblies. I already looked at the possibility to write a transpiler myself, but the ambiguous syntax of Delphi is too hard to implement in a straight forward grammar.
So now the great question, is there any possibilty left that does not include translating all of the code by hand? Or maybe a migration path which allows to migrate partially and stepwise?
Check out tools like:
http://www.9rays.net/TourStep.aspx?TourStepID=21
While it may not be as simple as running your entire Delphi app through it and getting C# source in return, these kind of IL translators / decompilers are pretty accurate. My advice would be to run a few assemblies through and regression test to spot check accuracy. Chances are very high that you'll have to do some remediation in the generated code, but it's better than rewriting the entire application.
Also: check out http://blogs.msdn.com/b/danielfe/archive/2004/06/15/156087.aspx

Converting C# console app to VB.NET and VBA

I have to convert a C# project to VB.NET and VBA. It's a console app that uses the Microsoft Office API (Microsoft.Office.Interop.*).
My two questions:
C# -> VB.NET: .NET Reflector is a good tool for this, except that it doesn't preserve source code comments. Is there a way to do this?
.NET -> VBA: is there an automated tool to do this conversion? It would need to be a client side tool (because the code is proprietary).
The best conversion seems to be done by SharpDevelop. Open your project and from the tools menu and select convert code to. Since it has all of your c# source code it does a very good job.
There are many ways to convert C# to VB.Net; for example, try Developer Fusion.
It is not possible to automatically convert C# to VBA because the environment, language, and libraries are radically different. However, you could expose the C# code as a COM server and call it from a thin wrapper in VBA.
Answer for question #1:
You cannot use Reflector to preserve source code comments because Reflector is disassembling the compiled code, which has had all comments removed.
Consider using a commercial tool like InstantVB, which will converts C# source code into VB source code (including comments):
http://tangiblesoftwaresolutions.com/Product_Details/Instant_VB.html
You could convert the C# source code to VB.Net manually with this:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
which will preserve comments. As far as automatically converting C# to VBA or VB6, I'd be willing to say that that is almost certainly going to be completely impossible. There are many aspects of C#/.Net that are not available in VBA or VB6 (like inheritance, for one).

C#/.NET scripting library

I want to enhance an application with scripting support like many other applications have, e.g. MS Office using VBA or UltraEdit using JavaScript.
Which libraries do exist for C#/.NET (and which language(s) do they support)?
Please check CS Scripting library
Here is an article about scripting Photoshop CS with C#
This one discusses using LUA as scripting lib with C#.
IronPython is a dynamic .NET scripting language.
IronPython is an implementation of the Python programming language running under .NET and Silverlight. It supports an interactive console with fully dynamic compilation. It's well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining compatibility with the Python language.
See IronPython embedding for examples showing ways to call IronPython from .NET apps.
The IronPython Calculator and the Evaluator goes into the details of using IronPython from a C# application.
Lua is often touted as being one of the better ones... Try looking at this other question for more information: What are the most effective ways to use Lua with C#?
Also:
Lua Interface
Binding code to Lua
See "What is the best scripting language to embed in a C# desktop application."
Also see "It Already Is A Scripting Language" from Eric Lippert.
Don't forget LSharp, LISP in .NET. Something to keep an eye on if you are exploring functionality stage. Maybe Rob Blackwell will be glad to hear you're considering it.
You can bake your own scripting environment with Mono.CSharp (just one simple dll) or Roslyn, both are getting quite mature now.
Mono contains the Evaluator class and Roslyn the ScriptEngine, both make it a breeze setting up a script environment. Of course something like ScriptCS already builds on that (Roslyn) and gives you more features.
For an C# script environment built on Mono.CSharp you can check out CShell (which I made).
Depending on your needs, the SILK library might be a good option.
It's an easy to use interpreter. The interpreted language is not C#. It's a custom language that was designed to be easy to use (very little punctuation, not case sensitive, etc.) But it does support functions.
Built-in functions are handled via events. That is, when the interpreted code calls one of your internal functions, it raises an event in your program.

Where can I find C# 3.0 grammar?

I'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation?
Preferably one that works with ANTLR v3 without modification.
Take a look at C# Language Specification. In the chapter B. Grammar you'll find the grammar.
I ran into ANTLR C# Grammar on CodePlex. It's a relatively new project and uses ANTLR 3.2. It says it supports C# 4.0 and is licensed under the Eclipse Public License (EPL).
I played with it a little. It has a bunch of test files containing expressions. It supports lambdas, unsafe context, ... as you'd naturally expect. It parses a C# file and hands you an abstract syntax tree. You can do whatever you want with it.
Are you looking for something like this or this?
Please also refer to C# ANLTR grammar question.
Take a look at COCO/R it seems that they have the language specification for C# 3.0.

Reference Java DLL in C# Assembly?

There are instructions here to create a C# assembly using the SimMetrics library. The link they provided to this library is at SourceForge. It looks like the most recent version of the SimMetrics library was created in Java. Is it possibly to compile java code and then reference it in C# to be used as an assembly in SQL Server 2008?
The best you can do is
compile the java as J# (now obsolete and largely unsupported) with minimal code changes.
this is very dependent on how much of the libraries are used.
convert the code to c# (idiomatic or otherwise)
this can sometimes be fairly easy on highly mathematical code. As an advantage the java code likely assumes 16 bit unicode as well.
use something like IKVM to host the java byte code within the CLR
this may be outright impossible with the sql server hosted runtime, certainly I would think the performance would be poor (since you would have to 'thunk' across the hosting barrier on each call.
The SF page strongly implies that there is both a java and a .net release.
Here's the latest .net release and documentation
However based on the read me file in that
This is an updated version of the original .NET implementation and not a conversion of the newest Java Code.
The .Net implementation is largely c# so you could diff the recent changes in the java implementation then attempt to recreate them in the .Net code. Since the conversion to c# seems to be largely a direct copy with only basic consideration given to idiomatic c# (camel casing, properties and parameter names) you stand a good chance of being able to do this.
If you do consider submitting the changes as a patch, this would give you a chance of getting someone else to validate your changes and may jump start the .Net side of the project to be kept more closely in sync in future.

Categories

Resources