com.google.javascript.rhino.InputId Java Examples

The following examples show how to use com.google.javascript.rhino.InputId. 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: GlobalVarReferenceMap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Finds the range of references associated to {@code sourceName}. Note that
 * even if there is no sourceName references the returned information can be
 * used to decide where to insert new sourceName refs.
 */
private SourceRefRange findSourceRefRange(List<Reference> refList,
    InputId inputId) {
  Preconditions.checkNotNull(inputId);

  // TODO(bashir): We can do binary search here, but since this is fast enough
  // right now, we just do a linear search for simplicity.
  int lastBefore = -1;
  int firstAfter = refList.size();
  int index = 0;

  Preconditions.checkState(inputOrder.containsKey(inputId), inputId.getIdName());
  int sourceInputOrder = inputOrder.get(inputId);
  for (Reference ref : refList) {
    Preconditions.checkNotNull(ref.getInputId());
    int order = inputOrder.get(ref.getInputId());
    if (order < sourceInputOrder) {
      lastBefore = index;
    } else if (order > sourceInputOrder) {
      firstAfter = index;
      break;
    }
    index++;
  }
  return new SourceRefRange(refList, lastBefore, firstAfter);
}
 
Example #2
Source File: GlobalVarReferenceMap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void replaceReferences(String varName, InputId inputId,
    ReferenceCollection newSourceCollection) {
  ReferenceCollection combined = new ReferenceCollection();
  List<Reference> combinedRefs = combined.references;
  ReferenceCollection oldCollection = refMap.get(varName);
  refMap.put(varName, combined);
  if (oldCollection == null) {
    combinedRefs.addAll(newSourceCollection.references);
    return;
  }
  // otherwise replace previous references that are from sourceName
  SourceRefRange range = findSourceRefRange(oldCollection.references,
    inputId);
  combinedRefs.addAll(range.refsBefore());
  combinedRefs.addAll(newSourceCollection.references);
  combinedRefs.addAll(range.refsAfter());
}
 
Example #3
Source File: GlobalVarReferenceMap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void removeScriptReferences(InputId inputId) {
  Preconditions.checkNotNull(inputId);

  if (!inputOrder.containsKey(inputId)) {
    return; // Input did not exist when last computed, so skip
  }
  // TODO(bashir): If this is too slow it is not too difficult to make it
  // faster with keeping an index for variables accessed in sourceName.
  for (ReferenceCollection collection : refMap.values()) {
    if (collection == null) {
      continue;
    }
    List<Reference> oldRefs = collection.references;
    SourceRefRange range = findSourceRefRange(oldRefs, inputId);
    List<Reference> newRefs = Lists.newArrayList(range.refsBefore());
    newRefs.addAll(range.refsAfter());
    collection.references = newRefs;
  }
}
 
Example #4
Source File: GlobalVarReferenceMap.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Updates the internal reference map based on the provided parameters. If
 * {@code scriptRoot} is not SCRIPT, it basically replaces the internal map
 * with the new one, otherwise it replaces all the information associated to
 * the given script.
 *
 * @param refMapPatch The reference map result of a
 *     {@link ReferenceCollectingCallback} pass which might be collected from
 *     the whole AST or just a sub-tree associated to a SCRIPT node.
 * @param root AST sub-tree root on which reference collection was done.
 */
void updateGlobalVarReferences(Map<Var, ReferenceCollection>
    refMapPatch, Node root) {
  if (refMap == null || !root.isScript()) {
    resetGlobalVarReferences(refMapPatch);
    return;
  }

  InputId inputId = root.getInputId();
  Preconditions.checkNotNull(inputId);
  // Note there are two assumptions here (i) the order of compiler inputs
  // has not changed and (ii) all references are in the order they appear
  // in AST (this is enforced in ReferenceCollectionCallback).
  removeScriptReferences(inputId);
  for (Entry<Var, ReferenceCollection> entry : refMapPatch.entrySet()) {
    Var var = entry.getKey();
    if (var.isGlobal()) {
      replaceReferences(var.getName(), inputId, entry.getValue());
    }
  }
}
 
Example #5
Source File: AstValidatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testValidScript() {
  Node n = new Node(Token.SCRIPT);
  expectInvalid(n, Check.SCRIPT);
  n.setInputId(new InputId("something_input"));
  n.setStaticSourceFile(new SimpleSourceFile("something", false));
  expectValid(n, Check.SCRIPT);
  expectInvalid(n, Check.STATEMENT);
  expectInvalid(n, Check.EXPRESSION);
}
 
