Search in sources :

Example 1 with Expression

use of org.structr.core.parser.Expression in project structr by structr.

the class Functions method evaluate.

public static Object evaluate(final ActionContext actionContext, final GraphObject entity, final String expression) throws FrameworkException, UnlicensedException {
    final String expressionWithoutNewlines = expression.replace('\n', ' ').replace('\r', ' ');
    final StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(expressionWithoutNewlines));
    tokenizer.eolIsSignificant(true);
    tokenizer.ordinaryChar('.');
    tokenizer.wordChars('_', '_');
    tokenizer.wordChars('.', '.');
    tokenizer.wordChars('!', '!');
    Expression root = new RootExpression();
    Expression current = root;
    Expression next = null;
    String lastToken = null;
    int token = 0;
    int level = 0;
    while (token != StreamTokenizer.TT_EOF) {
        token = nextToken(tokenizer);
        switch(token) {
            case StreamTokenizer.TT_EOF:
                break;
            case StreamTokenizer.TT_EOL:
                break;
            case StreamTokenizer.TT_NUMBER:
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched opening bracket before NUMBER");
                }
                next = new ConstantExpression(tokenizer.nval);
                current.add(next);
                lastToken += "NUMBER";
                break;
            case StreamTokenizer.TT_WORD:
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
                }
                next = checkReservedWords(tokenizer.sval);
                Expression previousExpression = current.getPrevious();
                if (tokenizer.sval.startsWith(".") && previousExpression != null && previousExpression instanceof FunctionExpression && next instanceof ValueExpression) {
                    final FunctionExpression previousFunctionExpression = (FunctionExpression) previousExpression;
                    final ValueExpression valueExpression = (ValueExpression) next;
                    current.replacePrevious(new FunctionValueExpression(previousFunctionExpression, valueExpression));
                } else {
                    current.add(next);
                }
                lastToken = tokenizer.sval;
                break;
            case '(':
                if (((current == null || current instanceof RootExpression) && next == null) || current == next) {
                    // an additional bracket without a new function,
                    // this can only be an execution group.
                    next = new GroupExpression();
                    current.add(next);
                }
                current = next;
                lastToken += "(";
                level++;
                break;
            case ')':
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched opening bracket before " + lastToken);
                }
                current = current.getParent();
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched closing bracket after " + lastToken);
                }
                lastToken += ")";
                level--;
                break;
            case '[':
                // bind directly to the previous expression
                next = new ArrayExpression();
                current.add(next);
                current = next;
                lastToken += "[";
                level++;
                break;
            case ']':
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched closing bracket before " + lastToken);
                }
                current = current.getParent();
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched closing bracket after " + lastToken);
                }
                lastToken += "]";
                level--;
                break;
            case ';':
                next = null;
                lastToken += ";";
                break;
            case ',':
                next = current;
                lastToken += ",";
                break;
            default:
                if (current == null) {
                    throw new FrameworkException(422, "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
                }
                current.add(new ConstantExpression(tokenizer.sval));
                lastToken = tokenizer.sval;
        }
    }
    if (level > 0) {
        throw new FrameworkException(422, "Invalid expression: mismatched closing bracket after " + lastToken);
    }
    return root.evaluate(actionContext, entity);
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ConstantExpression(org.structr.core.parser.ConstantExpression) FunctionValueExpression(org.structr.core.parser.FunctionValueExpression) GroupExpression(org.structr.core.parser.GroupExpression) FunctionExpression(org.structr.core.parser.FunctionExpression) NoneExpression(org.structr.core.parser.NoneExpression) IsExpression(org.structr.core.parser.IsExpression) AnyExpression(org.structr.core.parser.AnyExpression) EachExpression(org.structr.core.parser.EachExpression) GroupExpression(org.structr.core.parser.GroupExpression) ArrayExpression(org.structr.core.parser.ArrayExpression) FunctionExpression(org.structr.core.parser.FunctionExpression) ValueExpression(org.structr.core.parser.ValueExpression) Expression(org.structr.core.parser.Expression) FilterExpression(org.structr.core.parser.FilterExpression) NullExpression(org.structr.core.parser.NullExpression) ConstantExpression(org.structr.core.parser.ConstantExpression) IfExpression(org.structr.core.parser.IfExpression) SliceExpression(org.structr.core.parser.SliceExpression) RootExpression(org.structr.core.parser.RootExpression) BatchExpression(org.structr.core.parser.BatchExpression) CacheExpression(org.structr.core.parser.CacheExpression) FunctionValueExpression(org.structr.core.parser.FunctionValueExpression) AllExpression(org.structr.core.parser.AllExpression) RootExpression(org.structr.core.parser.RootExpression) ValueExpression(org.structr.core.parser.ValueExpression) FunctionValueExpression(org.structr.core.parser.FunctionValueExpression) StringReader(java.io.StringReader) ArrayExpression(org.structr.core.parser.ArrayExpression) StreamTokenizer(java.io.StreamTokenizer)

Aggregations

StreamTokenizer (java.io.StreamTokenizer)1 StringReader (java.io.StringReader)1 FrameworkException (org.structr.common.error.FrameworkException)1 AllExpression (org.structr.core.parser.AllExpression)1 AnyExpression (org.structr.core.parser.AnyExpression)1 ArrayExpression (org.structr.core.parser.ArrayExpression)1 BatchExpression (org.structr.core.parser.BatchExpression)1 CacheExpression (org.structr.core.parser.CacheExpression)1 ConstantExpression (org.structr.core.parser.ConstantExpression)1 EachExpression (org.structr.core.parser.EachExpression)1 Expression (org.structr.core.parser.Expression)1 FilterExpression (org.structr.core.parser.FilterExpression)1 FunctionExpression (org.structr.core.parser.FunctionExpression)1 FunctionValueExpression (org.structr.core.parser.FunctionValueExpression)1 GroupExpression (org.structr.core.parser.GroupExpression)1 IfExpression (org.structr.core.parser.IfExpression)1 IsExpression (org.structr.core.parser.IsExpression)1 NoneExpression (org.structr.core.parser.NoneExpression)1 NullExpression (org.structr.core.parser.NullExpression)1 RootExpression (org.structr.core.parser.RootExpression)1