Search in sources :

Example 6 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class TypeChecker method validateTags.

private void validateTags(BLangXMLElementLiteral bLangXMLElementLiteral, SymbolEnv xmlElementEnv) {
    // check type for start and end tags
    BLangExpression startTagName = (BLangExpression) bLangXMLElementLiteral.startTagName;
    checkExpr(startTagName, xmlElementEnv, Lists.of(symTable.stringType));
    BLangExpression endTagName = (BLangExpression) bLangXMLElementLiteral.endTagName;
    if (endTagName != null) {
        checkExpr(endTagName, xmlElementEnv, Lists.of(symTable.stringType));
    }
    if (endTagName == null) {
        return;
    }
    if (startTagName.getKind() == NodeKind.XML_QNAME && startTagName.getKind() == NodeKind.XML_QNAME && startTagName.equals(endTagName)) {
        return;
    }
    if (startTagName.getKind() != NodeKind.XML_QNAME && startTagName.getKind() != NodeKind.XML_QNAME) {
        return;
    }
    dlog.error(startTagName.pos, DiagnosticCode.XML_TAGS_MISMATCH);
}
Also used : BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 7 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangXMLElementLiteral xmlElementLiteral) {
    SymbolEnv xmlElementEnv = SymbolEnv.getXMLElementEnv(xmlElementLiteral, env);
    xmlElementLiteral.regIndex = calcAndGetExprRegIndex(xmlElementLiteral);
    // Visit in-line namespace declarations. These needs to be visited first before visiting the
    // attributes, start and end tag names of the element.
    xmlElementLiteral.inlineNamespaces.forEach(xmlns -> {
        genNode(xmlns, xmlElementEnv);
    });
    // Create start tag name
    BLangExpression startTagName = (BLangExpression) xmlElementLiteral.getStartTagName();
    RegIndex startTagNameRegIndex = visitXMLTagName(startTagName, xmlElementEnv, xmlElementLiteral);
    // Create end tag name. If there is no endtag name (empty XML tag),
    // then consider start tag name as the end tag name too.
    BLangExpression endTagName = (BLangExpression) xmlElementLiteral.getEndTagName();
    RegIndex endTagNameRegIndex = endTagName == null ? startTagNameRegIndex : visitXMLTagName(endTagName, xmlElementEnv, xmlElementLiteral);
    // Create an XML with the given QName
    RegIndex defaultNsURIIndex = getNamespaceURIIndex(xmlElementLiteral.defaultNsSymbol, xmlElementEnv);
    emit(InstructionCodes.NEWXMLELEMENT, xmlElementLiteral.regIndex, startTagNameRegIndex, endTagNameRegIndex, defaultNsURIIndex);
    // Add namespaces decelerations visible to this element.
    xmlElementLiteral.namespacesInScope.forEach((name, symbol) -> {
        BLangXMLQName nsQName = new BLangXMLQName(name.getValue(), XMLConstants.XMLNS_ATTRIBUTE);
        genNode(nsQName, xmlElementEnv);
        RegIndex uriIndex = getNamespaceURIIndex(symbol, xmlElementEnv);
        emit(InstructionCodes.XMLATTRSTORE, xmlElementLiteral.regIndex, nsQName.regIndex, uriIndex);
    });
    // Add attributes
    xmlElementLiteral.attributes.forEach(attribute -> {
        genNode(attribute, xmlElementEnv);
        emit(InstructionCodes.XMLATTRSTORE, xmlElementLiteral.regIndex, attribute.name.regIndex, attribute.value.regIndex);
    });
    // Add children
    xmlElementLiteral.modifiedChildren.forEach(child -> {
        genNode(child, xmlElementEnv);
        emit(InstructionCodes.XMLSEQSTORE, xmlElementLiteral.regIndex, child.regIndex);
    });
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 8 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class BLangPackageBuilder method endXMLElement.

public void endXMLElement(Set<Whitespace> ws) {
    BLangExpression endTag = (BLangExpression) exprNodeStack.pop();
    BLangXMLElementLiteral xmlElement = (BLangXMLElementLiteral) exprNodeStack.peek();
    xmlElement.addWS(ws);
    xmlElement.endTagName = endTag;
}
Also used : BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 9 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addXMLTextToElement.

public void addXMLTextToElement(DiagnosticPos pos, Set<Whitespace> ws, Stack<String> precedingTextFragments, String endingText) {
    List<BLangExpression> templateExprs = getExpressionsInTemplate(pos, ws, precedingTextFragments, endingText, NodeKind.XML_TEXT_LITERAL);
    BLangXMLElementLiteral parentElement = (BLangXMLElementLiteral) exprNodeStack.peek();
    templateExprs.forEach(expr -> parentElement.addChild(expr));
}
Also used : BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 10 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class BLangPackageBuilder method startXMLElement.

public void startXMLElement(DiagnosticPos pos, Set<Whitespace> ws, boolean isRoot) {
    BLangXMLElementLiteral xmlElement = (BLangXMLElementLiteral) TreeBuilder.createXMLElementLiteralNode();
    BLangExpression startTag = (BLangExpression) exprNodeStack.pop();
    xmlElement.addWS(ws);
    xmlElement.startTagName = startTag;
    xmlElement.pos = pos;
    xmlElement.isRoot = isRoot;
    xmlAttributeNodeStack.forEach(attribute -> xmlElement.addAttribute(attribute));
    xmlAttributeNodeStack.clear();
    addExpressionNode(xmlElement);
}
Also used : BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)7 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)4 BLangXMLElementLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)3 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)3 BLangXMLAttribute (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute)2 XMLLiteralNode (org.ballerinalang.model.tree.expressions.XMLLiteralNode)1 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)1 BLangLocalXMLNS (org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)1 Name (org.wso2.ballerinalang.compiler.util.Name)1