Package com.google.caja.parser.js

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


          propData.add(Pair.pair((CssSchema.CssPropertyInfo) null, fnData));
        }
      }
    }

    Statement poolDecls = null;
    if (!stringPool.isEmpty()) {
      poolDecls = joinDeclarations(
          poolDecls,
          new Declaration(unk, new Identifier(unk, "s"),
              new ArrayConstructor(unk, stringPool)));
View Full Code Here


              .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;
View Full Code Here

        String oldBlankLabel = blankLabel;
        StmtLabel sl = new StmtLabel(labelGenerator.next());
        labels.put(label, sl);
        blankLabel = label;
        boolean wrap = ls instanceof LabeledStmtWrapper;
        Statement unlabeled = wrap ? ((LabeledStmtWrapper) ls).getBody() : ls;
        Statement opt = (Statement) optimizeUnlabeled(unlabeled, needsBlock);
        if (oldSl == null) {
          labels.remove(label);
        } else {
          labels.put(label, oldSl);
        }
        blankLabel = oldBlankLabel;
        if (sl.nUses == 0) {
          if (!wrap && opt instanceof LabeledStatement
              && !"".equals(((LabeledStatement) opt).getLabel())) {
            return ParseTreeNodes.newNodeInstance(
                opt.getClass(), opt.getFilePosition(), "", opt.children());
          }
        } else {
          if (!wrap && opt instanceof LabeledStatement) {
            return ParseTreeNodes.newNodeInstance(
                opt.getClass(), opt.getFilePosition(), sl.newName,
                opt.children());
          } else {
            if (opt instanceof LabeledStatement) {
              // Add a block so we don't get ambiguity
              opt = new Block(opt.getFilePosition(), Arrays.asList(opt));
            }
            return new LabeledStmtWrapper(n.getFilePosition(), sl.newName, opt);
          }
        }
        return opt;
      } else {
        String oldBlankLabel = blankLabel;
        blankLabel = "";
        Statement opt = (Statement) optimizeUnlabeled(n, needsBlock);
        blankLabel = oldBlankLabel;
        return opt;
      }
    } else if (n instanceof BreakStmt || n instanceof ContinueStmt) {
      String lbl = (String) n.getValue();
View Full Code Here

          newChildren = Collections.<ParseTreeNode>singletonList(simple);
        }
      } else if (n instanceof Conditional) {
        List<ParseTreeNode> condParts = newChildren != null
            ? newChildren : Lists.newArrayList(children);
        Statement optCond = optimizeConditional(n.getFilePosition(), condParts);
        if (optCond != null) { return optCond; }
        int nCondParts = condParts.size();
        if ((nCondParts & 1) == 1) {  // There is an else clause
          // Remove a useless else clause.
          if (condParts.get(nCondParts - 1) instanceof Noop) {
View Full Code Here

    if (stmts.isEmpty()) { return null; }
    boolean changed = false;
    boolean progress;
    do {
      progress = false;
      Statement last = stmts.get(0);
      for (int i = 1, n = stmts.size(); i < n; ++i) {
        Statement next = stmts.get(i);
        if (isExpressionListTerminator(next) && last instanceof Conditional) {
          // Handle cases like
          //   if (baz) return boo(); return far();
          // where the statement following the if, is implicitly an else
          // statement since the if always returns or throws.
          // We create a fake conditional, and try to optimize it in isolation,
          // which would yield (return baz ? boo() : far()) for the above.
          // This feeds into the tail handling for expression runs below.
          Conditional combined = condAndImplicitElse((Conditional) last, next);
          if (combined != null) {
            ParseTreeNode optCond = optimize(combined, false);
            if (isExpressionListTerminator(optCond)) {
              stmts.subList(i - 1, i + 1).clear();
              stmts.add(i - 1, last = (Statement) optCond);
              --n;
              --i;
              progress = true;
              continue;
            }
          }
        }
        last = next;
      }

      // Turning adjacent statements into comma operations replaces semis with
      // commas, and lets us eliminate a lot more brackets.  It also enables
      // better conditional/return statement optimizations.
      int firstExprStmt = -1;
      for (int i = 0, n = stmts.size(); ; ++i) {
        Statement s = i != n ? stmts.get(i) : null;

        if (firstExprStmt != -1 && (i == n || !(s instanceof ExpressionStmt))) {
          // We've finished a run of ExpressionStmts
          int start = firstExprStmt;
          int end = i;
          firstExprStmt = -1;

          if (isExpressionListTerminator(s)) {
            // We can combine the child onto it, a la
            //   { foo(); return bar(); }  ->  { return foo(), bar(); }
            ++end;
          }
          if (end - start >= 2) {
            progress = true;
            Expression joined = null;
            for (Statement toJoin : stmts.subList(start, end)) {
              List<? extends ParseTreeNode> tjChildren = toJoin.children();
              // tjChildren can be empty for return statements that implicitly
              // return undefined.
              Expression e = tjChildren.isEmpty()
                  ? undef(FilePosition.endOf(toJoin.getFilePosition()))
                  : (Expression) tjChildren.get(0);
              joined = joined == null ? e : commaOp(joined, e);
            }
            Statement newChild;
            assert joined != null// because start < end, loop assigns it
            FilePosition exprPos = joined.getFilePosition();
            if (s instanceof ReturnStmt) {
              newChild = new ReturnStmt(exprPos, joined);
            } else if (s instanceof ThrowStmt) {
View Full Code Here

  private static Block combine(Block a, Block b) {
    if (b.children().isEmpty()) { return a; }
    if (a.children().isEmpty()) { return b; }
    FilePosition pos = FilePosition.span(
        a.getFilePosition(), b.getFilePosition());
    Statement s = combine(pos, a, b);
    if (s instanceof Block) {
      return (Block) s;
    } else if (s instanceof Noop) {
      return new Block(pos);
    } else {
View Full Code Here

        int i = 0;
        if (i < pos && startStatements.get(i) instanceof DirectivePrologue) {
          ++i;
        }
        if (i < pos) {
          Statement si = startStatements.get(i);
          if (si instanceof MultiDeclaration) {
            ((MultiDeclaration) si).appendChild(d);
            return;
          } else if (si.getClass() == Declaration.class) {
            startStatements.set(
                i,
                new MultiDeclaration(
                    FilePosition.UNKNOWN, Arrays.asList((Declaration) si, d)));
            return;
View Full Code Here

        }
      }
    } catch (ParseException ex) {
      ex.toMessageQueue(mq);
    }
    Statement out = opt.optimize();
    for (Message msg : mq.getMessages()) {
      msg.format(mc, System.err);
      System.err.println();
    }
    JsMinimalPrinter printer = new JsMinimalPrinter(
        new Concatenator(System.out, null));
    RenderContext rc = new RenderContext(printer)
        .withPropertyNameQuotingMode(PropertyNameQuotingMode.NO_QUOTES);
    if (out instanceof Block) {
      ((Block) out).renderBody(rc);
    } else {
      out.render(rc);
    }
    printer.noMoreTokens();
  }
View Full Code Here

        });
    sa.apply(program);
  }

  private boolean examineDeclaration(AncestorChain<Statement> ac, OptScope s) {
    Statement stmt = ac.node;
    if (stmt instanceof MultiDeclaration) {
      for (Declaration d : ((MultiDeclaration) stmt).children()) {
        if (!examineDeclaration(ac.child((Statement) d), s)) {
          return false;
        }
View Full Code Here

                true),
            inputSource),
        DevNullMessageQueue.singleton(),
        true);

    Statement topLevelStatement = parser.parse();
    parser.getTokenQueue().expectEmpty();
    return topLevelStatement;
  }
View Full Code Here

TOP

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

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.