Example #6
Source File: Cardumen_00149_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #7
Source File: jMutRepair_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #8
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #9
Source File: jMutRepair_003_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #10
Source File: Cardumen_00149_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #11
Source File: Compiler.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace a source input dynamically. Intended for incremental
 * re-compilation.
 *
 * If the new source input doesn't parse, then keep the old input
 * in the AST and return false.
 *
 * @return Whether the new AST was attached successfully.
 */
boolean replaceIncrementalSourceAst(JsAst ast) {
  CompilerInput oldInput = getInput(ast.getInputId());
  Preconditions.checkNotNull(oldInput, "No input to replace: %s", ast.getInputId().getIdName());
  Node newRoot = ast.getAstRoot(this);
  if (newRoot == null) {
    return false;
  }

  Node oldRoot = oldInput.getAstRoot(this);
  if (oldRoot != null) {
    oldRoot.getParent().replaceChild(oldRoot, newRoot);
  } else {
    getRoot().getLastChild().addChildToBack(newRoot);
  }

  CompilerInput newInput = new CompilerInput(ast);
  putCompilerInput(ast.getInputId(), newInput);

  JSModule module = oldInput.getModule();
  if (module != null) {
    module.addAfter(newInput, oldInput);
    module.remove(oldInput);
  }

  // Verify the input id is set properly.
  Preconditions.checkState(
      newInput.getInputId().equals(oldInput.getInputId()));
  InputId inputIdOnAst = newInput.getAstRoot(this).getInputId();
  Preconditions.checkState(newInput.getInputId().equals(inputIdOnAst));

  inputs.remove(oldInput);
  return true;
}
 
Example #12
Source File: Closure_18_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace a source input dynamically. Intended for incremental
 * re-compilation.
 *
 * If the new source input doesn't parse, then keep the old input
 * in the AST and return false.
 *
 * @return Whether the new AST was attached successfully.
 */
boolean replaceIncrementalSourceAst(JsAst ast) {
  CompilerInput oldInput = getInput(ast.getInputId());
  Preconditions.checkNotNull(oldInput, "No input to replace: %s", ast.getInputId().getIdName());
  Node newRoot = ast.getAstRoot(this);
  if (newRoot == null) {
    return false;
  }

  Node oldRoot = oldInput.getAstRoot(this);
  if (oldRoot != null) {
    oldRoot.getParent().replaceChild(oldRoot, newRoot);
  } else {
    getRoot().getLastChild().addChildToBack(newRoot);
  }

  CompilerInput newInput = new CompilerInput(ast);
  putCompilerInput(ast.getInputId(), newInput);

  JSModule module = oldInput.getModule();
  if (module != null) {
    module.addAfter(newInput, oldInput);
    module.remove(oldInput);
  }

  // Verify the input id is set properly.
  Preconditions.checkState(
      newInput.getInputId().equals(oldInput.getInputId()));
  InputId inputIdOnAst = newInput.getAstRoot(this).getInputId();
  Preconditions.checkState(newInput.getInputId().equals(inputIdOnAst));

  inputs.remove(oldInput);
  return true;
}
 
Example #13
Source File: Closure_18_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
@Override
SourceFile getSourceFileByName(String sourceName) {
  // Here we assume that the source name is the input name, this
  // is try of JavaScript parsed from source.
  if (sourceName != null) {
    CompilerInput input = inputsById.get(new InputId(sourceName));
    if (input != null) {
      return input.getSourceFile();
    }
  }
  return null;
}
 
Example #14
Source File: Closure_18_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Adds a new Script AST to the compile state. If a script for the same file
 * already exists the script will not be added, instead a call to
 * #replaceScript should be used.
 *
 * @param ast the ast of the new file
 */
public void addNewScript(JsAst ast) {
  if (!addNewSourceAst(ast)) {
    return;
  }
  Node emptyScript = new Node(Token.SCRIPT);
  InputId inputId = ast.getInputId();
  emptyScript.setInputId(inputId);
  emptyScript.setStaticSourceFile(
      SourceFile.fromCode(inputId.getIdName(), ""));

  processNewScript(ast, emptyScript);
}
 
Example #15
Source File: Closure_120_ReferenceCollectingCallback_s.java    From coming with MIT License 5 votes vote down vote up
private Reference(Node nameNode,
    BasicBlock basicBlock, Scope scope, InputId inputId) {
  this.nameNode = nameNode;
  this.basicBlock = basicBlock;
  this.scope = scope;
  this.inputId = inputId;
  this.sourceFile = nameNode.getStaticSourceFile();
}
 
