Debian Codegraph @

src/com/google/javascript/jscomp/parsing/ParserRunner.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.parsing;
18
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Sets;
21 import com.google.javascript.jscomp.parsing.Config.LanguageMode;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.head.CompilerEnvirons;
24 import com.google.javascript.rhino.head.Context;
25 import com.google.javascript.rhino.head.ErrorReporter;
26 import com.google.javascript.rhino.head.EvaluatorException;
27 import com.google.javascript.rhino.head.Parser;
28 import com.google.javascript.rhino.head.ast.AstRoot;
29 import com.google.javascript.rhino.jstype.StaticSourceFile;
30
31 import java.io.IOException;
32 import java.util.HashSet;
33 import java.util.ResourceBundle;
34 import java.util.Set;
35 import java.util.logging.Logger;
36
37 public class ParserRunner {
38
39 private static final String configResource =
40 "com.google.javascript.jscomp.parsing.ParserConfig";
41
42 private static Set<String> annotationNames = null;
43
44 private static Set<String> suppressionNames = null;
45 private static Set<String> reservedVars = null;
46
47 // Should never need to instantiate class of static methods.
48 private ParserRunner() {}
49
50 @Deprecated
51 public static Config createConfig(boolean isIdeMode) {
52 return createConfig(isIdeMode, LanguageMode.ECMASCRIPT3, false);
53 }
54
55 public static Config createConfig(boolean isIdeMode,
56 LanguageMode languageMode,
57 boolean acceptConstKeyword) {
58 return createConfig(isIdeMode, languageMode, acceptConstKeyword, null);
59 }
60
61 public static Config createConfig(boolean isIdeMode,
62 LanguageMode languageMode,
63 boolean acceptConstKeyword,
64 Set<String> extraAnnotationNames) {
65 initResourceConfig();
66 Set<String> effectiveAnnotationNames;
67 if (extraAnnotationNames == null) {
68 effectiveAnnotationNames = annotationNames;
69 } else {
70 effectiveAnnotationNames = new HashSet<String>(annotationNames);
71 effectiveAnnotationNames.addAll(extraAnnotationNames);
72 }
73 return new Config(effectiveAnnotationNames, suppressionNames,
74 isIdeMode, languageMode, acceptConstKeyword);
75 }
76
77 public static Set<String> getReservedVars() {
78 initResourceConfig();
79 return reservedVars;
80 }
81
82 private static synchronized void initResourceConfig() {
83 if (annotationNames != null) {
84 return;
85 }
86
87 ResourceBundle config = ResourceBundle.getBundle(configResource);
88 annotationNames = extractList(config.getString("jsdoc.annotations"));
89 suppressionNames = extractList(config.getString("jsdoc.suppressions"));
90 reservedVars = extractList(config.getString("compiler.reserved.vars"));
91 }
92
93 private static Set<String> extractList(String configProp) {
94 String[] names = configProp.split(",");
95 Set<String> trimmedNames = Sets.newHashSet();
96 for (String name : names) {
97 trimmedNames.add(name.trim());
98 }
99 return ImmutableSet.copyOf(trimmedNames);
100 }
101
102 /**
103 * Parses the JavaScript text given by a reader.
104 *
105 * @param sourceString Source code from the file.
106 * @param errorReporter An error.
107 * @param logger A logger.
108 * @return The AST of the given text.
109 * @throws IOException
110 */
111 public static ParseResult parse(StaticSourceFile sourceFile,
112 String sourceString,
113 Config config,
114 ErrorReporter errorReporter,
115 Logger logger) throws IOException {
116 Context cx = Context.enter();
117 cx.setErrorReporter(errorReporter);
118 cx.setLanguageVersion(Context.VERSION_1_5);
119 CompilerEnvirons compilerEnv = new CompilerEnvirons();
120 compilerEnv.initFromContext(cx);
121 compilerEnv.setRecordingComments(true);
122 compilerEnv.setRecordingLocalJsDocComments(true);
123
124 // ES5 specifically allows trailing commas
125 compilerEnv.setWarnTrailingComma(
126 config.languageMode == LanguageMode.ECMASCRIPT3);
127
128 // Do our own identifier check for ECMASCRIPT 5
129 boolean acceptEs5 =
130 config.isIdeMode || config.languageMode != LanguageMode.ECMASCRIPT3;
131 compilerEnv.setReservedKeywordAsIdentifier(acceptEs5);
132
133 compilerEnv.setAllowMemberExprAsFunctionName(false);
134 compilerEnv.setIdeMode(config.isIdeMode);
135 compilerEnv.setRecoverFromErrors(config.isIdeMode);
136
137 Parser p = new Parser(compilerEnv, errorReporter);
138 AstRoot astRoot = null;
139 try {
140 astRoot = p.parse(sourceString, sourceFile.getName(), 1);
141 } catch (EvaluatorException e) {
142 logger.info(
143 "Error parsing " + sourceFile.getName() + ": " + e.getMessage());
144 } finally {
145 Context.exit();
146 }
147 Node root = null;
148 if (astRoot != null) {
149 root = IRFactory.transformTree(
150 astRoot, sourceFile, sourceString, config, errorReporter);
151 root.setIsSyntheticBlock(true);
152 }
153 return new ParseResult(root, astRoot);
154 }
155
156 /**
157 * Holds results of parsing. Includes both ast formats.
158 */
159 public static class ParseResult {
160 public final Node ast;
161 public final AstRoot oldAst;
162
163 public ParseResult(Node ast, AstRoot oldAst) {
164 this.ast = ast;
165 this.oldAst = oldAst;
166 }
167 }
168 }