org.mozilla.javascript.Node Java Examples

The following examples show how to use org.mozilla.javascript.Node. 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: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param n
 * @param objectName
 * @return
 */
private static void getScriptObjectName( Node n, String objectName, Set nameSet )
{
	if ( n == null )
		return;
	String result = null;
	if ( n.getType( ) == Token.NAME )
	{
		if ( objectName.equals( n.getString( ) ) )
		{
			Node dimNameNode = n.getNext( );
			if ( dimNameNode == null
					|| dimNameNode.getType( ) != Token.STRING )
				return;

			nameSet.add( dimNameNode.getString( ) );
		}
	}

	getScriptObjectName( n.getFirstChild( ), objectName, nameSet );
	getScriptObjectName( n.getNext( ), objectName, nameSet );
	getScriptObjectName( n.getLastChild( ), objectName, nameSet );
}
 
Example #2
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param expr
 * @param objectName
 * @return
 */
public static String getReferencedScriptObject( String expr,
		String objectName )
{
	if ( expr == null )
		return null;
	try
	{
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr, null, 0 );
		Node root = new IRFactory( ce).transformTree(tree);

		return getScriptObjectName( root, objectName );
	}
	catch( Exception ex )
	{
		return null;
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #3
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * compile outer column expression
 * @param refNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileOuterColRef( Node refNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	int level = compileOuterColRefExpr( refNode );
	if ( level == -1 )
	{
		compileComplexExpr( refNode, tree, columnExprList );
	}
	else
	{
		Node nextNode = refNode.getLastChild( );
		if ( nextNode.getType( ) == Token.STRING )
		{
			ColumnBinding info = new ColumnBinding( nextNode.getString( ),
					"",
					level );
			columnExprList.add( info );
		}
	}
	return;
}
 
Example #4
Source File: CubeQueryUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 
 * @param n
 * @param objectName
 * @return
 */
private static String getScriptObjectName( Node n, String objectName )
{
	if ( n == null )
		return null;
	String result = null;
	if ( n.getType( ) == Token.NAME )
	{
		if ( objectName.equals( n.getString( ) ) )
		{
			Node dimNameNode = n.getNext( );
			if ( dimNameNode == null
					|| dimNameNode.getType( ) != Token.STRING )
				return null;

			return dimNameNode.getString( );
		}
	}

	result = getScriptObjectName( n.getFirstChild( ), objectName );
	if ( result == null )
		result = getScriptObjectName( n.getLastChild( ), objectName );

	return result;
}
 
Example #5
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * extract arguments from aggregation expression
 * 
 * @param context
 * @param callNode
 * @throws BirtException
 */
private void extractArguments( Node callNode, ScriptNode tree,
		List columnExprList ) throws BirtException
{
	Node arg = callNode.getFirstChild( ).getNext( );

	while ( arg != null )
	{
		// need to hold on to the next argument because the tree extraction
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext( );
		processChild( arg, tree, columnExprList );

		arg = nextArg;
	}
}
 
Example #6
Source File: ExpressionCompiler.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected AggregateExpression compileAggregateExpr( Context context, Node parent, Node callNode ) throws DataException
{
	assert( callNode.getType() == Token.CALL );
	
	IAggrFunction aggregation = getAggregationFunction( callNode );
	// not an aggregation function being called, then it's considered 
	// a complex expression
	if( aggregation == null )
		return null;
	
	AggregateExpression aggregateExpression = 
		new AggregateExpression( aggregation );
	
	extractArguments( context, aggregateExpression, callNode );
	replaceAggregateNode( aggregateExpression, parent, callNode );
	
	return aggregateExpression;
}
 
Example #7
Source File: Block.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("{\n");
    for (Node kid : this) {
        sb.append(((AstNode)kid).toSource(depth+1));
    }
    sb.append(makeIndent(depth));
    sb.append("}\n");
    return sb.toString();
}
 
