package org.twig.syntax;

import org.twig.exception.SyntaxErrorException;

import java.util.ArrayList;
import java.util.List;

public class TokenStream {
    private List<Token> tokens;
    private Integer current = -1;
    private String filename;

    public TokenStream() {
        tokens = new ArrayList<>();
    }

    /**
     * Constructs a token stream without file/template name
     * @param tokens The tokens
     */
    public TokenStream(List<Token> tokens) {
        this.tokens = tokens;
    }

    /**
     * Constructs a token stream with file/template name
     * @param tokens The tokens
     * @param filename The name of the template wich the tokens are associated with
     */
    public TokenStream(List<Token> tokens, String filename) {
        this.tokens = tokens;
        this.filename = filename;
    }

    public Token next() throws SyntaxErrorException {
        try {
            current ++;
            return tokens.get(current);
        } catch (IndexOutOfBoundsException e) {
            throw SyntaxErrorException.unexpectedEndOfTemplate(filename, tokens.get(current-1).getLine(), e);
        }
    }

    /**
     * Sneak peak at a token at some specific index
     * @param index The index to look at
     * @return The found token
     * @throws SyntaxErrorException If the requested token doesn't exist
     */
    public Token look(Integer index) throws SyntaxErrorException {
        try {
            return tokens.get(index);
        } catch (IndexOutOfBoundsException e) {
            throw SyntaxErrorException.unexpectedEndOfTemplate(filename, tokens.get(current).getLine(), e);
        }
    }

    /**
     * Adds a new token
     * @param token The token to add
     */
    public void add(Token token) {
        tokens.add(token);
    }
}

@@ -243,16 +243,73 @@
 ename;%0A%0A
+    /**%0A     * Constructs an empty tokens stream%0A     */%0A
     publ
@@ -323,24 +323,24 @@
 nStream() %7B%0A
-
         toke
@@ -706,16 +706,17 @@
 mplate w
+h
 ich the 
@@ -879,24 +879,306 @@
 ame;%0A    %7D%0A%0A
+    /**%0A     * Constructs a token stream with file/template name%0A     * @param filename The name of the template which the tokens are associated with%0A     */%0A    public TokenStream(String filename) %7B%0A        this.tokens = new ArrayList%3C%3E();%0A        this.filename = filename;%0A    %7D%0A%0A
     public T
@@ -2085,24 +2085,24 @@
 en token) %7B%0A
-
         toke
@@ -2121,11 +2121,737 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Get all tokens (not recommended to use)%0A     * @return All tokens%0A     */%0A    public List%3CToken%3E getTokens() %7B%0A        return tokens;%0A    %7D%0A%0A    /**%0A     * Set all tokens%0A     * @param tokens All tokens%0A     */%0A    public void setTokens(List%3CToken%3E tokens) %7B%0A        this.tokens = tokens;%0A    %7D%0A%0A    /**%0A     * Get the template name%0A     * @return filename The name of the template which the tokens are associated with%0A     */%0A    public String getFilename() %7B%0A        return filename;%0A    %7D%0A%0A    /**%0A     * Set the template name%0A     * @param filename The name of the template which the tokens are associated with%0A     */%0A    public void setFilename(String filename) %7B%0A        this.filename = filename;%0A    %7D
 %0A%7D%0A

Add constructor with filname only and getters and setters to TokenStream