1 18 19 package org.objectweb.jac.core.parsers.acc; 20 21 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.Reader ; 28 import java.io.StringWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Set ; 32 import java_cup.runtime.Scanner; 33 import java_cup.runtime.Symbol; 34 import org.apache.log4j.Logger; 35 import org.objectweb.jac.util.PushbackReader; 36 37 public class AccScanner implements Scanner { 38 static Logger logger = Logger.getLogger("acc.scanner"); 39 static Logger loggerParser = Logger.getLogger("acc.parser"); 40 41 PushbackReader input; 42 String streamName; 43 int line; 44 boolean bol; boolean previousBol; 47 AccScanner include = null; 48 Set blockKeywords; 49 50 public AccScanner(Reader input, String streamName, Set blockKeywords) { 51 logger.debug("FileInputStream("+streamName+")"); 52 this.input = new PushbackReader(input,2); 53 this.streamName = streamName; 54 this.line = 1; 55 this.bol = true; 56 this.blockKeywords = blockKeywords; 57 } 59 60 public AccScanner(String streamName, Set blockKeywords) 61 throws IOException 62 { 63 InputStream inputStream = null; 64 try { 65 logger.debug("FileInputStream("+streamName+")"); 66 inputStream = new FileInputStream (streamName); 67 } catch ( FileNotFoundException e ) { 68 try { 69 logger.debug("getResourceAsStream("+streamName+")"); 70 inputStream = getClass().getClassLoader().getResourceAsStream(streamName); 71 } catch (Exception e2 ) { 72 logger.debug("new URL("+streamName+")"); 73 try { 74 URL url = new URL (streamName); 75 inputStream = url.openStream(); 76 } catch (MalformedURLException e3) { 77 e3.printStackTrace(); 78 } 79 } 80 } 81 82 if (inputStream==null) 83 throw new FileNotFoundException ("file not found : "+streamName); 84 85 this.input = new PushbackReader(new InputStreamReader (inputStream),2); 86 this.streamName = streamName; 87 this.line = 1; 88 this.bol = true; 89 this.blockKeywords = blockKeywords; 90 91 } 93 94 public void printState() { 95 logger.error(getLine()); 96 if (include!=null) 97 include.printState(); 98 } 99 100 protected boolean isEof(int c) { 101 return c==-1 || c==65535; 102 } 103 104 public Symbol next_token() throws java.lang.Exception 105 { 106 if (include!=null) { 107 Symbol token = include.next_token(); 108 if (token.sym != -1) { 109 return token; 110 } 111 include = null; 112 } 113 114 boolean quoted = false; 115 int c = input.read(); 116 while (true) { 117 while (c==' ' || c=='\t' || c=='\n' || c=='\r') { 119 if (c=='\n' || c=='\r') { 121 line++; 123 bol = true; 124 if (c=='\r') { 125 c = input.read(); 126 if (c != '\n') 127 input.unread(c); 128 } 129 } 130 c = input.read(); 131 } 132 if (c=='/') { 134 int ahead = input.read(); 135 if (ahead=='/') { 136 c = input.read(); 139 while (c!='\n' && c!='\r' && c!=-1) { 140 c = input.read(); 141 } 142 continue; 143 } if (ahead=='*') { 144 int prev = c; 147 c = input.read(); 148 boolean state = false; 149 while (!(state && c=='/') && c!=-1) { 150 if ((c=='\n' && prev!='\r') || c=='\r') line++; 151 state = (c=='*'); 152 prev = c; 153 c = input.read(); 154 } 155 if (c==-1) { 156 logger.error("unclosed comment"); 157 printState(); 158 break; 159 } 160 c = input.read(); 161 continue; 162 163 } else { 164 input.unread(ahead); 165 break; 166 } 167 } 168 break; 169 } 170 171 int start = input.getPosition()-1; 172 Symbol token = null; 173 174 switch (c) { 176 case -1: 177 case 65535: 178 return null; 179 case '{': 183 token = new Symbol(AccSymbols.LBRACE,start,start,"{"); 184 break; 185 case '}': 186 token = new Symbol(AccSymbols.RBRACE,start,start,"}"); 187 break; 188 case ',': 189 token = new Symbol(AccSymbols.COMMA,start,start,","); 190 break; 191 case ';': 193 token = new Symbol(AccSymbols.EOL,start,start,";"); 194 break; 195 case '/': 196 token = new Symbol(AccSymbols.EOL,start,start,"/"); 197 break; 198 case '"': 199 { 201 quoted = true; 202 StringWriter value = new StringWriter (); 203 c = input.read(); 204 boolean escaped = false; 205 if (c=='\\') { 206 escaped = true; 207 c=readEscaped(input.read()); 208 } 209 while ((c!='"' || escaped) && !isEof(c)) { 210 value.write(c); 211 c = input.read(); 212 if (c=='\n') 213 line++; 214 escaped = false; 215 if (c=='\\') { 216 c=readEscaped(input.read()); 217 escaped = true; 218 } 219 } 220 String str = value.toString(); 222 token = new Symbol(AccSymbols.ATOMIC_VALUE, 223 start,start+str.length()-1,str); 224 } 225 break; 226 default: 227 { 228 StringWriter value = new StringWriter (); 229 String delim="/{},; \t\n\r"; 230 while (delim.indexOf(c)==-1 && c!=-1) { 231 value.write(c); 232 c = input.read(); 233 } 234 input.unread(c); 235 String str = value.toString(); 237 token = new Symbol(AccSymbols.ATOMIC_VALUE, 238 start,start+str.length()-1,str); 239 } 240 } 241 242 previousBol = bol; 243 244 if (bol && token.sym == AccSymbols.ATOMIC_VALUE && token.value.equals("include")) { 245 bol = false; 246 String filename = (String )next_token().value; 247 include = new AccScanner(filename,blockKeywords); 249 return next_token(); 250 } 251 if (!quoted && token.sym == AccSymbols.ATOMIC_VALUE && 252 ( token.value.equals("class") || 253 token.value.equals("member") || 254 token.value.equals("method") || 255 token.value.equals("attribute") || 256 token.value.equals("block") || 257 blockKeywords.contains(token.value) ) ) 258 { 259 token.sym = AccSymbols.CLASS; 260 } 262 if (!quoted && token.sym == AccSymbols.ATOMIC_VALUE && 263 token.value.equals("import")) { 264 token.sym = AccSymbols.IMPORT; 265 } 266 if (!quoted && token.sym == AccSymbols.ATOMIC_VALUE && 267 token.value.equals("null")) { 268 token.value = null; 269 } 270 token.left = start; 271 if (token.value instanceof String ) 272 token.right = start + ((String )token.value).length() - 1; 273 if (quoted) 274 token.right += 2; 275 276 loggerParser.debug("read token "+token+" = "+token.value); 278 bol = false; 279 return token; 280 } 281 282 285 int readEscaped(int c) throws IOException 286 { 287 switch (c) { 288 case 'n': 289 return '\n'; 290 case 'r': 291 return '\r'; 292 case 't': 293 return '\t'; 294 default: 295 return c; 296 } 297 } 298 299 302 public String getLine() { 303 if (include!=null) 304 return include.getLine(); 305 else 306 return streamName+":"+(previousBol?line-1:line); 307 } 308 309 } 310 | Popular Tags |