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

The following examples show how to use com.google.javascript.rhino.Node#addChildToBack() . 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: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * TypeExpressionList := TopLevelTypeExpression
 *     | TopLevelTypeExpression ',' TypeExpressionList
 */
private Node parseTypeExpressionList(JsDocToken token) {
  Node typeExpr = parseTopLevelTypeExpression(token);
  if (typeExpr == null) {
    return null;
  }
  Node typeList = new Node(Token.BLOCK);
  typeList.addChildToBack(typeExpr);
  while (match(JsDocToken.COMMA)) {
    next();
    skipEOLs();
    typeExpr = parseTopLevelTypeExpression(next());
    if (typeExpr == null) {
      return null;
    }
    typeList.addChildToBack(typeExpr);
  }
  return typeList;
}
 
Example 2
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 6 votes vote down vote up
@Override
Node processSwitchCase(SwitchCase caseNode) {
  Node node;
  if (caseNode.isDefault()) {
    node = newNode(Token.DEFAULT);
  } else {
    AstNode expr = caseNode.getExpression();
    node = newNode(Token.CASE, transform(expr));
  }
  Node block = newNode(Token.BLOCK);
  block.putBooleanProp(Node.SYNTHETIC_BLOCK_PROP, true);
  block.setLineno(caseNode.getLineno());
  block.setCharno(position2charno(caseNode.getAbsolutePosition()));
  if (caseNode.getStatements() != null) {
    for (AstNode child : caseNode.getStatements()) {
      block.addChildToBack(transform(child));
    }
  }
  node.addChildToBack(block);
  return node;
}
 
Example 3
Source File: jKali_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a VAR node containing the given name and initial value expression.
 */
static Node newVarNode(String name, Node value) {
  Node nodeName = IR.name(name);
  if (value != null) {
    Preconditions.checkState(value.getNext() == null);
    nodeName.addChildToBack(value);
    nodeName.srcref(value);
  }
  Node var = IR.var(nodeName).srcref(nodeName);

  return var;
}
 
Example 4
Source File: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * FieldType := FieldName | FieldName ':' TypeExpression
 */
private Node parseFieldType(JsDocToken token) {
  Node fieldName = parseFieldName(token);

  if (fieldName == null) {
    return null;
  }

  skipEOLs();
  if (!match(JsDocToken.COLON)) {
    return fieldName;
  }

  // Move to the colon.
  next();

  // Move to the token after the colon and parse
  // the type expression.
  skipEOLs();
  Node typeExpression = parseTypeExpression(next());

  if (typeExpression == null) {
    return null;
  }

  Node fieldType = newNode(Token.COLON);
  fieldType.addChildToBack(fieldName);
  fieldType.addChildToBack(typeExpression);
  return fieldType;
}
 
Example 5
Source File: jKali_0043_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Protect side-effect free nodes by making them parameters
 * to a extern function call.  This call will be removed
 * after all the optimizations passes have run.
 */
private void protectSideEffects() {
  if (!problemNodes.isEmpty()) {
    addExtern();
    for (Node n : problemNodes) {
      Node name = IR.name(PROTECTOR_FN).srcref(n);
      name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      Node replacement = IR.call(name).srcref(n);
      replacement.putBooleanProp(Node.FREE_CALL, true);
      n.getParent().replaceChild(n, replacement);
      replacement.addChildToBack(n);
    }
    compiler.reportCodeChange();
  }
}
 
Example 6
Source File: jKali_0034_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Protect side-effect free nodes by making them parameters
 * to a extern function call.  This call will be removed
 * after all the optimizations passes have run.
 */
private void protectSideEffects() {
  if (!problemNodes.isEmpty()) {
    addExtern();
    for (Node n : problemNodes) {
      Node name = IR.name(PROTECTOR_FN).srcref(n);
      name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      Node replacement = IR.call(name).srcref(n);
      replacement.putBooleanProp(Node.FREE_CALL, true);
      n.getParent().replaceChild(n, replacement);
      replacement.addChildToBack(n);
    }
    compiler.reportCodeChange();
  }
}
 