Example #8
Source File: Scope.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns a copy of the child list, with each child cast to an
 * {@link AstNode}.
 * @throws ClassCastException if any non-{@code AstNode} objects are
 * in the child list, e.g. if this method is called after the code
 * generator begins the tree transformation.
 */
public List<AstNode> getStatements() {
    List<AstNode> stmts = new ArrayList<AstNode>();
    Node n = getFirstChild();
    while (n != null) {
        stmts.add((AstNode)n);
        n = n.getNext();
    }
    return stmts;
}
 
Example #9
Source File: AbstractExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * An aggregation expression in the form of Total.xxx for example Total.sum(
 * row.x ) This means the first child is a GETPROP node, and its left child
 * is "Total" and its right child is "sum"
 * 
 * @param callNode
 * @return @throws
 *         DataException
 */
protected IAggrFunction getAggregationFunction( Node callNode )
		throws DataException
{

	Node firstChild = callNode.getFirstChild( );
	if ( firstChild.getType( ) != Token.GETPROP )
		return null;

	Node getPropLeftChild = firstChild.getFirstChild( );
	if ( getPropLeftChild.getType( ) != Token.NAME
			|| !getPropLeftChild.getString( ).equals( TOTAL ) )
		return null;

	Node getPropRightChild = firstChild.getLastChild( );
	if ( getPropRightChild.getType( ) != Token.STRING )
		return null;

	String aggrFuncName = getPropRightChild.getString( );
	IAggrFunction agg = AggregationManager.getInstance( )
			.getAggregation( aggrFuncName );
	if ( agg == null )
	{
		// Aggr function name after Total is invalid; this will eventuall
		// cause
		// an error. Report error now
		throw new DataException( ResourceConstants.INVALID_TOTAL_NAME,
				aggrFuncName );
	}
	return agg;
}
 
Example #10
Source File: MultiPassExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * parse the aggregate expression's arguments
 * 
 * @param context
 * @param aggregateExpression
 * @param callNode
 * @throws DataException
 */
private void extractArguments( Context context,
		AggregateExpression aggregateExpression, Node callNode )
		throws DataException
{
	Node arg = callNode.getFirstChild( ).getNext( );
					
	while ( arg != null )
	{
		// need to hold on to the next argument because the tree extraction
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext( );
		CompiledExpression expr = processChild( context,
				true,
				callNode,
				arg,
				null );
		if ( !( expr instanceof BytecodeExpression ) )
		{
			aggregateExpression.addArgument( expr );
			arg = nextArg;
			continue;
		}

		AstRoot tree = new AstRoot( Token.SCRIPT );
		Node exprNode = new Node( Token.EXPR_RESULT );
		exprNode.addChildToFront( arg );
		tree.addChildrenToFront( exprNode );
		if ( expr instanceof AggregateExpression )
		{
			int registry = getRegisterId( new AggregateObject( (AggregateExpression) expr ) );
			if ( registry >= 0 )
				replaceAggregateNode( registry, exprNode, arg );
		}
		
		compileForBytecodeExpr( context, tree, expr );
		aggregateExpression.addArgument( expr );
		arg = nextArg;
	}
}
 
Example #11
Source File: MultiPassExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * replace the aggregate node with AGGR_VALUE <id>
 * 
 * @param registry
 * @param aggregateExpression
 * @param parent
 * @param aggregateCallNode
 * @throws DataException
 */
private void replaceAggregateNode( int registry, Node parent,
		Node aggregateCallNode ) throws DataException
{
	if ( registry < 0 )
		throw new DataException( ResourceConstants.INVALID_CALL_AGGR );

	int aggregateId = registry;
	Node newFirstChild = Node.newString( Token.NAME, AGGR_VALUE );
	Node newSecondChild = Node.newNumber( aggregateId );
	Node aggregateNode = new Node( Token.GETELEM,
			newFirstChild,
			newSecondChild );
	parent.replaceChild( aggregateCallNode, aggregateNode );
}
 
