Debian Codegraph @

src/com/google/javascript/jscomp/deps/JsFileParser.java

raw symbols SCIP
1 /*
2 * Copyright 2008 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.deps;
18
19 import com.google.common.base.CharMatcher;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.ErrorManager;
22
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.util.List;
28 import java.util.logging.Logger;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 /**
33 * A parser that can extract goog.require() and goog.provide() dependency
34 * information from a .js file.
35 *
36 * @author [email protected] (Andrew Grieve)
37 */
38 public class JsFileParser extends JsFileLineParser {
39
40 private static Logger logger = Logger.getLogger(JsFileParser.class.getName());
41
42 /** Pattern for matching goog.provide(*) and goog.require(*). */
43 private static final Pattern GOOG_PROVIDE_REQUIRE_PATTERN = Pattern.compile(
44 "(?:^|;)\\s*goog\\.(provide|require|addDependency)\\s*\\((.*?)\\)");
45
46 /** The first non-comment line of base.js */
47 private static final String BASE_JS_START = "var COMPILED = false;";
48
49 /** Matchers used in the parsing. */
50 private Matcher googMatcher = GOOG_PROVIDE_REQUIRE_PATTERN.matcher("");
51
52 /** The info for the file we are currently parsing. */
53 private List<String> provides;
54 private List<String> requires;
55 private boolean fileHasProvidesOrRequires;
56
57 /** Whether to provide/require the root namespace. */
58 private boolean includeGoogBase = false;
59
60 /**
61 * Constructor
62 *
63 * @param errorManager Handles parse errors.
64 */
65 public JsFileParser(ErrorManager errorManager) {
66 super(errorManager);
67 }
68
69 /**
70 * Sets whether we should create implicit provides and requires of the
71 * root namespace.
72 *
73 * When generating deps files, you do not want this behavior. Deps files
74 * need base.js to run anyway, so they don't need information about it.
75 *
76 * When generating abstract build graphs, you probably do want this behavior.
77 * It will create an implicit dependency of all files with provides/requires
78 * on base.js.
79 *
80 * @return this for easy chaining.
81 */
82 public JsFileParser setIncludeGoogBase(boolean include) {
83 includeGoogBase = include;
84 return this;
85 }
86
87 /**
88 * Parses the given file and returns the dependency information that it
89 * contained.
90 *
91 * @param filePath Path to the file to parse.
92 * @param closureRelativePath Path of the file relative to closure.
93 * @return A DependencyInfo containing all provides/requires found in the
94 * file.
95 * @throws IOException Thrown if there was an problem reading the given file.
96 */
97 public DependencyInfo parseFile(String filePath, String closureRelativePath)
98 throws IOException {
99 return parseReader(filePath, closureRelativePath, new FileReader(filePath));
100 }
101
102 /**
103 * Parses the given file and returns the dependency information that it
104 * contained.
105 *
106 * @param filePath Path to the file to parse.
107 * @param closureRelativePath Path of the file relative to closure.
108 * @param fileContents The contents to parse.
109 * @return A DependencyInfo containing all provides/requires found in the
110 * file.
111 */
112 public DependencyInfo parseFile(String filePath, String closureRelativePath,
113 String fileContents) {
114 return parseReader(filePath, closureRelativePath,
115 new StringReader(fileContents));
116 }
117
118 private DependencyInfo parseReader(String filePath,
119 String closureRelativePath, Reader fileContents) {
120 provides = Lists.newArrayList();
121 requires = Lists.newArrayList();
122 fileHasProvidesOrRequires = false;
123
124 logger.fine("Parsing Source: " + filePath);
125 doParse(filePath, fileContents);
126
127 DependencyInfo dependencyInfo = new SimpleDependencyInfo(
128 closureRelativePath, filePath, provides, requires);
129 logger.fine("DepInfo: " + dependencyInfo);
130 return dependencyInfo;
131 }
132
133 /**
134 * Parses a line of JavaScript, extracting goog.provide and goog.require
135 * information.
136 */
137 @Override
138 protected boolean parseLine(String line) throws ParseException {
139 boolean lineHasProvidesOrRequires = false;
140
141 // Quick sanity check that will catch most cases. This is a performance
142 // win for people with a lot of JS.
143 if (line.indexOf("provide") != -1 ||
144 line.indexOf("require") != -1 ||
145 line.indexOf("addDependency") != -1) {
146 // Iterate over the provides/requires.
147 googMatcher.reset(line);
148 while (googMatcher.find()) {
149 lineHasProvidesOrRequires = true;
150
151 if (includeGoogBase && !fileHasProvidesOrRequires) {
152 fileHasProvidesOrRequires = true;
153 requires.add("goog");
154 }
155
156 // See if it's a require or provide.
157 char firstChar = googMatcher.group(1).charAt(0);
158 boolean isProvide = firstChar == 'p';
159 boolean isRequire = firstChar == 'r';
160
161 if (isProvide || isRequire) {
162 // Parse the param.
163 String arg = parseJsString(googMatcher.group(2));
164
165 // Add the dependency.
166 if (isRequire) {
167 // goog is always implicit.
168 // TODO(nicksantos): I'm pretty sure we don't need this anymore.
169 // Remove this later.
170 if (!"goog".equals(arg)) {
171 requires.add(arg);
172 }
173 } else {
174 provides.add(arg);
175 }
176 }
177 }
178 } else if (includeGoogBase && line.startsWith(BASE_JS_START) &&
179 provides.isEmpty() && requires.isEmpty()) {
180 provides.add("goog");
181
182 // base.js can't provide or require anything else.
183 return false;
184 }
185
186 return !shortcutMode || lineHasProvidesOrRequires ||
187 CharMatcher.WHITESPACE.matchesAllOf(line);
188 }
189 }