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

The following examples show how to use com.google.javascript.rhino.Node#setLineno() . 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: IRFactory.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
Node processLabeledStatement(LabeledStatement statementNode) {
  Node node = newNode(Token.LABEL);
  Node prev = null;
  Node cur = node;
  for (Label label : statementNode.getLabels()) {
    if (prev != null) {
      prev.addChildToBack(cur);
    }
    cur.addChildToBack(transform(label));

    cur.setLineno(label.getLineno());
    maybeSetLengthFrom(cur, label);

    int clauseAbsolutePosition =
        position2charno(label.getAbsolutePosition());
    cur.setCharno(clauseAbsolutePosition);

    prev = cur;
    cur = newNode(Token.LABEL);
  }
  prev.addChildToBack(transform(statementNode.getStatement()));
  return node;
}
 
Example 2
Source File: Closure_81_IRFactory_s.java    From coming with MIT License 6 votes vote down vote up
@Override
Node processLabeledStatement(LabeledStatement statementNode) {
  Node node = newNode(Token.LABEL);
  Node prev = null;
  Node cur = node;
  for (Label label : statementNode.getLabels()) {
    if (prev != null) {
      prev.addChildToBack(cur);
    }
    cur.addChildToBack(transform(label));

    cur.setLineno(label.getLineno());
    int clauseAbsolutePosition =
        position2charno(label.getAbsolutePosition());
    cur.setCharno(clauseAbsolutePosition);

    prev = cur;
    cur = newNode(Token.LABEL);
  }
  prev.addChildToBack(transform(statementNode.getStatement()));
  return node;
}
 
Example 3
Source File: Closure_81_IRFactory_t.java    From coming with MIT License 6 votes vote down vote up
@Override
Node processLabeledStatement(LabeledStatement statementNode) {
  Node node = newNode(Token.LABEL);
  Node prev = null;
  Node cur = node;
  for (Label label : statementNode.getLabels()) {
    if (prev != null) {
      prev.addChildToBack(cur);
    }
    cur.addChildToBack(transform(label));

    cur.setLineno(label.getLineno());
    int clauseAbsolutePosition =
        position2charno(label.getAbsolutePosition());
    cur.setCharno(clauseAbsolutePosition);

    prev = cur;
    cur = newNode(Token.LABEL);
  }
  prev.addChildToBack(transform(statementNode.getStatement()));
  return node;
}
 
Example 4
Source File: Closure_42_IRFactory_s.java    From coming with MIT License 6 votes vote down vote up
@Override
Node processRegExpLiteral(RegExpLiteral literalNode) {
  Node literalStringNode = newStringNode(literalNode.getValue());
  // assume it's on the same line.
  literalStringNode.setLineno(literalNode.getLineno());
  maybeSetLengthFrom(literalStringNode, literalNode);
  Node node = newNode(Token.REGEXP, literalStringNode);
  String flags = literalNode.getFlags();
  if (flags != null && !flags.isEmpty()) {
    Node flagsNode = newStringNode(flags);
    // Assume the flags are on the same line as the literal node.
    flagsNode.setLineno(literalNode.getLineno());
    maybeSetLengthFrom(flagsNode, literalNode);
    node.addChildToBack(flagsNode);
  }
  return node;
}
 
Example 5
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 6 votes vote down vote up
private Node transformBlock(AstNode node) {
  Node irNode = transform(node);
  if (!irNode.isBlock()) {
    if (irNode.isEmpty()) {
      irNode.setType(Token.BLOCK);
      irNode.setWasEmptyNode(true);
    } else {
      Node newBlock = newNode(Token.BLOCK, irNode);
      newBlock.setLineno(irNode.getLineno());
      newBlock.setCharno(irNode.getCharno());
      maybeSetLengthFrom(newBlock, node);
      irNode = newBlock;
    }
  }
  return irNode;
}
 
Example 6
Source File: Closure_81_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processTryStatement(TryStatement statementNode) {
  Node node = newNode(Token.TRY,
      transformBlock(statementNode.getTryBlock()));
  Node block = newNode(Token.BLOCK);
  boolean lineSet = false;

  for (CatchClause cc : statementNode.getCatchClauses()) {
    // Mark the enclosing block at the same line as the first catch
    // clause.
    if (lineSet == false) {
        block.setLineno(cc.getLineno());
        lineSet = true;
    }
    block.addChildToBack(transform(cc));
  }
  node.addChildToBack(block);

  AstNode finallyBlock = statementNode.getFinallyBlock();
  if (finallyBlock != null) {
    node.addChildToBack(transformBlock(finallyBlock));
  }

  // If we didn't set the line on the catch clause, then
  // we've got an empty catch clause.  Set its line to be the same
  // as the finally block (to match Old Rhino's behavior.)
  if ((lineSet == false) && (finallyBlock != null)) {
    block.setLineno(finallyBlock.getLineno());
  }

  return node;
}
 
