| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
|
| 21 |
import com.google.javascript.jscomp.parsing.ParserRunner; |
| 22 |
|
| 23 |
import com.google.javascript.rhino.IR; |
| 24 |
import com.google.javascript.rhino.InputId; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
import java.io.IOException; |
| 27 |
|
| 28 |
import java.util.logging.Logger; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class JsAst implements SourceAst { |
| 35 |
private static final Logger logger_ = Logger.getLogger(JsAst.class.getName()); |
| 36 |
private static final long serialVersionUID = 1L; |
| 37 |
|
| 38 |
private transient InputId inputId; |
| 39 |
private transient SourceFile sourceFile; |
| 40 |
private String fileName; |
| 41 |
private Node root; |
| 42 |
|
| 43 |
public JsAst(SourceFile sourceFile) { |
| 44 |
this.inputId = new InputId(sourceFile.getName()); |
| 45 |
this.sourceFile = sourceFile; |
| 46 |
this.fileName = sourceFile.getName(); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public Node getAstRoot(AbstractCompiler compiler) { |
| 51 |
if (root == null) { |
| 52 |
parse(compiler); |
| 53 |
root.setInputId(inputId); |
| 54 |
} |
| 55 |
return root; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void clearAst() { |
| 60 |
root = null; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
sourceFile.clearCachedSource(); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public InputId getInputId() { |
| 69 |
return inputId; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public SourceFile getSourceFile() { |
| 74 |
return sourceFile; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void setSourceFile(SourceFile file) { |
| 79 |
Preconditions.checkState(fileName.equals(file.getName())); |
| 80 |
sourceFile = file; |
| 81 |
} |
| 82 |
|
| 83 |
private void parse(AbstractCompiler compiler) { |
| 84 |
try { |
| 85 |
logger_.fine("Parsing: " + sourceFile.getName()); |
| 86 |
ParserRunner.ParseResult result = ParserRunner.parse(sourceFile, sourceFile.getCode(), |
| 87 |
compiler.getParserConfig(), |
| 88 |
compiler.getDefaultErrorReporter(), |
| 89 |
logger_); |
| 90 |
root = result.ast; |
| 91 |
compiler.setOldParseTree(sourceFile.getName(), result.oldAst); |
| 92 |
} catch (IOException e) { |
| 93 |
compiler.report( |
| 94 |
JSError.make(AbstractCompiler.READ_ERROR, sourceFile.getName())); |
| 95 |
} |
| 96 |
|
| 97 |
if (root == null || compiler.hasHaltingErrors()) { |
| 98 |
|
| 99 |
root = IR.script(); |
| 100 |
} else { |
| 101 |
compiler.prepareAst(root); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
root.setStaticSourceFile(sourceFile); |
| 107 |
} |
| 108 |
} |