Package com.google.caja.parser

Examples of com.google.caja.parser.ParseTreeNode


  private static void checkFilePositionInvariants(ParseTreeNode root) {
    checkFilePositionInvariants(AncestorChain.instance(root));
  }

  private static void checkFilePositionInvariants(AncestorChain<?> nChain) {
    ParseTreeNode n = nChain.node;
    String msg = n + " : " + n.getFilePosition();
    try {
      // require that n start on or after its previous sibling
      int indexInParent = nChain.parent != null
          ? nChain.parent.node.children().indexOf(nChain.node) : -1;
      ParseTreeNode prev = indexInParent > 0
          ? nChain.parent.node.children().get(indexInParent - 1) : null;
      if (prev != null) {
        if (prev instanceof Identifier && n instanceof FunctionConstructor
            && nChain.parent != null
            && nChain.parent.node instanceof FunctionDeclaration) {
          // Special case for FunctionDeclarations which look like this
          // FunctionDeclaration
          //   Identifier
          //   FunctionConstructor
          //     Identifier
          // with the FunctionConstructor having the same position as the
          // declaration which makes the identifier overlap with its sibling.
          assertEquals(msg, prev.getFilePosition(),
                       nChain.parent.cast(FunctionDeclaration.class).node
                       .getIdentifier().getFilePosition());
        } else {
          assertTrue(msg, (prev.getFilePosition().endCharInFile()
                           <= n.getFilePosition().startCharInFile()));
        }
      }
      // require that n encompass its children
      List<? extends ParseTreeNode> children = n.children();
      if (!children.isEmpty()) {
        ParseTreeNode first = children.get(0),
                       last = children.get(children.size() - 1);
        assertTrue(msg + " > " + first + " : " + first.getFilePosition(),
                   (first.getFilePosition().startCharInFile()
                    >= n.getFilePosition().startCharInFile()));
        assertTrue(msg + " < " + last + " : " + last.getFilePosition(),
                   (last.getFilePosition().endCharInFile()
                    <= n.getFilePosition().endCharInFile()));
      }
View Full Code Here


   * a new lexical scope.
   * @return true whether the given node introduces a new lexical scope.
   */
  @SuppressWarnings("static-method")
  protected boolean introducesScope(AncestorChain<?> ac) {
    ParseTreeNode node = ac.node;
    return node instanceof FunctionConstructor || node instanceof CatchStmt
        || node instanceof WithStmt;
  }
View Full Code Here

                CharProducer.Factory.fromString(
                    "var " + candidate + ";",
                    InputSource.UNKNOWN)),
            InputSource.UNKNOWN),
        mq);
    ParseTreeNode node;
    try { node = parser.parse(); } catch (ParseException e) { return false; }
    if (node == null || !mq.getMessages().isEmpty()) { return false; }
    Map<String, ParseTreeNode> bindings = Maps.newHashMap();
    if (!QuasiBuilder.match("{ var @p; }", node, bindings)) { return false; }
    if (bindings.size() != 1) { return false; }
View Full Code Here

    if ("javascript".equals(language) && "concat".equals(rendererType)) {
      return concat(inputs, output, logger);
    }

    boolean passed = true;
    ParseTreeNode outputJs;
    if ("caja".equals(language)) {
      throw new IllegalArgumentException("language=caja no longer supported");
    } else if ("javascript".equals(language)) {
      PluginMeta meta = new PluginMeta(fetcher, policy);
      passed = true;
      JsOptimizer optimizer = new JsOptimizer(mq);
      for (File f : inputs) {
        try {
          if (f.getName().endsWith(".env.json")) {
            loadEnvJsonFile(f, optimizer, mq);
          } else {
            ParseTreeNode parsedInput = new ParserContext(mq)
            .withInput(new InputSource(f.getCanonicalFile().toURI()))
            .withConfig(meta)
            .build();
            if (parsedInput != null) {
              optimizer.addInput((Statement) parsedInput);
View Full Code Here

            }
          }
        }
      }
      boolean isLoopy(AncestorChain<?> ac) {
        ParseTreeNode node = ac.node;
        return node instanceof ForEachLoop || node instanceof Loop;
      }
    };
    List<LexicalScope> scopes = sa.computeLexicalScopes(ac);
    LexicalScope globalScope = scopes.get(0);
View Full Code Here

            op.getPropertyNameNode().getFilePosition(),
            MessagePart.Factory.valueOf(key));
      }
    }
    if (Operation.is(node, Operator.MEMBER_ACCESS)) {
      ParseTreeNode right = node.children().get(1);
      if (right instanceof Reference) {
        String key = ((Reference) right).getIdentifierName();
        if (Keyword.isKeyword(key)) {
          mq.addMessage(LinterMessageType.BARE_KEYWORD,
              right.getFilePosition(),
              MessagePart.Factory.valueOf(key));
        }
      }
    }
    for (ParseTreeNode child : node.children()) {
View Full Code Here

          unk, StringLiteral.valueOf(unk, data.key), value));
    }

    ObjectConstructor cssSchema = new ObjectConstructor(unk, cssSchemaProps);

    ParseTreeNode js = QuasiBuilder.substV(
        ""
        + "var cssSchema = (function () {"
        + "  @poolDecls?;"
        + "  var schema = @cssSchema;"
        + "  if (@hasAliases) {"
        + "    for (var key in schema) {"
        + "      if ('string' === typeof schema[key]"
        + "          && Object.hasOwnProperty.call(schema, key)) {"
        + "        schema[key] = schema[schema[key]];"
        + "      }"
        + "    }"
        + "  }"
        + "  return schema;"
        + "})();",
        "poolDecls", poolDecls,
        "cssSchema", cssSchema,
        "hasAliases", new BooleanLiteral(unk, hasAliases));
    TokenConsumer tc = js.makeRenderer(out, null);
    js.render(new RenderContext(tc));
    tc.noMoreTokens();
    out.append(";\n");
  }
View Full Code Here

  protected void childrenChanged() {
    super.childrenChanged();
    if (children().size() != 1) {
      throw new IllegalStateException("TranslatedCode may only have one child");
    }
    ParseTreeNode module = children().get(0);
    if (!(module instanceof Statement)) {
      throw new ClassCastException("Expected block, not " + module);
    }
  }
View Full Code Here

      // Build a replacement, making sure to return the original if
      // no-optimization was done, so that we can easily tell whether or not we
      // need to do another optimization pass.
      ParseTreeNode[] newChildren = null;
      for (int i = 0; i < n; ++i) {
        ParseTreeNode child = children.get(i);
        optimize(
            s, child, i < fuzzyLimit, i < lhsLimit, i < touLimit,
            i < fnLimit, out);
        sb = addDigest(out.digest, sb);
        if (out.node != child) {
View Full Code Here

    List<ParseTreeNode> newChildren = null;
    if (i != 0) {
      newChildren = Lists.newArrayListWithCapacity(n);
    }
    while (i < n) {
      ParseTreeNode child = children.get(i);
      optimize(s, child, true, false, false, false, out);
      ParseTreeNode newChild = out.node;
      String newDigest = out.digest;
      Boolean optCond = (i & 1) == 0 && i + 1 < n
          ? ((Expression) newChild).conditionResult() : null;
      if (optCond != null || child != newChild) {
        if (newChildren == null) {
View Full Code Here

TOP

Related Classes of com.google.caja.parser.ParseTreeNode

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.