Visual Studio 2010-2012 support for using the ANTLR 4 C# target (coming soon)

2,772 views
Skip to first unread message

Sam Harwell

unread,
Feb 28, 2013, 1:38:37 PM2/28/13
to antlr-di...@googlegroups.com

Hello everyone,

 

The update described below is not yet released; keep reading for details. :)

 

I’m working on updating the ANTLR Language Support extension for Visual Studio 2010-2012 to also support working with ANTLR 4 grammars. This extension will integrate direct support for several aspects of the ANTLR 4 C# target into the IDE. This includes support for exceptions and grammar file properties, as shown in the screenshots below.

 

·         (Figure 1) Adds the exceptions in the ANTLR 4 C# runtime to the Debug -> Exceptions… dialog for easy configuration (otherwise you must manually add them)

·         (Figure 2) Adds item templates for Lexer, Parser, and Combined ANTLR 4 grammars, including automatically creating the “.g4.cs” partial/helper classes

·         (Figure 3) Provides direct access to ANTLR 4 code generation properties when you select a grammar in Solution Explorer

·         When an ANTLR 4 grammar file (*.g4) is added to the project

o   (Figure 3) Automatically sets the Build Action to Antlr4 and the Target Language to CSharp

o   (Figure 3) Automatically configures the Custom Tool Namespace so the generated code is placed in a namespace based on where in the project you added the grammar

·         When the “helper file” for an ANTLR 4 grammar (*.g4.cs) is added to the project

o   (Figure 4) Automatically sets the file a “dependent on” the grammar itself, so it shows as a child of the grammar node in Solution Explorer

 

The above general features are already fully implemented. I’m planning to release the update as soon as I complete some minimal syntax highlighting support for ANTLR 4 files (full editing abilities to follow in the future).

 

Figure 1. Updated Exceptions dialog

 

Figure 2. ANTLR item templates (v3 and v4)

 

Figure 3. ANTLR-specific code generation properties

 

Figure 4. Automatic creation of helper files from ANTLR 4 item templates

 

Thanks,

--

Sam Harwell

Owner, Lead Developer

Description: C:\Users\sam\Documents\Work\TVL\tvl_logo_small.png

http://tunnelvisionlabs.com

 

Roberto Mencia Franco

unread,
Mar 3, 2013, 11:43:42 PM3/3/13
to
Hi Sam,

It's great that you've got this so quickly.
I've just updated the ANTLR plug-in in VS and I have a couple of questions:

1 - How can I make it work to generate the antlr4 parser and lexer from VS? I downloaded the C# target already. Do we need to do manual updates of the projects (same as antlr3) in order to generate the files when the grammar is saved, or it is expected that the plugin does all the code generation without having to do vs project files surgery?

2 - I Have added a new anrtlr4 g4 file to VS and it adds the helper files (as you said above), however it doesn't automatically add them as child files of the grammar (as your images show). Only when you reload the project, then, files appear as childen.


I can't wait to get all my stuff working with antlr4.

Thanks.

Sam Harwell

unread,
Mar 4, 2013, 9:20:17 AM3/4/13
to antlr-di...@googlegroups.com

Hi Roberto,

 

1.       It does add them as children, but they won’t show that way in Solution Explorer until you reload the project (either by the Unload Project / Reload Project commands, or by closing and reopening the solution).

2.       The plugin is not responsible for any of the actual code generation, as that would lead to a solution build that’s unnecessarily dependent on the particular set of extensions a user has installed. Instructions for updating the project file can be found on the following page:
https://github.com/sharwell/antlr4cs

 

Thank you,

--

Sam Harwell

Owner, Lead Developer

 

From: antlr-di...@googlegroups.com [mailto:antlr-di...@googlegroups.com] On Behalf Of Roberto Mencia Franco
Sent: Sunday, March 03, 2013 10:20 PM
To: antlr-di...@googlegroups.com
Subject: [antlr-discussion] Re: Visual Studio 2010-2012 support for using the ANTLR 4 C# target (coming soon)

 

Hi Sam,

It's great that you've got this so quickly.
I've just updated the ANTLR plug-in in VS and I have a couple of questions:

1 - I Have added a new anrtlr4 g4 file to VS and it adds the helper files (as you said above), however it doesn't automatically add them as child files of the grammar (as your images show). Is this a bug or a future feature that you are currently working on?