Example #12
Source File: FunctionNode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets function body, and sets its parent to this node.
 * Also sets the encoded source bounds based on the body bounds.
 * Assumes the function node absolute position has already been set,
 * and the body node's absolute position and length are set.<p>
 *
 * @param body function body.  Its parent is set to this node, and its
 * position is updated to be relative to this node.
 *
 * @throws IllegalArgumentException if body is {@code null}
 */
public void setBody(AstNode body) {
    assertNotNull(body);
    this.body = body;
    if (Boolean.TRUE.equals(body.getProp(Node.EXPRESSION_CLOSURE_PROP))) {
        setIsExpressionClosure(true);
    }
    int absEnd = body.getPosition() + body.getLength();
    body.setParent(this);
    this.setLength(absEnd - this.position);
    setEncodedSourceBounds(this.position, absEnd);
}
 
Example #13
Source File: Scope.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeVisitor v) {
    if (v.visit(this)) {
        for (Node kid : this) {
            ((AstNode)kid).visit(v);
        }
    }
}
 
Example #14
Source File: Scope.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a copy of the child list, with each child cast to an
 * {@link AstNode}.
 * @throws ClassCastException if any non-{@code AstNode} objects are
 * in the child list, e.g. if this method is called after the code
 * generator begins the tree transformation.
 */
public List<AstNode> getStatements() {
    List<AstNode> stmts = new ArrayList<AstNode>();
    Node n = getFirstChild();
    while (n != null) {
        stmts.add((AstNode)n);
        n = n.getNext();
    }
    return stmts;
}
 
Example #15
Source File: ScriptNode.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void visit(NodeVisitor v) {
    if (v.visit(this)) {
        for (Node kid : this) {
            ((AstNode)kid).visit(v);
        }
    }
}
 
Example #16
Source File: Block.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void visit(NodeVisitor v) {
    if (v.visit(this)) {
        for (Node kid : this) {
            ((AstNode)kid).visit(v);
        }
    }
}
 
Example #17
Source File: ScriptNode.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public int getIndexForNameNode(Node nameNode) {
    if (variableNames == null) codeBug();
    Scope node = nameNode.getScope();
    Symbol symbol = node == null
        ? null
        : node.getSymbol(((Name)nameNode).getIdentifier());
    return (symbol == null) ? -1 : symbol.getIndex();
}
 
Example #18
Source File: Block.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("{\n");
    for (Node kid : this) {
        sb.append(((AstNode)kid).toSource(depth+1));
    }
    sb.append(makeIndent(depth));
    sb.append("}\n");
    return sb.toString();
}
 
Example #19
Source File: Block.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeVisitor v) {
    if (v.visit(this)) {
        for (Node kid : this) {
            ((AstNode)kid).visit(v);
        }
    }
}
 
Example #20
Source File: ScriptNode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public int getIndexForNameNode(Node nameNode) {
    if (variableNames == null) codeBug();
    Scope node = nameNode.getScope();
    Symbol symbol = node == null
        ? null
        : node.getSymbol(((Name)nameNode).getIdentifier());
    return (symbol == null) ? -1 : symbol.getIndex();
}
 
Example #21
Source File: ScriptNode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeVisitor v) {
    if (v.visit(this)) {
        for (Node kid : this) {
            ((AstNode)kid).visit(v);
        }
    }
}
 
Example #22
Source File: Scope.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("{\n");
    for (Node kid : this) {
        sb.append(((AstNode)kid).toSource(depth+1));
    }
    sb.append(makeIndent(depth));
    sb.append("}\n");
    return sb.toString();
}
 
