1 package persistence.antlr; 2 3 8 9 import java.util.Enumeration ; 10 11 import persistence.antlr.collections.impl.BitSet; 12 import persistence.antlr.collections.impl.Vector; 13 14 import java.io.PrintWriter ; import java.io.IOException ; 16 import java.io.FileWriter ; 17 18 19 public class HTMLCodeGenerator extends CodeGenerator { 20 21 protected int syntacticPredLevel = 0; 22 23 24 protected boolean doingLexRules = false; 25 26 protected boolean firstElementInAlt; 27 28 protected AlternativeElement prevAltElem = null; 30 34 public HTMLCodeGenerator() { 35 super(); 36 charFormatter = new JavaCharFormatter(); 37 } 38 39 43 static String HTMLEncode(String s) { 44 StringBuffer buf = new StringBuffer (); 45 46 for (int i = 0, len = s.length(); i < len; i++) { 47 char c = s.charAt(i); 48 if (c == '&') 49 buf.append("&"); 50 else if (c == '\"') 51 buf.append("""); 52 else if (c == '\'') 53 buf.append("'"); 54 else if (c == '<') 55 buf.append("<"); 56 else if (c == '>') 57 buf.append(">"); 58 else 59 buf.append(c); 60 } 61 return buf.toString(); 62 } 63 64 public void gen() { 65 try { 67 Enumeration grammarIter = behavior.grammars.elements(); 69 while (grammarIter.hasMoreElements()) { 70 Grammar g = (Grammar)grammarIter.nextElement(); 71 72 77 g.setCodeGenerator(this); 78 79 g.generate(); 81 82 if (antlrTool.hasError()) { 83 antlrTool.fatalError("Exiting due to errors."); 84 } 85 86 } 87 88 } 89 catch (IOException e) { 90 antlrTool.reportException(e, null); 91 } 92 } 93 94 97 public void gen(ActionElement action) { 98 } 100 101 104 public void gen(AlternativeBlock blk) { 105 genGenericBlock(blk, ""); 106 } 107 108 113 public void gen(BlockEndElement end) { 114 } 116 117 120 public void gen(CharLiteralElement atom) { 121 if (atom.not) { 122 _print("~"); 123 } 124 _print(HTMLEncode(atom.atomText) + " "); 125 } 126 127 130 public void gen(CharRangeElement r) { 131 print(r.beginText + ".." + r.endText + " "); 132 } 133 134 135 public void gen(LexerGrammar g) throws IOException { 136 setGrammar(g); 137 antlrTool.reportProgress("Generating " + grammar.getClassName() + TokenTypesFileExt); 138 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + TokenTypesFileExt); 139 141 tabs = 0; 142 doingLexRules = true; 143 144 genHeader(); 146 147 151 println(""); 153 154 if (grammar.comment != null) { 156 _println(HTMLEncode(grammar.comment)); 157 } 158 159 println("Definition of lexer " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + "."); 160 161 164 188 189 genNextToken(); 193 194 196 Enumeration ids = grammar.rules.elements(); 197 while (ids.hasMoreElements()) { 198 RuleSymbol rs = (RuleSymbol)ids.nextElement(); 199 if (!rs.id.equals("mnextToken")) { 200 genRule(rs); 201 } 202 } 203 204 currentOutput.close(); 206 currentOutput = null; 207 doingLexRules = false; 208 } 209 210 213 public void gen(OneOrMoreBlock blk) { 214 genGenericBlock(blk, "+"); 215 } 216 217 218 public void gen(ParserGrammar g) throws IOException { 219 setGrammar(g); 220 antlrTool.reportProgress("Generating " + grammar.getClassName() + ".html"); 222 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".html"); 223 224 tabs = 0; 225 226 genHeader(); 228 229 println(""); 231 232 if (grammar.comment != null) { 234 _println(HTMLEncode(grammar.comment)); 235 } 236 237 println("Definition of parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + "."); 238 239 Enumeration rules = grammar.rules.elements(); 241 while (rules.hasMoreElements()) { 242 println(""); 243 GrammarSymbol sym = (GrammarSymbol)rules.nextElement(); 245 if (sym instanceof RuleSymbol) { 247 genRule((RuleSymbol)sym); 248 } 249 } 250 tabs--; 251 println(""); 252 253 genTail(); 254 255 currentOutput.close(); 257 currentOutput = null; 258 } 259 260 263 public void gen(RuleRefElement rr) { 264 RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule); 265 266 _print("<a HREF=\"" + grammar.getClassName() + ".html#" + rr.targetRule + "\">"); 268 _print(rr.targetRule); 269 _print("</a>"); 270 _print(" "); 275 } 276 277 280 public void gen(StringLiteralElement atom) { 281 if (atom.not) { 282 _print("~"); 283 } 284 _print(HTMLEncode(atom.atomText)); 285 _print(" "); 286 } 287 288 291 public void gen(TokenRangeElement r) { 292 print(r.beginText + ".." + r.endText + " "); 293 } 294 295 298 public void gen(TokenRefElement atom) { 299 if (atom.not) { 300 _print("~"); 301 } 302 _print(atom.atomText); 303 _print(" "); 304 } 305 306 public void gen(TreeElement t) { 307 print(t + " "); 308 } 309 310 311 public void gen(TreeWalkerGrammar g) throws IOException { 312 setGrammar(g); 313 antlrTool.reportProgress("Generating " + grammar.getClassName() + ".html"); 315 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".html"); 316 318 tabs = 0; 319 320 genHeader(); 322 323 println(""); 325 332 println(""); 334 335 if (grammar.comment != null) { 337 _println(HTMLEncode(grammar.comment)); 338 } 339 340 println("Definition of tree parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + "."); 341 342 351 println(""); 353 tabs++; 355 356 Enumeration rules = grammar.rules.elements(); 358 while (rules.hasMoreElements()) { 359 println(""); 360 GrammarSymbol sym = (GrammarSymbol)rules.nextElement(); 362 if (sym instanceof RuleSymbol) { 364 genRule((RuleSymbol)sym); 365 } 366 } 367 tabs--; 368 println(""); 369 371 374 currentOutput.close(); 376 currentOutput = null; 377 } 378 379 380 public void gen(WildcardElement wc) { 381 386 _print(". "); 387 } 388 389 392 public void gen(ZeroOrMoreBlock blk) { 393 genGenericBlock(blk, "*"); 394 } 395 396 protected void genAlt(Alternative alt) { 397 if (alt.getTreeSpecifier() != null) { 398 _print(alt.getTreeSpecifier().getText()); 399 } 400 prevAltElem = null; 401 for (AlternativeElement elem = alt.head; 402 !(elem instanceof BlockEndElement); 403 elem = elem.next) { 404 elem.generate(); 405 firstElementInAlt = false; 406 prevAltElem = elem; 407 } 408 } 409 414 426 public void genCommonBlock(AlternativeBlock blk) { 427 for (int i = 0; i < blk.alternatives.size(); i++) { 428 Alternative alt = blk.getAlternativeAt(i); 429 AlternativeElement elem = alt.head; 430 431 if (i > 0 && blk.alternatives.size() > 1) { 433 _println(""); 434 print("|\t"); 435 } 436 437 boolean save = firstElementInAlt; 440 firstElementInAlt = true; 441 tabs++; 443 genAlt(alt); 456 tabs--; 457 firstElementInAlt = save; 458 } 459 } 460 461 465 public void genFollowSetForRuleBlock(RuleBlock blk) { 466 Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode); 467 printSet(grammar.maxk, 1, follow); 468 } 469 470 protected void genGenericBlock(AlternativeBlock blk, String blkOp) { 471 if (blk.alternatives.size() > 1) { 472 if (!firstElementInAlt) { 474 if (prevAltElem == null || 476 !(prevAltElem instanceof AlternativeBlock) || 477 ((AlternativeBlock)prevAltElem).alternatives.size() == 1) { 478 _println(""); 479 print("(\t"); 480 } 481 else { 482 _print("(\t"); 483 } 484 } 487 else { 488 _print("(\t"); 489 } 490 } 491 else { 492 _print("( "); 493 } 494 genCommonBlock(blk); 497 if (blk.alternatives.size() > 1) { 498 _println(""); 499 print(")" + blkOp + " "); 500 if (!(blk.next instanceof BlockEndElement)) { 502 _println(""); 503 print(""); 504 } 505 } 506 else { 507 _print(")" + blkOp + " "); 508 } 509 } 510 511 512 protected void genHeader() { 513 println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 514 println("<HTML>"); 515 println("<HEAD>"); 516 println("<TITLE>Grammar " + antlrTool.grammarFile + "</TITLE>"); 517 println("</HEAD>"); 518 println("<BODY>"); 519 println("<table summary=\"\" border=\"1\" cellpadding=\"5\">"); 520 println("<tr>"); 521 println("<td>"); 522 println("<font size=\"+2\">Grammar " + grammar.getClassName() + "</font><br>"); 523 println("<a HREF=\"http://www.ANTLR.org\">ANTLR</a>-generated HTML file from " + antlrTool.grammarFile); 524 println("<p>"); 525 println("Terence Parr, <a HREF=\"http://www.magelang.com\">MageLang Institute</a>"); 526 println("<br>ANTLR Version " + antlrTool.version + "; 1989-1999"); 527 println("</td>"); 528 println("</tr>"); 529 println("</table>"); 530 println("<PRE>"); 531 } 536 537 538 protected void genLookaheadSetForAlt(Alternative alt) { 539 if (doingLexRules && alt.cache[1].containsEpsilon()) { 540 println("MATCHES ALL"); 541 return; 542 } 543 int depth = alt.lookaheadDepth; 544 if (depth == GrammarAnalyzer.NONDETERMINISTIC) { 545 depth = grammar.maxk; 548 } 549 for (int i = 1; i <= depth; i++) { 550 Lookahead lookahead = alt.cache[i]; 551 printSet(depth, i, lookahead); 552 } 553 } 554 555 559 public void genLookaheadSetForBlock(AlternativeBlock blk) { 560 int depth = 0; 562 for (int i = 0; i < blk.alternatives.size(); i++) { 563 Alternative alt = blk.getAlternativeAt(i); 564 if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) { 565 depth = grammar.maxk; 566 break; 567 } 568 else if (depth < alt.lookaheadDepth) { 569 depth = alt.lookaheadDepth; 570 } 571 } 572 573 for (int i = 1; i <= depth; i++) { 574 Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk); 575 printSet(depth, i, lookahead); 576 } 577 } 578 579 583 public void genNextToken() { 584 println(""); 585 println("/** Lexer nextToken rule:"); 586 println(" * The lexer nextToken rule is synthesized from all of the user-defined"); 587 println(" * lexer rules. It logically consists of one big alternative block with"); 588 println(" * each user-defined rule being an alternative."); 589 println(" */"); 590 591 RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken"); 594 595 RuleSymbol nextTokenRs = new RuleSymbol("mnextToken"); 597 nextTokenRs.setDefined(); 598 nextTokenRs.setBlock(blk); 599 nextTokenRs.access = "private"; 600 grammar.define(nextTokenRs); 601 602 612 613 genCommonBlock(blk); 614 } 615 616 619 public void genRule(RuleSymbol s) { 620 if (s == null || !s.isDefined()) return; println(""); 622 if (s.comment != null) { 623 _println(HTMLEncode(s.comment)); 624 } 625 if (s.access.length() != 0) { 626 if (!s.access.equals("public")) { 627 _print(s.access + " "); 628 } 629 } 630 _print("<a name=\"" + s.getId() + "\">"); 631 _print(s.getId()); 632 _print("</a>"); 633 634 RuleBlock rblk = s.getBlock(); 636 637 _println(""); 648 tabs++; 649 print(":\t"); 650 651 654 genCommonBlock(rblk); 656 657 _println(""); 658 println(";"); 659 tabs--; 660 } 661 662 666 protected void genSynPred(SynPredBlock blk) { 667 syntacticPredLevel++; 668 genGenericBlock(blk, " =>"); 669 syntacticPredLevel--; 670 } 671 672 public void genTail() { 673 println("</PRE>"); 674 println("</BODY>"); 675 println("</HTML>"); 676 } 677 678 679 protected void genTokenTypes(TokenManager tm) throws IOException { 680 antlrTool.reportProgress("Generating " + tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt); 682 currentOutput = antlrTool.openOutputFile(tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt); 683 tabs = 0; 685 686 genHeader(); 688 689 println(""); 692 println("*** Tokens used by the parser"); 693 println("This is a list of the token numeric values and the corresponding"); 694 println("token identifiers. Some tokens are literals, and because of that"); 695 println("they have no identifiers. Literals are double-quoted."); 696 tabs++; 697 698 Vector v = tm.getVocabulary(); 700 for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) { 701 String s = (String )v.elementAt(i); 702 if (s != null) { 703 println(s + " = " + i); 704 } 705 } 706 707 tabs--; 709 println("*** End of tokens used by the parser"); 710 711 currentOutput.close(); 713 currentOutput = null; 714 } 715 716 719 public String getASTCreateString(Vector v) { 720 return null; 721 } 722 723 726 public String getASTCreateString(GrammarAtom atom, String str) { 727 return null; 728 } 729 730 736 public String mapTreeId(String id, ActionTransInfo tInfo) { 737 return id; 738 } 739 740 protected String processActionForSpecialSymbols(String actionStr, 742 int line, 743 RuleBlock currentRule, 744 ActionTransInfo tInfo) { 745 return actionStr; 746 } 747 748 753 public void printSet(int depth, int k, Lookahead lookahead) { 754 int numCols = 5; 755 756 int[] elems = lookahead.fset.toArray(); 757 758 if (depth != 1) { 759 print("k==" + k + ": {"); 760 } 761 else { 762 print("{ "); 763 } 764 if (elems.length > numCols) { 765 _println(""); 766 tabs++; 767 print(""); 768 } 769 770 int column = 0; 771 for (int i = 0; i < elems.length; i++) { 772 column++; 773 if (column > numCols) { 774 _println(""); 775 print(""); 776 column = 0; 777 } 778 if (doingLexRules) { 779 _print(charFormatter.literalChar(elems[i])); 780 } 781 else { 782 _print((String )grammar.tokenManager.getVocabulary().elementAt(elems[i])); 783 } 784 if (i != elems.length - 1) { 785 _print(", "); 786 } 787 } 788 789 if (elems.length > numCols) { 790 _println(""); 791 tabs--; 792 print(""); 793 } 794 _println(" }"); 795 } 796 } 797 | Popular Tags |