package site.kason.tempera.lexer;

import java.util.ArrayList;
import java.util.List;
import site.kason.tempera.lex.LexException;

/**
 *
 * @author Kason Yang
 */
public class BufferedTokenStream implements TokenStream {
  
  TokenStream tokenStream;
  
  List<TexToken> bufferedTokens = new ArrayList();

  public BufferedTokenStream(TokenStream tokenStream) {
    this.tokenStream = tokenStream;
  }

  @Override
  public TexToken nextToken() throws LexException {
    if(bufferedTokens.isEmpty()){
      return tokenStream.nextToken();
    }else{
      return bufferedTokens.remove(0);
    }
  }
  
  public TexToken LA(int offset) throws LexException{
    while(bufferedTokens.size()<=offset){
      //TODO maybe EOF
      TexToken next = tokenStream.nextToken();
      bufferedTokens.add(next);
    }
    return bufferedTokens.get(offset);
  }

}

@@ -650,16 +650,118 @@
 eption%7B%0A
+    if(offset%3C=0)%7B%0A      throw new IllegalArgumentException(%22offset should be greater than 0%22);%0A    %7D%0A
     whil
@@ -784,17 +784,16 @@
 .size()%3C
-=
 offset)%7B
@@ -937,16 +937,18 @@
 t(offset
+-1
 );%0A  %7D%0A%0A

fix LA