Package com.google.caja.parser.js

Examples of com.google.caja.parser.js.Reference


        new ParseTreeNodeVisitor() {
          public boolean visit(ParseTreeNode node) {
            // Do not recurse into closures.
            if (node instanceof FunctionConstructor) { return false; }
            if (node instanceof Reference) {
              Reference r = (Reference) node;
              if (Keyword.THIS.toString().equals(r.getIdentifierName())) {
                Identifier oldRef = r.getIdentifier();
                Identifier thisNode = new Identifier(
                    oldRef.getFilePosition(), ReservedNames.THIS_NODE);
                r.replaceChild(SyntheticNodes.s(thisNode), oldRef);
              }
              return false;
            }
            return true;
          }
View Full Code Here


    // Emit a statement to attach the dynamic attribute.
    if (dynamicValue instanceof FunctionConstructor) {
      emitStatement(
          quasiStmt(
              "el___.@name = @eventAdapter;",
              "name", new Reference(
                  SyntheticNodes.s(new Identifier(pos, name))),
              "eventAdapter", dynamicValue),
          source);
    } else {
      emitStatement(
View Full Code Here

                  pos, xoper, xoperands.get(0),
                  optimizeExpressionFlow(
                      Operation.createTernary(
                          c, xoperands.get(1), yoperands.get(1))));
            } else {
              Reference xref = (Reference) xoperands.get(1);
              Reference yref = (Reference) yoperands.get(1);
              StringLiteral xname = StringLiteral.valueOf(
                  xref.getFilePosition(), xref.getIdentifierName());
              StringLiteral yname = StringLiteral.valueOf(
                  yref.getFilePosition(), yref.getIdentifierName());
              return Operation.create(
                  pos, Operator.SQUARE_BRACKET,
                  xoperands.get(0),
                  optimizeExpressionFlow(
                       Operation.createTernary(
View Full Code Here

  private int longestKeyLength = 0;

  private static final FilePosition UNK = FilePosition.UNKNOWN;

  public ParseTreeKB() {
    addFactInt(new Reference(new Identifier(UNK, "undefined")), Fact.UNDEFINED);
    addFactInt(new Reference(new Identifier(UNK, "this")), Fact.GLOBAL);
  }
View Full Code Here

      boolean throwsOnUndefined, Result out) {
    StringBuilder sb = new StringBuilder();
    sb.append('(');
    Expression obj = ma.children().get(0);
    optimize(s, obj, false, false, false, false, out);
    Reference prop = (Reference) ma.children().get(1);
    if (out.node != obj) {
      ma = Operation.createInfix(
          Operator.MEMBER_ACCESS, (Expression) out.node, prop);
    }
    sb = addDigest(out.digest, sb);
    int objDigestEnd = sb != null ? sb.length() : -1;
    if (sb != null) {
      nodeDigest(ma.children().get(1), sb);
      nodeTail(ma, sb);
    }
    String digest = sb != null ? sb.toString() : null;
    out.node = ma;
    out.digest = digest;
    if (digest != null) {
      if (!isLhs) {
        Fact f = getFact(digest);
        if (f != null && f.isSubstitutable(isFuzzy)) {
          out.node = f.value.clone();
          out.digest = nodeDigest(out.node);
          return;
        }
      }
      // window.addEventListener -> addEventListener
      String objDigest = digest.substring(1, objDigestEnd);
      Pair<Expression, Fact> objFe = facts.get(objDigest);
      if (objFe != null && objFe.b.isGlobal()
          && s.isOuter(prop.getIdentifierName())) {
        String propDigest = nodeDigest(prop);
        boolean canSimplify = false;
        if (isLhs || throwsOnUndefined) {
          // If it's being set, we don't need to worry about undefined global
          // errors, and we don't need to worry if the containing expression
View Full Code Here

  }

  private static Expression withTopRef(Expression e, String lhs) {
    if (e instanceof Reference) {
      return Operation.createInfix(
          Operator.MEMBER_ACCESS, new Reference(new Identifier(UNK, lhs)), e);
    } else {
      Operation op = (Operation) e;
      List<? extends Expression> operands = op.children();
      return Operation.create(
          e.getFilePosition(), op.getOperator(),
View Full Code Here

        // TODO(mikesamuel): choose a guaranteed non-interfering name.
        String name = "$_$__litpool__" + decls.size() + "$_$";
        decls.add(new Declaration(
            pos, new Identifier(pos, name), v.uses.get(0).node));
        for (AncestorChain<Literal> use : v.uses) {
          Reference ref = new Reference(
              new Identifier(use.node.getFilePosition(), name));
          use.parent.cast(MutableParseTreeNode.class).node
              .replaceChild(ref, use.node);
        }
      }
View Full Code Here

      values.add(new IntegerLiteral(unk, valueMaker.apply(e)));
    }
    return new ExpressionStmt(unk,
        (Expression) QuasiBuilder.substV(
            "html4.@i = { @k*: @v* };",
            "i", new Reference(new Identifier(unk, key)),
            "k", new ParseTreeNodeContainer(keys),
            "v", new ParseTreeNodeContainer(values)));
  }
View Full Code Here

            if (isDeclaration && !isSynthetic(name)
                && newScope.isFunction(name.getName())
                && !newScope.isDeclaredFunction(name.getName())) {
              headDecls.add((Declaration) QuasiBuilder.substV(
                  "var @innerName = @outerName;",
                  "outerName", new Reference(rewrittenName),
                  "innerName", new Identifier(
                      name.getFilePosition(),
                      newContext.lookup(name.getName()).newName)));
              // TODO(mikesamuel): skip if the self name is never used.
            }

            FunctionConstructor out = (FunctionConstructor) substV(
                "name", rewrittenName,
                "headDecls", optionalDeclarations(headDecls),
                "params", new ParseTreeNodeContainer(newFormals),
                "body", expandAll(bindings.get("body"), newScope));
            return isDeclaration ? new FunctionDeclaration(out) : out;
          }
          return NONE;
        }
      },

      new Rule() {
        @Override
        @RuleDescription(
            name="block",
            synopsis="block scoping",
            reason="",
            matches="{ @body* }",
            substitutes="{ @body* }")
        public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          if (node instanceof Block) {
            Block bl = (Block) node;
            List<Statement> stmts = Lists.newArrayList();
            Scope newScope = Scope.fromPlainBlock(scope);
            NameContext<String, ?> newContext = contexts.get(scope)
                .makeChildContext();
            contexts.put(newScope, newContext);
            for (String local : newScope.getLocals()) {
              try {
                newContext.declare(
                    local, newScope.getLocationOfDeclaration(local));
              } catch (NameContext.RedeclarationException ex) {
                throw new SomethingWidgyHappenedError(
                    "Local variable unexpectedly not set", ex);
              }
            }
            for (Statement s : bl.children()) {
              stmts.add((Statement) expand(s, newScope));
            }
            stmts.addAll(0, newScope.getStartStatements());
            return new Block(bl.getFilePosition(), stmts);
          }
          return NONE;
        }
      },

      new Rule() {
        @Override
        @RuleDescription(
            name="catch",
            synopsis="catch block scoping",
            reason="",
            matches="catch (@e) { @body* }",
            substitutes="catch (@e) { @body* }")
        public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          if (node instanceof CatchStmt) {
            CatchStmt cs = (CatchStmt) node;
            Scope newScope = Scope.fromCatchStmt(scope, cs);
            NameContext<String, ?> context = contexts.get(scope);
            NameContext<String, ?> newContext = context.makeChildContext();
            contexts.put(newScope, newContext);
            try {
              newContext.declare(cs.getException().getIdentifierName(),
                                 cs.getException().getFilePosition());
            } catch (NameContext.RedeclarationException ex) {
              ex.toMessageQueue(mq);
            }
            return expandAll(cs, newScope);
          }
          return NONE;
        }
      },

      //////////////
      // Renaming //
      //////////////

      new Rule() {
        @Override
        @RuleDescription(
            name="memberAccess",
            synopsis="",
            reason="so that we do not mistakenly rename property names",
            matches="@o.@r",
            substitutes="@o.@r")
        public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          Map<String, ParseTreeNode> bindings = match(node);
          if (bindings != null) {
            return substV("o", expand(bindings.get("o"), scope),
                          "r", bindings.get("r"));
          }
          return NONE;
        }
      },
      new Rule() {
        @Override
        @RuleDescription(
            name="thisReference",
            synopsis="Disallow this in the global scope.",
            reason="The declaration cannot be rewritten.",
            matches="this",
            substitutes="this")
            public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          if (match(node) != null) {
            if (scope.isOuter()) {
              mq.addMessage(
                  RewriterMessageType.THIS_IN_GLOBAL_CONTEXT,
                  node.getFilePosition());
              return new NullLiteral(node.getFilePosition());
            }
          }
          return NONE;
        }
      },
      new Rule() {
        @Override
        @RuleDescription(
            name="argumentsReference",
            synopsis="Disallow arguments in the global scope.",
            reason="The declaration cannot be rewritten.",
            matches="arguments",
            substitutes="arguments")
            public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          if (match(node) != null) {
            if (scope.isOuter()) {
              mq.addMessage(
                  RewriterMessageType.ARGUMENTS_IN_GLOBAL_CONTEXT,
                  node.getFilePosition());
              return new NullLiteral(node.getFilePosition());
            }
          }
          return NONE;
        }
      },
      new Rule() {
        @Override
        @RuleDescription(
            name="rename",
            synopsis="",
            reason="",
            matches="/* Reference */ @r",
            substitutes="@r")
        public ParseTreeNode fire(ParseTreeNode node, Scope scope) {
          if (node instanceof Reference) {
            Reference r = (Reference) node;
            if (!isSynthetic(r)) {
              FilePosition pos = r.getFilePosition();
              String rname = r.getIdentifierName();
              NameContext<String, ?> context = contexts.get(scope);
              NameContext.VarInfo<String, ?> vi = context.lookup(rname);
              if (vi != null) {
                return new Reference(new Identifier(pos, vi.newName));
              } else {
                mq.addMessage(
                    RewriterMessageType.FREE_VARIABLE, pos,
                    MessagePart.Factory.valueOf(rname));
                return new NullLiteral(pos);
View Full Code Here

            return true;
          }
          break;
      }
    } else if (e instanceof Reference) {
      Reference r = (Reference) e;
      String name = r.getIdentifierName();
      if (!identifiersExpanding.contains(name)) {
        identifiersExpanding.add(name);
        return doesVarReferenceVisibleProperty(
            r, scopeTree, identifiersExpanding);
      }
View Full Code Here

TOP

Related Classes of com.google.caja.parser.js.Reference

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.