Package org.mvel2.ast

Examples of org.mvel2.ast.ASTNode


      case TERNARY:
        if (!stk.popBoolean()) {
          stk.clear();

          ASTNode tk;

          for (;;){
            if ((tk = nextToken()) == null || tk.isOperator(Operator.TERNARY_ELSE))
              break;
          }
        }

        return OP_RESET_FRAME;
View Full Code Here


   *
   * @param operator -
   * @return -
   */
  private boolean unwindStatement(int operator) {
    ASTNode tk;

    switch (operator) {
      case AND:
        while ((tk = nextToken()) != null && !tk.isOperator(Operator.END_OF_STMT) && !tk.isOperator(Operator.OR)) {
          //nothing
        }
        break;
      default:
        while ((tk = nextToken()) != null && !tk.isOperator(Operator.END_OF_STMT)) {
          //nothing
        }
    }
    return tk == null;
  }
View Full Code Here

    Object v1, v2;
    ExecutionStack stk = new ExecutionStack();
    variableFactory.setTiltFlag(false);

    ASTNode tk = expression.getFirstNode();
    Integer operator;

    if (tk == null) return null;
    try {
      do {
        if (tk.fields == -1) {
          /**
           * This may seem silly and redundant, however, when an MVEL script recurses into a block
           * or substatement, a new runtime loop is entered.   Since the debugger state is not
           * passed through the AST, it is not possible to forward the state directly.  So when we
           * encounter a debugging symbol, we check the thread local to see if there is are registered
           * breakpoints.  If we find them, we assume that we are debugging.
           *
           * The consequence of this of course, is that it's not ideal to compileShared expressions with
           * debugging symbols which you plan to use in a production enviroment.
           */
          if (debugger || (debugger = hasDebuggerContext())) {
            try {
              debuggerContext.get().checkBreak((LineLabel) tk, variableFactory, expression);
            }
            catch (NullPointerException e) {
              // do nothing for now.  this isn't as calus as it seems.
            }
          }
          continue;
        }
        else if (stk.isEmpty()) {
          stk.push(tk.getReducedValueAccelerated(ctx, ctx, variableFactory));
        }

        if (variableFactory.tiltFlag()) {
          return stk.pop();
        }

        switch (operator = tk.getOperator()) {
          case RETURN:
            variableFactory.setTiltFlag(true);
            return stk.pop();
          //     throw new EndWithValue(stk.pop());

          case NOOP:
            continue;

          case TERNARY:
            if (!stk.popBoolean()) {
              //noinspection StatementWithEmptyBody
              while (tk.nextASTNode != null && !(tk = tk.nextASTNode).isOperator(TERNARY_ELSE)) ;
            }
            stk.clear();
            continue;

          case TERNARY_ELSE:
            return stk.pop();

          case END_OF_STMT:
            /**
             * If the program doesn't end here then we wipe anything off the stack that remains.
             * Althought it may seem like intuitive stack optimizations could be leveraged by
             * leaving hanging values on the stack,  trust me it's not a good idea.
             */
            if (tk.nextASTNode != null) {
              stk.clear();
            }

            continue;
        }

        stk.push(tk.nextASTNode.getReducedValueAccelerated(ctx, ctx, variableFactory), operator);

        try {
          while (stk.isReduceable()) {
            if ((Integer) stk.peek() == CHOR) {
              stk.pop();
              v1 = stk.pop();
              v2 = stk.pop();
              if (!isEmpty(v2) || !isEmpty(v1)) {
                stk.clear();
                stk.push(!isEmpty(v2) ? v2 : v1);
              }
              else stk.push(null);
            }
            else {
              stk.op();
            }
          }
        }
        catch (ClassCastException e) {
          throw new CompileException("syntax error or incomptable types", new char[0], 0, e);
        }
        catch (CompileException e) {
          throw e;
        }
        catch (Exception e) {
          throw new CompileException("failed to compileShared sub expression", new char[0], 0, e);
        }
      }
      while ((tk = tk.nextASTNode) != null);

      return stk.peek();
    }
    catch (NullPointerException e) {
      if (tk != null && tk.isOperator() && tk.nextASTNode != null) {
        throw new CompileException("incomplete statement: "
            + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", tk.getExpr(), tk.getStart());
      }
      else {
        throw e;
      }
    }
View Full Code Here

    return "UNKNOWN_OPERATOR";
  }

  public static Class determineType(String name, CompiledExpression compiledExpression) {
    ASTIterator iter = new ASTLinkedList(compiledExpression.getFirstNode());
    ASTNode node;
    while (iter.hasMoreNodes()) {
      if (name.equals((node = iter.nextNode()).getName()) && node.isAssignment()) {
        return node.getEgressType();
      }
    }

    return null;
  }
