Java Code Examples for org.mozilla.javascript.Token#BLOCK

The following examples show how to use org.mozilla.javascript.Token#BLOCK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForInLoop.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("for ");
    if (isForEach()) {
        sb.append("each ");
    }
    sb.append("(");
    sb.append(iterator.toSource(0));
    if (isForOf) {
        sb.append(" of ");
    } else {
        sb.append(" in ");
    }
    sb.append(iteratedObject.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim()).append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 2
Source File: ForLoop.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("for (");
    sb.append(initializer.toSource(0));
    sb.append("; ");
    sb.append(condition.toSource(0));
    sb.append("; ");
    sb.append(increment.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim()).append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 3
Source File: ForLoop.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("for (");
    sb.append(initializer.toSource(0));
    sb.append("; ");
    sb.append(condition.toSource(0));
    sb.append("; ");
    sb.append(increment.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim()).append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 4
Source File: ForInLoop.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("for ");
    if (isForEach()) {
        sb.append("each ");
    }
    sb.append("(");
    sb.append(iterator.toSource(0));
    sb.append(" in ");
    sb.append(iteratedObject.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim()).append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 5
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * compile the expression from a script tree
 * 
 * @param expression
 * @param context
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void CompiledExprFromTree( String expression, Context context,
		ScriptNode tree, List columnExprList ) throws BirtException
{
	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		if ( tree.getFirstChild( ).getType( ) == Token.FUNCTION )
		{
			int index = getFunctionIndex( tree.getFirstChild( ).getString( ),
					tree );
			compileFunctionNode( tree.getFunctionNode( index ),
					tree,
					columnExprList );
		}
		else
		{
			// A single expression
			if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
					&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
					&& tree.getFirstChild( ).getType( ) != Token.BLOCK
					&& tree.getFirstChild( ).getType( ) != Token.SCRIPT)
			{
				// This should never happen?
				throw new CoreException( pluginId,
						ResourceConstants.INVALID_EXPRESSION );
			}
			Node exprNode = tree.getFirstChild( );
			processChild( exprNode, tree, columnExprList );
		}
	}
	else
	{
		compileComplexExpr( tree, tree, columnExprList );
	}
}
 
Example 6
Source File: WithStatement.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("with (");
    sb.append(expression.toSource(0));
    sb.append(") ");
    if (statement.getType() == Token.BLOCK) {
        sb.append(statement.toSource(depth).trim());
        sb.append("\n");
    } else {
        sb.append("\n").append(statement.toSource(depth + 1));
    }
    return sb.toString();
}
 
Example 7
Source File: IfStatement.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    String pad = makeIndent(depth);
    StringBuilder sb = new StringBuilder(32);
    sb.append(pad);
    sb.append("if (");
    sb.append(condition.toSource(0));
    sb.append(") ");
    if (thenPart.getType() != Token.BLOCK) {
        sb.append("\n").append(makeIndent(depth + 1));
    }
    sb.append(thenPart.toSource(depth).trim());
    if (elsePart != null) {
        if (thenPart.getType() != Token.BLOCK) {
            sb.append("\n").append(pad).append("else ");
        } else {
            sb.append(" else ");
        }
        if (elsePart.getType() != Token.BLOCK
                && elsePart.getType() != Token.IF) {
            sb.append("\n").append(makeIndent(depth + 1));
        }
        sb.append(elsePart.toSource(depth).trim());
    }
    sb.append("\n");
    return sb.toString();
}
 
Example 8
Source File: WhileLoop.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("while (");
    sb.append(condition.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim());
        sb.append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 9
Source File: WithStatement.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("with (");
    sb.append(expression.toSource(0));
    sb.append(") ");
    if (statement.getType() == Token.BLOCK) {
        sb.append(statement.toSource(depth).trim());
        sb.append("\n");
    } else {
        sb.append("\n").append(statement.toSource(depth + 1));
    }
    return sb.toString();
}
 
Example 10
Source File: IfStatement.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    String pad = makeIndent(depth);
    StringBuilder sb = new StringBuilder(32);
    sb.append(pad);
    sb.append("if (");
    sb.append(condition.toSource(0));
    sb.append(") ");
    if (thenPart.getType() != Token.BLOCK) {
        sb.append("\n").append(makeIndent(depth + 1));
    }
    sb.append(thenPart.toSource(depth).trim());
    if (elsePart != null) {
        if (thenPart.getType() != Token.BLOCK) {
            sb.append("\n").append(pad).append("else ");
        } else {
            sb.append(" else ");
        }
        if (elsePart.getType() != Token.BLOCK
                && elsePart.getType() != Token.IF) {
            sb.append("\n").append(makeIndent(depth + 1));
        }
        sb.append(elsePart.toSource(depth).trim());
    }
    sb.append("\n");
    return sb.toString();
}
 
