1	package antlr;
2	
3	/**
4	 * <b>SOFTWARE RIGHTS</b>
5	 * <p>
6	 * ANTLR 2.5.0 MageLang Institute, 1998
7	 * <p>
8	 * We reserve no legal rights to the ANTLR--it is fully in the
9	 * public domain. An individual or company may do whatever
10	 * they wish with source code distributed with ANTLR or the
11	 * code generated by ANTLR, including the incorporation of
12	 * ANTLR, or its output, into commerical software.
13	 * <p>
14	 * We encourage users to develop software with ANTLR. However,
15	 * we do ask that credit is given to us for developing
16	 * ANTLR. By "credit", we mean that if you use ANTLR or
17	 * incorporate any source code into one of your programs
18	 * (commercial product, research project, or otherwise) that
19	 * you acknowledge this fact somewhere in the documentation,
20	 * research report, etc... If you like ANTLR and have
21	 * developed a nice tool with the output, please mention that
22	 * you developed it using ANTLR. In addition, we ask that the
23	 * headers remain intact in our source code. As long as these
24	 * guidelines are kept, we expect to continue enhancing this
25	 * system and expect to make other tools available as they are
26	 * completed.
27	 * <p>
28	 * The ANTLR gang:
29	 * @version ANTLR 2.5.0 MageLang Institute, 1998
30	 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31	 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32	 */
33	/** A private circular buffer object used by CharBuffer */
34	class CharQueue 
35	{
36		// Physical circular buffer of characters
37		private char[] buffer;
38		// buffer.length-1 for quick modulous
39		private int sizeLessOne;
40		// physical index of front token
41		private int offset;
42		// number of characters in the queue
43		protected int nbrEntries;
44	
45	
46		public CharQueue(int minSize) {
47			// Find first power of 2 >= to requested size
48			int size;
49			for (size = 2; size < minSize; size *= 2) {;}
50			init(size);
51		}
52		/** Add token to end of the queue
53		 * @param tok The token to add
54		 */
55		public final void append(char tok)
56		{
57			if (nbrEntries == buffer.length)
58			{
59				expand();
60			}
61			buffer[(offset + nbrEntries) & sizeLessOne] = tok;
62			nbrEntries++;
63		}
64		/** Fetch a token from the queue by index
65		 * @param idx The index of the token to fetch, where zero is the token at the front of the queue
66		 */
67		public final char elementAt(int idx) { 
68			return buffer[(offset + idx) & sizeLessOne];
69		}
70		/** Expand the token buffer by doubling its capacity */
71		private final void expand()
72		{
73			char[] newBuffer = new char[buffer.length * 2];
74			// Copy the contents to the new buffer
75			// Note that this will store the first logical item in the 
76			// first physical array element.
77			for (int i = 0; i < buffer.length; i++)
78			{
79				newBuffer[i] = elementAt(i);
80			}
81			// Re-initialize with new contents, keep old nbrEntries
82			buffer = newBuffer;
83			sizeLessOne = buffer.length - 1;
84			offset = 0;
85		}
86		/** Initialize the queue.
87		 * @param size The initial size of the queue
88		 */
89		private final void init(int size) {
90			// Allocate buffer
91			buffer = new char[size];
92			// Other initialization
93			sizeLessOne = size - 1;
94			offset = 0;
95			nbrEntries = 0;
96		}
97		/** Remove char from front of queue */
98		public final void removeFirst() { 
99			offset = (offset+1) & sizeLessOne;
100			nbrEntries--;
101		}
102	}
103