View Full Code Here

    return firstNode != null && firstNode instanceof TypeCast;
  }

  public String toString() {
    StringBuilder appender = new StringBuilder();
    ASTNode node = firstNode;
    while (node != null) {
      appender.append(node.toString()).append(";\n");
      node = node.nextASTNode;
    }
    return appender.toString();
  }
View Full Code Here

    public int node = 0;
  }

  private static String decompile(CompiledExpression cExp, boolean nest, DecompileContext context) {
    ASTIterator iter = new ASTLinkedList(cExp.getFirstNode());
    ASTNode tk;

    StringBuffer sbuf = new StringBuffer();

    if (!nest) {
      sbuf.append("Expression Decompile\n-------------\n");
    }

    while (iter.hasMoreNodes()) {
      sbuf.append("(").append(context.node++).append(") ");

      if ((tk = iter.nextNode()) instanceof NestedStatement
          && ((NestedStatement) tk).getNestedStatement() instanceof CompiledExpression) {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
        sbuf.append(decompile((CompiledExpression) ((NestedStatement) tk).getNestedStatement(), true, context));
      }
      if (tk instanceof Substatement
          && ((Substatement) tk).getStatement() instanceof CompiledExpression) {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
        sbuf.append(decompile((CompiledExpression) ((Substatement) tk).getStatement(), true, context));
      }
//            else if (tk instanceof Function) {
//                sbuf.append("FUNCTION [" + tk.getName() + "]: ")
//                        .append(decompile((CompiledExpression) ((Function)tk).getCompiledBlock(), true, context));
//            }
      else if (tk.isDebuggingSymbol()) {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("DEBUG_SYMBOL :: " + tk.toString());
      }
      else if (tk.isLiteral()) {
        sbuf.append("LITERAL :: ").append(tk.getLiteralValue()).append("'");
      }
      else if (tk.isOperator()) {
        sbuf.append("OPERATOR [").append(getOperatorName(tk.getOperator())).append("]: ")
            .append(tk.getName());

        if (tk.isOperator(Operator.END_OF_STMT)) sbuf.append("\n");
      }
      else if (tk.isIdentifier()) {
        sbuf.append("REFERENCE :: ").append(tk.getClass().getSimpleName()).append(":").append(tk.getName());
      }
      else if (tk instanceof BinaryOperation) {
        BinaryOperation bo = (BinaryOperation) tk;
        sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName())
            .append("} {").append(bo.getRight().getName()).append("}");
      }
      else {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NODE [" + tk.getClass().getSimpleName() + "] :: " + tk.getName());
      }

      sbuf.append("\n");

    }
View Full Code Here

  private static Serializable _optimizeTree(final CompiledExpression compiled) {
    /**
     * If there is only one token, and it's an identifier, we can optimize this as an accessor expression.
     */
    if (compiled.isSingleNode()) {
      ASTNode tk = compiled.getFirstNode();

      if (tk.isLiteral() && !tk.isThisVal()) {
        return new ExecutableLiteral(tk.getLiteralValue());
      }
      return tk.canSerializeAccessor() ? new ExecutableAccessorSafe(tk, compiled.getKnownEgressType()) :
          new ExecutableAccessor(tk, compiled.getKnownEgressType());
    }

    return compiled;
  }
View Full Code Here

                return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
            }
        }

        if (node instanceof Union) {
            ASTNode main = ((Union)node).getMain();
            Accessor accessor = node.getAccessor();

            EvaluatedExpression expression = new EvaluatedExpression();
            expression.firstExpression = analyzeNode(main);
            if (accessor instanceof DynamicGetAccessor) {
View Full Code Here

            ExecutableAccessor accessor = (ExecutableAccessor) ((TypeCast)node).getStatement();
            return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
        }

        if (node instanceof Union) {
            ASTNode main = ((Union)node).getMain();
            Accessor accessor = node.getAccessor();

            EvaluatedExpression expression = new EvaluatedExpression();
            expression.firstExpression = analyzeNode(main);
            if (accessor instanceof DynamicGetAccessor) {
View Full Code Here

    private BooleanOperator operation;
    private Expression right;
    private ParserContext parserContext;

    public AnalyzedCondition(ExecutableStatement stmt) {
        ASTNode node;
        if (stmt instanceof CompiledExpression) {
            parserContext = ((CompiledExpression)stmt).getParserContext();
            node = ((CompiledExpression)stmt).getFirstNode();
        } else {
            node = ((ExecutableAccessor)stmt).getNode();
View Full Code Here

TOP

Related Classes of org.mvel2.ast.ASTNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.