Search in sources :

Example 1 with XmlString

use of org.mozilla.javascript.ast.XmlString 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 2 with XmlString

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

the class Parser method xmlInitializer.

private AstNode xmlInitializer() throws IOException {
    if (currentToken != Token.LT)
        codeBug();
    int pos = ts.tokenBeg, tt = ts.getFirstXMLToken();
    if (tt != Token.XML && tt != Token.XMLEND) {
        reportError("msg.syntax");
        return makeErrorNode();
    }
    XmlLiteral pn = new XmlLiteral(pos);
    pn.setLineno(ts.lineno);
    for (; ; tt = ts.getNextXMLToken()) {
        switch(tt) {
            case Token.XML:
                pn.addFragment(new XmlString(ts.tokenBeg, ts.getString()));
                mustMatchToken(Token.LC, "msg.syntax");
                int beg = ts.tokenBeg;
                AstNode expr = (peekToken() == Token.RC) ? new EmptyExpression(beg, ts.tokenEnd - beg) : expr();
                mustMatchToken(Token.RC, "msg.syntax");
                XmlExpression xexpr = new XmlExpression(beg, expr);
                xexpr.setIsXmlAttribute(ts.isXMLAttribute());
                xexpr.setLength(ts.tokenEnd - beg);
                pn.addFragment(xexpr);
                break;
            case Token.XMLEND:
                pn.addFragment(new XmlString(ts.tokenBeg, ts.getString()));
                return pn;
            default:
                reportError("msg.syntax");
                return makeErrorNode();
        }
    }
}
Also used : XmlLiteral(org.mozilla.javascript.ast.XmlLiteral) XmlString(org.mozilla.javascript.ast.XmlString) XmlExpression(org.mozilla.javascript.ast.XmlExpression) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)2 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)2 XmlExpression (org.mozilla.javascript.ast.XmlExpression)2 XmlString (org.mozilla.javascript.ast.XmlString)2 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 LetNode (org.mozilla.javascript.ast.LetNode)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 XmlFragment (org.mozilla.javascript.ast.XmlFragment)1 XmlLiteral (org.mozilla.javascript.ast.XmlLiteral)1