package de.dhbw.compiler.xparser.api;

import de.dhbw.compiler.xparser.Token;
import de.dhbw.compiler.xparser.TokenReader;
import de.dhbw.compiler.xparser.Tree;

public abstract class Parser {
    protected final TokenReader in;

    protected Parser(TokenReader in) {
        this.in = in;
    }

    public abstract Tree parseProgram();

    protected Tree parseToken(int tokenType) {
        int oldPosition = in.getPosition();
        Token nextToken = in.nextToken();
        System.out.println("parseToken> " + nextToken + " = " + Token.getTypeName(tokenType));
        if (nextToken.getType() == tokenType) {
            return new Tree(nextToken);
        } else {
            // Reset to last position as it needs to be re-read again
            in.setPosition(oldPosition);
            return null;
        }
    }
}
