Package com.google.caja.parser.js

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


        if (isStringy(parts.get(0), false) || isStringy(parts.get(1), false)) {
          return;
        }
        break;
    }
    Expression e = ((StringPart) parts.get(0)).e;
    parts.set(0, new StringPart(makeStringy(e, parts.size() == 1)));
  }
View Full Code Here


  }

  static Expression makeStringy(Expression e, boolean strict) {
    if (isStringy(e, strict)) { return e; }

    Expression stringier = null;
    if (e instanceof Operation) {
      Operation op = (Operation) e;
      List<? extends Expression> operands = op.children();
      switch (op.getOperator()) {
        // Change arguments to ||,&& would change semantics.
View Full Code Here

  static StringLiteral asStringLiteral(Expression e) {
    if (e instanceof Literal) {
      if (e instanceof StringLiteral) { return (StringLiteral) e; }
      return StringLiteral.valueOf(e.getFilePosition(), asString((Literal) e));
    } else if (is(e, Operator.NEGATION)) {
      Expression child = ((Operation) e).children().get(0);
      if (child instanceof NumberLiteral) {
        return StringLiteral.valueOf(
            e.getFilePosition(), "-" + asString((Literal) child));
      }
    }
View Full Code Here

    String renderedModule = render(
        makeTestCajoledModule(),
        jsExpr(fromString("foo.bar.baz")));

    // Re-parse the rendered output so we can apply quasi matches to it.
    Expression reparsedModule = (Expression)
        js(fromString(renderedModule))
        // Extract the innermost Expression since the quasi will match that.
        .children().get(0).children().get(0).children().get(0);

    // Check that the reparsed structure matches what we expect.
View Full Code Here

  private void assertConcatenate(String goldenJs, String... parts)
      throws ParseException {
    JsConcatenator concat = new JsConcatenator();
    for (String part : parts) {
      Expression e = jsExpr(fromString(part));
      if (Operation.is(e, Operator.VOID)) {
        concat.forSideEffect(((Operation) e).children().get(0));
      } else {
        concat.append(e);
      }
View Full Code Here

      throws ParseException {
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(
        lexer, sourceOf(cp), JsTokenQueue.NO_COMMENT);
    Parser p = new Parser(tq, mq, quasi);
    Expression e = p.parseExpression(true);
    tq.expectEmpty();
    return e;
  }
View Full Code Here

    assertEquals(0xffffffffL, Operation.toUint32(-1.5d));
  }

  private void assertSimplified(String golden, String input)
      throws ParseException {
    Expression simple = jsExpr(fromString(input)).simplifyForSideEffect();
    if (golden == null) {
      assertNull(input, simple);
      return;
    }
    assertEquals(input, render(jsExpr(fromString(golden))), render(simple));
View Full Code Here

    assertFolded(result, expr, false);
  }

  private void assertFolded(String result, String expr, boolean isFn)
      throws ParseException {
    Expression input = jsExpr(fromString(expr));
    if (input instanceof Operation) {
      Operation op = (Operation) input;
      for (Expression operand : op.children()) {
        // Fold some operands so we can test negative numbers.
        if ((Operation.is(operand, Operator.NEGATION)
            // and so that we can test corner cases around NaN and Infinity.
            || Operation.is(operand, Operator.DIVISION))
            && operand.children().get(0) instanceof NumberLiteral) {
          op.replaceChild(operand.fold(false), operand);
        }
      }
    }
    Expression actual = input.fold(isFn);
    assertEquals(expr, result, actual != null ? render(actual) : null);
  }
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

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.