1 2 23 24 25 package org.javacc.jjdoc; 26 27 import java.io.PrintWriter ; 28 import java.util.Hashtable ; 29 30 import org.javacc.parser.*; 31 32 public class HTMLGenerator extends Generator { 33 private Hashtable id_map = new Hashtable (); 34 private int id = 1; 35 36 public HTMLGenerator(PrintWriter o) { 37 super(o); 38 } 39 40 private String get_id(String nt) { 41 String i = (String )id_map.get(nt); 42 if (i == null) { 43 i = "prod" + id++; 44 id_map.put(nt, i); 45 } 46 return i; 47 } 48 49 private void println(String s) { 50 print(s + "\n"); 51 } 52 53 public void text(String s) { 54 String ss = ""; 55 for (int i = 0; i < s.length(); ++i) { 56 if (s.charAt(i) == '<') { 57 ss += "<"; 58 } else if (s.charAt(i) == '>') { 59 ss += ">"; 60 } else if (s.charAt(i) == '&') { 61 ss += "&"; 62 } else { 63 ss += s.charAt(i); 64 } 65 } 66 print(ss); 67 } 68 69 public void print(String s) { 70 ostr.print(s); 71 } 72 73 public void specialTokens(String s) { 74 if (!JJDocOptions.getOneTable()) { 75 println("<PRE>"); 76 print(s); 77 println("</PRE>"); 78 } 79 } 80 81 public void documentStart() { 82 println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">"); 83 println("<HTML>"); 84 println("<HEAD>"); 85 if (JJDocGlobals.input_file != null) { 86 println("<TITLE>BNF for " + JJDocGlobals.input_file + "</TITLE>"); 87 } else { 88 println("<TITLE>A BNF grammar by JJDoc</TITLE>"); 89 } 90 println("</HEAD>"); 91 println("<BODY>"); 92 println("<H1 ALIGN=CENTER>BNF for " + JJDocGlobals.input_file + "</H1>"); 93 } 94 public void documentEnd() { 95 println("</BODY>"); 96 println("</HTML>"); 97 } 98 99 public void nonterminalsStart() { 100 println("<H2 ALIGN=CENTER>NON-TERMINALS</H2>"); 101 if (JJDocOptions.getOneTable()) { 102 println("<TABLE>"); 103 } 104 } 105 public void nonterminalsEnd() { 106 if (JJDocOptions.getOneTable()) { 107 println("</TABLE>"); 108 } 109 } 110 111 public void tokensStart() { 112 println("<H2 ALIGN=CENTER>TOKENS</H2>"); 113 println("<TABLE>"); 114 } 115 public void tokensEnd() { 116 println("</TABLE>"); 117 } 118 119 public void javacode(JavaCodeProduction jp) { 120 productionStart(jp); 121 println("<I>java code</I></TD></TR>"); 122 productionEnd(jp); 123 } 124 125 public void productionStart(NormalProduction np) { 126 if (!JJDocOptions.getOneTable()) { 127 println(""); 128 println("<TABLE ALIGN=CENTER>"); 129 println("<CAPTION><STRONG>" + np.lhs + "</STRONG></CAPTION>"); 130 } 131 println("<TR>"); 132 println("<TD ALIGN=RIGHT VALIGN=BASELINE><A NAME=\"" + get_id(np.lhs) + "\">" + np.lhs + "</A></TD>"); 133 println("<TD ALIGN=CENTER VALIGN=BASELINE>::=</TD>"); 134 print("<TD ALIGN=LEFT VALIGN=BASELINE>"); 135 } 136 public void productionEnd(NormalProduction np) { 137 if (!JJDocOptions.getOneTable()) { 138 println("</TABLE>"); 139 println("<HR>"); 140 } 141 } 142 143 public void expansionStart(Expansion e, boolean first) { 144 if (!first) { 145 println("<TR>"); 146 println("<TD ALIGN=RIGHT VALIGN=BASELINE></TD>"); 147 println("<TD ALIGN=CENTER VALIGN=BASELINE>|</TD>"); 148 print("<TD ALIGN=LEFT VALIGN=BASELINE>"); 149 } 150 } 151 public void expansionEnd(Expansion e, boolean first) { 152 println("</TD>"); 153 println("</TR>"); 154 } 155 156 public void nonTerminalStart(NonTerminal nt) { 157 print("<A HREF=\"#" + get_id(nt.name) + "\">"); 158 } 159 public void nonTerminalEnd(NonTerminal nt) { 160 print("</A>"); 161 } 162 163 public void reStart(RegularExpression r) { 164 } 165 public void reEnd(RegularExpression r) { 166 } 167 } 168 | Popular Tags |