Example 7
Source File: Closure_81_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processForLoop(ForLoop loopNode) {
  Node node = newNode(
      Token.FOR,
      transform(loopNode.getInitializer()),
      transform(loopNode.getCondition()),
      transform(loopNode.getIncrement()));
  node.addChildToBack(transformBlock(loopNode.getBody()));
  return node;
}
 
Example 8
Source File: Closure_21_CheckSideEffects_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Protect side-effect free nodes by making them parameters
 * to a extern function call.  This call will be removed
 * after all the optimizations passes have run.
 */
private void protectSideEffects() {
  if (!problemNodes.isEmpty()) {
    addExtern();
    for (Node n : problemNodes) {
      Node name = IR.name(PROTECTOR_FN).srcref(n);
      name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      Node replacement = IR.call(name).srcref(n);
      replacement.putBooleanProp(Node.FREE_CALL, true);
      n.getParent().replaceChild(n, replacement);
      replacement.addChildToBack(n);
    }
    compiler.reportCodeChange();
  }
}
 
Example 9
Source File: ProcessTweaks.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a JS object that holds a map of tweakId -> default value override.
 */
private Node createCompilerDefaultValueOverridesVarNode(
    Node sourceInformationNode) {
  Node objNode = IR.objectlit().srcref(sourceInformationNode);
  for (Entry<String, Node> entry : compilerDefaultValueOverrides.entrySet()) {
    Node objKeyNode = IR.stringKey(entry.getKey())
        .copyInformationFrom(sourceInformationNode);
    Node objValueNode = entry.getValue().cloneNode()
        .copyInformationFrom(sourceInformationNode);
    objKeyNode.addChildToBack(objValueNode);
    objNode.addChildToBack(objKeyNode);
  }
  return objNode;
}
 
Example 10
Source File: Closure_37_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processVariableDeclaration(VariableDeclaration declarationNode) {
  if (!config.acceptConstKeyword && declarationNode.getType() ==
      com.google.javascript.rhino.head.Token.CONST) {
    processIllegalToken(declarationNode);
  }

  Node node = newNode(Token.VAR);
  for (VariableInitializer child : declarationNode.getVariables()) {
    node.addChildToBack(transform(child));
  }
  return node;
}
 
Example 11
Source File: Reader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void transformIdPatt(JsonML element, Node parent)
    throws JsonMLException {
  Node node = IR.name(
      getStringAttribute(element, TagAttr.NAME));
  setPosition(node);
  parent.addChildToBack(node);
}
 
Example 12
Source File: Closure_42_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processAstRoot(AstRoot rootNode) {
  Node node = newNode(Token.SCRIPT);
  for (com.google.javascript.rhino.head.Node child : rootNode) {
    node.addChildToBack(transform((AstNode)child));
  }
  parseDirectives(node);
  return node;
}
 
Example 13
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processContinueStatement(ContinueStatement statementNode) {
  Node node = newNode(Token.CONTINUE);
  if (statementNode.getLabel() != null) {
    Node labelName = transform(statementNode.getLabel());
    // Change the NAME to LABEL_NAME
    labelName.setType(Token.LABEL_NAME);
    node.addChildToBack(labelName);
  }
  return node;
}
 
Example 14
Source File: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ArrayType := '[' ElementTypeList ']'
 * ElementTypeList := <empty> | TypeExpression | '...' TypeExpression
 *     | TypeExpression ',' ElementTypeList
 */
private Node parseArrayType(JsDocToken token) {
  Node array = newNode(Token.LB);
  Node arg = null;
  boolean hasVarArgs = false;

  do {
    if (arg != null) {
      next();
      skipEOLs();
      token = next();
    }
    if (token == JsDocToken.ELLIPSIS) {
      arg = wrapNode(Token.ELLIPSIS, parseTypeExpression(next()));
      hasVarArgs = true;
    } else {
      arg = parseTypeExpression(token);
    }

    if (arg == null) {
      return null;
    }

    array.addChildToBack(arg);
    if (hasVarArgs) {
      break;
    }
    skipEOLs();
  } while (match(JsDocToken.COMMA));

  if (!match(JsDocToken.RB)) {
    return reportTypeSyntaxWarning("msg.jsdoc.missing.rb");
  }
  next();
  return array;
}
 
