Search in sources :

Example 1 with VarDefn

use of com.google.template.soy.exprtree.VarDefn in project closure-templates by google.

the class SoyTreeUtilsTest method testClone.

@Test
public final void testClone() throws Exception {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(SOY_SOURCE_FOR_TESTING_CLONING).declaredSyntaxVersion(SyntaxVersion.V2_4).parse().fileSet();
    SoyFileSetNode clone = soyTree.copy(new CopyState());
    assertEquals(1, clone.numChildren());
    assertEquals(clone.getChild(0).toSourceString(), soyTree.getChild(0).toSourceString());
    // All the localvarnodes, there is one of each type
    ForNonemptyNode forNonemptyNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, ForNonemptyNode.class));
    LetValueNode letValueNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, LetValueNode.class));
    for (VarRefNode varRef : SoyTreeUtils.getAllNodesOfType(clone, VarRefNode.class)) {
        VarDefn defn = varRef.getDefnDecl();
        LocalVar local;
        switch(varRef.getName()) {
            case "local":
                local = (LocalVar) defn;
                assertSame(letValueNode, local.declaringNode());
                assertSame(letValueNode.getVar(), local);
                break;
            case "item":
                local = (LocalVar) defn;
                assertSame(forNonemptyNode, local.declaringNode());
                assertSame(forNonemptyNode.getVar(), defn);
                break;
            default:
        }
    }
}
Also used : VarRefNode(com.google.template.soy.exprtree.VarRefNode) VarDefn(com.google.template.soy.exprtree.VarDefn) LocalVar(com.google.template.soy.soytree.defn.LocalVar) CopyState(com.google.template.soy.basetree.CopyState) Test(org.junit.Test)

Example 2 with VarDefn

use of com.google.template.soy.exprtree.VarDefn in project closure-templates by google.

the class EnhancedAbstractExprNodeVisitor method visitVarRefNode.

@Override
protected final T visitVarRefNode(VarRefNode node) {
    VarDefn defn = node.getDefnDecl();
    switch(defn.kind()) {
        case LOCAL_VAR:
            LocalVar local = (LocalVar) defn;
            LocalVarNode declaringNode = local.declaringNode();
            switch(declaringNode.getKind()) {
                case FOR_NONEMPTY_NODE:
                    return visitForLoopVar(node, local);
                case LET_CONTENT_NODE:
                case LET_VALUE_NODE:
                    return visitLetNodeVar(node, local);
                default:
                    throw new AssertionError("Unexpected local variable: " + local);
            }
        case PARAM:
            return visitParam(node, (TemplateParam) defn);
        case IJ_PARAM:
            return visitIjParam(node, (InjectedParam) defn);
        case UNDECLARED:
            throw new RuntimeException("undeclared params are not supported by jbcsrc");
        default:
            throw new AssertionError();
    }
}
Also used : VarDefn(com.google.template.soy.exprtree.VarDefn) LocalVar(com.google.template.soy.soytree.defn.LocalVar) LocalVarNode(com.google.template.soy.soytree.SoyNode.LocalVarNode)

Aggregations

VarDefn (com.google.template.soy.exprtree.VarDefn)2 LocalVar (com.google.template.soy.soytree.defn.LocalVar)2 CopyState (com.google.template.soy.basetree.CopyState)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 LocalVarNode (com.google.template.soy.soytree.SoyNode.LocalVarNode)1 Test (org.junit.Test)1