Example 11
Source File: WhileLoop.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public String toSource(int depth) {
    StringBuilder sb = new StringBuilder();
    sb.append(makeIndent(depth));
    sb.append("while (");
    sb.append(condition.toSource(0));
    sb.append(") ");
    if (body.getType() == Token.BLOCK) {
        sb.append(body.toSource(depth).trim());
        sb.append("\n");
    } else {
        sb.append("\n").append(body.toSource(depth+1));
    }
    return sb.toString();
}
 
Example 12
Source File: AstNode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public boolean hasSideEffects()
{
    switch (getType()) {
      case Token.ASSIGN:
      case Token.ASSIGN_ADD:
      case Token.ASSIGN_BITAND:
      case Token.ASSIGN_BITOR:
      case Token.ASSIGN_BITXOR:
      case Token.ASSIGN_DIV:
      case Token.ASSIGN_LSH:
      case Token.ASSIGN_MOD:
      case Token.ASSIGN_MUL:
      case Token.ASSIGN_RSH:
      case Token.ASSIGN_SUB:
      case Token.ASSIGN_URSH:
      case Token.BLOCK:
      case Token.BREAK:
      case Token.CALL:
      case Token.CATCH:
      case Token.CATCH_SCOPE:
      case Token.CONST:
      case Token.CONTINUE:
      case Token.DEC:
      case Token.DELPROP:
      case Token.DEL_REF:
      case Token.DO:
      case Token.ELSE:
      case Token.ENTERWITH:
      case Token.ERROR:         // Avoid cascaded error messages
      case Token.EXPORT:
      case Token.EXPR_RESULT:
      case Token.FINALLY:
      case Token.FUNCTION:
      case Token.FOR:
      case Token.GOTO:
      case Token.IF:
      case Token.IFEQ:
      case Token.IFNE:
      case Token.IMPORT:
      case Token.INC:
      case Token.JSR:
      case Token.LABEL:
      case Token.LEAVEWITH:
      case Token.LET:
      case Token.LETEXPR:
      case Token.LOCAL_BLOCK:
      case Token.LOOP:
      case Token.NEW:
      case Token.REF_CALL:
      case Token.RETHROW:
      case Token.RETURN:
      case Token.RETURN_RESULT:
      case Token.SEMI:
      case Token.SETELEM:
      case Token.SETELEM_OP:
      case Token.SETNAME:
      case Token.SETPROP:
      case Token.SETPROP_OP:
      case Token.SETVAR:
      case Token.SET_REF:
      case Token.SET_REF_OP:
      case Token.SWITCH:
      case Token.TARGET:
      case Token.THROW:
      case Token.TRY:
      case Token.VAR:
      case Token.WHILE:
      case Token.WITH:
      case Token.WITHEXPR:
      case Token.YIELD:
        return true;

      default:
        return false;
    }
}
 
Example 13
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * process the script tree to produce a <code>CompiledExpression</code>
 * 
 * @param expression
 * @param tree
 * @param context
 * @return @throws
 *         DataException
 */
private CompiledExpression processScriptTree( String expression,
		ScriptNode tree, Context context ) throws DataException

{
	CompiledExpression expr;
	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		if( tree.getFirstChild( ) == null )
			throw new DataException( "Expression parse error: first child is null. The expression is " + expression,
					expression );
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			// This should never happen?
			throw new DataException( ResourceConstants.INVALID_JS_EXPR,
					expression );
		}
		Node child, parent = tree;
		Node exprNode = parent.getFirstChild( );
		child = exprNode.getFirstChild( );
		if ( child.getNext( ) != null )
			child = exprNode;
		else
		{
			parent = exprNode;
		}
		assert ( child != null && parent != null );
		expr = processChild( context, false, parent, child, tree );
	}
	else
	{
		// complex expressions
		// Multiple expressions exist; we should produce complex expressions
		// However, individual subexpressions still needs to be processed
		// to identify the interesting subexpressions
		expr = compileComplexExpr( context, tree, false );
	}
	if ( expr instanceof BytecodeExpression )
		compileForBytecodeExpr( context, tree, expr );
	return expr;
}
 
