/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fasp.parser.parsers;
import fasp.parser.tokenizer.SymbolToken;
import fasp.parser.*;
import fasp.parser.tokenizer.Token;
import fasp.parser.tokenizer.TokenState;
import fasp.datatypes.FaspVariable;
/**
*
* @author Jeroen Janssen <Jeroen.Janssen@vub.ac.be>
*/
public class VariableParser extends AbstractParser<FaspVariable> {
/**
* This parse function consumes no input on failure and is thus
* safe to use in tryParse.
* @param st
* @return
* @throws fasp.parser.ParseException
*/
@Override
public FaspVariable parse(TokenState st) throws ParseException {
if (getResult() == null) {
Token lookahead = st.lookahead();
if(lookahead.getType().equals(Token.Type.SYMBOL)) {
SymbolToken sym = (SymbolToken)lookahead;
if(Character.isUpperCase(sym.getSymbol().charAt(0))) {
st.eat();
return new FaspVariable(sym.getSymbol());
} else {
throw new ParseException(st,"variable","constant");
}
} else {
throw new ParseException(st,"variable",st.lookahead().toString());
}
} else {
return getResult();
}
}
}