1:import java.util.Scanner; 2:import java.util.regex.*; 3:import java.util.ArrayList; 4:import java.io.*; 5:public class ParserMachine { 6: public static void main(String[] args) throws Exception { 7: String[] keywords = { "abstract", "continue", "for", "new", "switch", 8: "assert", "default", "goto", "package", "synchronized", 9: "boolean", "if", "private", "this", "break", "double", "do", 10: "implements", "protected", "throws", "throw", "byte", "else", 11: "import", "public", "case", "enum", "instanceof", "return", 12: "transient", "catch", "extends", "int", "short", "try", "char", 13: "final", "interface", "static", "void", "class", "finally", 14: "long", "strictfp", "volatile", "const", "float", "native", 15: "super", "while"}; 16: String[] literals = {"true", "false", "null"}; 17: String[] separators = {"(", ")", "{", "}", "[", "]"}; 18: Scanner request = null; 19: String filename = ""; 20: String content = ""; 21: Scanner scanner = null; 22: boolean promptForFile = true; 23: boolean blockComm = false; 24: boolean lineComm = false; 25: boolean quote = false; 26: boolean character = false; 27: boolean keyword = false; 28: int lineNum = 1; 29: int index = 0; 30: final String lnBr = System.getProperty ( "line.separator" ); 31: int ls = 0; // literals state 32: String tempLS = ""; // temporary literals string 33: int ks = 0; // keyword state 34: String tempKS = ""; // temporary keyword string 35: int ns = 0; // number state 36: String tempNums = ""; // temporary numbers string 37: int as = 0; // annotation state 38: String tempAnnot = ""; // temporary annotation string 39: 40: int bs = 0; // block comment state 41: 42: while(promptForFile == true){ 43: System.out.println("Enter path and file name (without the java extention):\n" + 44: "(for example: myComputer/myStuff/myFile)"); 45: request = new Scanner(System.in); 46: filename = request.next(); 47: File file = new File(filename+".java"); 48: try { 49: scanner = new Scanner(file); 50: //scanner = new Scanner(new FileReader(filename+".java")); // not sure what the difference is. 51: while ( scanner.hasNextLine() ){ 52: index = 0; 53: String origLine = scanner.nextLine(); 54: Scanner scannerLine = new Scanner(origLine); 55: outer: 56: while(scannerLine.hasNextLine()){ 57: // Read line by line 58: String line = scannerLine.nextLine(); 59: // first convert all the brackets to html 60: String gt = "([>])"; 61: line = Pattern.compile(gt).matcher(line) 62: .replaceAll(">"); 63: String lt = "([<])"; 64: line = Pattern.compile(lt).matcher(line) 65: .replaceAll("<"); 66: // reset the ArrayList on every line 67: ArrayList<String> characters = new ArrayList<String>(); 68: // In case weird shit happens, don't let it propagate to rest of file: 69: if(quote == true){ 70: quote = false; 71: characters.add("</span>"); 72: index++; 73: } 74: if(lineComm == true){ 75: lineComm = false; 76: characters.add("</span>"); 77: index++; 78: } 79: // end weird shit reset - for now... 80: tempKS = ""; 81: tempLS = ""; 82: // read character by character: 83: inner: 84: for (int i = 0; i < line.length(); i++){ 85: // Copy the input character by character into the ArrayList 86: characters.add(i + index, Character.toString(line.charAt(i))); 87: // char: 88: if( 89: (i > 0 90: && character == false 91: && quote == false 92: && lineComm == false 93: && blockComm == false 94: && line.charAt(i) == '\'' 95: && line.charAt(i-1) != '\\') 96: || (i == 0 97: && character == false 98: && quote == false 99: && lineComm == false 100: && blockComm == false 101: && line.charAt(i) == '\'') 102: ){ 103: characters.add(i+index, "<span class='char'>"); // insert the tag before the single quote 104: index++; 105: character = true; 106: }else if(i > 0 107: && character == true 108: && line.charAt(i) == '\'' 109: && (line.charAt(i-1) != '\\' 110: || (line.charAt(i-1) == '\\' && line.charAt(i-2) == '\\'))){ 111: 112: ensureSize(characters, line.length()+index+1); 113: characters.add(i+index+1, "</span>"); // insert the tag after the single quote 114: index++; 115: character = false; 116: } 117: // line comment: 118: if(i < line.length()-1 119: && line.charAt(i) == '/' 120: && line.charAt(i+1) == '/' 121: && quote == false 122: && blockComm == false 123: && lineComm == false){ 124: characters.add(i+index, "<span class='lnComm'>"); 125: index++; 126: lineComm = true; 127: } 128: if(lineComm == true && i == line.length()-1){ 129: ensureSize(characters, line.length()+index+1); 130: characters.add(line.length()+1+index, "</span>"); 131: index++; 132: lineComm = false; 133: } 134: // block comments: 135: if(i < line.length()-1 136: && line.charAt(i) == '/' 137: && line.charAt(i+1) == '*' 138: && lineComm == false 139: && blockComm == false 140: && quote == false ){ 141: characters.add(i+index, "<span class='blComm'>"); 142: index++; 143: blockComm = true; 144: }else if(i > 1 145: && line.charAt(i-1) == '*' 146: && line.charAt(i) == '/' 147: && line.charAt(i-2) != '/' 148: && blockComm == true ){ 149: characters.add(i+1+index, "</span>"); 150: index++; 151: blockComm = false; 152: } 153: // quoted strings: 154: if(quote == false 155: && lineComm == false 156: && blockComm == false 157: && character == false 158: && line.charAt(i) == '"'){ 159: characters.add(i+index, "<span class='string'>"); 160: index++; 161: quote = true; 162: }else if(quote == true 163: && line.charAt(i) == '"' 164: && line.charAt(i-1) != '\\'){ 165: ensureSize(characters, line.length()+index+1); 166: characters.add(i+1+index, "</span>"); 167: index++; 168: quote = false; 169: } 170: // keywords 171: if( quote == false 172: && lineComm == false 173: && blockComm == false 174: && character == false){ 175: if(i == 0) { 176: ks = 1; 177: }else if(ks == 0 // characters before the keyword: 178: && (line.charAt(i) == ' ' 179: || line.charAt(i) == '\t' 180: || line.charAt(i) == '(' 181: || line.charAt(i) == ')' 182: || line.charAt(i) == '{' 183: || line.charAt(i) == '}' 184: || line.charAt(i) == '[' 185: || line.charAt(i) == ']' 186: || line.charAt(i) == '/' 187: || line.charAt(i) == '.' 188: || line.charAt(i) == ',' 189: || line.charAt(i) == ';')) 190: { 191: ks = 2; 192: } 193: if(ks == 1 || ks == 2){ 194: tempKS += line.charAt(i); 195: } 196: 197: if((// characters after the keyword: 198: line.charAt(i) == ' ' 199: || line.charAt(i) == '\t' 200: || line.charAt(i) == '\r' 201: || line.charAt(i) == '\n' 202: || line.charAt(i) == '(' 203: || line.charAt(i) == ')' 204: || line.charAt(i) == '{' 205: || line.charAt(i) == '}' 206: || line.charAt(i) == '[' 207: || line.charAt(i) == ']' 208: || line.charAt(i) == '/' 209: || line.charAt(i) == ',' 210: || line.charAt(i) == '.' 211: || line.charAt(i) == ';')){ 212: tempKS = ""; 213: } 214: if(i < line.length()-1){ 215: for(String key : keywords){ 216: if(tempKS.equals(key) && 217: ( line.charAt(i+1) == ' ' 218: || line.charAt(i+1) == '\t' 219: || line.charAt(i+1) == '/' 220: || line.charAt(i+1) == '(' 221: || line.charAt(i+1) == ')' 222: || line.charAt(i+1) == '{' 223: || line.charAt(i+1) == '}' 224: || line.charAt(i+1) == '[' 225: || line.charAt(i+1) == ']' 226: || line.charAt(i+1) == '.' 227: || line.charAt(i+1) == ',' 228: || line.charAt(i+1) == ';')){ 229: ensureSize(characters, line.length()+index+tempKS.length()+2); 230: characters.add(i+index-tempKS.length()+1, "<span class='keyword'>"); 231: index++; 232: characters.add(i+index+1, "</span>"); 233: index++; 234: tempKS = ""; 235: ks = 0; 236: } 237: } 238: }else if(i == line.length()-1){ // end of line 239: for(String key : keywords){ 240: if(tempKS.equals(key)){ 241: ensureSize(characters, line.length()+index+tempKS.length()+2); 242: characters.add(i+index-tempKS.length()+1, "<span class='keyword'>"); 243: index++; 244: characters.add(i+index+1, "</span>"); 245: index++; 246: tempKS = ""; 247: ks = 0; 248: } 249: } 250: } 251: } 252: // literals 253: if( quote == false && lineComm == false && blockComm == false && character == false){ 254: if(i == 0) { 255: ls = 1; 256: }else if(ls == 0 && 257: (line.charAt(i) == ' ' 258: || line.charAt(i) == '\t' 259: || line.charAt(i) == '(' 260: || line.charAt(i) == ')' 261: || line.charAt(i) == '/')) 262: { 263: ls = 2; 264: } 265: if(ls == 1 || ls == 2){ 266: tempLS += line.charAt(i); 267: } 268: if((line.charAt(i) == ' ' 269: || line.charAt(i) == '\t' 270: || line.charAt(i) == '/' 271: || line.charAt(i) == '(')){ 272: tempLS = ""; 273: } 274: if(i < line.length()-1){ 275: for(String lit : literals){ 276: if(tempLS.equals(lit) && 277: (line.charAt(i+1) == ' ' 278: || line.charAt(i+1) == '\t' 279: || line.charAt(i+1) == '/' 280: || line.charAt(i+1) == '(' 281: || line.charAt(i+1) == ')' 282: || line.charAt(i+1) == ';' 283: || line.charAt(i+1) == '{' 284: || line.charAt(i+1) == ',')){ 285: ensureSize(characters, line.length()+index+tempLS.length()); 286: characters.add(i+index-tempLS.length()+1, "<span class='literal'>"); 287: index++; 288: characters.add(i+index+1, "</span>"); 289: index++; 290: tempLS = ""; 291: ls = 0; 292: } 293: } 294: }else if(i == line.length()-1){ 295: for(String lit : literals){ 296: if(tempLS.equals(lit)){ 297: ensureSize(characters, line.length()+index+tempLS.length()); 298: characters.add(i+index-tempLS.length()+1, "<span class='literal'>"); 299: index++; 300: characters.add(i+index+1, "</span>"); 301: index++; 302: tempLS = ""; 303: ls = 0; 304: } 305: } 306: } 307: } 308: // annotations 309: if(as == 0 && quote == false && character == false){ 310: if(i== 0 && line.charAt(i) == '@') 311: { 312: ensureSize(characters, line.length()+index+tempAnnot.length()); 313: characters.add(i+index, "<span class='annotation'>"); 314: index++; 315: as = 1; 316: }else if(i > 0 && (line.charAt(i-1) == ' ' || line.charAt(i-1) == '\t') 317: && line.charAt(i) == '@') 318: { 319: ensureSize(characters, line.length()+index+tempAnnot.length()); 320: characters.add(i+index, "<span class='annotation'>"); 321: index++; 322: as = 1; 323: } 324: } 325: if(as == 1 326: && (line.charAt(i) == ' ' 327: || line.charAt(i) == '\t' 328: || line.charAt(i) == '\n' 329: || line.charAt(i) == '(')){ 330: characters.add(i+index, "</span>"); 331: index++; 332: as = 0; 333: }else if(as == 1 && i == line.length()-1){ 334: characters.add(i+index+1, "</span>"); 335: index++; 336: as = 0; 337: } 338: // numbers 339: if(ns == 0 && quote == false && lineComm == false && blockComm == false && character == false){ 340: if(i== 0 && (line.charAt(i) == '0' 341: || line.charAt(i) == '1' 342: || line.charAt(i) == '2' 343: || line.charAt(i) == '3' 344: || line.charAt(i) == '4' 345: || line.charAt(i) == '5' 346: || line.charAt(i) == '6' 347: || line.charAt(i) == '7' 348: || line.charAt(i) == '8' 349: || line.charAt(i) == '9' 350: || line.charAt(i) == '\\') 351: ) 352: { 353: ensureSize(characters, line.length()+index+tempNums.length()); 354: characters.add(i+index, "<span class='number'>"); 355: index++; 356: ns = 1; 357: }else if(i > 0 && ( 358: line.charAt(i-1) == ' ' 359: || line.charAt(i-1) == '+' 360: || line.charAt(i-1) == '-' 361: || line.charAt(i-1) == '(' 362: || line.charAt(i-1) == '.' 363: || line.charAt(i-1) == ',' 364: || line.charAt(i-1) == '[' 365: || line.charAt(i-1) == '/' 366: || line.charAt(i-1) == '%' 367: || line.charAt(i-1) == '<' 368: || line.charAt(i-1) == '>' 369: || line.charAt(i-1) == '!' 370: || line.charAt(i-1) == '=' 371: || line.charAt(i-1) == '^' 372: || line.charAt(i-1) == ':' 373: || line.charAt(i-1) == '~') 374: && (line.charAt(i) == '0' 375: || line.charAt(i) == '1' 376: || line.charAt(i) == '2' 377: || line.charAt(i) == '3' 378: || line.charAt(i) == '4' 379: || line.charAt(i) == '5' 380: || line.charAt(i) == '6' 381: || line.charAt(i) == '7' 382: || line.charAt(i) == '8' 383: || line.charAt(i) == '9' 384: || line.charAt(i) == '\\') 385: ){ 386: ensureSize(characters, line.length()+index+tempNums.length()); 387: characters.add(i+index, "<span class='number'>"); 388: index++; 389: ns = 1; 390: } 391: } 392: if(ns == 1 && (line.charAt(i) == ' ' 393: || line.charAt(i) == '\t' 394: || line.charAt(i) == ';' 395: || line.charAt(i) == '-' 396: || line.charAt(i) == '+' 397: || line.charAt(i) == ')' 398: || line.charAt(i) == ',' 399: || line.charAt(i) == '.' 400: || line.charAt(i) == ']' 401: || line.charAt(i) == '*' 402: || line.charAt(i) == '%' 403: || line.charAt(i) == '/' 404: || line.charAt(i) == '>' 405: || line.charAt(i) == '<' 406: || line.charAt(i) == '!' 407: || line.charAt(i) == '=' 408: || line.charAt(i) == ':' 409: || line.charAt(i) == '^' 410: || line.charAt(i) == '~') 411: ){ 412: characters.add(i+index, "</span>"); 413: index++; 414: ns = 0; 415: }else if(ns == 1 && i == line.length()-1){ 416: characters.add(i+index+1, "</span>"); 417: index++; 418: ns = 0; 419: } 420: // separators 421: if(quote == false && lineComm == false && blockComm == false && character == false){ 422: for(String sep : separators){ 423: if(Character.toString(line.charAt(i)).equals(sep)){ 424: characters.add(i+index, "<span class='separator'>"); 425: index++; 426: ensureSize(characters, line.length()+index+1); 427: characters.add(i+index+1, "</span>"); 428: index++; 429: } 430: } 431: } 432: } // end char by char loop 433: content += "\n"; 434: // comment out the 2 lines below to disable line numbers: 435: content += "<span class='lnNumb'>" + lineNum+":</span>"; 436: lineNum++; 437: // Make one big content string out of the characters ArrayList: 438: String str = characters.toString(); 439: String revStr = str.substring(1, str.length() - 1); // get rid of the square brackets 440: String[] temp; 441: temp = revStr.split(", "); 442: for(int i = 0; i < temp.length ; i++){ 443: content += temp[i]; 444: } 445: } 446: } 447: promptForFile = false; 448: }catch(IOException ioe){ 449: System.out.println("An Exception has occured"); 450: System.out.println(ioe.toString()); 451: //System.exit(1); 452: continue; 453: } 454: } 455: PrintWriter writer = new PrintWriter(filename+".html"); 456: writer.println("<!DOCTYPE HTML>\n" 457: + "<html lang='en'>\n" 458: + "<head>\n" 459: + "<meta charset='UTF-8'>\n" 460: + "<style>\n" 461: + "pre{color:#444;}\n" 462: + ".lnComm{color:#85bd7f;}\n" 463: + ".string{color:#015de6;}\n" 464: + ".char{color:purple;}\n" 465: + ".blComm{color:#7f9ed2;}\n" 466: + ".lnNumb{color:#b5b5b5; font-size:80%; padding-right:30px; display:inline-block; width: 40px;}\n" 467: + ".number{color:#fa4903;}\n" 468: + ".annotation{color:#9756aa;}\n" 469: + ".keyword{color:hotpink;font-weight:bold;}\n" 470: + ".literal{color:darkblue;font-weight:bold;}\n" 471: + ".separator{color:#b10148;font-weight:bold;}\n" 472: + "</style>\n" 473: + "</head>\n" + "<body>\n" + "<pre>"); 474: writer.print(content); 475: writer.println("</pre> " + "</body>" + "</html>"); 476: writer.flush(); // flush the output 477: writer.close(); // close the file 478: System.out.println("A "+filename+".html file was created in the same directory as your java file."); 479: } 480: public static String emptyString(String s){ // not being used 481: s =""; 482: return s; 483: } 484: public static void ensureSize(ArrayList<String> list, int size) { 485: list.ensureCapacity(size); 486: while (list.size() < size) { 487: list.add(""); 488: } 489: } 490:}