transcendental-lisp/src/token/Token.java

43 lines
992 B
Java

package token;
import java.util.function.Supplier;
import file.FilePosition;
import sexpression.*;
/**
* A token in Lisp.
*/
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;
}
// sExpr ::= NUMBER | IDENTIFIER | STRING | QUOTE_MARK sExpr | LEFT_PAREN sExprTail
public abstract SExpression parseSExpression(Supplier<Token> getNextToken);
// sExprTail ::= RIGHT_PAREN | sExpr sExprTail
public SExpression parseSExpressionTail(Supplier<Token> getNextToken) {
SExpression car = parseSExpression(getNextToken);
Token nextToken = getNextToken.get();
SExpression cdr = nextToken.parseSExpressionTail(getNextToken);
return new Cons(car, cdr);
}
}