Package com.google.caja.parser.js

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


  private static BreakStmt b(String label) {
    return new BreakStmt(FilePosition.UNKNOWN, label);
  }
  private static Declaration decl(String name) {
    return new Declaration(
        FilePosition.UNKNOWN, new Identifier(FilePosition.UNKNOWN, name), null);
  }
View Full Code Here


    Block n = js(fromString(
        "try { } catch (e) { function foo() { var e; } }"));

    TryStmt t = (TryStmt)n.children().get(0);
    CatchStmt c = (CatchStmt)t.children().get(1);
    Declaration d = findNodeWithIdentifier(n, Declaration.class, "foo");
    FunctionConstructor fc = (FunctionConstructor)d.getInitializer();

    Scope s0 = fromProgram(n);
    Scope s1 = Scope.fromCatchStmt(s0, c);
    Scope.fromFunctionConstructor(s1, fc);
View Full Code Here

    Statement poolDecls = null;
    if (!stringPool.isEmpty()) {
      poolDecls = joinDeclarations(
          poolDecls,
          new Declaration(unk, new Identifier(unk, "s"),
              new ArrayConstructor(unk, stringPool)));
    }
    if (!regexPool.isEmpty()) {
      poolDecls = joinDeclarations(
          poolDecls,
          new Declaration(unk, new Identifier(unk, "c"),
              new ArrayConstructor(unk, regexPool)));
    }

    // Given keyword sets like
    // [['red','blue','green','transparent','inherit',;none'],
    //  ['red','blue','green'],
    //  ['inherit','none','bold','bolder']]
    // recognize that ['red','blue','green'] probably occurs frequently and
    // create a partition like
    // [['red','blue','green'],['bold','bolder'],['inherit',none'],
    //  ['transparent']]
    // and then store indices into the array of partition elements with
    // CSS property names so they can be unioned as needed.
    List<Set<String>> literalSets = Lists.newArrayList();
    for (Pair<CssSchema.CssPropertyInfo, CssPropertyData> p : propData) {
      literalSets.add(p.b.literals);
    }
    Partitions.Partition<String> litPartition = Partitions.partition(
        literalSets, String.class, null);
    List<ArrayConstructor> literalSetArrs = Lists.newArrayList();
    for (int[] literalIndices : litPartition.partition) {
      List<StringLiteral> literalArr = Lists.newArrayList();
      for (int litIndex : literalIndices) {
        literalArr.add(StringLiteral.valueOf(
            unk, litPartition.universe[litIndex]));
      }
      literalSetArrs.add(new ArrayConstructor(unk, literalArr));
    }
    if (!literalSetArrs.isEmpty()) {
      poolDecls = joinDeclarations(
          poolDecls,
          new Declaration(unk, new Identifier(unk, "L"),
              new ArrayConstructor(unk, literalSetArrs)));
    }

    List<ValueProperty> cssSchemaProps = Lists.newArrayList();
    StringLiteral propbitsObjKey = new StringLiteral(unk, "cssPropBits");
View Full Code Here

      int canonLen = v.canonForm().length();
      int nUses = v.uses.size();
      if (canonLen * nUses > 8 + canonLen + 2 * nUses + requiredSavings) {
        // 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);
        }
      }
    }
    if (!decls.isEmpty()) {
      Statement first = body.children().get(0);
      MultiDeclaration md;
      if (first instanceof MultiDeclaration) {
        md = (MultiDeclaration) first;
      } else if (first instanceof Declaration) {
        md = new MultiDeclaration(
            FilePosition.span(pos, first.getFilePosition()),
            Collections.singletonList((Declaration) first));
        body.replaceChild(md, first);
      } else if (decls.size() == 1) {
        body.insertBefore(decls.get(0), first);
        return;
      } else {
        md = new MultiDeclaration(pos, Collections.<Declaration>emptyList());
        body.insertBefore(md, first);
      }
      MutableParseTreeNode.Mutation mut = md.createMutation();
      Declaration firstDecl = md.children().get(0);
      for (Declaration decl : decls) {
        mut = mut.insertBefore(decl, firstDecl);
      }
      mut.execute();
    }