2 - How can I make it work to generate the antlr4 parser and lexer from VS? I downloaded the C# target already. Do we need to do manual updates of the projects (same as antlr3) in order to generate the files when the grammar is saved, or it is expected that the plugin does all the code generation without having to do vs project files surgery?



I can't wait to get all my stuff working with antlr4.

Thanks.


On Friday, March 1, 2013 5:38:37 AM UTC+11, Sam Harwell wrote:

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Roberto Mencia Franco

unread,
Mar 5, 2013, 6:03:51 PM3/5/13
to antlr-di...@googlegroups.com
I have tried a number of times and I couldn't make it work
  1. I followed the steps and the project builds with no errors.
  2. then I added a lexer using VS add ANTLR4 Lexer
  3. I compile and I get "Object reference error" (no more info)
  4. Where should the files be generated? in the obj folder? same as antlr3?
I have attached the solution if you want to have a look at it.

It may be good if you could create a sample project for the default templates in a way that
  1. we donwload and just unzip the solution (and includes the Reference folder)
  2. you open it in VS2012 (with the plugin already installed for syntax highlighting)
  3. compile and done, it works

This way we can use it as a reference in case something doesn't work.

WindowsFormsApplication1.zip

Sam Harwell

unread,
Mar 5, 2013, 6:40:49 PM3/5/13
to antlr-di...@googlegroups.com

Hi Roberto,

 

Your sample is configured correctly and builds without problems here. Do you have Java installed?

 

If you run into problems where the build says it can’t find a required .tokens file, you should update your Antlr4.targets file to the one in this commit:

https://github.com/sharwell/antlr4cs/commit/d41f4ba906a3f7e90f904316e64b27d345ab4b5f

Figure 1. Updated Exceptions dialog

 

Figure 2. ANTLR item templates (v3 and v4)

 

Figure 3. ANTLR-specific code generation properties

 

Figure 4. Automatic creation of helper files from ANTLR 4 item templates

 

Thanks,

--

Sam Harwell

Owner, Lead Developer

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted

Roberto Mencia Franco

unread,
Mar 5, 2013, 10:36:10 PM3/5/13
to antlr-di...@googlegroups.com
I'm sending this as email because I'm having issues posting it in the forums.

I've got the source code of the build task and I've recompiled the dll in debug mode and added a breakpoint to see where it's failing and it seems that there is an exception trying to access the registry for the Java path.
I'm still debuging, but it seems that it gets to "SOFTWARE\\JavaSoft" but not the last bit.
I'll let you know if I can find a solution to the problem.


Roberto.
(by the way, thanks for making this open source)

 


--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/yJR7ezuDqu4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Roberto Mencia Franco

unread,
Mar 5, 2013, 10:38:56 PM3/5/13
to antlr-di...@googlegroups.com
I forgot to attach the image.


On Wed, Mar 6, 2013 at 10:40 AM, Sam Harwell <s...@tunnelvisionlabs.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/yJR7ezuDqu4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.
build exception.png

Roberto Mencia Franco

unread,
Mar 5, 2013, 11:37:55 PM3/5/13
to antlr-di...@googlegroups.com
I've finally found what reason was and I've got a fix that you can include in your code.
The problem is that there are 2 ways of accesing the Registry, in 32bits or 64 bits mode. Some keys are stored in one and some in the other.

You were using RegistryView.Default as access mode and it couldn't find the full path (I've got Windows 7 64bits)
This code will try 32 bits first and then 64 if it couldn't find it. Finally it would throw an exception if Java is not installed.
With this fix my solution is already generating files.
        private string JavaHome
        {
            get
            {
 
                string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
 
		var value =  GetJavaHomeFromRegistry(javaKey, RegistryView.Registry32);
 
		if(string.IsNullOrEmpty(value) )
			value = GetJavaHomeFromRegistry(javaKey, RegistryView.Registry64);
 
		if (string.IsNullOrEmpty(value))
			throw new FileNotFoundException("I couldn't find java install");
 
	         return value;
            }
        }
 
	private static string GetJavaHomeFromRegistry(string javaKey, RegistryView registryView)
	    {
		using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(javaKey))
		    {
			    if (baseKey != null)
			    {
				    string currentVersion = baseKey.GetValue("CurrentVersion").ToString();
				    using (var homeKey = baseKey.OpenSubKey(currentVersion))
					    return homeKey.GetValue("JavaHome").ToString();
			    }
		    }
		return null;
	    }




