1 package persistence.antlr; 2 3 8 9 11 12 import java.util.Enumeration ; 13 14 import persistence.antlr.collections.impl.BitSet; 15 import persistence.antlr.collections.impl.Vector; 16 17 import java.io.PrintWriter ; import java.io.IOException ; 19 import java.io.FileWriter ; 20 21 22 public class DocBookCodeGenerator extends CodeGenerator { 23 24 protected int syntacticPredLevel = 0; 25 26 27 protected boolean doingLexRules = false; 28 29 protected boolean firstElementInAlt; 30 31 protected AlternativeElement prevAltElem = null; 33 37 public DocBookCodeGenerator() { 38 super(); 39 charFormatter = new JavaCharFormatter(); 40 } 41 42 46 static String HTMLEncode(String s) { 47 StringBuffer buf = new StringBuffer (); 48 49 for (int i = 0, len = s.length(); i < len; i++) { 50 char c = s.charAt(i); 51 if (c == '&') 52 buf.append("&"); 53 else if (c == '\"') 54 buf.append("""); 55 else if (c == '\'') 56 buf.append("'"); 57 else if (c == '<') 58 buf.append("<"); 59 else if (c == '>') 60 buf.append(">"); 61 else 62 buf.append(c); 63 } 64 return buf.toString(); 65 } 66 67 71 static String QuoteForId(String s) { 72 StringBuffer buf = new StringBuffer (); 73 74 for (int i = 0, len = s.length(); i < len; i++) { 75 char c = s.charAt(i); 76 if (c == '_') 77 buf.append("."); 78 else 79 buf.append(c); 80 } 81 return buf.toString(); 82 } 83 84 public void gen() { 85 try { 87 Enumeration grammarIter = behavior.grammars.elements(); 89 while (grammarIter.hasMoreElements()) { 90 Grammar g = (Grammar)grammarIter.nextElement(); 91 92 97 g.setCodeGenerator(this); 98 99 g.generate(); 101 102 if (antlrTool.hasError()) { 103 antlrTool.fatalError("Exiting due to errors."); 104 } 105 106 } 107 108 } 109 catch (IOException e) { 110 antlrTool.reportException(e, null); 111 } 112 } 113 114 117 public void gen(ActionElement action) { 118 } 120 121 124 public void gen(AlternativeBlock blk) { 125 genGenericBlock(blk, ""); 126 } 127 128 133 public void gen(BlockEndElement end) { 134 } 136 137 140 public void gen(CharLiteralElement atom) { 141 if (atom.not) { 142 _print("~"); 143 } 144 _print(HTMLEncode(atom.atomText) + " "); 145 } 146 147 150 public void gen(CharRangeElement r) { 151 print(r.beginText + ".." + r.endText + " "); 152 } 153 154 155 public void gen(LexerGrammar g) throws IOException { 156 setGrammar(g); 157 antlrTool.reportProgress("Generating " + grammar.getClassName() + TokenTypesFileExt); 158 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + TokenTypesFileExt); 159 161 tabs = 0; 162 doingLexRules = true; 163 164 genHeader(); 166 167 171 println(""); 173 174 if (grammar.comment != null) { 176 _println(HTMLEncode(grammar.comment)); 177 } 178 179 println("<para>Definition of lexer " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>"); 180 181 184 208 209 genNextToken(); 213 214 216 Enumeration ids = grammar.rules.elements(); 217 while (ids.hasMoreElements()) { 218 RuleSymbol rs = (RuleSymbol)ids.nextElement(); 219 if (!rs.id.equals("mnextToken")) { 220 genRule(rs); 221 } 222 } 223 224 currentOutput.close(); 226 currentOutput = null; 227 doingLexRules = false; 228 } 229 230 233 public void gen(OneOrMoreBlock blk) { 234 genGenericBlock(blk, "+"); 235 } 236 237 238 public void gen(ParserGrammar g) throws IOException { 239 setGrammar(g); 240 antlrTool.reportProgress("Generating " + grammar.getClassName() + ".sgml"); 242 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".sgml"); 243 244 tabs = 0; 245 246 genHeader(); 248 249 println(""); 251 252 if (grammar.comment != null) { 254 _println(HTMLEncode(grammar.comment)); 255 } 256 257 println("<para>Definition of parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>"); 258 259 Enumeration rules = grammar.rules.elements(); 261 while (rules.hasMoreElements()) { 262 println(""); 263 GrammarSymbol sym = (GrammarSymbol)rules.nextElement(); 265 if (sym instanceof RuleSymbol) { 267 genRule((RuleSymbol)sym); 268 } 269 } 270 tabs--; 271 println(""); 272 273 genTail(); 274 275 currentOutput.close(); 277 currentOutput = null; 278 } 279 280 283 public void gen(RuleRefElement rr) { 284 RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule); 285 286 _print("<link linkend=\"" + QuoteForId(rr.targetRule) + "\">"); 288 _print(rr.targetRule); 289 _print("</link>"); 290 _print(" "); 295 } 296 297 300 public void gen(StringLiteralElement atom) { 301 if (atom.not) { 302 _print("~"); 303 } 304 _print(HTMLEncode(atom.atomText)); 305 _print(" "); 306 } 307 308 311 public void gen(TokenRangeElement r) { 312 print(r.beginText + ".." + r.endText + " "); 313 } 314 315 318 public void gen(TokenRefElement atom) { 319 if (atom.not) { 320 _print("~"); 321 } 322 _print(atom.atomText); 323 _print(" "); 324 } 325 326 public void gen(TreeElement t) { 327 print(t + " "); 328 } 329 330 331 public void gen(TreeWalkerGrammar g) throws IOException { 332 setGrammar(g); 333 antlrTool.reportProgress("Generating " + grammar.getClassName() + ".sgml"); 335 currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".sgml"); 336 338 tabs = 0; 339 340 genHeader(); 342 343 println(""); 345 352 println(""); 354 355 if (grammar.comment != null) { 357 _println(HTMLEncode(grammar.comment)); 358 } 359 360 println("<para>Definition of tree parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>"); 361 362 371 println(""); 373 tabs++; 375 376 Enumeration rules = grammar.rules.elements(); 378 while (rules.hasMoreElements()) { 379 println(""); 380 GrammarSymbol sym = (GrammarSymbol)rules.nextElement(); 382 if (sym instanceof RuleSymbol) { 384 genRule((RuleSymbol)sym); 385 } 386 } 387 tabs--; 388 println(""); 389 391 394 currentOutput.close(); 396 currentOutput = null; 397 } 398 399 400 public void gen(WildcardElement wc) { 401 406 _print(". "); 407 } 408 409 412 public void gen(ZeroOrMoreBlock blk) { 413 genGenericBlock(blk, "*"); 414 } 415 416 protected void genAlt(Alternative alt) { 417 if (alt.getTreeSpecifier() != null) { 418 _print(alt.getTreeSpecifier().getText()); 419 } 420 prevAltElem = null; 421 for (AlternativeElement elem = alt.head; 422 !(elem instanceof BlockEndElement); 423 elem = elem.next) { 424 elem.generate(); 425 firstElementInAlt = false; 426 prevAltElem = elem; 427 } 428 } 429 434 446 public void genCommonBlock(AlternativeBlock blk) { 447 if (blk.alternatives.size() > 1) 448 println("<itemizedlist mark=\"none\">"); 449 for (int i = 0; i < blk.alternatives.size(); i++) { 450 Alternative alt = blk.getAlternativeAt(i); 451 AlternativeElement elem = alt.head; 452 453 if (blk.alternatives.size() > 1) 454 print("<listitem><para>"); 455 456 if (i > 0 && blk.alternatives.size() > 1) { 458 _print("| "); 459 } 460 461 boolean save = firstElementInAlt; 464 firstElementInAlt = true; 465 tabs++; 467 genAlt(alt); 468 tabs--; 469 firstElementInAlt = save; 470 if (blk.alternatives.size() > 1) 471 _println("</para></listitem>"); 472 } 473 if (blk.alternatives.size() > 1) 474 println("</itemizedlist>"); 475 } 476 477 481 public void genFollowSetForRuleBlock(RuleBlock blk) { 482 Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode); 483 printSet(grammar.maxk, 1, follow); 484 } 485 486 protected void genGenericBlock(AlternativeBlock blk, String blkOp) { 487 if (blk.alternatives.size() > 1) { 488 _println(""); 490 if (!firstElementInAlt) { 491 _println("("); 497 } 505 else { 506 _print("("); 507 } 508 } 509 else { 510 _print("( "); 511 } 512 genCommonBlock(blk); 515 if (blk.alternatives.size() > 1) { 516 _println(""); 517 print(")" + blkOp + " "); 518 if (!(blk.next instanceof BlockEndElement)) { 520 _println(""); 521 print(""); 522 } 523 } 524 else { 525 _print(")" + blkOp + " "); 526 } 527 } 528 529 530 protected void genHeader() { 531 println("<?xml version=\"1.0\" standalone=\"no\"?>"); 532 println("<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook V3.1//EN\">"); 533 println("<book lang=\"en\">"); 534 println("<bookinfo>"); 535 println("<title>Grammar " + grammar.getClassName() + "</title>"); 536 println(" <author>"); 537 println(" <firstname></firstname>"); 538 println(" <othername></othername>"); 539 println(" <surname></surname>"); 540 println(" <affiliation>"); 541 println(" <address>"); 542 println(" <email></email>"); 543 println(" </address>"); 544 println(" </affiliation>"); 545 println(" </author>"); 546 println(" <othercredit>"); 547 println(" <contrib>"); 548 println(" Generated by <ulink url=\"http://www.ANTLR.org/\">ANTLR</ulink>" + antlrTool.version); 549 println(" from " + antlrTool.grammarFile); 550 println(" </contrib>"); 551 println(" </othercredit>"); 552 println(" <pubdate></pubdate>"); 553 println(" <abstract>"); 554 println(" <para>"); 555 println(" </para>"); 556 println(" </abstract>"); 557 println("</bookinfo>"); 558 println("<chapter>"); 559 println("<title></title>"); 560 } 561 562 563 protected void genLookaheadSetForAlt(Alternative alt) { 564 if (doingLexRules && alt.cache[1].containsEpsilon()) { 565 println("MATCHES ALL"); 566 return; 567 } 568 int depth = alt.lookaheadDepth; 569 if (depth == GrammarAnalyzer.NONDETERMINISTIC) { 570 depth = grammar.maxk; 573 } 574 for (int i = 1; i <= depth; i++) { 575 Lookahead lookahead = alt.cache[i]; 576 printSet(depth, i, lookahead); 577 } 578 } 579 580 584 public void genLookaheadSetForBlock(AlternativeBlock blk) { 585 int depth = 0; 587 for (int i = 0; i < blk.alternatives.size(); i++) { 588 Alternative alt = blk.getAlternativeAt(i); 589 if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) { 590 depth = grammar.maxk; 591 break; 592 } 593 else if (depth < alt.lookaheadDepth) { 594 depth = alt.lookaheadDepth; 595 } 596 } 597 598 for (int i = 1; i <= depth; i++) { 599 Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk); 600 printSet(depth, i, lookahead); 601 } 602 } 603 604 608 public void genNextToken() { 609 println(""); 610 println("/** Lexer nextToken rule:"); 611 println(" * The lexer nextToken rule is synthesized from all of the user-defined"); 612 println(" * lexer rules. It logically consists of one big alternative block with"); 613 println(" * each user-defined rule being an alternative."); 614 println(" */"); 615 616 RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken"); 619 620 RuleSymbol nextTokenRs = new RuleSymbol("mnextToken"); 622 nextTokenRs.setDefined(); 623 nextTokenRs.setBlock(blk); 624 nextTokenRs.access = "private"; 625 grammar.define(nextTokenRs); 626 627 637 638 genCommonBlock(blk); 639 } 640 641 644 public void genRule(RuleSymbol s) { 645 if (s == null || !s.isDefined()) return; println(""); 647 648 if (s.access.length() != 0) { 649 if (!s.access.equals("public")) { 650 _print("<para>" + s.access + " </para>"); 651 } 652 } 653 654 println("<section id=\"" + QuoteForId(s.getId()) + "\">"); 655 println("<title>" + s.getId() + "</title>"); 656 if (s.comment != null) { 657 _println("<para>" + HTMLEncode(s.comment) + "</para>"); 658 } 659 println("<para>"); 660 661 RuleBlock rblk = s.getBlock(); 663 664 _println(""); 675 print(s.getId() + ":\t"); 676 tabs++; 677 678 681 genCommonBlock(rblk); 683 684 _println(""); 685 tabs--; 687 _println("</para>"); 688 _println("</section><!-- section \"" + s.getId() + "\" -->"); 689 } 690 691 695 protected void genSynPred(SynPredBlock blk) { 696 } 698 699 public void genTail() { 700 println("</chapter>"); 701 println("</book>"); 702 } 703 704 705 protected void genTokenTypes(TokenManager tm) throws IOException { 706 antlrTool.reportProgress("Generating " + tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt); 708 currentOutput = antlrTool.openOutputFile(tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt); 709 tabs = 0; 711 712 genHeader(); 714 715 println(""); 718 println("*** Tokens used by the parser"); 719 println("This is a list of the token numeric values and the corresponding"); 720 println("token identifiers. Some tokens are literals, and because of that"); 721 println("they have no identifiers. Literals are double-quoted."); 722 tabs++; 723 724 Vector v = tm.getVocabulary(); 726 for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) { 727 String s = (String )v.elementAt(i); 728 if (s != null) { 729 println(s + " = " + i); 730 } 731 } 732 733 tabs--; 735 println("*** End of tokens used by the parser"); 736 737 currentOutput.close(); 739 currentOutput = null; 740 } 741 742 protected String processActionForSpecialSymbols(String actionStr, 744 int line, 745 RuleBlock currentRule, 746 ActionTransInfo tInfo) { 747 return actionStr; 748 } 749 750 753 public String getASTCreateString(Vector v) { 754 return null; 755 } 756 757 760 public String getASTCreateString(GrammarAtom atom, String str) { 761 return null; 762 } 763 764 770 public String mapTreeId(String id, ActionTransInfo tInfo) { 771 return id; 772 } 773 774 779 public void printSet(int depth, int k, Lookahead lookahead) { 780 int numCols = 5; 781 782 int[] elems = lookahead.fset.toArray(); 783 784 if (depth != 1) { 785 print("k==" + k + ": {"); 786 } 787 else { 788 print("{ "); 789 } 790 if (elems.length > numCols) { 791 _println(""); 792 tabs++; 793 print(""); 794 } 795 796 int column = 0; 797 for (int i = 0; i < elems.length; i++) { 798 column++; 799 if (column > numCols) { 800 _println(""); 801 print(""); 802 column = 0; 803 } 804 if (doingLexRules) { 805 _print(charFormatter.literalChar(elems[i])); 806 } 807 else { 808 _print((String )grammar.tokenManager.getVocabulary().elementAt(elems[i])); 809 } 810 if (i != elems.length - 1) { 811 _print(", "); 812 } 813 } 814 815 if (elems.length > numCols) { 816 _println(""); 817 tabs--; 818 print(""); 819 } 820 _println(" }"); 821 } 822 } 823 | Popular Tags |