| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.deps; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.CheckLevel; |
| 21 |
import com.google.javascript.jscomp.DiagnosticType; |
| 22 |
import com.google.javascript.jscomp.ErrorManager; |
| 23 |
import com.google.javascript.jscomp.JSError; |
| 24 |
|
| 25 |
import java.io.BufferedReader; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.Reader; |
| 28 |
import java.util.List; |
| 29 |
import java.util.regex.Matcher; |
| 30 |
import java.util.regex.Pattern; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public abstract class JsFileLineParser { |
| 39 |
|
| 40 |
static final DiagnosticType PARSE_WARNING = DiagnosticType.warning( |
| 41 |
"DEPS_PARSE_WARNING", "{0}\n{1}"); |
| 42 |
static final DiagnosticType PARSE_ERROR = DiagnosticType.error( |
| 43 |
"DEPS_PARSE_ERROR", "{0}\n{1}"); |
| 44 |
|
| 45 |
boolean shortcutMode = false; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static class ParseException extends Exception { |
| 51 |
public static final long serialVersionUID = 1L; |
| 52 |
private boolean fatal; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public ParseException(String message, boolean fatal) { |
| 61 |
super(message); |
| 62 |
this.fatal = fatal; |
| 63 |
} |
| 64 |
|
| 65 |
public boolean isFatal() { |
| 66 |
return fatal; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
private static final Pattern STRING_LITERAL_PATTERN = Pattern.compile( |
| 72 |
"\\s*(?:'((?:\\\\'|[^'])*?)'|\"((?:\\\\\"|[^\"])*?)\")\\s*"); |
| 73 |
|
| 74 |
|
| 75 |
private Matcher valueMatcher = STRING_LITERAL_PATTERN.matcher(""); |
| 76 |
|
| 77 |
|
| 78 |
String filePath; |
| 79 |
|
| 80 |
int lineNum; |
| 81 |
|
| 82 |
ErrorManager errorManager; |
| 83 |
|
| 84 |
boolean parseSucceeded; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public JsFileLineParser(ErrorManager errorManager) { |
| 92 |
this.errorManager = errorManager; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setShortcutMode(boolean mode) { |
| 103 |
this.shortcutMode = mode; |
| 104 |
} |
| 105 |
|
| 106 |
public boolean didParseSucceed() { |
| 107 |
return parseSucceeded; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void doParse(String filePath, Reader fileContents) { |
| 120 |
this.filePath = filePath; |
| 121 |
parseSucceeded = true; |
| 122 |
|
| 123 |
BufferedReader lineBuffer = new BufferedReader(fileContents); |
| 124 |
|
| 125 |
|
| 126 |
String line = null; |
| 127 |
lineNum = 0; |
| 128 |
boolean inMultilineComment = false; |
| 129 |
|
| 130 |
try { |
| 131 |
while (null != (line = lineBuffer.readLine())) { |
| 132 |
++lineNum; |
| 133 |
try { |
| 134 |
String revisedLine = line; |
| 135 |
if (inMultilineComment) { |
| 136 |
int endOfComment = revisedLine.indexOf("*/"); |
| 137 |
if (endOfComment != -1) { |
| 138 |
revisedLine = revisedLine.substring(endOfComment + 2); |
| 139 |
inMultilineComment = false; |
| 140 |
} else { |
| 141 |
revisedLine = ""; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if (!inMultilineComment) { |
| 146 |
while (true) { |
| 147 |
int startOfLineComment = revisedLine.indexOf("//"); |
| 148 |
int startOfMultilineComment = revisedLine.indexOf("/*"); |
| 149 |
if (startOfLineComment != -1 && |
| 150 |
(startOfMultilineComment == -1 || |
| 151 |
startOfLineComment < startOfMultilineComment)) { |
| 152 |
revisedLine = revisedLine.substring(0, startOfLineComment); |
| 153 |
break; |
| 154 |
} else if (startOfMultilineComment != -1) { |
| 155 |
int endOfMultilineComment = revisedLine.indexOf("*/", |
| 156 |
startOfMultilineComment + 2); |
| 157 |
if (endOfMultilineComment == -1) { |
| 158 |
revisedLine = revisedLine.substring( |
| 159 |
0, startOfMultilineComment); |
| 160 |
inMultilineComment = true; |
| 161 |
break; |
| 162 |
} else { |
| 163 |
revisedLine = |
| 164 |
revisedLine.substring(0, startOfMultilineComment) + |
| 165 |
revisedLine.substring(endOfMultilineComment + 2); |
| 166 |
} |
| 167 |
} else { |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (!revisedLine.isEmpty()) { |
| 174 |
|
| 175 |
|
| 176 |
if (!parseLine(revisedLine) && shortcutMode) { |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
} catch (ParseException e) { |
| 181 |
|
| 182 |
errorManager.report( |
| 183 |
e.isFatal() ? CheckLevel.ERROR : CheckLevel.WARNING, |
| 184 |
JSError.make(filePath, lineNum, 0 , |
| 185 |
e.isFatal() ? PARSE_ERROR : PARSE_WARNING, |
| 186 |
e.getMessage(), line)); |
| 187 |
parseSucceeded = parseSucceeded && !e.isFatal(); |
| 188 |
} |
| 189 |
} |
| 190 |
} catch (IOException e) { |
| 191 |
errorManager.report(CheckLevel.ERROR, |
| 192 |
JSError.make(filePath, 0, 0 , |
| 193 |
PARSE_ERROR, "Error reading file: " + filePath)); |
| 194 |
parseSucceeded = false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
abstract boolean parseLine(String line) throws ParseException; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
String parseJsString(String jsStringLiteral) throws ParseException { |
| 215 |
valueMatcher.reset(jsStringLiteral); |
| 216 |
if (!valueMatcher.matches()) { |
| 217 |
throw new ParseException("Syntax error in JS String literal", true ); |
| 218 |
} |
| 219 |
return valueMatcher.group(1) != null ? valueMatcher.group(1) : valueMatcher.group(2); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
List<String> parseJsStringArray(String input) |
| 229 |
throws ParseException { |
| 230 |
List<String> results = Lists.newArrayList(); |
| 231 |
int indexStart = input.indexOf('['); |
| 232 |
int indexEnd = input.lastIndexOf(']'); |
| 233 |
if ((indexStart == -1) || (indexEnd == -1)) { |
| 234 |
throw new ParseException("Syntax error when parsing JS array", true ); |
| 235 |
} |
| 236 |
String innerValues = input.substring(indexStart + 1, indexEnd); |
| 237 |
|
| 238 |
if (!innerValues.trim().isEmpty()) { |
| 239 |
valueMatcher.reset(innerValues); |
| 240 |
for (;;) { |
| 241 |
|
| 242 |
if (!valueMatcher.lookingAt()) { |
| 243 |
throw new ParseException("Syntax error in JS String literal", true ); |
| 244 |
} |
| 245 |
|
| 246 |
results.add(valueMatcher.group(1) != null ? |
| 247 |
valueMatcher.group(1) : valueMatcher.group(2)); |
| 248 |
if (valueMatcher.hitEnd()) { |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
if (innerValues.charAt(valueMatcher.end()) != ',') { |
| 253 |
throw new ParseException("Missing comma in string array", true ); |
| 254 |
} |
| 255 |
|
| 256 |
valueMatcher.region(valueMatcher.end() + 1, valueMatcher.regionEnd()); |
| 257 |
} |
| 258 |
} |
| 259 |
return results; |
| 260 |
} |
| 261 |
} |