1 6 7 package analyzer.listeners; 8 9 import java.io.*; 10 11 17 public abstract class Parser { 18 19 protected StreamTokenizer st; 20 21 protected static int EOF=StreamTokenizer.TT_EOF; 22 protected static int NUMBER=StreamTokenizer.TT_NUMBER; 23 protected static int WORD=StreamTokenizer.TT_WORD; 24 25 protected int ttype; 26 protected long timestamp,startTime; 27 28 protected String fileName; 29 30 31 32 33 public Parser(String myFileName) { 34 init(); 35 fileName=myFileName; 36 timestamp=0; 37 openFile(fileName); 38 } 39 40 private void firstTimeStamp(){ 41 try{ 42 nextNUMBER(); 43 startTime=new Double (st.nval).longValue(); 44 }catch(SyntaxException e){ 45 System.err.println("first ts not found"); 46 startTime=-1; 47 } 48 st.pushBack(); 49 } 50 51 private static String getCWD() { 52 java.io.File f = new java.io.File ("."); 53 String cwd = f.getAbsolutePath(); 54 return cwd.substring(0, cwd.length() - 1); 55 } 56 57 private void openFile(String filepath){ 58 try{ 59 File file=new File(filepath); 60 file=file.getAbsoluteFile(); 61 FileInputStream fstream=new FileInputStream(filepath); 62 63 Reader r=new BufferedReader(new InputStreamReader(fstream)); 64 st=new StreamTokenizer(r); 65 st.parseNumbers(); 66 st.eolIsSignificant(false); 67 firstTimeStamp(); 68 while(ttype!=EOF){ 69 try{ 70 analyzeTokenInput(); 71 } catch(SyntaxException se){ 72 String msg=se.getMessage(); 73 if(msg.length()>0)recover(msg); 74 } 75 } 76 System.out.println("file length: "+st.lineno()+" lines"); 77 fstream.close(); 78 eof(); 79 } catch(java.io.FileNotFoundException e){ 80 System.err.println("file not found:"+fileName); 81 e.printStackTrace(); 82 } catch(Exception e){ 83 e.printStackTrace(); 84 } 85 } 86 87 protected void nextWORD() throws SyntaxException{ 88 next(); 89 if(ttype!=WORD) throw(new SyntaxException("WORD expected, ttype: "+ttype)); 90 } 91 92 protected void nextNUMBER() throws SyntaxException{ 93 next(); 94 if(ttype!=NUMBER) throw(new SyntaxException("NUMBER expected, ttype: "+ttype)); 95 } 96 97 protected void next() throws SyntaxException{ 98 try{ 99 ttype = st.nextToken(); 100 while(ttype!=WORD && ttype!=NUMBER && ttype!=EOF) ttype = st.nextToken(); 101 }catch(Exception e){ 102 e.printStackTrace(); 103 } 104 if(ttype==EOF) throw(new SyntaxException("")); 105 } 106 107 private void recover(String msg){ 108 try{ 109 if(msg.length()>0)error(msg); 110 while(ttype!=NUMBER && ttype!=EOF) next(); 111 st.pushBack(); 112 }catch(SyntaxException e){ 113 String myMSG=e.getMessage(); 114 if(myMSG.length()==0)return; 115 else System.out.println("you are shit"); 116 } 117 } 118 119 protected void error(String msg){ 120 System.err.println(st.lineno()+":"+msg); 121 } 122 123 protected String prettyTime(long l){ 124 int i = new Long (l/1000).intValue(); 125 int h = i/3600; 126 String hs=new Integer (h).toString(); 127 if(h<10) hs="0"+hs; 128 int m = (i%3600)/60; 129 String ms=new Integer (m).toString(); 130 if(m<10) ms="0"+ms; 131 int s = i%60; 132 String ss=new Integer (s).toString(); 133 if(s<10) ss="0"+ss; 134 return hs+":"+ms+":"+ss; 135 } 136 137 protected boolean svalIs(String s){ 138 return st.sval.equalsIgnoreCase(s); 139 } 140 141 abstract protected void eof(); 142 143 abstract protected void init(); 144 145 abstract protected void analyzeTokenInput() throws SyntaxException; 146 147 } 148 | Popular Tags |