[antlr-interest] Antlr-3.2 C# Target: RecognitionException.cs Constructor Bug

John Moses jmoses at hops.com
Tue Dec 15 11:37:59 PST 2009


Howdy,

I believe I'm running into a bug in the constructors of class RecognitionException in the CSharp code of Antlr.Runtime.

5 of the 6 constructors of RecognitionException call this version (with various parameters 'nulled' out):
	public RecognitionException(string message, Exception inner, IIntStream input)

3 of the 6 call the above constructor with null for the IIntStream input parameter. Unfortunately, the 2nd line of this constructor is:
	this.index = input.Index();
which immediately throws the infamous 'Object reference not set to an instance of an object' exception (since input is null).

The most obvious way to encounter the bug is to throw a new RecognitionException upon discovery of some 'otherwise-not-an-Antlr-exception' situation (like the number of expressions in an expression_list doesn't match what a function call needed), e.g.:
	throw new RecognitionException("Improper number of parameters in function");

(Just a simple example, in real life I do a better job of reporting an error than this! :-)

My workaround is either:
	1) Capture the IIntStream from earlier in the process and call a different constructor:
	throw new RecognitionException("Improper blah, blah",MyCapturedIIntStream);
	2) Throw an exception unrelated to RecognitionException.

But it should be easy to fix.
Thanks,

-John


More information about the antlr-interest mailing list