Search in sources :

Example 1 with FloatConstantNode

use of org.cytoscape.equations.internal.parse_tree.FloatConstantNode in project cytoscape-impl by cytoscape.

the class EquationParserImpl method parseFactor.

/**
 *  Implements factor --> constant | variable_ref | "(" expr ")" | ("-"|"+") factor  | func_call
 */
private AbstractNode parseFactor() {
    Token token = tokeniser.getToken();
    int sourceLocation = tokeniser.getStartPos();
    // 1. a constant
    if (token == Token.FLOAT_CONSTANT)
        return new FloatConstantNode(sourceLocation, tokeniser.getFloatConstant());
    else if (token == Token.STRING_CONSTANT)
        return new StringConstantNode(sourceLocation, tokeniser.getStringConstant());
    else if (token == Token.BOOLEAN_CONSTANT)
        return new BooleanConstantNode(sourceLocation, tokeniser.getBooleanConstant());
    // 2. a variable reference
    if (token == Token.DOLLAR) {
        final int varRefStartPos = sourceLocation;
        token = tokeniser.getToken();
        sourceLocation = tokeniser.getStartPos();
        final boolean usingOptionalBraces = token == Token.OPEN_BRACE;
        if (usingOptionalBraces) {
            token = tokeniser.getToken();
            sourceLocation = tokeniser.getStartPos();
        }
        if (token != Token.IDENTIFIER)
            throw new IllegalStateException(sourceLocation + ": identifier expected.");
        final String ident = tokeniser.getIdent();
        final Class<?> varRefType = variableNameToTypeMap.get(ident);
        if (varRefType == null)
            throw new IllegalStateException(sourceLocation + ": unknown variable reference name: \"" + ident + "\".");
        variableReferences.add(ident);
        Object defaultValue = null;
        if (usingOptionalBraces) {
            token = tokeniser.getToken();
            // Do we have a default value?
            if (token == Token.COLON) {
                token = tokeniser.getToken();
                sourceLocation = tokeniser.getStartPos();
                if (token != Token.FLOAT_CONSTANT && token != Token.STRING_CONSTANT && token != Token.BOOLEAN_CONSTANT)
                    throw new IllegalStateException(sourceLocation + ": expected default value for variable reference.");
                switch(token) {
                    case FLOAT_CONSTANT:
                        defaultValue = new Double(tokeniser.getFloatConstant());
                        break;
                    case BOOLEAN_CONSTANT:
                        defaultValue = new Boolean(tokeniser.getBooleanConstant());
                        break;
                    case STRING_CONSTANT:
                        defaultValue = new String(tokeniser.getStringConstant());
                        break;
                }
                token = tokeniser.getToken();
                sourceLocation = tokeniser.getStartPos();
            }
            if (token != Token.CLOSE_BRACE)
                throw new IllegalStateException(sourceLocation + ": closing brace expected.");
            defaultVariableValues.put(ident, defaultValue);
        }
        return new IdentNode(varRefStartPos, tokeniser.getIdent(), defaultValue, varRefType);
    }
    // 3. a parenthesised expression
    if (token == Token.OPEN_PAREN) {
        final AbstractNode exprNode = parseExpr();
        token = tokeniser.getToken();
        if (token != Token.CLOSE_PAREN)
            throw new IllegalStateException(sourceLocation + ": '(' expected.");
        return exprNode;
    }
    // 4. a unary operator
    if (token == Token.PLUS || token == Token.MINUS) {
        final TreeNode factor = parseFactor();
        return handleUnaryOp(sourceLocation, token, factor);
    }
    // 5. function call
    if (token == Token.IDENTIFIER) {
        tokeniser.ungetToken(token);
        return parseFunctionCall();
    }
    if (token == Token.ERROR)
        throw new IllegalStateException(sourceLocation + ": " + tokeniser.getErrorMsg());
    throw new IllegalStateException(sourceLocation + ": unexpected input token: " + token + ".");
}
Also used : StringConstantNode(org.cytoscape.equations.internal.parse_tree.StringConstantNode) AbstractNode(org.cytoscape.equations.AbstractNode) ListToString(org.cytoscape.equations.internal.builtins.ListToString) BooleanConstantNode(org.cytoscape.equations.internal.parse_tree.BooleanConstantNode) TreeNode(org.cytoscape.equations.TreeNode) IdentNode(org.cytoscape.equations.internal.parse_tree.IdentNode) FloatConstantNode(org.cytoscape.equations.internal.parse_tree.FloatConstantNode)

Aggregations

AbstractNode (org.cytoscape.equations.AbstractNode)1 TreeNode (org.cytoscape.equations.TreeNode)1 ListToString (org.cytoscape.equations.internal.builtins.ListToString)1 BooleanConstantNode (org.cytoscape.equations.internal.parse_tree.BooleanConstantNode)1 FloatConstantNode (org.cytoscape.equations.internal.parse_tree.FloatConstantNode)1 IdentNode (org.cytoscape.equations.internal.parse_tree.IdentNode)1 StringConstantNode (org.cytoscape.equations.internal.parse_tree.StringConstantNode)1