[antlr-interest] Is the -1 bug in .NET ANTLR 3.0.1 runtime's ANTLRReaderStream fixed or not? (was: Antlr and Visual Studio)

Harald M. Müller harald_m_mueller at gmx.de
Fri Dec 21 13:31:15 PST 2007


Hi ANTLR maintainers (Kunle?!?) -

In ANTLRReaderStream (in the C# runtime), there is or was a bug in method
Load(...): The condition of the do-while loop is in some sources

	while (num != -1);

whereas in other sources (interim builds) it is

	while (numRead != 0);

The latter is correct, the former is wrong and leads to an endless loop.

However, when I download the current source 3.0.1 from the ANTLR page
(ANTLR.Tool.zip from the link "Binary of ANTLR tool"; with
runtime\CSharp\bin\net-2.0\Antlr3.Runtime.dll with Version 0.3.0.37313 and
file date Aug. 13th, 2007, 11:40pm - i.e., the "latest ANTLR version") and
look into ANTLRReaderStream.Load() with Reflector, I still see the first -
wrong - condition!!
It seems something got mixed up here ...

Will/might this be repaired in some official 3.0.x release, or so we have to
wait for an official 3.1 for this?

BTW, array doubling is a somewhat slow algorithm for very long files - I
have input files with more than 100MB, which yields about 10 doublings and
hence copies around 1 GB of data. Below is a (not really beautified; and
intentionally .Net 1.1) variant which does only one copying over and hence
is faster (but requires twice the final memory instead of only 1.5 for the
original algorithm).

Regards
Harald M.

		/// <summary>
		/// Loads and buffers the contents of the specified reader
to be 
		/// used as this ANTLRReaderStream's source
		/// </summary>
		public virtual void Load(TextReader reader, int size, int
readChunkSize)
		{
			if (reader == null)
			{
				return;
			}
			if (size <= 0)
			{
				size = INITIAL_BUFFER_SIZE;
			}
			if (readChunkSize <= 0)
			{
				readChunkSize = READ_BUFFER_SIZE;
			}

			try
			{
			    IList buffers = new ArrayList(100);
			    int length;
                        for (int i = 0; ; i++) {
                            char[] segment = new char[readChunkSize];
                            int numRead = reader.Read(segment, p,
readChunkSize);
                            if (numRead != readChunkSize) 
                            {
                                char[] lastSegment = new char[numRead];
                                Array.Copy(segment, 0, lastSegment, 0,
numRead);
                                buffers.Add(lastSegment);
                                length = (buffers.Count - 1) * readChunkSize
+ numRead;
                                break;
                            } 
                            else 
                            {
                                buffers.Add(segment);
                            }
                        }
                        data = new char[length];
                        p = 0;
                        foreach (char[] segment in buffers) {
                            Array.Copy(segment, 0, data, p, segment.Length);
                            p += segment.Length;
                        }
			}
			finally
			{
				reader.Close();
			}
		}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071221/f33f7245/attachment-0001.html 


More information about the antlr-interest mailing list