ErrorManager.java revision 953:221a84ef44c0
1224133Sdim/* 2224133Sdim * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3224133Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4224133Sdim * 5224133Sdim * This code is free software; you can redistribute it and/or modify it 6224133Sdim * under the terms of the GNU General Public License version 2 only, as 7224133Sdim * published by the Free Software Foundation. Oracle designates this 8224133Sdim * particular file as subject to the "Classpath" exception as provided 9224133Sdim * by Oracle in the LICENSE file that accompanied this code. 10224133Sdim * 11224133Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 12224133Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13224133Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14224133Sdim * version 2 for more details (a copy is included in the LICENSE file that 15224133Sdim * accompanied this code). 16224133Sdim * 17226633Sdim * You should have received a copy of the GNU General Public License version 18224133Sdim * 2 along with this work; if not, write to the Free Software Foundation, 19224133Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20224133Sdim * 21226633Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22226633Sdim * or visit www.oracle.com if you need additional information or have any 23226633Sdim * questions. 24226633Sdim */ 25226633Sdim 26239462Sdimpackage jdk.nashorn.internal.runtime; 27224133Sdim 28226633Sdimimport static jdk.nashorn.internal.runtime.ECMAErrors.rangeError; 29224133Sdim 30226633Sdimimport java.io.PrintWriter; 31224133Sdimimport jdk.nashorn.internal.parser.Token; 32224133Sdim 33224133Sdim/** 34224133Sdim * Handles JavaScript error reporting. 35239462Sdim */ 36224133Sdimpublic class ErrorManager { 37224133Sdim // TODO - collect and sort/collapse error messages. 38224133Sdim // TODO - property based error messages. 39224133Sdim /** Reporting writer. */ 40224133Sdim private final PrintWriter writer; 41224133Sdim 42224133Sdim /** Error count. */ 43224133Sdim private int errors; 44226633Sdim 45239462Sdim /** Warning count */ 46226633Sdim private int warnings; 47226633Sdim 48226633Sdim /** Limit of the number of messages. */ 49226633Sdim private int limit; 50226633Sdim 51234353Sdim /** Treat warnings as errors. */ 52234353Sdim private boolean warningsAsErrors; 53234353Sdim 54234353Sdim /** 55226633Sdim * Constructor 56226633Sdim */ 57226633Sdim public ErrorManager() { 58226633Sdim this(new PrintWriter(System.err, true)); //bootstrapping, context may not be initialized 59226633Sdim } 60226633Sdim 61224133Sdim /** 62224133Sdim * Constructor. 63224133Sdim * @param writer I/O writer to report on. 64224133Sdim */ 65224133Sdim public ErrorManager(final PrintWriter writer) { 66224133Sdim this.writer = writer; 67224133Sdim this.limit = 100; 68224133Sdim this.warningsAsErrors = false; 69224133Sdim } 70224133Sdim 71224133Sdim /** 72224133Sdim * Check to see if number of errors exceed limit. 73224133Sdim */ 74224133Sdim private void checkLimit() { 75224133Sdim int count = errors; 76224133Sdim 77224133Sdim if (warningsAsErrors) { 78 count += warnings; 79 } 80 81 if (limit != 0 && count > limit) { 82 throw rangeError("too.many.errors", Integer.toString(limit)); 83 } 84 } 85 86 /** 87 * Format an error message to include source and line information. 88 * @param message Error message string. 89 * @param source Source file information. 90 * @param line Source line number. 91 * @param column Source column number. 92 * @param token Offending token descriptor. 93 * @return formatted string 94 */ 95 public static String format(final String message, final Source source, final int line, final int column, final long token) { 96 final String eoln = System.lineSeparator(); 97 final int position = Token.descPosition(token); 98 final StringBuilder sb = new StringBuilder(); 99 100 // Source description and message. 101 sb.append(source.getName()). 102 append(':'). 103 append(line). 104 append(':'). 105 append(column). 106 append(' '). 107 append(message). 108 append(eoln); 109 110 // Source content. 111 final String sourceLine = source.getSourceLine(position); 112 sb.append(sourceLine).append(eoln); 113 114 // Pointer to column. 115 for (int i = 0; i < column; i++) { 116 if (sourceLine.charAt(i) == '\t') { 117 sb.append('\t'); 118 } else { 119 sb.append(' '); 120 } 121 } 122 123 sb.append('^'); 124 // Use will append eoln. 125 // buffer.append(eoln); 126 127 return sb.toString(); 128 } 129 130 /** 131 * Report an error using information provided by the ParserException 132 * 133 * @param e ParserException object 134 */ 135 136 public void error(final ParserException e) { 137 error(e.getMessage()); 138 } 139 140 /** 141 * Report an error message provided 142 * 143 * @param message Error message string. 144 */ 145 public void error(final String message) { 146 writer.println(message); 147 writer.flush(); 148 errors++; 149 checkLimit(); 150 } 151 152 /** 153 * Report a warning using information provided by the ParserException 154 * 155 * @param e ParserException object 156 */ 157 public void warning(final ParserException e) { 158 warning(e.getMessage()); 159 } 160 161 /** 162 * Report a warning message provided 163 * 164 * @param message Error message string. 165 */ 166 public void warning(final String message) { 167 writer.println(message); 168 writer.flush(); 169 warnings++; 170 checkLimit(); 171 } 172 173 /** 174 * Test to see if errors have occurred. 175 * @return True if errors. 176 */ 177 public boolean hasErrors() { 178 return errors != 0; 179 } 180 181 /** 182 * Get the message limit 183 * @return max number of messages 184 */ 185 public int getLimit() { 186 return limit; 187 } 188 189 /** 190 * Set the message limit 191 * @param limit max number of messages 192 */ 193 public void setLimit(final int limit) { 194 this.limit = limit; 195 } 196 197 /** 198 * Check whether warnings should be treated like errors 199 * @return true if warnings should be treated like errors 200 */ 201 public boolean isWarningsAsErrors() { 202 return warningsAsErrors; 203 } 204 205 /** 206 * Set warnings to be treated as errors 207 * @param warningsAsErrors true if warnings should be treated as errors, false otherwise 208 */ 209 public void setWarningsAsErrors(final boolean warningsAsErrors) { 210 this.warningsAsErrors = warningsAsErrors; 211 } 212 213 /** 214 * Get the number of errors 215 * @return number of errors 216 */ 217 public int getNumberOfErrors() { 218 return errors; 219 } 220 221 /** 222 * Get number of warnings 223 * @return number of warnings 224 */ 225 public int getNumberOfWarnings() { 226 return warnings; 227 } 228 229 /** 230 * Clear warnings and error count. 231 */ 232 void reset() { 233 warnings = 0; 234 errors = 0; 235 } 236} 237