[antlr-interest] Visual Studio syntax highlighting for an Antlr grammar

Pete Gonzalez pgonzalez at bluel.com
Mon Dec 5 00:10:43 PST 2005


I am interested in implementing IDE syntax highlighting and underlining 
(i.e. squiggly underlines marking syntax errors).  The Microsoft Visual 
Studio SDK (called "VSIP" in previous versions) has a really interesting 
template called "Babel".  Basically if you have a flex/bison grammar, an 
IDE wizard can be used to automatically generate a DLL add-in implementing 
syntax highlighting/underlining for that language.  It's completely 
automated; the wizard even includes a GUI for mapping flex token types to 
syntax color classes.

One disadvantage of Babel is that it's old-skool C++.  However, Visual 
Studio 2005 expands this API with managed wrappers enabling an entire 
language service to be implemented in C#.  (The C# classes lack flex/bison 
integration however.)  Our Antlr grammars are in C#, so my hope was to 
accomplish something similar to the Babel package but in C# and with Antlr.

Currently I'm just focusing on syntax highlighting (which uses the lexer 
but not the parser).  The IDE text editor is optimized to prevent the 
entire file from being rescanned whenever something changes.  The required 
C# interface looks like this:

   void IScanner.SetSource(string source, int offset);
   bool IScanner.ScanTokenAndProvideInfoAboutIt(TokenInfo tokenInfo,
     ref int state);

The idea is that the editor passes a single line of text to SetSource(), 
and then calls ScanTokenAndProvideInfoAboutIt() repeatedly to obtain the 
colored tokens for that line.  In this situation, the only context 
available to the lexer is a single "state" integer (which for Babel stores 
flex's "yy_start" global variable).  Unfortunately, since Antlr is a 
recursive descent design, there isn't an obvious way to restart the lexer 
e.g. in the middle of a multiline comment.  Has anyone else dealt with this 
problem before?

More generally, has anyone implemented a Visual Studio language service 
using an Antlr grammar?  Or, is it difficult to port an Antlr grammar to 
flex/bison?  :-)

Cheers,
-Pete

____


-------------- next part --------------
A non-text attachment was scrubbed...
Name: wizard3.gif
Type: image/gif
Size: 12823 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20051205/79cb4d35/wizard3-0001.gif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wizard4.gif
Type: image/gif
Size: 15433 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20051205/79cb4d35/wizard4-0001.gif


More information about the antlr-interest mailing list