Package com.google.caja.parser.js

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


      e = operands.get(0);
      chain.add(operands.get(1));
    }
    chain.add(e);
    for (int i = 0, j = chain.size(); i < --j; ++i) {
      Expression t = chain.get(i);
      chain.set(i, chain.get(j));
      chain.set(j, t);
    }
    return chain;
  }
View Full Code Here


  private static Expression commaOp(
      FilePosition pos, Expression a, Expression b) {
    while (Operation.is(b, Operator.COMMA)) {
      // (a, (b, c)) -> (a, b, c)
      List<? extends Expression> operands = ((Operation) b).children();
      Expression op0 = operands.get(0);
      a = commaOp(a, op0);
      b = operands.get(1);
    }
    return Operation.create(pos, Operator.COMMA, a, b);
  }
View Full Code Here

    if (optimizer == null) { optimizer = new ParseTreeKB(); }
    List<? extends ObjProperty> props = envJson.children();
    for (ObjProperty prop : props) {
      // JSON had better not have getters
      ValueProperty vprop = (ValueProperty) prop;
      Expression value = vprop.getValueExpr().fold(false); // fold negative nums
      if (!(value instanceof Literal)) {
        // True for "*useragent*" property inserted by JSKB.
        continue;
      }
      StringLiteral sl = vprop.getPropertyNameNode();
      String rawExpr = sl.getValue();
      rawExpr = " " + rawExpr.substring(1, rawExpr.length() - 1) + " ";
      CharProducer valueCp = CharProducer.Factory.fromJsString(
          CharProducer.Factory.fromString(rawExpr, sl.getFilePosition()));
      try {
        Expression expr = jsExpr(valueCp, DevNullMessageQueue.singleton());
        optimizer.addFact(expr, Fact.is((Literal) value));
      } catch (ParseException ex) {
        continue// Triggered for browser specific extensions such as for each
      }
    }
View Full Code Here

  }

  private static Expression jsExpr(CharProducer cp, MessageQueue mq)
      throws ParseException {
    Parser p = jsParser(cp, mq);
    Expression e = p.parseExpression(true);
    p.getTokenQueue().expectEmpty();
    return e;
  }
View Full Code Here

          && substitutes.get(subsSize) instanceof StringLiteral) {
        if (getValue().createSubstitutes(substitutes, bindings)) {
          int subsSizeValue = substitutes.size();
          if (subsSizeValue == subsSizeKey + 1) {
            StringLiteral key = (StringLiteral) substitutes.get(subsSize);
            Expression value = (Expression) substitutes.get(subsSize + 1);
            substitutes.subList(subsSize, substitutes.size()).clear();
            substitutes.add(new ValueProperty(key, value));
            return true;
          }
        }
View Full Code Here

    // body.
    var:
    for (Var v : vars) {
      if ("arguments".equals(v.name)) { continue; // special in function body
      Symbol s = v.s.getSymbol(v.name);
      Expression value;
      int initPos;
      boolean isConst;
      AncestorChain<Identifier> write;

      switch (s.writes.size()) {
        case 1:
          write = s.writes.get(0);
          if (write.parent.node instanceof Declaration) {
            initPos = write.parent.node instanceof FunctionDeclaration
                ? -// hoisted
                : positions.get(write);
            AncestorChain<Declaration> d = write.parent.cast(Declaration.class);
            value = d.node.getInitializer();

            isConst = isConst(value);
            if (!isConst
                && (!isSinglyInlineable(value) || s.reads.size() > 1)) {
              continue var;
            }
          } else {
            continue var;
          }
          break;
        case 0:
          initPos = -1;
          value = null;
          write = null;
          isConst = true;
          break;
        default:
          continue var;
      }

      // If there is at most one assignment at the top level of the function
      // and all reads occur after that, and the value is constant, then
      // inline uses.

      // TODO: we can safely inline the reads that are okay.

      // TODO: check that reads are in the same function
      // or before the first call or member dereference op.

      int nInlined = 0;
      for (AncestorChain<Identifier> read : s.reads) {
        // If the read occur afterwards lexically, then, since we
        // know the declaration appears at the top of a run of
        // declarations in a function body, then it isn't being read by
        // any of the earlier declarations ; so the read must happen
        // after the var is initialized.
        if (positions.get(read) < initPos) { continue; }
        if (write != null && !inSameFn(write, read)) {
          // Can't reuse reference values across function boundaries.
          if (!isConst) { continue; }
          // If the use is across function boundaries, and there might have
          // been a non-local transfer of control, then we can't inline.
          if (initPos > v.s.earliestNonLocalXfer) { continue; }
        }

        AncestorChain<Reference> toReplace = read.parent.cast(Reference.class);
        Expression repl;
        if (value == null) {
          FilePosition fp = toReplace.node.getFilePosition();
          repl = Operation.create(fp, Operator.VOID, new IntegerLiteral(fp, 0));
        } else {
          repl = (Expression) value.clone();
View Full Code Here

  private static QuasiNode buildObjectConstructorNode(ObjectConstructor obj) {
    List<QuasiNode> propQuasis = Lists.newArrayList();
    for (ObjProperty prop : obj.children()) {
      StringLiteral key = prop.getPropertyNameNode();
      if (prop instanceof ValueProperty) {
        Expression value = ((ValueProperty) prop).getValueExpr();
        String keyIdent = quasiIdent(key);
        if (value instanceof Reference) {
          String valueStr = ((Reference) value).getIdentifierName();
          if (keyIdent != null && keyIdent.endsWith("*")
              && valueStr.startsWith("@") && valueStr.endsWith("*")) {
View Full Code Here

            && !(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()) {
            replacement = new Noop(node.getFilePosition());
          } else if (replacements.size() == 1) {
            Expression e = replacements.get(0);
            replacement = new ExpressionStmt(e.getFilePosition(), e);
          } else if (chain.parent.node instanceof Block) {
            List<Statement> stmts = Lists.newArrayList();
            for (Expression e : replacements) {
              stmts.add(new ExpressionStmt(e));
            }
            replacement = new Block(node.getFilePosition(), stmts);
          } else {
            Expression combo = null;
            for (Expression e : replacements) {
              combo = combo == null
                  ? e : Operation.createInfix(Operator.COMMA, combo, e);
            }
            replacement = new ExpressionStmt(node.getFilePosition(), combo);
View Full Code Here

        } else {
          break;
        }
      }
      if (!(first instanceof ExpressionStmt)) { break; }
      Expression e = ((ExpressionStmt) first).getExpression();
      if (!Operation.is(e, Operator.ASSIGN)) { break; }
      Operation op = (Operation) e;
      Expression lhs = op.children().get(0);
      if (!(lhs instanceof Reference)) { break; }
      Reference r = (Reference) lhs;
      if (!unassigned.contains(r.getIdentifier())) { break; }
      // Don't return two with the same name, because we don't want to have
      // multiple var declarations for the same name.
View Full Code Here

      new PrintWriter(System.err), new MessageContext(), true);
  static String normJs(String js, MessageQueue mq) throws ParseException {
    JsLexer lexer = new JsLexer(
        CharProducer.Factory.fromString(js, FilePosition.UNKNOWN));
    JsTokenQueue tq = new JsTokenQueue(lexer, InputSource.UNKNOWN);
    Expression e = new Parser(tq, mq).parseExpression(true);
    tq.expectEmpty();
    StringBuilder sb = new StringBuilder(js.length() + 16);
    RenderContext rc = new RenderContext(new JsMinimalPrinter(sb));
    e.render(rc);
    rc.getOut().noMoreTokens();
    return sb.toString();
  }
View Full Code Here

TOP

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

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.