Debian Codegraph @

src/com/google/javascript/jscomp/JsAst.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Generates an AST for a JavaScript source file.
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 // While we're at it, clear out any saved text in the source file on
62 // the assumption that if we're dumping the parse tree, then we probably
63 // assume regenerating everything else is a smart idea also.
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 // There was a parse error or IOException, so use a dummy block.
99 root = IR.script();
100 } else {
101 compiler.prepareAst(root);
102 }
103
104 // Set the source name so that the compiler passes can track
105 // the source file and module.
106 root.setStaticSourceFile(sourceFile);
107 }
108 }