Example #16
Source File: Closure_120_ReferenceCollectingCallback_t.java    From coming with MIT License 5 votes vote down vote up
private Reference(Node nameNode,
    BasicBlock basicBlock, Scope scope, InputId inputId) {
  this.nameNode = nameNode;
  this.basicBlock = basicBlock;
  this.scope = scope;
  this.inputId = inputId;
  this.sourceFile = nameNode.getStaticSourceFile();
}
 
Example #17
Source File: LinkCommentsForOneFile.java    From clutz with MIT License 5 votes vote down vote up
private boolean linkFunctionArgs(Node n, int line) {
  if (n.getNext() == null) {
    // the last argument in the call, attach the comment here
    linkCommentBufferToNode(n);
  } else {
    int endOfComment = getCurrentComment().location.end.column;
    int startOfNextNode = n.getNext().getCharno();
    if (endOfComment < startOfNextNode) {
      // the comment is between this node and the next node, so check which side of the comment
      // the comma
      // separating the arguments is on to decide which argument to attach it to.
      String lineContents =
          compiler.getInput(new InputId(n.getSourceFileName())).getSourceFile().getLine(line);
      // nextNode could be on a different line.
      int searchForCommaUntil =
          getCurrentComment().location.end.line == n.getNext().getLineno()
              ? startOfNextNode
              : lineContents.length();
      String interval = lineContents.substring(endOfComment, searchForCommaUntil);
      if (interval.contains(",")) {
        linkCommentBufferToNode(n);
      } else {
        linkCommentBufferToNode(n.getNext());
      }
    } else {
      // comment is after this node, keep traversing the node graph
      return true;
    }
  }
  return false;
}
 
Example #18
Source File: Closure_31_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Removes an input file from AST.
 * @param id The id of the input to be removed.
 */
protected void removeExternInput(InputId id) {
  CompilerInput input = getInput(id);
  if (input == null) {
    return;
  }
  Preconditions.checkState(input.isExtern(), "Not an extern input: %s", input.getName());
  inputsById.remove(id);
  externs.remove(input);
  Node root = input.getAstRoot(this);
  if (root != null) {
    root.detachFromParent();
  }
}
 
Example #19
Source File: Closure_31_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace a source input dynamically. Intended for incremental
 * re-compilation.
 *
 * If the new source input doesn't parse, then keep the old input
 * in the AST and return false.
 *
 * @return Whether the new AST was attached successfully.
 */
boolean replaceIncrementalSourceAst(JsAst ast) {
  CompilerInput oldInput = getInput(ast.getInputId());
  Preconditions.checkNotNull(oldInput, "No input to replace: %s", ast.getInputId().getIdName());
  Node newRoot = ast.getAstRoot(this);
  if (newRoot == null) {
    return false;
  }

  Node oldRoot = oldInput.getAstRoot(this);
  if (oldRoot != null) {
    oldRoot.getParent().replaceChild(oldRoot, newRoot);
  } else {
    getRoot().getLastChild().addChildToBack(newRoot);
  }

  CompilerInput newInput = new CompilerInput(ast);
  putCompilerInput(ast.getInputId(), newInput);

  JSModule module = oldInput.getModule();
  if (module != null) {
    module.addAfter(newInput, oldInput);
    module.remove(oldInput);
  }

  // Verify the input id is set properly.
  Preconditions.checkState(
      newInput.getInputId().equals(oldInput.getInputId()));
  InputId inputIdOnAst = newInput.getAstRoot(this).getInputId();
  Preconditions.checkState(newInput.getInputId().equals(inputIdOnAst));

  inputs.remove(oldInput);
  return true;
}
 
Example #20
Source File: Closure_31_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
private SourceFile getSourceFileByName(String sourceName) {
  // Here we assume that the source name is the input name, this
  // is try of javascript parsed from source.
  if (sourceName != null) {
    CompilerInput input = inputsById.get(new InputId(sourceName));
    if (input != null) {
      return input.getSourceFile();
    }
  }
  return null;
}
 
Example #21
Source File: Closure_31_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Adds a new Script AST to the compile state. If a script for the same file
 * already exists the script will not be added, instead a call to
 * #replaceScript should be used.
 *
 * @param ast the ast of the new file
 */
