msgbartop
A site for my programming pet projects
msgbarbottom

20 Mar 09 Coco/R plugin for Visual Studio

I’ve blogged before about the excellent Coco/R parser generator. I’m using it a lot in my masters project and I’m happy with it but there were a few things I wished worked differently. The main thing was that I wanted better Visual Studio integration. I had set up a pre-build event that generated the parser and scanner before every build. However there is obviously no need to re-generate the files unless the grammar file has changed. Generating on every build also had the effect that Visual Studio kept prompting me about reloading changed files and I had to build to see if there were any errors in my grammar. So, I decided to create a Visual Studio plugin for Coco/R myself.

Coco/R is open source and written in C# (at least one version of it) so it was easy to get the source. I then looked at a couple of tutorials on Visual Studio plugins and managed to hack together an plugin that works well enough for my needs. I also made a small change to the way Coco/R generates its parsers and scanners from frame files. Here are the features that are unique to the plugin:

  • Works with Visual Studio 2005 and 2008
  • When you click ‘Add a new item’ in a C# project, you’ll find a ‘Coco/R Attributed Grammar’ option under My Templates at the bottom of the screen. The .atg file you get has a simple example grammar that just reads numbers or identifiers. I wanted this because everytime I create a new .atg I start by finding an old one and copying the basics from it.
  • Every time an .atg file is saved, the parser and scanner are re-generated. If the generated files are open then the AddIn closes them before re-generating, to avoid the dreaded “Files were reloaded” prompt.
  • Errors and warnings from Coco show up in Visual Studio’s error list window just like build errors as soon as you save the .atg file.
  • Instead of using frame files, the plugin uses partial classes for the parser and scanner. There are four files, Parser.cs, Parser.generated.cs, Scanner.cs and Scanner.generated.cs. This allows you to add stuff to your parser and scanner in an actual .cs file so you get the benefit of the Visual Studio editor, instead of having to write it in the .frame file or the .atg file.

So, that’s it. You can download an MSI installer for it and you can also view or checkout the source at http://einaregilsson.googlecode.com/svn/dotnet/CocoPlugin/. Enjoy.

Reader's Comments

  1. |

    Hi,
    thanks a lot. Great tool.
    Just a question: I am about to use coco/r with Silverlight which doesn’t support Hashtable. I found that I should use System.Collections.Generic.Dictionary. I changed the definition of start in scanner.cs to static readonly Dictionary start. But when scanner.generated.cs is regenerated, there is still Hashtable there.
    Is there any flexible way of setting the underlying frame file and specifically this datatype?
    Thanks a lot.
    Pavel K.

  2. |

    Hi

    Glad you could use the tool. Unfortunately you can’t really change the frame file without re-compiling the plugin since the frame files are embedded in the CocoPlugin.dll file. Maybe not the best decision I made there…

    But you can if you want check out the source from Subversion at http://einaregilsson.googlecode.com/svn/dotnet/CocoPlugin/

    Then change the Scanner.frame file in the Frames folder and rebuild the .dll. You don’t need to rebuild the installer or anything, just build the dll and dump it in \Visual Studio 200X\AddIns or something like that, where the old CocoPlugin.dll is.

    If that doesn’t work for you for some reason then just let me know exactly how you want the line to read (” = new Dictionary“) and I’ll do a custom build for you.

  3. |

    Hi,
    thanks. I changed it, recompiled it and have been using it for a few day.
    Great tool!! What a relief. All’s changed just with hitting the save button.

    Thanks again.
    Pavel K.

Leave a Comment