* Finds the next term we are going to serialize, based on the current state of the stack.
*
* @return The next term we are going to serialize.
*/
private ATerm getNextTerm(){
ATerm next = null;
// Make sure the stack remains large enough
ensureStackCapacity();
while(next == null && stackPosition > -1){
ATermMapping current = stack[stackPosition];
ATerm term = current.term;
if(term.getChildCount() > current.subTermIndex + 1){
if(term.getType() != ATerm.LIST){
next = (ATerm) term.getChildAt(++current.subTermIndex);
}else{
ATermList nextList = current.nextPartOfList;
next = nextList.getFirst();
current.nextPartOfList = nextList.getNext();
current.subTermIndex++;
}
ATermMapping child = new ATermMapping();
child.term = next;
stack[++stackPosition] = child;
}else if(!current.annosDone && term.hasAnnotations()){
next = term.getAnnotations();
ATermMapping annos = new ATermMapping();
annos.term = next;
stack[++stackPosition] = annos;