Example 15
Source File: jMutRepair_005_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Protect side-effect free nodes by making them parameters
 * to a extern function call.  This call will be removed
 * after all the optimizations passes have run.
 */
private void protectSideEffects() {
  if (!problemNodes.isEmpty()) {
    addExtern();
    for (Node n : problemNodes) {
      Node name = IR.name(PROTECTOR_FN).srcref(n);
      name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
      Node replacement = IR.call(name).srcref(n);
      replacement.putBooleanProp(Node.FREE_CALL, true);
      n.getParent().replaceChild(n, replacement);
      replacement.addChildToBack(n);
    }
    compiler.reportCodeChange();
  }
}
 
Example 16
Source File: Closure_37_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processAstRoot(AstRoot rootNode) {
  Node node = newNode(Token.SCRIPT);
  for (com.google.javascript.rhino.head.Node child : rootNode) {
    node.addChildToBack(transform((AstNode)child));
  }
  parseDirectives(node);
  return node;
}
 
Example 17
Source File: Closure_133_JsDocInfoParser_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * FieldType := FieldName | FieldName ':' TypeExpression
 */
private Node parseFieldType(JsDocToken token) {
  Node fieldName = parseFieldName(token);

  if (fieldName == null) {
    return null;
  }

  skipEOLs();
  if (!match(JsDocToken.COLON)) {
    return fieldName;
  }

  // Move to the colon.
  next();

  // Move to the token after the colon and parse
  // the type expression.
  skipEOLs();
  Node typeExpression = parseTypeExpression(next());

  if (typeExpression == null) {
    return null;
  }

  Node fieldType = newNode(Token.COLON);
  fieldType.addChildToBack(fieldName);
  fieldType.addChildToBack(typeExpression);
  return fieldType;
}
 
Example 18
Source File: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ParametersType := RestParameterType | NonRestParametersType
 *     | NonRestParametersType ',' RestParameterType
 * RestParameterType := '...' Identifier
 * NonRestParametersType := ParameterType ',' NonRestParametersType
 *     | ParameterType
 *     | OptionalParametersType
 * OptionalParametersType := OptionalParameterType
 *     | OptionalParameterType, OptionalParametersType
 * OptionalParameterType := ParameterType=
 * ParameterType := TypeExpression | Identifier ':' TypeExpression
 */
// NOTE(nicksantos): The official ES4 grammar forces optional and rest
// arguments to come after the required arguments. Our parser does not
// enforce this. Instead we allow them anywhere in the function at parse-time,
// and then warn about them during type resolution.
//
// In theory, it might be mathematically nicer to do the order-checking here.
// But in practice, the order-checking for structural functions is exactly
// the same as the order-checking for @param annotations. And the latter
// has to happen during type resolution. Rather than duplicate the
// order-checking in two places, we just do all of it in type resolution.
private Node parseParametersType(JsDocToken token) {
  Node paramsType = newNode(Token.LP);
  boolean isVarArgs = false;
  Node paramType = null;
  if (token != JsDocToken.RP) {
    do {
      if (paramType != null) {
        // skip past the comma
        next();
        skipEOLs();
        token = next();
      }

      if (token == JsDocToken.ELLIPSIS) {
        // In the latest ES4 proposal, there are no type constraints allowed
        // on variable arguments. We support the old syntax for backwards
        // compatibility, but we should gradually tear it out.
        skipEOLs();
        if (match(JsDocToken.RP)) {
          paramType = newNode(Token.ELLIPSIS);
        } else {
          skipEOLs();
          if (!match(JsDocToken.LB)) {
            return reportTypeSyntaxWarning("msg.jsdoc.missing.lb");
          }

          next();
          skipEOLs();
          paramType = wrapNode(Token.ELLIPSIS, parseTypeExpression(next()));
          skipEOLs();
          if (!match(JsDocToken.RB)) {
            return reportTypeSyntaxWarning("msg.jsdoc.missing.rb");
          }
          skipEOLs();
          next();
        }

        isVarArgs = true;
      } else {
        paramType = parseTypeExpression(token);
        if (match(JsDocToken.EQUALS)) {
          skipEOLs();
          next();
          paramType = wrapNode(Token.EQUALS, paramType);
        }
      }

      if (paramType == null) {
        return null;
      }
      paramsType.addChildToBack(paramType);
      if (isVarArgs) {
        break;
      }
    } while (match(JsDocToken.COMMA));
  }

  if (isVarArgs && match(JsDocToken.COMMA)) {
    return reportTypeSyntaxWarning("msg.jsdoc.function.varargs");
  }

  // The right paren will be checked by parseFunctionType

  return paramsType;
}
 
