Search in sources :

Example 1 with EmptyExpression

use of org.mozilla.javascript.ast.EmptyExpression in project st-js by st-js.

the class RhinoJavaScriptWriter method printList.

/**
 * <p>printList.</p>
 *
 * @param items a {@link java.util.List} object.
 * @param param a {@link java.lang.Boolean} object.
 */
protected <T extends AstNode> void printList(List<T> items, Boolean param) {
    int max = items.size();
    int count = 0;
    for (AstNode item : items) {
        visitorSupport.accept(item, this, param);
        if (count < max - 1) {
            count++;
            print(", ");
        } else if (item instanceof EmptyExpression) {
            print(",");
        }
    }
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 2 with EmptyExpression

use of org.mozilla.javascript.ast.EmptyExpression in project HL4A by HL4A.

the class IRFactory method transformXmlLiteral.

private Node transformXmlLiteral(XmlLiteral node) {
    // a literal like <foo>{bar}</foo> is rewritten as
    // new XML("<foo>" + bar + "</foo>");
    Node pnXML = new Node(Token.NEW, node.getLineno());
    List<XmlFragment> frags = node.getFragments();
    XmlString first = (XmlString) frags.get(0);
    boolean anon = first.getXml().trim().startsWith("<>");
    pnXML.addChildToBack(createName(anon ? "XMLList" : "XML"));
    Node pn = null;
    for (XmlFragment frag : frags) {
        if (frag instanceof XmlString) {
            String xml = ((XmlString) frag).getXml();
            decompiler.addName(xml);
            if (pn == null) {
                pn = createString(xml);
            } else {
                pn = createBinary(Token.ADD, pn, createString(xml));
            }
        } else {
            XmlExpression xexpr = (XmlExpression) frag;
            boolean isXmlAttr = xexpr.isXmlAttribute();
            Node expr;
            decompiler.addToken(Token.LC);
            if (xexpr.getExpression() instanceof EmptyExpression) {
                expr = createString("");
            } else {
                expr = transform(xexpr.getExpression());
            }
            decompiler.addToken(Token.RC);
            if (isXmlAttr) {
                // Need to put the result in double quotes
                expr = createUnary(Token.ESCXMLATTR, expr);
                Node prepend = createBinary(Token.ADD, createString("\""), expr);
                expr = createBinary(Token.ADD, prepend, createString("\""));
            } else {
                expr = createUnary(Token.ESCXMLTEXT, expr);
            }
            pn = createBinary(Token.ADD, pn, expr);
        }
    }
    pnXML.addChildToBack(pn);
    return pnXML;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlFragment(org.mozilla.javascript.ast.XmlFragment) XmlString(org.mozilla.javascript.ast.XmlString) XmlExpression(org.mozilla.javascript.ast.XmlExpression) XmlString(org.mozilla.javascript.ast.XmlString) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 3 with EmptyExpression

use of org.mozilla.javascript.ast.EmptyExpression in project HL4A by HL4A.

the class Parser method arrayLiteral.

/**
 * May return an {@link ArrayLiteral} or {@link ArrayComprehension}.
 */
private AstNode arrayLiteral() throws IOException {
    if (currentToken != Token.LB)
        codeBug();
    int pos = ts.tokenBeg, end = ts.tokenEnd;
    List<AstNode> elements = new ArrayList<AstNode>();
    ArrayLiteral pn = new ArrayLiteral(pos);
    boolean after_lb_or_comma = true;
    int afterComma = -1;
    int skipCount = 0;
    for (; ; ) {
        int tt = peekToken();
        if (tt == Token.COMMA) {
            consumeToken();
            afterComma = ts.tokenEnd;
            if (!after_lb_or_comma) {
                after_lb_or_comma = true;
            } else {
                elements.add(new EmptyExpression(ts.tokenBeg, 1));
                skipCount++;
            }
        } else if (tt == Token.RB) {
            consumeToken();
            // for ([a,] in obj) is legal, but for ([a] in obj) is
            // not since we have both key and value supplied. The
            // trick is that [a,] and [a] are equivalent in other
            // array literal contexts. So we calculate a special
            // length value just for destructuring assignment.
            end = ts.tokenEnd;
            pn.setDestructuringLength(elements.size() + (after_lb_or_comma ? 1 : 0));
            pn.setSkipCount(skipCount);
            if (afterComma != -1)
                warnTrailingComma(pos, elements, afterComma);
            break;
        } else if (tt == Token.FOR && !after_lb_or_comma && elements.size() == 1) {
            return arrayComprehension(elements.get(0), pos);
        } else if (tt == Token.EOF) {
            reportError("msg.no.bracket.arg");
            break;
        } else {
            if (!after_lb_or_comma) {
                reportError("msg.no.bracket.arg");
            }
            elements.add(assignExpr());
            after_lb_or_comma = false;
            afterComma = -1;
        }
    }
    for (AstNode e : elements) {
        pn.addElement(e);
    }
    pn.setLength(end - pos);
    return pn;
}
Also used : ArrayList(java.util.ArrayList) ArrayLiteral(org.mozilla.javascript.ast.ArrayLiteral) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 4 with EmptyExpression

use of org.mozilla.javascript.ast.EmptyExpression in project HL4A by HL4A.

the class Parser method parenExpr.

private AstNode parenExpr() throws IOException {
    boolean wasInForInit = inForInit;
    inForInit = false;
    try {
        Comment jsdocNode = getAndResetJsDoc();
        int lineno = ts.lineno;
        int begin = ts.tokenBeg;
        AstNode e = (peekToken() == Token.RP ? new EmptyExpression(begin) : expr());
        if (peekToken() == Token.FOR) {
            return generatorExpression(e, begin);
        }
        ParenthesizedExpression pn = new ParenthesizedExpression(e);
        if (jsdocNode == null) {
            jsdocNode = getAndResetJsDoc();
        }
        if (jsdocNode != null) {
            pn.setJsDocNode(jsdocNode);
        }
        mustMatchToken(Token.RP, "msg.no.paren");
        if (e.getType() == Token.EMPTY && peekToken() != Token.ARROW) {
            reportError("msg.syntax");
            return makeErrorNode();
        }
        pn.setLength(ts.tokenEnd - pn.getPosition());
        pn.setLineno(lineno);
        return pn;
    } finally {
        inForInit = wasInForInit;
    }
}
Also used : ParenthesizedExpression(org.mozilla.javascript.ast.ParenthesizedExpression) Comment(org.mozilla.javascript.ast.Comment) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 5 with EmptyExpression

use of org.mozilla.javascript.ast.EmptyExpression in project st-js by st-js.

the class RhinoJavaScriptBuilder method binary.

/**
 * {@inheritDoc}
 */
@Override
public AstNode binary(BinaryOperator operator, Iterable<AstNode> operands) {
    // this is to deal with the COMMA operator who can have less than two operands
    if (Iterables.isEmpty(operands)) {
        return new EmptyExpression();
    }
    if (Iterables.size(operands) == 1) {
        return operands.iterator().next();
    }
    InfixExpression list = new InfixExpression();
    list.setOperator(operator.getJavaScript());
    Iterator<AstNode> it = operands.iterator();
    list.setLeft(it.next());
    list.setRight(it.next());
    while (it.hasNext()) {
        InfixExpression tmpIncrements = new InfixExpression();
        tmpIncrements.setOperator(operator.getJavaScript());
        tmpIncrements.setLeft(list);
        tmpIncrements.setRight(it.next());
        list = tmpIncrements;
    }
    return list;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)10 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)10 XmlString (org.mozilla.javascript.ast.XmlString)4 FunctionNode (org.mozilla.javascript.ast.FunctionNode)3 LetNode (org.mozilla.javascript.ast.LetNode)3 ScriptNode (org.mozilla.javascript.ast.ScriptNode)3 ParenthesizedExpression (org.mozilla.javascript.ast.ParenthesizedExpression)2 XmlExpression (org.mozilla.javascript.ast.XmlExpression)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 ArrayLiteral (org.mozilla.javascript.ast.ArrayLiteral)1 Block (org.mozilla.javascript.ast.Block)1 CatchClause (org.mozilla.javascript.ast.CatchClause)1 Comment (org.mozilla.javascript.ast.Comment)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 ForInLoop (org.mozilla.javascript.ast.ForInLoop)1