On Wed, Mar 6, 2013 at 10:40 AM, Sam Harwell <s...@tunnelvisionlabs.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/yJR7ezuDqu4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Roberto Mencia Franco

unread,
Mar 6, 2013, 4:40:28 PM3/6/13
to antlr-di...@googlegroups.com
The code I posted (in this thread) for Antlr4BuildTasks.dll fixed the issue in my machine due to the access of a 64bits registry entry.

I've been using my version of the Antlr4BuildTasks.dll and works well, so far.

mi...@cooperusa.net

unread,
Aug 10, 2013, 3:57:39 PM8/10/13
to antlr-di...@googlegroups.com
How's the progress on this?

-mike

Sam Harwell

unread,
Aug 10, 2013, 5:46:23 PM8/10/13
to antlr-di...@googlegroups.com
Hi Mike,

Support should be pretty good now.

1. Install the ANTLR Language Support extension through the Visual Studio Extension Manager.
2. Use the NuGet Package Manager to add the ANTLR 4 prerelease package to your solution (you'll need to explicitly enable "Include Prereleases" when you search).

Let me know if you have any troubles!

Sam

-----Original Message-----
From: antlr-di...@googlegroups.com [mailto:antlr-di...@googlegroups.com] On Behalf Of mi...@cooperusa.net
Sent: Saturday, August 10, 2013 2:58 PM
To: antlr-di...@googlegroups.com

sco...@cox.net

unread,
Aug 27, 2013, 10:52:52 AM8/27/13
to antlr-di...@googlegroups.com

I've just installed the antlr extension into vs 2012 pro and i'm not sure what it is that i'm supposed to see. is it supposed to give me new project templates? if so, it doesn't. when I open a .g or g4 file it appears to colorize it properly as well as intellisense etc, however i'm unsure how to create the lexer/parser files for c#.

I've tried adding a .g file to a dll project and it seems to adjust the build info for the file, but that's about it. is there a walkthrough or something I can look at that might explain the workings in vs a bit better? so far I haven't found anything..

martin.c...@datron.de

unread,
Aug 29, 2013, 5:50:53 AM8/29/13
to antlr-di...@googlegroups.com, sco...@cox.net
Hello sco...

i would suggest that your read this post: https://groups.google.com/forum/#!topic/antlr-discussion/QSuJXphaBDg
and excatly follow the steps. For me, this worked perfectly

good luck, Martin

Ignazio Selvaggio

unread,
Feb 4, 2014, 5:43:54 AM2/4/14
to antlr-di...@googlegroups.com
Hi Roberto
great job.
I've the same problem.
Could you post your Antlr4BuildTasks.dll for 64 bit operating system?
Thanks
Ignazio

robertical .

unread,
Feb 4, 2014, 5:57:53 AM2/4/14
to antlr-di...@googlegroups.com

I don't have the code or dll anymore but I think that the issue was corrected.

You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/yJR7ezuDqu4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Ignazio Selvaggio

unread,
Feb 4, 2014, 6:26:10 AM2/4/14
to antlr-di...@googlegroups.com
Thanks for your quickly response
but in http://www.antlr.org/download.html
2013-02-27-antlr4-csharp-4.0.1-SNAPSHOT.7z zip contains still old files.
Is there another version officially released?
Thanks again.
Ignazio

Sam Harwell

unread,
Feb 4, 2014, 7:06:07 AM2/4/14
to antlr-di...@googlegroups.com

Hi Ignazio,

 

Here are more recent instructions.

https://github.com/sharwell/antlr4cs/wiki/Installation

 

Sam

Ignazio Selvaggio

unread,
Feb 4, 2014, 8:51:20 AM2/4/14
to antlr-di...@googlegroups.com
Hi Sam
Now it works.
Thanks

Ignazio

robertical .

unread,
Feb 5, 2014, 7:24:29 PM2/5/14
to antlr-di...@googlegroups.com
The error seems to be fixed. You can get latest source and recompile it, in case you still have issues.

https://github.com/sharwell/antlr4cs/issues/2

Ignazio Selvaggio

unread,
Feb 10, 2014, 6:39:36 AM2/10/14
to antlr-di...@googlegroups.com
Yes,
it works.
But NuGet Package is currently on prerelase version.
(for example I didn't find dottreegeneator)
Is a stable version planned?
Thanks
Ignazio
Reply all
Reply to author
Forward
0 new messages