package token;

import java.util.function.Supplier;

import file.FilePosition;
import sexpression.Cons;
import sexpression.SExpression;

public abstract class Token {

    private String text;
    private FilePosition position;

    public Token(String text, FilePosition position) {
        this.text = text;
        this.position = position;
    }

    public String getText() {
        return text;
    }

    public FilePosition getPosition() {
        return position;
    }

    // s-expression ::= NUMBER | IDENTIFIER | STRING | QUOTE_MARK s-expression | LEFT_PAREN
    // s-expression-tail
    // s-expression-tail ::= RIGHT_PAREN | s-expression s-expression-tail

    public abstract SExpression parseSExpression(Supplier<Token> getNextToken);

    public SExpression parseSExpressionTail(Supplier<Token> getNextToken) {
        SExpression first = parseSExpression(getNextToken);

        Token nextToken = getNextToken.get();
        SExpression rest = nextToken.parseSExpressionTail(getNextToken);

        return new Cons(first, rest);
    }

}
