View Javadoc
1 /* 2 * $Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/ListingAnalyzer.java,v 1.1 2003/11/07 16:57:27 schaefep Exp $ 3 * 4 * Copyright 2002 Riege Software International, All Rights Reserved. 5 * 6 * This software is the proprietary information of Riege Software International 7 * Use is subject to license terms. 8 * 9 */ 10 package net.sf.plb4jedit.plb; 11 12 import java.io.*; 13 import java.util.HashMap; 14 import java.util.Vector; 15 16 import org.apache.regexp.RE; 17 import org.apache.regexp.RESyntaxException; 18 /*** 19 * $Log: ListingAnalyzer.java,v $ 20 * Revision 1.1 2003/11/07 16:57:27 schaefep 21 * initial ps 22 * 23 * Revision 1.13 2003/05/27 09:34:09 schaefer 24 * disable debug info 25 * 26 * Revision 1.12 2003/05/27 09:31:08 schaefer 27 * fix: recognize error position, e.g. varnames again. 28 * Don't trim line! 29 * 30 * Revision 1.11 2003/05/08 10:52:18 schaefer 31 * recognize if Listing is empty 32 * 33 * Revision 1.10 2003/05/06 12:32:48 schaefer 34 * no debug output 35 * 36 * Revision 1.9 2003/03/27 15:39:57 msk 37 * organized imports, add seaport master data 38 * 39 * Revision 1.8 2002/12/06 07:50:43 schaefer 40 * hot fix: recognize FATAL ERRORs 41 * 42 * Revision 1.7 2002/05/17 09:40:53 brink 43 * corrected ifLoopSwitchNotTerminatedRE 44 * 45 * Revision 1.6 2002/04/30 13:32:25 schaefer 46 * recognize # in undefined variable 47 * 48 * Revision 1.5 2002/04/30 13:00:54 schaefer 49 * locate error position 50 * 51 * Revision 1.4 2002/04/25 12:56:22 brink 52 * added support for "if/loop/switch statement not terminated" 53 * 54 * Revision 1.3 2002/04/02 15:36:24 schaefer 55 * fix: handle undefined execution label 56 * 57 * Revision 1.2 2002/04/02 08:20:41 schaefer 58 * fix in locating includes 59 * 60 * Revision 1.1 2002/03/29 15:07:25 schaefer 61 * program to analyze compile output from plbcmp 62 * 63 * Analyze listings created by the plb compiler using options PE 999999 64 * Compiling a source xxxxxx.dbs with these options produce a xxxxxx.lst in 65 * the directory where the xxxxxx.plc is stored. 66 * @autor Peter Schaefer 67 */ 68 public class ListingAnalyzer { 69 public final static String cvsId = "$Header: /cvsroot/plb4jedit/plb4jedit/Plb/src/java/net/sf/plb4jedit/plb/ListingAnalyzer.java,v 1.1 2003/11/07 16:57:27 schaefep Exp $"; 70 /*** 71 * stores the original listing; each entry one line of the listing as String 72 */ 73 private Vector listing; 74 /*** 75 * stores include key from listing (e.g. AD) with the appropriate include (filename, absolute path) 76 */ 77 private HashMap includes; 78 79 private Vector errors; 80 private Vector warnings; 81 private String programName; 82 private boolean DEBUG=false; 83 private int errorNo; 84 85 private RE includeRE = null; 86 private RE errorRE = null; 87 private RE warningRE = null; 88 private RE locateErrorRE = null; 89 private RE undefinedExecutionLabelRE = null; 90 private RE ifLoopSwitchNotTerminatedRE = null; 91 private RE lastWordRE = null; 92 private RE symbolRE = null; 93 private RE fatalErrorRE = null; 94 /*----------------------------------------------------------------------------------*/ 95 96 /*** 97 * avoid construction without an listing 98 */ 99 private ListingAnalyzer(){} 100 101 public ListingAnalyzer(String listName, String sourceName) throws IOException, FileNotFoundException { 102 this(new File(listName),sourceName); 103 } 104 105 public ListingAnalyzer(File f, String sourceName) throws IOException, FileNotFoundException { 106 this(new FileInputStream(f),sourceName); 107 } 108 109 /*** 110 * construct ListingAnalyzer with compile output in listing form 111 * compiled with options plb plbcmp src,plc -E=dbs PE \"header\" 999999 112 * maybe under windows/dos you don't have to escape the double ticks ... 113 */ 114 public ListingAnalyzer(InputStream in, String sourceName) throws IOException { 115 if (in == null) throw new IllegalArgumentException("need argument InputStream"); 116 if (sourceName == null || sourceName.length()==0) throw new IllegalArgumentException("need sourceName (full path)"); 117 programName = sourceName; 118 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 119 listing = new Vector(); 120 errors = new Vector(); 121 warnings = new Vector(); 122 includes = new HashMap(); 123 String line = null; 124 int i = 0; 125 while ((line = reader.readLine()) != null) { 126 //line = line.trim(); 127 if (line.trim().length() > 0 ) { 128 listing.add(line); 129 } 130 } 131 reader.close(); 132 if (listing.size() == 0) { 133 throw new IllegalArgumentException("Error: listing is empty. (May happen " 134 +"if compilation failed, because debugger is executing program!"); 135 } 136 initRE(); 137 for (i = 0; i < listing.size(); i++) { 138 analyze(i); 139 } 140 } 141 142 private void initRE () { 143 try { 144 // 85.AC INCLUDE AD: /opt/rsi/plbsrc/incs/misc/rsistart.dbs 145 includeRE = new RE("//s*//d.[ ,A-Z]{0,2}//s*INCLUDE ([ ,A-Z]{1,2})://s*(.*)"); 146 errorRE = new RE("^//s*(//*)ERROR//*"); 147 warningRE = new RE("^//s*(//*)WARNING//*"); 148 // 91.AA moe spaces,#alausw 149 locateErrorRE = new RE("//s*(//d*)//.([ ,A-Z]{0,2})//s*(.*)$"); 150 undefinedExecutionLabelRE = new RE("^(Undefined execution label:.*)"); 151 ifLoopSwitchNotTerminatedRE = new RE("^IF/LOOP/SWITCH statement at line //((//d+)(//.([ ,A-Z]{1,2}))?//) not terminated."); 152 lastWordRE = new RE("//.*([a-zA-Z0-9#]+)$"); 153 fatalErrorRE = new RE("//FATAL ERROR ENCOUNTERED.*$"); 154 } catch (RESyntaxException re) { 155 re.printStackTrace(); 156 System.exit(1); 157 } 158 } 159 160 private void analyze(int i) { 161 String line = (String)listing.get(i); 162 if (includeRE.match(line)) { 163 int found = includeRE.getParenCount(); 164 if (found == 3) { 165 String key = includeRE.getParen(1).trim(); 166 String includeName = includeRE.getParen(2); 167 debug(found+":"+ key + ":" + includeName); 168 includes.put(key,includeName); 169 } 170 } 171 if (errorRE.match(line)) { 172 if (locateErrorRE.match((String)listing.get(i-1))) { 173 String key = locateErrorRE.getParen(2).trim(); 174 //debug("key="+key+" " +includes.get(key.trim())); 175 String lineNo = locateErrorRE.getParen(1); 176 String source = locateErrorRE.getParen(3); 177 String msg = ((String)listing.get(i+1)).trim(); 178 String sourceName = programName; 179 if (key != null && key.trim().length() > 0 && includes.containsKey(key)) { 180 sourceName = (String)includes.get(key); 181 } 182 int col = line.indexOf("*"); 183 debug("errorRE->"+sourceName+" "+lineNo+" column="+col+" msg="+msg); 184 addError(new String(sourceName+":"+lineNo+":" +msg)); 185 if (col > 0) { 186 String prevLine = (String)listing.get(i-1); 187 String location = prevLine; 188 if (locateErrorRE.match(location)) { 189 location = locateErrorRE.getParen(3); 190 } 191 addError(new String("location: " + location)); 192 String symbol = null; 193 // cut up to position before error arised 194 if (col >= prevLine.length()) { 195 symbol = prevLine; 196 } else { 197 symbol = prevLine.substring(0,col); 198 } 199 if (lastWordRE.match(symbol)) { 200 symbol = lastWordRE.getParen(1); 201 } else { 202 symbol = "at the end of the line"; 203 } 204 addError(new String("symbol: "+symbol)); 205 } 206 } 207 debug("error: " + listing.get(i+1) + " at " + listing.get(i-1)); 208 debug("error starts at " + errorRE.getParenStart(1)); 209 } 210 if (warningRE.match(line)) { 211 if (locateErrorRE.match((String)listing.get(i-1))) { 212 String key = locateErrorRE.getParen(2).trim(); 213 debug(key+" " +includes.get(key.trim())); 214 String lineNo = locateErrorRE.getParen(1); 215 String source = locateErrorRE.getParen(3); 216 String msg = ((String)listing.get(i+1)).trim(); 217 String sourceName = programName; 218 if (key != null && key.trim().length() > 0 && includes.containsKey(key)) { 219 sourceName = (String)includes.get(key); 220 } 221 debug("Warning:"+sourceName+":"+lineNo+":"+msg); 222 warnings.add(new String(sourceName+":"+lineNo+":"+"Warning: "+msg)); 223 } 224 debug("warning: " + listing.get(i+1) + " at " + listing.get(i-1)); 225 } 226 if (undefinedExecutionLabelRE.match(line)) { 227 addError(new String(programName+":"+1+":"+undefinedExecutionLabelRE.getParen(0))); 228 } 229 if (ifLoopSwitchNotTerminatedRE.match(line)) { 230 String lineNumber = ifLoopSwitchNotTerminatedRE.getParen(1); 231 String key = ifLoopSwitchNotTerminatedRE.getParen(3); 232 //System.out.println("Key: "+key); 233 String sourceName = programName; 234 if (key != null && key.trim().length() > 0 && includes.containsKey(key)) { 235 sourceName = (String)includes.get(key); 236 } 237 addError(new String(sourceName+":"+lineNumber+": if/Loop/Switch/ not terminated")); 238 } 239 if (fatalErrorRE.match(line)) { 240 addError(new String(programName+":1: FATAL ERROR ENCOUNTERED")); 241 } 242 } 243 244 private void addError(String msg) { 245 errors.add(msg); 246 if (!(msg.startsWith("location") || msg.startsWith("symbol"))) { 247 errorNo++; 248 } 249 } 250 251 private void debug(String s) { 252 if (DEBUG) { 253 System.out.println(s); 254 } 255 } 256 257 /*** 258 * print errors and warnings in format that fits to JEdits "Generic" error pattern regexps 259 * see Utilities->General options -> Plugin options -> Console -> Error patterns 260 */ 261 public void printGeneric(PrintStream ps) { 262 for (int i = 0; i < errors.size(); i++) { 263 ps.println(errors.get(i).toString()); 264 } 265 for (int i = 0; i < warnings.size(); i++) { 266 ps.println(warnings.get(i).toString()); 267 } 268 } 269 270 public void printGeneric() { 271 printGeneric(System.out); 272 } 273 274 public void printListing() { 275 for(int i = 0; i < listing.size(); i++) { 276 System.out.println((String)listing.get(i)); 277 } 278 } 279 280 public int numberOfErrors() { 281 return errorNo; 282 } 283 284 public int numberOfWarnings() { 285 return warnings.size(); 286 } 287 288 public static void main(String[] args) throws Exception { 289 if (args.length != 2) { 290 System.out.println("ListingAnalyzer xxxxxxxxx.lst sourceName"); 291 System.exit(0); 292 } 293 ListingAnalyzer analyzer = new ListingAnalyzer(args[0],args[1]); 294 //analyzer.printListing(); 295 analyzer.printGeneric(); 296 System.out.println("Found " + analyzer.numberOfErrors() + " errors and " 297 + analyzer.numberOfWarnings() + " warnings."); 298 } 299 }

This page was automatically generated by Maven