Example 7
Source File: Closure_42_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processTryStatement(TryStatement statementNode) {
  Node node = newNode(Token.TRY,
      transformBlock(statementNode.getTryBlock()));
  Node block = newNode(Token.BLOCK);
  boolean lineSet = false;

  for (CatchClause cc : statementNode.getCatchClauses()) {
    // Mark the enclosing block at the same line as the first catch
    // clause.
    if (lineSet == false) {
      block.setLineno(cc.getLineno());
      maybeSetLengthFrom(block, cc);
      lineSet = true;
    }
    block.addChildToBack(transform(cc));
  }
  node.addChildToBack(block);

  AstNode finallyBlock = statementNode.getFinallyBlock();
  if (finallyBlock != null) {
    node.addChildToBack(transformBlock(finallyBlock));
  }

  // If we didn't set the line on the catch clause, then
  // we've got an empty catch clause.  Set its line to be the same
  // as the finally block (to match Old Rhino's behavior.)
  if ((lineSet == false) && (finallyBlock != null)) {
    block.setLineno(finallyBlock.getLineno());
    maybeSetLengthFrom(block, finallyBlock);
  }

  return node;
}
 
Example 8
Source File: Closure_81_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processRegExpLiteral(RegExpLiteral literalNode) {
  Node literalStringNode = newStringNode(literalNode.getValue());
  // assume it's on the same line.
  literalStringNode.setLineno(literalNode.getLineno());
  Node node = newNode(Token.REGEXP, literalStringNode);
  String flags = literalNode.getFlags();
  if (flags != null && !flags.isEmpty()) {
    Node flagsNode = newStringNode(flags);
    // Assume the flags are on the same line as the literal node.
    flagsNode.setLineno(literalNode.getLineno());
    node.addChildToBack(flagsNode);
  }
  return node;
}
 
Example 9
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processFunctionCall(FunctionCall callNode) {
  Node node = newNode(transformTokenType(callNode.getType()),
                       transform(callNode.getTarget()));
  for (AstNode child : callNode.getArguments()) {
    node.addChildToBack(transform(child));
  }

  int leftParamPos = callNode.getAbsolutePosition() + callNode.getLp();
  node.setLineno(callNode.getLineno());
  node.setCharno(position2charno(leftParamPos));
  return node;
}
 
Example 10
Source File: Closure_81_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processFunctionCall(FunctionCall callNode) {
  Node node = newNode(transformTokenType(callNode.getType()),
                       transform(callNode.getTarget()));
  for (AstNode child : callNode.getArguments()) {
    node.addChildToBack(transform(child));
  }

  int leftParamPos = callNode.getAbsolutePosition() + callNode.getLp();
  node.setLineno(callNode.getLineno());
  node.setCharno(position2charno(leftParamPos));
  return node;
}
 
Example 11
Source File: Closure_84_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processVariableInitializer(VariableInitializer initializerNode) {
  Node node = transform(initializerNode.getTarget());
  if (initializerNode.getInitializer() != null) {
    node.addChildToBack(transform(initializerNode.getInitializer()));
    node.setLineno(node.getLineno());
  }
  return node;
}
 
Example 12
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processVariableInitializer(VariableInitializer initializerNode) {
  Node node = transform(initializerNode.getTarget());
  if (initializerNode.getInitializer() != null) {
    node.addChildToBack(transform(initializerNode.getInitializer()));
    node.setLineno(node.getLineno());
  }
  return node;
}
 
Example 13
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processNewExpression(NewExpression exprNode) {
  Node node = newNode(
      transformTokenType(exprNode.getType()),
      transform(exprNode.getTarget()));
  for (AstNode child : exprNode.getArguments()) {
    node.addChildToBack(transform(child));
  }
  node.setLineno(exprNode.getLineno());
  node.setCharno(position2charno(exprNode.getAbsolutePosition()));
  maybeSetLengthFrom(node, exprNode);
  return node;
}
 
Example 14
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processInfixExpression(InfixExpression exprNode) {
  Node n =  newNode(
      transformTokenType(exprNode.getType()),
      transform(exprNode.getLeft()),
      transform(exprNode.getRight()));
  n.setLineno(exprNode.getLineno());
  n.setCharno(position2charno(exprNode.getAbsolutePosition()));
  maybeSetLengthFrom(n, exprNode);
  return n;
}
 
