/// Get tree node at current input pointer + i ahead where i=1 is next node.
/// i<0 indicates nodes in the past.  So -1 is previous node and -2 is
/// two nodes ago. LT(0) is undefined.  For i>=n, return null.
/// Return null for LT(0) and any index that results in an absolute address
/// that is negative.
///
/// This is analogous to the _LT() method of the TokenStream, but this
/// returns a tree node instead of a token.  Makes code gen identical
/// for both parser and tree grammars. :)
///
static  pANTLR3_BASE_TREE
_LT         (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k)
{
        if      (tns->ctns->p == -1)
        {
                fillBufferRoot(tns->ctns);
        }

        if      (k < 0)
        {
                return LB(tns, -k);
        }
        else if (k == 0)
        {
                return  &(tns->ctns->INVALID_NODE.baseTree);
        }

        // k was a legitimate request,
        //
        if      (( tns->ctns->p + k - 1) >= (ANTLR3_INT32)(tns->ctns->nodes->count))
        {
                return &(tns->ctns->EOF_NODE.baseTree);
        }

        return  tns->ctns->nodes->get(tns->ctns->nodes, tns->ctns->p + k - 1);
}
static  pANTLR3_BASE_TREE
get                                                     (pANTLR3_TREE_NODE_STREAM tns, ANTLR3_INT32 k)
{
        if      (tns->ctns->p == -1)
        {
                fillBufferRoot(tns->ctns);
        }

        return tns->ctns->nodes->get(tns->ctns->nodes, k);
}
/// Consume the next node from the input stream
///
static  void
consume (pANTLR3_INT_STREAM is)
{
    pANTLR3_TREE_NODE_STREAM            tns;
    pANTLR3_COMMON_TREE_NODE_STREAM     ctns;

    tns     = (pANTLR3_TREE_NODE_STREAM)(is->super);
    ctns    = tns->ctns;

        if      (ctns->p == -1)
        {
                fillBufferRoot(ctns);
        }
        ctns->p++;
}
/// Expensive to compute the size of the whole tree while parsing.
/// This method only returns how much input has been seen so far.  So
/// after parsing it returns true size.
///
static  ANTLR3_UINT32
size    (pANTLR3_INT_STREAM is)
{
    pANTLR3_TREE_NODE_STREAM            tns;
    pANTLR3_COMMON_TREE_NODE_STREAM     ctns;

    tns     = (pANTLR3_TREE_NODE_STREAM)(is->super);
    ctns    = tns->ctns;

        if      (ctns->p == -1)
        {
                fillBufferRoot(ctns);
        }

        return ctns->nodes->size(ctns->nodes);
}
/// Mark the state of the input stream so that we can come back to it
/// after a syntactic predicate and so on.
///
static  ANTLR3_MARKER
mark    (pANTLR3_INT_STREAM is)
{
        pANTLR3_TREE_NODE_STREAM                tns;
        pANTLR3_COMMON_TREE_NODE_STREAM ctns;

        tns         = (pANTLR3_TREE_NODE_STREAM)(is->super);
        ctns    = tns->ctns;

        if      (tns->ctns->p == -1)
        {
                fillBufferRoot(tns->ctns);
        }

        // Return the current mark point
        //
        ctns->tnstream->istream->lastMarker = ctns->tnstream->istream->index(ctns->tnstream->istream);

        return ctns->tnstream->istream->lastMarker;
}
/// Determine the stream index for a particular node.
/// NB: This uses internal knowledge of the Antlr3 Vector for the
/// sake of efficiency. Don't do this in application code. 
/// This is just an internal debugging type interface so does not need to be improved.
///
static ANTLR3_INT32
getNodeIndex(pANTLR3_COMMON_TREE_NODE_STREAM ctns, pANTLR3_BASE_TREE t)
{
	ANTLR3_UINT32	i;
	ANTLR3_UINT32	j;
	pANTLR3_VECTOR_ELEMENT	elements;

	if	(ctns->p == -1)
	{
		fillBufferRoot(ctns);
	}

	j	= ctns->nodes->count;

	elements = ctns->nodes->elements;

	for	(i = 0; i < j; i++)
	{
		if	(t == elements[i].element)
		{
			return i;
		}
	}
}