1 6 7 package org.netbeans.modules.piaget.analyze; 8 9 import java.io.BufferedReader ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileReader ; 13 import java.io.InputStreamReader ; 14 import java.io.Reader ; 15 16 17 18 24 public abstract class Parser { 25 26 protected Scanner scanner; 27 28 protected static final int EOF = Scanner.EOF; 29 protected static final int LONG = Scanner.LONG; 30 protected static final int WORD = Scanner.WORD; 31 32 protected int ttype; 33 protected long timestamp,startTime; 34 35 protected String filename; 36 37 38 39 40 public Parser(File f) { 41 init(); 42 filename = f.getName(); 43 timestamp = 0; 44 openFile(f); 45 } 46 47 private void openFile(File f){ 48 try{ 49 FileReader reader = new FileReader (f); 50 scanner = new Scanner(reader); 51 firstTimeStamp(); 52 while(ttype!=EOF){ 53 try{ 54 analyzeTokenInput(); 55 } catch(SyntaxException se){ 56 String msg = se.getMessage(); 57 if(msg.length()>0) recover(msg); 60 } 61 } 62 eof(); 63 } catch(java.io.FileNotFoundException e){ 64 System.out.println("file not found:"+filename); 65 e.printStackTrace(System.out); 66 } catch(Exception e){ 67 e.printStackTrace(System.out); 68 } 69 } 70 71 private void firstTimeStamp(){ 72 try{ 73 nextLONG(); 74 startTime = scanner.lval; 75 }catch(SyntaxException e){ 76 if(ttype != EOF){ 77 System.out.println(filename); 78 System.out.println("first ts not found"); 79 startTime = -1; 80 } 81 } 82 scanner.pushBack(); 83 } 84 85 private String ttype(int ttype){ 86 switch(ttype){ 87 case EOF: return "EOF"; 88 case LONG: return "LONG "+Long.toString(scanner.lval); 89 case WORD: return "WORD"+scanner.sval; 90 default: return "UNKNOWN!"; 91 } 92 } 93 94 protected void nextWORD() throws SyntaxException{ 95 next(); 96 if(ttype!=WORD) throw(new SyntaxException("WORD expected, found: "+ttype(ttype))); 97 } 98 99 protected void nextLONG() throws SyntaxException{ 100 next(); 101 if(ttype!=LONG) throw(new SyntaxException("LONG expected, found: "+ttype(ttype))); 102 } 103 104 protected void next() throws SyntaxException{ 105 try{ 106 ttype = scanner.nextToken(); 107 while(ttype!=WORD && ttype!=LONG && ttype!=EOF) ttype = scanner.nextToken(); 108 }catch(Exception e){ 109 e.printStackTrace(); 110 } 111 if(ttype==EOF) throw(new SyntaxException("")); 112 } 113 114 private void recover(String msg){ 115 try{ 116 if(msg.length()>0)error(msg); 117 while(ttype!=LONG && ttype!=EOF) next(); 118 scanner.pushBack(); 119 }catch(SyntaxException e){ 120 String myMSG = e.getMessage(); 121 if(myMSG.length()!=0) System.out.println("mystake, myMSG:"+myMSG);; 123 } 124 } 125 126 protected void error(String msg){ 127 System.out.println(filename+":"+scanner.lineno()+":"+msg); 128 } 129 130 public static String prettyTime(long l){ 131 int i = new Long (l/1000).intValue(); 132 int h = i/3600; 133 String hs = new Integer (h).toString(); 134 if(h<10) hs = "0"+hs; 135 int m = (i%3600)/60; 136 String ms = new Integer (m).toString(); 137 if(m<10) ms = "0"+ms; 138 int s = i%60; 139 String ss = new Integer (s).toString(); 140 if(s<10) ss = "0"+ss; 141 return hs+":"+ms+":"+ss; 142 } 143 144 protected boolean svalIs(String s){ 145 return scanner.sval.equalsIgnoreCase(s); 146 } 147 148 abstract protected void eof(); 149 150 abstract protected void init(); 151 152 abstract protected void analyzeTokenInput() throws SyntaxException; 153 154 } 155 | Popular Tags |