Java Code Examples for com.google.javascript.rhino.Node#setIsSyntheticBlock()

The following examples show how to use com.google.javascript.rhino.Node#setIsSyntheticBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Reader.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void transformCase(JsonML element, Node parent)
    throws JsonMLException {

  Node node = createNode(Token.CASE, element);
  parent.addChildToBack(node);

  // the first element represents case id
  JsonML child = element.getChild(0);
  transformElement(child, node);

  // always insert an extra BLOCK node
  Node block = IR.block();
  block.setIsSyntheticBlock(true);
  node.addChildToBack(block);

  transformAllChildrenFromIndex(element, block, 1, true);
}
 
Example 2
Source File: CodePrinterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
Node parse(String js, boolean checkTypes) {
  Compiler compiler = new Compiler();
  CompilerOptions options = new CompilerOptions();
  options.setTrustedStrings(trustedStrings);

  // Allow getters and setters.
  options.setLanguageIn(LanguageMode.ECMASCRIPT5);
  compiler.initOptions(options);
  Node n = compiler.parseTestCode(js);

  if (checkTypes) {
    DefaultPassConfig passConfig = new DefaultPassConfig(null);
    CompilerPass typeResolver = passConfig.resolveTypes.create(compiler);
    Node externs = new Node(Token.SCRIPT);
    externs.setInputId(new InputId("externs"));
    Node externAndJsRoot = new Node(Token.BLOCK, externs, n);
    externAndJsRoot.setIsSyntheticBlock(true);
    typeResolver.process(externs, n);
    CompilerPass inferTypes = passConfig.inferTypes.create(compiler);
    inferTypes.process(externs, n);
  }

  checkUnexpectedErrorsOrWarnings(compiler, 0);
  return n;
}
 
Example 3
Source File: Reader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void transformDefaultCase(JsonML element, Node parent)
    throws JsonMLException {
  Node node = createNode(Token.DEFAULT_CASE, element);
  parent.addChildToBack(node);

  // the first child represent body
  Node block = IR.block();
  block.setIsSyntheticBlock(true);
  node.addChildToBack(block);

  transformAllChildren(element, block, true);
}
 
Example 4
Source File: CreateSyntheticBlocks.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param marker The marker to add synthetic blocks for.
 */
private void addBlocks(Marker marker) {
  // Add block around the template section so that it looks like this:
  //  BLOCK (synthetic)
  //    START
  //      BLOCK (synthetic)
  //        BODY
  //    END
  // This prevents the start or end markers from mingling with the code
  // in the block body.


  Node originalParent = marker.endMarker.getParent();
  Node outerBlock = IR.block();
  outerBlock.setIsSyntheticBlock(true);
  originalParent.addChildBefore(outerBlock, marker.startMarker);

  Node innerBlock = IR.block();
  innerBlock.setIsSyntheticBlock(true);
  // Move everything after the start Node up to the end Node into the inner
  // block.
  moveSiblingExclusive(originalParent, innerBlock,
      marker.startMarker,
      marker.endMarker);

  // Add the start node.
  outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));
  // Add the inner block
  outerBlock.addChildToBack(innerBlock);
  // and finally the end node.
  outerBlock.addChildToBack(originalParent.removeChildAfter(outerBlock));

  compiler.reportCodeChange();
}
 
Example 5
Source File: NodeUtilTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testRemoveChildBlock() {
  // Test removing the inner block.
  Node actual = parse("{{x()}}");

  Node outerBlockNode = actual.getFirstChild();
  Node innerBlockNode = outerBlockNode.getFirstChild();
  innerBlockNode.setIsSyntheticBlock(true);

  NodeUtil.removeChild(outerBlockNode, innerBlockNode);
  String expected = "{{}}";
  String difference = parse(expected).checkTreeEquals(actual);
  if (difference != null) {
    assertTrue("Nodes do not match:\n" + difference, false);
  }
}
 
Example 6
Source File: ParserRunner.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses the JavaScript text given by a reader.
 *
 * @param sourceString Source code from the file.
 * @param errorReporter An error.
 * @param logger A logger.
 * @return The AST of the given text.
 * @throws IOException
 */
public static ParseResult parse(StaticSourceFile sourceFile,
                                String sourceString,
                                Config config,
                                ErrorReporter errorReporter,
                                Logger logger) throws IOException {
  Context cx = Context.enter();
  cx.setErrorReporter(errorReporter);
  cx.setLanguageVersion(Context.VERSION_1_5);
  CompilerEnvirons compilerEnv = new CompilerEnvirons();
  compilerEnv.initFromContext(cx);
  compilerEnv.setRecordingComments(true);
  compilerEnv.setRecordingLocalJsDocComments(true);

  // ES5 specifically allows trailing commas
  compilerEnv.setWarnTrailingComma(
      config.languageMode == LanguageMode.ECMASCRIPT3);

  // Do our own identifier check for ECMASCRIPT 5
  boolean acceptEs5 =
      config.isIdeMode || config.languageMode != LanguageMode.ECMASCRIPT3;
  compilerEnv.setReservedKeywordAsIdentifier(acceptEs5);

  compilerEnv.setAllowMemberExprAsFunctionName(false);
  compilerEnv.setIdeMode(config.isIdeMode);
  compilerEnv.setRecoverFromErrors(config.isIdeMode);

  Parser p = new Parser(compilerEnv, errorReporter);
  AstRoot astRoot = null;
  try {
    astRoot = p.parse(sourceString, sourceFile.getName(), 1);
  } catch (EvaluatorException e) {
    logger.info(
        "Error parsing " + sourceFile.getName() + ": " + e.getMessage());
  } finally {
    Context.exit();
  }
  Node root = null;
  if (astRoot != null) {
    root = IRFactory.transformTree(
        astRoot, sourceFile, sourceString, config, errorReporter);
    root.setIsSyntheticBlock(true);
  }
  return new ParseResult(root, astRoot);
}
 
Example 7
Source File: SpecializeModule.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an AST that consists solely of copies of the input roots for the
 * passed in module.
 *
 * Also records a map in {@link #functionInfoBySpecializedFunctionNode}
 * of information about the original function keyed on the copies of the
 * functions to specialized.
 */
private Node copyModuleInputs(JSModule module) {

  specializedInputRootsByOriginal = Maps.newLinkedHashMap();

  functionInfoBySpecializedFunctionNode = Maps.newLinkedHashMap();

  Node syntheticModuleJsRoot = IR.block();
  syntheticModuleJsRoot.setIsSyntheticBlock(true);

  for (CompilerInput input : module.getInputs()) {
    Node originalInputRoot = input.getAstRoot(compiler);

    Node copiedInputRoot = originalInputRoot.cloneTree();
    copiedInputRoot.copyInformationFromForTree(originalInputRoot);

    specializedInputRootsByOriginal.put(originalInputRoot,
        copiedInputRoot);

    matchTopLevelFunctions(originalInputRoot, copiedInputRoot);

    syntheticModuleJsRoot.addChildToBack(copiedInputRoot);
  }

  // The jsRoot needs a parent (in a normal compilation this would be the
  // node that contains jsRoot and the externs).
  Node syntheticExternsAndJsRoot = IR.block();
  syntheticExternsAndJsRoot.addChildToBack(syntheticModuleJsRoot);

  return syntheticModuleJsRoot;
}