Wiki

Ticket #320 (new task)

Opened 12 years ago

Last modified 12 years ago

Provide a tool to automate conversion of C# code to cobra code

Reported by: hopscc Owned by:
Priority: medium Milestone:
Component: Cobra Compiler Version: 0.9.3
Keywords: tools Cc:

Description

As per WishList provide a tool (cobra app or script) to automate the conversion of C# source code to equivalent cobra source code (or at least automate the easy 90% trivial deletions/changes)

See list of manual changes in wiki:PortingC

Change History

Changed 12 years ago by Charles

I took a small, private crack at this one night long ago. My approach was to implement the trivial deletions/changes that we all know about without truly parsing the C# code/grammar. But as I tested it on various chunks of code from around the web, it did not work well at all. It was easily tripped up into doing the wrong thing or not doing enough to make it worthwhile.

My conclusion for such an effort was that the best approach would be to use a legit C# parser, preferably one that can keep comments around so they can be retained easily.

Of course, anyone tackling this is free to use any approach they like, but I thought I would record my experience here for what it's worth.

Changed 12 years ago by nerdzero

One approach could be to parse the C# code using NRefactory:  https://github.com/icsharpcode/NRefactory

 https://github.com/icsharpcode/NRefactory/blob/master/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

Then, create a class that inherits from the AST visitor:  https://github.com/icsharpcode/NRefactory/blob/master/ICSharpCode.NRefactory/TypeSystem/TypeVisitor.cs

Visit the tree and for each C# node, generate a corresponding Cobra node. Then visit the Cobra AST and write out the corresponding Cobra source.

Seems like it should work in theory. Might be tricky to create nodes with bogus tokens though. Not a trivial project, but lots of fun i'm sure :)

Changed 12 years ago by Charles

Thanks. I agree with your thoughts here.

What did you mean about the bogus tokens though? Was that a reference to the comments, or does NRefactory have a "closed" set of tokens, or something else?

Do you know if NRefactory can retain comments?

Changed 12 years ago by nerdzero

You can correct me if I'm wrong. By bogus tokens I meant that, normally, to create Cobra AST nodes you'd have a token stream that you obtained from the Cobra source code file. In this case, with only the C# source, you'd have to create bogus tokens before you can create the Cobra nodes. I don't know how much that matters, if at all. Maybe you could just use Token.empty for all the nodes?

As for comments, I believe NRefactory only retains XML doc comments and the rest are ignored.

Changed 12 years ago by Charles

You can create Cobra tokens with the correct line number, token type, etc. It wouldn't be too hard. Token.empty wouldn't work because some of the Cobra AST nodes are expecting the token.which to be correct. There may be other expectations with other specific things, like int values and so on.

Regarding comments, they could be slurped up separately and injected back into the Cobra result (with some effort).

Note: See TracTickets for help on using tickets.