Search in sources :

Example 6 with Expr

use of org.csstudio.autocomplete.parser.engine.expr.Expr 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 7 with Expr

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

the class ExprParser method parseFunction.

private void parseFunction(ExprToken token, ExprLexer lexer) throws ExprException, IOException {
    Expr c = current;
    current = null;
    ExprToken e = null;
    ArrayList<Expr> args = new ArrayList<Expr>();
    boolean complete = false;
    while ((e = lexer.next()) != null) {
        if (e.type.equals(ExprTokenType.Comma)) {
            if ((e = lexer.next()) != null) {
                current = null;
                parseToken(lexer, e);
                args.add(current == null ? new ExprMissing() : current);
            } else {
                args.add(new ExprMissing());
            }
        } else if (e.type.equals(ExprTokenType.CloseBracket)) {
            // end
            complete = true;
            current = c;
            break;
        } else {
            // first arg
            parseToken(lexer, e);
            args.add(current == null ? new ExprMissing() : current);
        }
    }
    ExprFunction f = new ExprFunction(token.val, (Expr[]) args.toArray(new Expr[0]));
    f.setComplete(complete);
    setValue(f);
}
Also used : Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) ExprFunction(org.csstudio.autocomplete.parser.engine.expr.ExprFunction) ArrayList(java.util.ArrayList) ExprMissing(org.csstudio.autocomplete.parser.engine.expr.ExprMissing)

Example 8 with Expr

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

the class ExprParser method parseOperator.

private void parseOperator(ExprToken e, ExprLexer lexer) throws ExprException, IOException {
    // handle negative numbers
    if ((e.type == ExprTokenType.Minus || e.type == ExprTokenType.Plus) && current == null) {
        ExprToken nextToken = lexer.next();
        if (nextToken == null)
            return;
        Expr value = null;
        switch(nextToken.type) {
            case Decimal:
                value = new ExprDouble(e.type == ExprTokenType.Minus ? -nextToken.doubleValue : nextToken.doubleValue);
                setValue(value);
                return;
            case Integer:
                value = new ExprInteger(e.type == ExprTokenType.Minus ? -nextToken.integerValue : nextToken.integerValue);
                setValue(value);
                return;
            default:
                break;
        }
        current = new ExprBinaryOperator(ExprType.BinaryOperation, null, null);
        parseToken(lexer, nextToken);
        return;
    }
    current = new ExprBinaryOperator(ExprType.BinaryOperation, current, null);
}
Also used : ExprDouble(org.csstudio.autocomplete.parser.engine.expr.ExprDouble) Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) ExprBinaryOperator(org.csstudio.autocomplete.parser.engine.expr.ExprBinaryOperator) ExprInteger(org.csstudio.autocomplete.parser.engine.expr.ExprInteger)

Example 9 with Expr

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

the class ExprParser method parseConditionalOperator.

private void parseConditionalOperator(ExprToken token, ExprLexer lexer) throws ExprException, IOException {
    Expr c = current;
    current = null;
    ExprToken e = null;
    ExprConditionalOperator co = new ExprConditionalOperator(c, null, null);
    while ((e = lexer.next()) != null) {
        if (e.type.equals(ExprTokenType.Colon)) {
            if ((e = lexer.next()) != null) {
                current = null;
                parseToken(lexer, e);
                co.setValueIfFalse(current == null ? new ExprMissing() : current);
            } else {
                co.setValueIfFalse(new ExprMissing());
            }
            break;
        } else {
            parseToken(lexer, e);
            co.setValueIfTrue(current == null ? new ExprMissing() : current);
        }
    }
    setValue(co);
}
Also used : Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) ExprConditionalOperator(org.csstudio.autocomplete.parser.engine.expr.ExprConditionalOperator) ExprMissing(org.csstudio.autocomplete.parser.engine.expr.ExprMissing)

Example 10 with Expr

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

the class ExprParser method parseArray.

private void parseArray(ExprLexer lexer) throws ExprException, IOException {
    Expr c = current;
    current = null;
    ExprToken e = null;
    int cols = -1;
    int count = 0;
    ArrayList<Expr> args = new ArrayList<Expr>();
    while ((e = lexer.next()) != null) {
        if (e.type.equals(ExprTokenType.Comma)) {
            // if (current == null)
            // throw new ExprException("Arrays cannot contain empty values");
            args.add(current == null ? new ExprMissing() : current);
            current = null;
            count++;
        } else if (e.type.equals(ExprTokenType.SemiColon)) {
            // if (current == null)
            // throw new ExprException("Arrays cannot contain empty values");
            args.add(current == null ? new ExprMissing() : current);
            current = null;
            count++;
            // if (count == 0) {
            // throw new ExprException("Array rows must contain at least one element");
            // }
            // if (cols != -1 && count != cols) {
            // throw new ExprException("Array rows must be equal sizes");
            // }
            cols = count;
            count = 0;
        } else if (e.type.equals(ExprTokenType.CloseBrace)) {
            args.add(current == null ? new ExprMissing() : current);
            current = c;
            int rows = 1;
            if (cols == -1)
                cols = args.size();
            else
                rows = args.size() / cols;
            ExprArray a = new ExprArray(rows, cols);
            for (int i = 0; i < args.size(); i++) {
                a.set(0, i, (Expr) args.get(i));
            }
            setValue(a);
            break;
        } else {
            parseToken(lexer, e);
        }
    }
}
Also used : Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) ArrayList(java.util.ArrayList) ExprArray(org.csstudio.autocomplete.parser.engine.expr.ExprArray) ExprMissing(org.csstudio.autocomplete.parser.engine.expr.ExprMissing)

Aggregations

Expr (org.csstudio.autocomplete.parser.engine.expr.Expr)11 ExprMissing (org.csstudio.autocomplete.parser.engine.expr.ExprMissing)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ExprDouble (org.csstudio.autocomplete.parser.engine.expr.ExprDouble)2 ExprException (org.csstudio.autocomplete.parser.engine.expr.ExprException)2 ExprFunction (org.csstudio.autocomplete.parser.engine.expr.ExprFunction)2 ExprInteger (org.csstudio.autocomplete.parser.engine.expr.ExprInteger)2 ExprVariable (org.csstudio.autocomplete.parser.engine.expr.ExprVariable)2 FunctionDescriptor (org.csstudio.autocomplete.parser.FunctionDescriptor)1 ExprArray (org.csstudio.autocomplete.parser.engine.expr.ExprArray)1 ExprBinaryOperator (org.csstudio.autocomplete.parser.engine.expr.ExprBinaryOperator)1 ExprConditionalOperator (org.csstudio.autocomplete.parser.engine.expr.ExprConditionalOperator)1 ExprExpression (org.csstudio.autocomplete.parser.engine.expr.ExprExpression)1 ExprPV (org.csstudio.autocomplete.parser.engine.expr.ExprPV)1 ExprString (org.csstudio.autocomplete.parser.engine.expr.ExprString)1