use of com.google.template.soy.exprtree.ExprNode in project closure-templates by google.
the class ResolveExpressionTypesVisitor method addTypeSubstitutions.
// Given a map of type subsitutions, add all the entries to the current set of
// active substitutions.
private void addTypeSubstitutions(Map<Wrapper<ExprNode>, SoyType> substitutionsToAdd) {
for (Map.Entry<Wrapper<ExprNode>, SoyType> entry : substitutionsToAdd.entrySet()) {
ExprNode expr = entry.getKey().get();
// Get the existing type
SoyType previousType = expr.getType();
for (TypeSubstitution subst = substitutions; subst != null; subst = subst.parent) {
if (ExprEquivalence.get().equivalent(subst.expression, expr)) {
previousType = subst.type;
break;
}
}
// If the new type is different than the current type, then add a new type substitution.
if (!entry.getValue().equals(previousType)) {
substitutions = new TypeSubstitution(substitutions, expr, entry.getValue());
}
}
}
use of com.google.template.soy.exprtree.ExprNode in project closure-templates by google.
the class TranslateToPyExprVisitor method visitLegacyObjectMapLiteralOrMapLiteralNode.
private PyExpr visitLegacyObjectMapLiteralOrMapLiteralNode(AbstractParentExprNode node) {
Preconditions.checkState(node.getKind() == ExprNode.Kind.LEGACY_OBJECT_MAP_LITERAL_NODE || node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE);
Preconditions.checkArgument(node.numChildren() % 2 == 0);
Map<PyExpr, PyExpr> dict = new LinkedHashMap<>();
boolean needsRuntimeNullCheck = node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE;
for (int i = 0, n = node.numChildren(); i < n; i += 2) {
ExprNode keyNode = node.getChild(i);
PyExpr key = visit(keyNode);
if (needsRuntimeNullCheck) {
key = new PyFunctionExprBuilder("runtime.check_not_null").addArg(key).asPyExpr();
}
ExprNode valueNode = node.getChild(i + 1);
dict.put(key, visit(valueNode));
}
// to index into the map with the wrong convention.
return PyExprUtils.convertMapToOrderedDict(dict);
}
use of com.google.template.soy.exprtree.ExprNode in project closure-templates by google.
the class SimplifyExprVisitor method visitExprNode.
// -----------------------------------------------------------------------------------------------
// Fallback implementation.
@Override
protected void visitExprNode(ExprNode node) {
if (!(node instanceof ParentExprNode)) {
return;
}
ParentExprNode nodeAsParent = (ParentExprNode) node;
// Recurse.
visitChildren(nodeAsParent);
// constant.
for (ExprNode child : nodeAsParent.getChildren()) {
if (!isConstant(child)) {
// cannot preevaluate
return;
}
}
attemptPreeval(nodeAsParent);
}
use of com.google.template.soy.exprtree.ExprNode in project closure-templates by google.
the class XidPass method run.
@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(file, FunctionNode.class)) {
if (fn.getSoyFunction() == BuiltinFunction.XID) {
if (fn.numChildren() != 1) {
// if it isn't == 1, then an error has already been reported, move along.
continue;
}
ExprNode child = fn.getChild(0);
switch(child.getKind()) {
case GLOBAL_NODE:
GlobalNode global = (GlobalNode) child;
if (global.isResolved()) {
// This doesn't have to be an error. but it is confusing if it is is since it is
// unclear if the user intended to xid the identifier or the value.
reporter.report(global.getSourceLocation(), GLOBAL_XID_ARG_IS_RESOLVED, global.getType().toString(), global.getValue().toSourceString());
}
fn.replaceChild(0, new StringNode(global.getName(), QuoteStyle.SINGLE, global.getSourceLocation()));
break;
case STRING_NODE:
break;
default:
reporter.report(child.getSourceLocation(), STRING_OR_GLOBAL_REQUIRED);
}
}
}
}
use of com.google.template.soy.exprtree.ExprNode in project closure-templates by google.
the class SoyTreeUtils method visitAllNodes.
/**
* Runs the visitor on all nodes (including {@link ExprNode expr nodes}) reachable from the given
* node. The order of visiting is breadth first.
*
* <p>If the visitor return {@code false} from {@link NodeVisitor#exec(Node)} we will short
* circuit visiting.
*/
public static void visitAllNodes(Node node, NodeVisitor<? super Node, VisitDirective> visitor) {
ArrayDeque<Node> queue = new ArrayDeque<>();
queue.add(node);
Node current;
while ((current = queue.poll()) != null) {
switch(visitor.exec(current)) {
case ABORT:
return;
case CONTINUE:
if (current instanceof ParentNode<?>) {
queue.addAll(((ParentNode<?>) current).getChildren());
}
if (current instanceof ExprHolderNode) {
queue.addAll(((ExprHolderNode) current).getExprList());
}
continue;
case SKIP_CHILDREN:
continue;
default:
throw new AssertionError();
}
}
}
Aggregations