Example 14
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 15
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression, boolean mode )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( getCompiledExpCacheMap( mode ).containsKey( expression ) )
	{
		return ( (Boolean)  getCompiledExpCacheMap( mode ).get( expression ) ).booleanValue( );
	}
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		getCompiledExpCacheMap( mode ).put( expression,
				Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child, mode );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}
	getCompiledExpCacheMap( mode ).put( expression,
			Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 16
Source File: ExpressionUtility.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * whether the expression is column reference
 * 
 * @param expression
 * @return
 */
public static boolean isColumnExpression( String expression )
{
	boolean isColumn = false;
	if ( expression == null || expression.trim( ).length( ) == 0 )
		return isColumn;
	if ( compiledExprCache.containsKey( expression ) )
		return ( (Boolean) compiledExprCache.get( expression ) ).booleanValue( );
	Context context = Context.enter( );
	ScriptNode tree;
	try
	{
		CompilerEnvirons m_compilerEnv = new CompilerEnvirons( );
		m_compilerEnv.initFromContext( context );
		Parser p = new Parser( m_compilerEnv, context.getErrorReporter( ) );
		AstRoot root = p.parse( expression, null, 0 );
		IRFactory ir = new IRFactory( m_compilerEnv );
		tree = ir.transformTree( root );
	}
	catch ( Exception e )
	{
		compiledExprCache.put( expression, Boolean.valueOf( false ) );
		return false;
	}
	finally
	{
		Context.exit( );
	}

	if ( tree.getFirstChild( ) == tree.getLastChild( ) )
	{
		// A single expression
		if ( tree.getFirstChild( ).getType( ) != Token.EXPR_RESULT
				&& tree.getFirstChild( ).getType( ) != Token.EXPR_VOID
				&& tree.getFirstChild( ).getType( ) != Token.BLOCK )
		{
			isColumn = false;
		}
		Node exprNode = tree.getFirstChild( );
		Node child = exprNode.getFirstChild( );
		assert ( child != null );
		if ( child.getType( ) == Token.GETELEM
				|| child.getType( ) == Token.GETPROP )
			isColumn = getDirectColRefExpr( child );
		else
			isColumn = false;
	}
	else
	{
		isColumn = false;
	}

	compiledExprCache.put( expression, Boolean.valueOf( isColumn ) );
	return isColumn;
}
 
Example 17
Source File: AstNode.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean hasSideEffects()
{
    switch (getType()) {
      case Token.ASSIGN:
      case Token.ASSIGN_ADD:
      case Token.ASSIGN_BITAND:
      case Token.ASSIGN_BITOR:
      case Token.ASSIGN_BITXOR:
      case Token.ASSIGN_DIV:
      case Token.ASSIGN_LSH:
      case Token.ASSIGN_MOD:
      case Token.ASSIGN_MUL:
      case Token.ASSIGN_RSH:
      case Token.ASSIGN_SUB:
      case Token.ASSIGN_URSH:
      case Token.BLOCK:
      case Token.BREAK:
      case Token.CALL:
      case Token.CATCH:
      case Token.CATCH_SCOPE:
      case Token.CONST:
      case Token.CONTINUE:
      case Token.DEC:
      case Token.DELPROP:
      case Token.DEL_REF:
      case Token.DO:
      case Token.ELSE:
      case Token.ENTERWITH:
      case Token.ERROR:         // Avoid cascaded error messages
      case Token.EXPORT:
      case Token.EXPR_RESULT:
      case Token.FINALLY:
      case Token.FUNCTION:
      case Token.FOR:
      case Token.GOTO:
      case Token.IF:
      case Token.IFEQ:
      case Token.IFNE:
      case Token.IMPORT:
      case Token.INC:
      case Token.JSR:
      case Token.LABEL:
      case Token.LEAVEWITH:
      case Token.LET:
      case Token.LETEXPR:
      case Token.LOCAL_BLOCK:
      case Token.LOOP:
      case Token.NEW:
      case Token.REF_CALL:
      case Token.RETHROW:
      case Token.RETURN:
      case Token.RETURN_RESULT:
      case Token.SEMI:
      case Token.SETELEM:
      case Token.SETELEM_OP:
      case Token.SETNAME:
      case Token.SETPROP:
      case Token.SETPROP_OP:
      case Token.SETVAR:
      case Token.SET_REF:
      case Token.SET_REF_OP:
      case Token.SWITCH:
      case Token.TARGET:
      case Token.THROW:
      case Token.TRY:
      case Token.VAR:
      case Token.WHILE:
      case Token.WITH:
      case Token.WITHEXPR:
      case Token.YIELD:
        return true;

      default:
        return false;
    }
}