View Full Code Here

    }
    return newStmts;
  }
  private static void hoistDecls(Statement s, List<Statement> out) {
    if (s instanceof Declaration) {
      Declaration d = (Declaration) s;
      if (d instanceof FunctionDeclaration || d.getInitializer() == null) {
        out.add(d);
      } else {
        out.add(new Declaration(d.getFilePosition(), d.getIdentifier(), null));
      }
    } else if (s instanceof CatchStmt) {
      hoistDecls(((CatchStmt) s).getBody(), out);
    } else {
      for (ParseTreeNode c : s.children()) {
View Full Code Here

  public void addStartStatement(Statement s) {
    int pos = startStatements.size();
    // Group certain kinds of statements
    if (s.getClass() == Declaration.class) {
      Declaration d = (Declaration) s;
      if (d.getInitializer() == null) {
        int i = 0;
        if (i < pos && startStatements.get(i) instanceof DirectivePrologue) {
          ++i;
        }
        if (i < pos) {
View Full Code Here

          body, newIdentSet(locals));
      List<Declaration> decls = Lists.newArrayList();
      for (Operation topAssign : topAssignments) {
        Identifier id = ((Reference) topAssign.children().get(0))
            .getIdentifier();
        decls.add(new Declaration(
            topAssign.getFilePosition(), id, topAssign.children().get(1)));
        needed.remove(id);
      }
      for (Identifier name : needed) {
        decls.add(new Declaration(name.getFilePosition(), name, null));
      }
      Statement decl;
      if (decls.size() == 1) {
        decl = decls.get(0);
      } else {
View Full Code Here

      public boolean visit(AncestorChain<?> chain) {
        ParseTreeNode node = chain.node;
        if (node instanceof Declaration
            && !(node instanceof FunctionDeclaration)) {
          if (chain.parent.node instanceof CatchStmt) { return true; }
          Declaration decl = (Declaration) node;
          Identifier id = decl.getIdentifier();
          removedIdents.add(id);
          Expression init = decl.getInitializer();
          Statement replacement;
          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
          } else if (chain.parent.node instanceof ForEachLoop) {
            replacement = new ExpressionStmt(new Reference(id));
          } else {
            replacement = new Noop(decl.getFilePosition());
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return true;
        } else if (node instanceof MultiDeclaration) {
          List<Expression> replacements = Lists.newArrayList();
          for (Declaration decl : ((MultiDeclaration) node).children()) {
            removedIdents.add(decl.getIdentifier());
            if (decl.getInitializer() == null) { continue; }
            visit(chain.child(decl).child(decl.getInitializer()));
            Expression assign = toAssignment(decl);
            replacements.add(assign);
          }
          Statement replacement;
          if (replacements.isEmpty()) {
View Full Code Here

          Block b = jsFromAttrib(attr);
          if (b == null || b.children().isEmpty()) { return noResult(attr); }
          rewriteEventHandlerReferences(b);

          handlerFnName = meta.generateUniqueName("c");
          Declaration handler = (Declaration) QuasiBuilder.substV(
              ""
              + "var @handlerName = ___./*@synthetic*/markFuncFreeze("
              + "    /*@synthetic*/function ("
              + "        event, " + ReservedNames.THIS_NODE + ") { @body*; });",
              "handlerName", SyntheticNodes.s(
View Full Code Here

    this.scriptsPerNode = scriptsPerNode;
    this.scriptsPerPlaceholder = scriptsPerPlaceholder;
    this.roots = roots;
    for (EventHandler handler : handlers) {
      if (handler.handler instanceof Declaration) {
        Declaration d = (Declaration) handler.handler;
        this.handlers.put(d.getIdentifierName(), Pair.pair(handler.source, d));
      } else {
        unnamedHandlers.add(handler);
      }
    }
  }
View Full Code Here

TOP

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

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.