Search in sources :

Example 1 with ExprException

use of org.csstudio.autocomplete.parser.engine.expr.ExprException in project yamcs-studio by yamcs.

the class ContentParserHelper method parseStandardFunction.

/**
 * @return {@link FunctionDescriptor} filled from the content.
 */
public static FunctionDescriptor parseStandardFunction(String content) {
    FunctionDescriptor token = new FunctionDescriptor();
    if (content == null || content.isEmpty())
        return token;
    ExprFunction function = null;
    try {
        Expr e = ExprParser.parse(content);
        if (e == null)
            return token;
        else if (e.type == ExprType.Function)
            function = (ExprFunction) e;
        else if (e.type == ExprType.Variable) {
            function = new ExprFunction(((ExprVariable) e).getName(), null);
        }
    } catch (IOException | ExprException e) {
        AutoCompletePlugin.getLogger().log(Level.SEVERE, "Failed to parse function \"" + content + "\": " + e.getMessage());
    }
    if (function == null)
        return token;
    token.setValue(content);
    token.setFunctionName(function.getName());
    if (function.getArgs() != null) {
        token.setOpenBracket(true);
        token.setCurrentArgIndex(function.size() > 0 ? function.size() - 1 : 0);
        for (Expr e : function.getArgs()) {
            switch(e.type) {
                case Boolean:
                    token.addArgument(((ExprBoolean) e).value);
                    break;
                case Double:
                    token.addArgument(((ExprDouble) e).value);
                    break;
                case Integer:
                    token.addArgument(((ExprInteger) e).value);
                    break;
                case String:
                    token.addArgument(((ExprString) e).str);
                    break;
                default:
                    // ignore other types
                    token.addArgument(new Object());
                    break;
            }
        }
    }
    token.setComplete(function.isComplete());
    return token;
}
Also used : ExprException(org.csstudio.autocomplete.parser.engine.expr.ExprException) Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) ExprFunction(org.csstudio.autocomplete.parser.engine.expr.ExprFunction) IOException(java.io.IOException) ExprVariable(org.csstudio.autocomplete.parser.engine.expr.ExprVariable)

Example 2 with ExprException

use of org.csstudio.autocomplete.parser.engine.expr.ExprException in project yamcs-studio by yamcs.

the class FormulaContentParser method parse.

@Override
public ContentDescriptor parse(final ContentDescriptor desc) {
    currentToken = null;
    // remove first '='
    contentToParse = new String(desc.getValue()).substring(1);
    try {
        Expr e = ExprParser.parse(contentToParse);
        handleExpr(e);
    } catch (IOException | ExprException e) {
        AutoCompletePlugin.getLogger().log(Level.WARNING, e.getMessage());
    }
    return currentToken;
}
Also used : ExprException(org.csstudio.autocomplete.parser.engine.expr.ExprException) Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 Expr (org.csstudio.autocomplete.parser.engine.expr.Expr)2 ExprException (org.csstudio.autocomplete.parser.engine.expr.ExprException)2 ExprFunction (org.csstudio.autocomplete.parser.engine.expr.ExprFunction)1 ExprVariable (org.csstudio.autocomplete.parser.engine.expr.ExprVariable)1