File: CharBuffer.cs

package info (click to toggle)
antlr 2.7.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,920 kB
  • sloc: java: 54,649; cs: 12,533; makefile: 8,963; cpp: 7,359; pascal: 5,273; sh: 4,337; python: 4,301; lisp: 1,969; xml: 220; lex: 192; ansic: 134
file content (92 lines) | stat: -rwxr-xr-x 2,367 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Runtime.InteropServices;
using TextReader		= System.IO.TextReader;
using IOException		= System.IO.IOException;


namespace antlr
{
	/*ANTLR Translator Generator
	* Project led by Terence Parr at http://www.jGuru.com
	* Software rights: http://www.antlr.org/license.html
	*
	* $Id:$
	*/

	//
	// ANTLR C# Code Generator by Micheal Jordan
	//                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
	//                            Anthony Oguntimehin
	//
	// With many thanks to Eric V. Smith from the ANTLR list.
	//

	/*A Stream of characters fed to the lexer from a InputStream that can
	* be rewound via mark()/rewind() methods.
	* <p>
	* A dynamic array is used to buffer up all the input characters.  Normally,
	* "k" characters are stored in the buffer.  More characters may be stored during
	* guess mode (testing syntactic predicate), or when LT(i>k) is referenced.
	* Consumption of characters is deferred.  In other words, reading the next
	* character is not done by conume(), but deferred until needed by LA or LT.
	* <p>
	*/
	
	// SAS: Move most functionality into InputBuffer -- just the file-specific
	//      stuff is in here
	public class CharBuffer : InputBuffer
	{
		// char source
		[NonSerialized()]
		internal TextReader input;

		private const int BUF_SIZE = 16;
		/// <summary>
		/// Small buffer used to avoid reading individual chars
		/// </summary>
		private char[] buf = new char[BUF_SIZE];

		
		/*Create a character buffer */
		public CharBuffer(TextReader input_) : base()
		{ 
			input = input_;
		}
		
		/*Ensure that the character buffer is sufficiently full */
		override public void  fill(int amount)
		{
			try
			{
				syncConsume();
				// Fill the buffer sufficiently to hold needed characters
				int charsToRead = (amount + markerOffset) - queue.Count;
				int c;

				while (charsToRead > 0)
				{
					// Read a few characters
					c = input.Read(buf, 0, BUF_SIZE);
					for (int i = 0; i < c; i++)
					{
						// Append the next character
						queue.Add(buf[i]);
					}
					if (c < BUF_SIZE)
					{
						while ((charsToRead-- > 0) && (queue.Count < BUF_SIZE))
						{
							queue.Add(CharScanner.EOF_CHAR);
						}
						break;
					}
					charsToRead -= c;
				}
			}
			catch (IOException io)
			{
				throw new CharStreamIOException(io);
			}
		}
	}
}