Example 19
Source File: Closure_32_JsDocInfoParser_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Create a new union type, with an alternate that has already been
 * parsed. The alternate may be null.
 */
private Node parseUnionTypeWithAlternate(JsDocToken token, Node alternate) {
  Node union = newNode(Token.PIPE);
  if (alternate != null) {
    union.addChildToBack(alternate);
  }

  Node expr = null;
  do {
    if (expr != null) {
      skipEOLs();
      token = next();
      Preconditions.checkState(
          token == JsDocToken.PIPE || token == JsDocToken.COMMA);

      boolean isPipe = token == JsDocToken.PIPE;
      if (isPipe && match(JsDocToken.PIPE)) {
        // We support double pipes for backwards compatiblity.
        next();
      }
      skipEOLs();
      token = next();
    }
    expr = parseTypeExpression(token);
    if (expr == null) {
      return null;
    }

    union.addChildToBack(expr);
    // We support commas for backwards compatiblity.
  } while (match(JsDocToken.PIPE, JsDocToken.COMMA));

  if (alternate == null) {
    skipEOLs();
    if (!match(JsDocToken.RP)) {
      return reportTypeSyntaxWarning("msg.jsdoc.missing.rp");
    }
    next();
  }
  return union;
}
 
Example 20
Source File: Nopol2017_0025_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * FunctionType := 'function' FunctionSignatureType
 * FunctionSignatureType :=
 *    TypeParameters '(' 'this' ':' TypeName, ParametersType ')' ResultType
 */
private Node parseFunctionType(JsDocToken token) {
  // NOTE(nicksantos): We're not implementing generics at the moment, so
  // just throw out TypeParameters.
  if (token != JsDocToken.LP) {
    restoreLookAhead(token);
    return reportTypeSyntaxWarning("msg.jsdoc.missing.lp");
  }

  Node functionType = newNode(Token.FUNCTION);
  Node parameters = null;
  skipEOLs();
  if (!match(JsDocToken.RP)) {
    token = next();

    boolean hasParams = true;
    if (token == JsDocToken.STRING) {
      String tokenStr = stream.getString();
      boolean isThis = "this".equals(tokenStr);
      boolean isNew = "new".equals(tokenStr);
      if (isThis || isNew) {
        if (match(JsDocToken.COLON)) {
          next();
          skipEOLs();
          Node contextType = wrapNode(
              isThis ? Token.THIS : Token.NEW,
              parseTypeName(next()));
          if (contextType == null) {
            return null;
          }

          functionType.addChildToFront(contextType);
        } else {
          return reportTypeSyntaxWarning("msg.jsdoc.missing.colon");
        }

        if (match(JsDocToken.COMMA)) {
          next();
          skipEOLs();
          token = next();
        } else {
          hasParams = false;
        }
      }
    }

    if (hasParams) {
      parameters = parseParametersType(token);
      if (parameters == null) {
        return null;
      }
    }
  }

  if (parameters != null) {
    functionType.addChildToBack(parameters);
  }

  skipEOLs();
  if (!match(JsDocToken.RP)) {
    return reportTypeSyntaxWarning("msg.jsdoc.missing.rp");
  }

  skipEOLs();
  Node resultType = parseResultType(next());
  if (resultType == null) {
    return null;
  } else {
    functionType.addChildToBack(resultType);
  }
  return functionType;
}