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 the token buffer */
34	class TokenQueue {
35		// Physical circular buffer of tokens
36		private Token[] buffer;
37		// buffer.length-1 for quick modulous
38		private int sizeLessOne;
39		// physical index of front token
40		private int offset;
41		// number of tokens in the queue
42		protected int nbrEntries;
43	
44	
45		public TokenQueue(int minSize) {
46			// Find first power of 2 >= to requested size
47			int size;
48			for (size = 2; size < minSize; size *= 2) {;}
49			init(size);
50		}
51		/** Add token to end of the queue
52		 * @param tok The token to add
53		 */
54		public final void append(Token tok)
55		{
56			if (nbrEntries == buffer.length)
57			{
58				expand();
59			}
60			buffer[(offset + nbrEntries) & sizeLessOne] = tok;
61			nbrEntries++;
62		}
63		/** Fetch a token from the queue by index
64		 * @param idx The index of the token to fetch, where zero is the token at the front of the queue
65		 */
66		public final Token elementAt(int idx) { 
67			return buffer[(offset + idx) & sizeLessOne];
68		}
69		/** Expand the token buffer by doubling its capacity */
70		private final void expand()
71		{
72			Token[] newBuffer = new Token[buffer.length * 2];
73			// Copy the contents to the new buffer
74			// Note that this will store the first logical item in the 
75			// first physical array element.
76			for (int i = 0; i < buffer.length; i++)
77			{
78				newBuffer[i] = elementAt(i);
79			}
80			// Re-initialize with new contents, keep old nbrEntries
81			buffer = newBuffer;
82			sizeLessOne = buffer.length - 1;
83			offset = 0;
84		}
85		/** Initialize the queue.
86		 * @param size The initial size of the queue
87		 */
88		private final void init(int size) {
89			// Allocate buffer
90			buffer = new Token[size];
91			// Other initialization
92			sizeLessOne = size - 1;
93			offset = 0;
94			nbrEntries = 0;
95		}
96		/** Remove token from front of queue */
97		public final void removeFirst() { 
98			offset = (offset+1) & sizeLessOne;
99			nbrEntries--;
100		}
101	}
102