1 6 7 package org.netbeans.modules.piaget.analyze; 8 9 import java.io.InputStreamReader ; 10 import java.io.IOException ; 11 12 13 19 class Scanner { 20 21 public static final int EOF = -1; 22 public static final int WORD = 1; 23 public static final int LONG = 2; 24 public int lastReturned; 25 26 28 private static final char EOF_CH = (char)-1; 29 30 private boolean pushed; 31 32 34 public String sval; 35 public long lval; 36 37 private StringBuffer buf; 38 39 41 private char ch; 42 43 private int line = 1; 44 45 46 47 49 private final InputStreamReader in; 50 51 public Scanner(InputStreamReader in) { 52 this.in = in; 53 pushed = false; 54 nextCh(); 55 } 56 57 59 public void close() throws IOException { 60 in.close(); 61 } 62 63 public void pushBack(){ 64 pushed = true; 65 } 66 67 68 public int nextToken() { 69 if(pushed){ 70 pushed = false; 71 return lastReturned; 72 } 73 74 buf = new StringBuffer (); 75 while (!interestingChar()) nextCh(); 76 if(ch == EOF_CH) return EOF; 77 while(interestingChar()){ 78 buf.append(ch); 79 nextCh(); 80 } 81 82 if(buf.length() == 0){ 83 nextToken(); 84 } 85 86 87 String result = buf.toString(); 88 89 try{ 90 long l = Long.parseLong(result); 91 lval = l; 92 lastReturned = LONG; 93 return lastReturned; 94 }catch (NumberFormatException e){ 95 sval = result; 96 lastReturned = WORD; 97 return lastReturned; 98 } 99 100 } 101 102 private boolean interestingChar(){ 103 return ch != '\n' && ch != '\r' && ch !='\t' && !Character.isWhitespace(ch); 104 } 105 106 public int lineno(){ 107 return line; 108 } 109 110 112 private void nextCh() { 113 switch (ch) { 114 case EOF_CH: 115 return; 116 case '\n': 117 line++; 118 break; 119 default: 120 break; 121 } 122 try { 123 ch = (char)in.read(); 124 oldch = ((oldch == '\r') && (ch == '\n')) ? (char)in.read() : ch; 125 ch = (oldch == '\r') ? '\n' : oldch; 126 } catch (IOException e) { 127 e.printStackTrace(System.out); 128 } 129 } 130 131 private char oldch; 132 } 133 | Popular Tags |