Example #23
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setDefault(Node defaultTarget)
{
    if (type != Token.SWITCH) codeBug();
    if (defaultTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = defaultTarget;
}
 
Example #24
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setFinally(Node finallyTarget)
{
    if (type != Token.TRY) codeBug();
    if (finallyTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = finallyTarget;
}
 
Example #25
Source File: Jump.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setContinue(Node continueTarget)
{
    if (type != Token.LOOP) codeBug();
    if (continueTarget.getType() != Token.TARGET) codeBug();
    if (target2 != null) codeBug(); //only once
    target2 = continueTarget;
}
 
Example #26
Source File: AstRoot.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();
    for (Node node : this) {
        sb.append(((AstNode)node).toSource(depth));
    }
    return sb.toString();
}
 
Example #27
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param expr
 * @param bindings
 * @param onlyFromDirectReferenceExpr
 * @return
 * @throws DataException
 */
private static Set getReferencedDimLevel( IScriptExpression expr,
		List bindings, boolean onlyFromDirectReferenceExpr )
		throws DataException
{
	if ( expr == null || expr.getText( ) == null || expr.getText( ).length( ) == 0 || BaseExpression.constantId.equals( expr.getScriptId( ) ) )
		return new HashSet( );
	try
	{
		Set result = new HashSet( );
		Context cx = Context.enter( );
		CompilerEnvirons ce = new CompilerEnvirons( );
		Parser p = new Parser( ce, cx.getErrorReporter( ) );
		AstRoot tree = p.parse( expr.getText( ), null, 0 );
		Node root = new IRFactory( ce).transformTree(tree);

		populateDimLevels( null,
				root,
				result,
				bindings,
				onlyFromDirectReferenceExpr );
		return result;
	}
	finally
	{
		Context.exit( );
	}
}
 
Example #28
Source File: OlapExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param n
 * @param objectName
 * @return
 */
private static String getScriptObjectName( Node n, String objectName )
{
	if ( n == null )
		return null;
	String result = null;
	if ( n.getType( ) == Token.NAME )
	{
		if ( objectName.equals( n.getString( ) ) )
		{
			Node dimNameNode = n.getNext( );
			if ( dimNameNode == null
					|| dimNameNode.getType( ) != Token.STRING )
				return null;

			return dimNameNode.getString( );
		}
	}

	result = getScriptObjectName( n.getFirstChild( ), objectName );
	if ( result == null )
		result = getScriptObjectName( n.getLastChild( ), objectName );
	
	if ( result == null )
		result = getScriptObjectName( n.getNext(), objectName );

	return result;
}
 
Example #29
Source File: ExpressionParserUtility.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @param callNode
 * @param tree
 * @param columnExprList
 * @throws BirtException
 */
private void compileAggregationFunction( Node callNode,
		ScriptNode tree, List columnExprList ) throws BirtException
{
	Node firstChild = callNode.getFirstChild( );
	if ( firstChild.getType( ) != Token.GETPROP )
		return;

	Node getPropLeftChild = firstChild.getFirstChild( );
	if ( getPropLeftChild.getType( ) == Token.NAME
			&& getPropLeftChild.getString( ).equals( TOTAL ) )
		hasAggregation = true;

	compileComplexExpr( firstChild, tree, columnExprList );
}
 
Example #30
Source File: ExpressionCompiler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param context
 * @param aggregateExpression
 * @param callNode
 * @throws DataException
 */
private void extractArguments( Context context,
		AggregateExpression aggregateExpression, Node callNode )
		throws DataException
{
	Node arg = callNode.getFirstChild().getNext();
	
	while( arg != null )
	{
		// need to hold on to the next argument because the tree extraction 
		// will cause us to lose the reference otherwise
		Node nextArg = arg.getNext();
		
		CompiledExpression expr = processChild( context,
				false,
				callNode,
				arg,
				null );
		if( ! ( expr instanceof BytecodeExpression ) )
		{
			aggregateExpression.addArgument( expr );
			arg = nextArg;
			continue;
		}
		
		AstRoot tree = new AstRoot( Token.SCRIPT );
		Node exprNode = new Node( Token.EXPR_RESULT);
		exprNode.addChildToFront( arg );
		tree.addChildrenToFront( exprNode );
		compileForBytecodeExpr( context, tree, expr );
		aggregateExpression.addArgument( expr );
		
		arg = nextArg;
	}
}