Example 15
Source File: Closure_84_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processTryStatement(TryStatement statementNode) {
  Node node = newNode(Token.TRY,
      transformBlock(statementNode.getTryBlock()));
  Node block = newNode(Token.BLOCK);
  boolean lineSet = false;

  for (CatchClause cc : statementNode.getCatchClauses()) {
    // Mark the enclosing block at the same line as the first catch
    // clause.
    if (lineSet == false) {
        block.setLineno(cc.getLineno());
        lineSet = true;
    }
    block.addChildToBack(transform(cc));
  }
  node.addChildToBack(block);

  AstNode finallyBlock = statementNode.getFinallyBlock();
  if (finallyBlock != null) {
    node.addChildToBack(transformBlock(finallyBlock));
  }

  // If we didn't set the line on the catch clause, then
  // we've got an empty catch clause.  Set its line to be the same
  // as the finally block (to match Old Rhino's behavior.)
  if ((lineSet == false) && (finallyBlock != null)) {
    block.setLineno(finallyBlock.getLineno());
  }

  return node;
}
 
Example 16
Source File: IRFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
Node processPropertyGet(PropertyGet getNode) {
  Node leftChild = transform(getNode.getTarget());
  Node newNode = newNode(
      Token.GETPROP, leftChild, transformAsString(getNode.getProperty()));
  newNode.setLineno(leftChild.getLineno());
  newNode.setCharno(leftChild.getCharno());
  maybeSetLengthFrom(newNode, getNode);
  return newNode;
}
 
Example 17
Source File: Closure_37_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processFunctionCall(FunctionCall callNode) {
  Node node = newNode(transformTokenType(callNode.getType()),
                       transform(callNode.getTarget()));
  for (AstNode child : callNode.getArguments()) {
    node.addChildToBack(transform(child));
  }

  node.setLineno(node.getFirstChild().getLineno());
  node.setCharno(node.getFirstChild().getCharno());
  maybeSetLengthFrom(node, callNode);
  return node;
}
 
Example 18
Source File: Closure_42_IRFactory_s.java    From coming with MIT License 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    lp.addChildToBack(transform(param));
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 19
Source File: Closure_42_IRFactory_t.java    From coming with MIT License 4 votes vote down vote up
@Override
Node processFunctionNode(FunctionNode functionNode) {
  Name name = functionNode.getFunctionName();
  Boolean isUnnamedFunction = false;
  if (name == null) {
    int functionType = functionNode.getFunctionType();
    if (functionType != FunctionNode.FUNCTION_EXPRESSION) {
      errorReporter.error(
        "unnamed function statement",
        sourceName,
        functionNode.getLineno(), "", 0);

      // Return the bare minimum to put the AST in a valid state.
      return newNode(Token.EXPR_RESULT, Node.newNumber(0));
    }
    name = new Name();
    name.setIdentifier("");
    isUnnamedFunction = true;
  }
  Node node = newNode(Token.FUNCTION);
  Node newName = transform(name);
  if (isUnnamedFunction) {
    // Old Rhino tagged the empty name node with the line number of the
    // declaration.
    newName.setLineno(functionNode.getLineno());
    // TODO(bowdidge) Mark line number of paren correctly.
    // Same problem as below - the left paren might not be on the
    // same line as the function keyword.
    int lpColumn = functionNode.getAbsolutePosition() +
        functionNode.getLp();
    newName.setCharno(position2charno(lpColumn));
    maybeSetLengthFrom(newName, name);
  }

  node.addChildToBack(newName);
  Node lp = newNode(Token.PARAM_LIST);
  // The left paren's complicated because it's not represented by an
  // AstNode, so there's nothing that has the actual line number that it
  // appeared on.  We know the paren has to appear on the same line as the
  // function name (or else a semicolon will be inserted.)  If there's no
  // function name, assume the paren was on the same line as the function.
  // TODO(bowdidge): Mark line number of paren correctly.
  Name fnName = functionNode.getFunctionName();
  if (fnName != null) {
    lp.setLineno(fnName.getLineno());
  } else {
    lp.setLineno(functionNode.getLineno());
  }
  int lparenCharno = functionNode.getLp() +
      functionNode.getAbsolutePosition();

  lp.setCharno(position2charno(lparenCharno));
  for (AstNode param : functionNode.getParams()) {
    lp.addChildToBack(transform(param));
  }
  node.addChildToBack(lp);

  Node bodyNode = transform(functionNode.getBody());
  parseDirectives(bodyNode);
  node.addChildToBack(bodyNode);
 return node;
}
 
Example 20
Source File: Reader.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Sets position for a node which refers to a particular JsonML element.
 * The position says which number (in pre-order) has the corresponding
 * JsonML element in the tree.
 */
private void setPosition(Node node) {
  node.setLineno(nodeIndex);
}