public void addNewScript(JsAst ast) {
  if (!addNewSourceAst(ast)) {
    return;
  }
  Node emptyScript = new Node(Token.SCRIPT);
  InputId inputId = ast.getInputId();
  emptyScript.setInputId(inputId);
  emptyScript.setStaticSourceFile(
      SourceFile.fromCode(inputId.getIdName(), ""));

  processNewScript(ast, emptyScript);
}
 
Example #22
Source File: CompilerInput.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public CompilerInput(SourceAst ast, InputId inputId, boolean isExtern) {
  this.ast = ast;
  this.id = inputId;

  // TODO(nicksantos): Add a precondition check here. People are passing
  // in null, but they should not be.
  if (ast != null && ast.getSourceFile() != null) {
    ast.getSourceFile().setIsExtern(isExtern);
  }
}
 
Example #23
Source File: Closure_31_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Removes an input file from AST.
 * @param id The id of the input to be removed.
 */
protected void removeExternInput(InputId id) {
  CompilerInput input = getInput(id);
  if (input == null) {
    return;
  }
  Preconditions.checkState(input.isExtern(), "Not an extern input: %s", input.getName());
  inputsById.remove(id);
  externs.remove(input);
  Node root = input.getAstRoot(this);
  if (root != null) {
    root.detachFromParent();
  }
}
 
Example #24
Source File: ReferenceCollectingCallback.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Reference(Node nameNode,
    BasicBlock basicBlock, Scope scope, InputId inputId) {
  this.nameNode = nameNode;
  this.basicBlock = basicBlock;
  this.scope = scope;
  this.inputId = inputId;
  this.sourceFile = nameNode.getStaticSourceFile();
}
 
Example #25
Source File: Closure_31_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replace a source input dynamically. Intended for incremental
 * re-compilation.
 *
 * If the new source input doesn't parse, then keep the old input
 * in the AST and return false.
 *
 * @return Whether the new AST was attached successfully.
 */
boolean replaceIncrementalSourceAst(JsAst ast) {
  CompilerInput oldInput = getInput(ast.getInputId());
  Preconditions.checkNotNull(oldInput, "No input to replace: %s", ast.getInputId().getIdName());
  Node newRoot = ast.getAstRoot(this);
  if (newRoot == null) {
    return false;
  }

  Node oldRoot = oldInput.getAstRoot(this);
  if (oldRoot != null) {
    oldRoot.getParent().replaceChild(oldRoot, newRoot);
  } else {
    getRoot().getLastChild().addChildToBack(newRoot);
  }

  CompilerInput newInput = new CompilerInput(ast);
  putCompilerInput(ast.getInputId(), newInput);

  JSModule module = oldInput.getModule();
  if (module != null) {
    module.addAfter(newInput, oldInput);
    module.remove(oldInput);
  }

  // Verify the input id is set properly.
  Preconditions.checkState(
      newInput.getInputId().equals(oldInput.getInputId()));
  InputId inputIdOnAst = newInput.getAstRoot(this).getInputId();
  Preconditions.checkState(newInput.getInputId().equals(inputIdOnAst));

  inputs.remove(oldInput);
  return true;
}
 
Example #26
Source File: Closure_31_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
private SourceFile getSourceFileByName(String sourceName) {
  // Here we assume that the source name is the input name, this
  // is try of javascript parsed from source.
  if (sourceName != null) {
    CompilerInput input = inputsById.get(new InputId(sourceName));
    if (input != null) {
      return input.getSourceFile();
    }
  }
  return null;
}
 
Example #27
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #28
Source File: Closure_31_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Adds a new Script AST to the compile state. If a script for the same file
 * already exists the script will not be added, instead a call to
 * #replaceScript should be used.
 *
 * @param ast the ast of the new file
 */
public void addNewScript(JsAst ast) {
  if (!addNewSourceAst(ast)) {
    return;
  }
  Node emptyScript = new Node(Token.SCRIPT);
  InputId inputId = ast.getInputId();
  emptyScript.setInputId(inputId);
  emptyScript.setStaticSourceFile(
      SourceFile.fromCode(inputId.getIdName(), ""));

  processNewScript(ast, emptyScript);
}
 
Example #29
Source File: Closure_10_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}
 
Example #30
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param n The node.
 * @return The InputId property on the node or its ancestors.
 */
public static InputId getInputId(Node n) {
  while (n != null && !n.isScript()) {
    n = n.getParent();
  }

  return (n != null && n.isScript()) ? n.getInputId() : null;
}