KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > DocBookCodeGenerator


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/dev/klaren.dev/antlr/HTMLCodeGenerator.java#3 $
8  */

9
10 /** TODO: strip comments from javadoc entries
11  */

12
13 import java.util.Enumeration JavaDoc;
14
15 import antlr.collections.impl.BitSet;
16 import antlr.collections.impl.Vector;
17
18 import java.io.PrintWriter JavaDoc; //SAS: changed for proper text file io
19
import java.io.IOException JavaDoc;
20 import java.io.FileWriter JavaDoc;
21
22 /**Generate P.sgml, a cross-linked representation of P with or without actions */
23 public class DocBookCodeGenerator extends CodeGenerator {
24     /** non-zero if inside syntactic predicate generation */
25     protected int syntacticPredLevel = 0;
26
27     /** true during lexer generation, false during parser generation */
28     protected boolean doingLexRules = false;
29
30     protected boolean firstElementInAlt;
31
32     protected AlternativeElement prevAltElem = null; // what was generated last?
33

34     /** Create a Diagnostic code-generator using the given Grammar
35      * The caller must still call setTool, setBehavior, and setAnalyzer
36      * before generating code.
37      */

38     public DocBookCodeGenerator() {
39         super();
40         charFormatter = new JavaCharFormatter();
41     }
42
43     /** Encode a string for printing in a HTML document..
44      * e.g. encode '<' '>' and similar stuff
45      * @param s the string to encode
46      */

47     static String JavaDoc HTMLEncode(String JavaDoc s) {
48         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
49
50         for (int i = 0, len = s.length(); i < len; i++) {
51             char c = s.charAt(i);
52             if (c == '&')
53                 buf.append("&amp;");
54             else if (c == '\"')
55                 buf.append("&quot;");
56             else if (c == '\'')
57                 buf.append("&#039;");
58             else if (c == '<')
59                 buf.append("&lt;");
60             else if (c == '>')
61                 buf.append("&gt;");
62             else
63                 buf.append(c);
64         }
65         return buf.toString();
66     }
67
68     /** Encode a string for printing in a HTML document..
69      * e.g. encode '<' '>' and similar stuff
70      * @param s the string to encode
71      */

72     static String JavaDoc QuoteForId(String JavaDoc s) {
73         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
74
75         for (int i = 0, len = s.length(); i < len; i++) {
76             char c = s.charAt(i);
77             if (c == '_')
78                 buf.append(".");
79             else
80                 buf.append(c);
81         }
82         return buf.toString();
83     }
84
85     public void gen() {
86         // Do the code generation
87
try {
88             // Loop over all grammars
89
Enumeration JavaDoc grammarIter = behavior.grammars.elements();
90             while (grammarIter.hasMoreElements()) {
91                 Grammar g = (Grammar)grammarIter.nextElement();
92
93                 // Connect all the components to each other
94
/*
95                 g.setGrammarAnalyzer(analyzer);
96                 analyzer.setGrammar(g);
97                 */

98                 g.setCodeGenerator(this);
99
100                 // To get right overloading behavior across hetrogeneous grammars
101
g.generate();
102
103                 if (antlrTool.hasError()) {
104                     System.out.println("Exiting due to errors.");
105                     System.exit(1);
106                 }
107
108             }
109
110         }
111         catch (IOException JavaDoc e) {
112             System.out.println(e.getMessage());
113         }
114     }
115
116     /** Generate code for the given grammar element.
117      * @param blk The {...} action to generate
118      */

119     public void gen(ActionElement action) {
120         // no-op
121
}
122
123     /** Generate code for the given grammar element.
124      * @param blk The "x|y|z|..." block to generate
125      */

126     public void gen(AlternativeBlock blk) {
127         genGenericBlock(blk, "");
128     }
129
130     /** Generate code for the given grammar element.
131      * @param blk The block-end element to generate. Block-end
132      * elements are synthesized by the grammar parser to represent
133      * the end of a block.
134      */

135     public void gen(BlockEndElement end) {
136         // no-op
137
}
138
139     /** Generate code for the given grammar element.
140      * @param blk The character literal reference to generate
141      */

142     public void gen(CharLiteralElement atom) {
143         if (atom.not) {
144             _print("~");
145         }
146         _print(HTMLEncode(atom.atomText) + " ");
147     }
148
149     /** Generate code for the given grammar element.
150      * @param blk The character-range reference to generate
151      */

152     public void gen(CharRangeElement r) {
153         print(r.beginText + ".." + r.endText + " ");
154     }
155
156     /** Generate the lexer HTML file */
157     public void gen(LexerGrammar g) throws IOException JavaDoc {
158         setGrammar(g);
159         System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
160         currentOutput = antlrTool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
161         //SAS: changed for proper text file io
162

163         tabs = 0;
164         doingLexRules = true;
165
166         // Generate header common to all TXT output files
167
genHeader();
168
169         // Output the user-defined lexer premamble
170
// RK: guess not..
171
// println(grammar.preambleAction.getText());
172

173         // Generate lexer class definition
174
println("");
175
176         // print javadoc comment if any
177
if (grammar.comment != null) {
178             _println(HTMLEncode(grammar.comment));
179         }
180
181         println("<para>Definition of lexer " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>");
182
183         // Generate user-defined parser class members
184
// printAction(grammar.classMemberAction.getText());
185

186         /*
187         // Generate string literals
188         println("");
189         println("*** String literals used in the parser");
190         println("The following string literals were used in the parser.");
191         println("An actual code generator would arrange to place these literals");
192         println("into a table in the generated lexer, so that actions in the");
193         println("generated lexer could match token text against the literals.");
194         println("String literals used in the lexer are not listed here, as they");
195         println("are incorporated into the mainstream lexer processing.");
196         tabs++;
197         // Enumerate all of the symbols and look for string literal symbols
198         Enumeration ids = grammar.getSymbols();
199         while ( ids.hasMoreElements() ) {
200             GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
201             // Only processing string literals -- reject other symbol entries
202             if ( sym instanceof StringLiteralSymbol ) {
203                 StringLiteralSymbol s = (StringLiteralSymbol)sym;
204                 println(s.getId() + " = " + s.getTokenType());
205             }
206         }
207         tabs--;
208         println("*** End of string literals used by the parser");
209         */

210
211         // Generate nextToken() rule.
212
// nextToken() is a synthetic lexer rule that is the implicit OR of all
213
// user-defined lexer rules.
214
genNextToken();
215
216         // Generate code for each rule in the lexer
217

218         Enumeration JavaDoc ids = grammar.rules.elements();
219         while (ids.hasMoreElements()) {
220             RuleSymbol rs = (RuleSymbol)ids.nextElement();
221             if (!rs.id.equals("mnextToken")) {
222                 genRule(rs);
223             }
224         }
225
226         // Close the lexer output file
227
currentOutput.close();
228         currentOutput = null;
229         doingLexRules = false;
230     }
231
232     /** Generate code for the given grammar element.
233      * @param blk The (...)+ block to generate
234      */

235     public void gen(OneOrMoreBlock blk) {
236         genGenericBlock(blk, "+");
237     }
238
239     /** Generate the parser HTML file */
240     public void gen(ParserGrammar g) throws IOException JavaDoc {
241         setGrammar(g);
242         // Open the output stream for the parser and set the currentOutput
243
System.out.println("Generating " + grammar.getClassName() + ".sgml");
244         currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".sgml");
245
246         tabs = 0;
247
248         // Generate the header common to all output files.
249
genHeader();
250
251         // Generate parser class definition
252
println("");
253
254         // print javadoc comment if any
255
if (grammar.comment != null) {
256             _println(HTMLEncode(grammar.comment));
257         }
258
259         println("<para>Definition of parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>");
260
261         // Enumerate the parser rules
262
Enumeration JavaDoc rules = grammar.rules.elements();
263         while (rules.hasMoreElements()) {
264             println("");
265             // Get the rules from the list and downcast it to proper type
266
GrammarSymbol sym = (GrammarSymbol)rules.nextElement();
267             // Only process parser rules
268
if (sym instanceof RuleSymbol) {
269                 genRule((RuleSymbol)sym);
270             }
271         }
272         tabs--;
273         println("");
274
275         genTail();
276
277         // Close the parser output stream
278
currentOutput.close();
279         currentOutput = null;
280     }
281
282     /** Generate code for the given grammar element.
283      * @param blk The rule-reference to generate
284      */

285     public void gen(RuleRefElement rr) {
286         RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
287
288         // Generate the actual rule description
289
_print("<link linkend=\"" + QuoteForId(rr.targetRule) + "\">");
290         _print(rr.targetRule);
291         _print("</link>");
292         // RK: Leave out args..
293
// if (rr.args != null) {
294
// _print("["+rr.args+"]");
295
// }
296
_print(" ");
297     }
298
299     /** Generate code for the given grammar element.
300      * @param blk The string-literal reference to generate
301      */

302     public void gen(StringLiteralElement atom) {
303         if (atom.not) {
304             _print("~");
305         }
306         _print(HTMLEncode(atom.atomText));
307         _print(" ");
308     }
309
310     /** Generate code for the given grammar element.
311      * @param blk The token-range reference to generate
312      */

313     public void gen(TokenRangeElement r) {
314         print(r.beginText + ".." + r.endText + " ");
315     }
316
317     /** Generate code for the given grammar element.
318      * @param blk The token-reference to generate
319      */

320     public void gen(TokenRefElement atom) {
321         if (atom.not) {
322             _print("~");
323         }
324         _print(atom.atomText);
325         _print(" ");
326     }
327
328     public void gen(TreeElement t) {
329         print(t + " ");
330     }
331
332     /** Generate the tree-walker TXT file */
333     public void gen(TreeWalkerGrammar g) throws IOException JavaDoc {
334         setGrammar(g);
335         // Open the output stream for the parser and set the currentOutput
336
System.out.println("Generating " + grammar.getClassName() + ".sgml");
337         currentOutput = antlrTool.openOutputFile(grammar.getClassName() + ".sgml");
338         //SAS: changed for proper text file io
339

340         tabs = 0;
341
342         // Generate the header common to all output files.
343
genHeader();
344
345         // Output the user-defined parser premamble
346
println("");
347 // println("*** Tree-walker Preamble Action.");
348
// println("This action will appear before the declaration of your tree-walker class:");
349
// tabs++;
350
// println(grammar.preambleAction.getText());
351
// tabs--;
352
// println("*** End of tree-walker Preamble Action");
353

354         // Generate tree-walker class definition
355
println("");
356
357         // print javadoc comment if any
358
if (grammar.comment != null) {
359             _println(HTMLEncode(grammar.comment));
360         }
361
362         println("<para>Definition of tree parser " + grammar.getClassName() + ", which is a subclass of " + grammar.getSuperClass() + ".</para>");
363
364         // Generate user-defined tree-walker class members
365
// println("");
366
// println("*** User-defined tree-walker class members:");
367
// println("These are the member declarations that you defined for your class:");
368
// tabs++;
369
// printAction(grammar.classMemberAction.getText());
370
// tabs--;
371
// println("*** End of user-defined tree-walker class members");
372

373         // Generate code for each rule in the grammar
374
println("");
375 // println("*** tree-walker rules:");
376
tabs++;
377
378         // Enumerate the tree-walker rules
379
Enumeration JavaDoc rules = grammar.rules.elements();
380         while (rules.hasMoreElements()) {
381             println("");
382             // Get the rules from the list and downcast it to proper type
383
GrammarSymbol sym = (GrammarSymbol)rules.nextElement();
384             // Only process tree-walker rules
385
if (sym instanceof RuleSymbol) {
386                 genRule((RuleSymbol)sym);
387             }
388         }
389         tabs--;
390         println("");
391 // println("*** End of tree-walker rules");
392

393 // println("");
394
// println("*** End of tree-walker");
395

396         // Close the tree-walker output stream
397
currentOutput.close();
398         currentOutput = null;
399     }
400
401     /** Generate a wildcard element */
402     public void gen(WildcardElement wc) {
403         /*
404         if ( wc.getLabel()!=null ) {
405             _print(wc.getLabel()+"=");
406         }
407         */

408         _print(". ");
409     }
410
411     /** Generate code for the given grammar element.
412      * @param blk The (...)* block to generate
413      */

414     public void gen(ZeroOrMoreBlock blk) {
415         genGenericBlock(blk, "*");
416     }
417
418     protected void genAlt(Alternative alt) {
419         if (alt.getTreeSpecifier() != null) {
420             _print(alt.getTreeSpecifier().getText());
421         }
422         prevAltElem = null;
423         for (AlternativeElement elem = alt.head;
424              !(elem instanceof BlockEndElement);
425              elem = elem.next) {
426             elem.generate();
427             firstElementInAlt = false;
428             prevAltElem = elem;
429         }
430     }
431     /** Generate the header for a block, which may be a RuleBlock or a
432      * plain AlternativeBLock. This generates any variable declarations,
433      * init-actions, and syntactic-predicate-testing variables.
434      * @blk The block for which the preamble is to be generated.
435      */

436 // protected void genBlockPreamble(AlternativeBlock blk) {
437
// RK: don't dump out init actions
438
// dump out init action
439
// if ( blk.initAction!=null ) {
440
// printAction("{" + blk.initAction + "}");
441
// }
442
// }
443
/** Generate common code for a block of alternatives; return a postscript
444      * that needs to be generated at the end of the block. Other routines
445      * may append else-clauses and such for error checking before the postfix
446      * is generated.
447      */

448     public void genCommonBlock(AlternativeBlock blk) {
449         if (blk.alternatives.size() > 1)
450             println("<itemizedlist mark=\"none\">");
451         for (int i = 0; i < blk.alternatives.size(); i++) {
452             Alternative alt = blk.getAlternativeAt(i);
453             AlternativeElement elem = alt.head;
454
455             if (blk.alternatives.size() > 1)
456                 print("<listitem><para>");
457
458             // dump alt operator |
459
if (i > 0 && blk.alternatives.size() > 1) {
460                 _print("| ");
461             }
462
463             // Dump the alternative, starting with predicates
464
//
465
boolean save = firstElementInAlt;
466             firstElementInAlt = true;
467             tabs++; // in case we do a newline in alt, increase the tab indent
468

469             genAlt(alt);
470             tabs--;
471             firstElementInAlt = save;
472             if (blk.alternatives.size() > 1)
473                 _println("</para></listitem>");
474         }
475         if (blk.alternatives.size() > 1)
476             println("</itemizedlist>");
477     }
478
479     /** Generate a textual representation of the follow set
480      * for a block.
481      * @param blk The rule block of interest
482      */

483     public void genFollowSetForRuleBlock(RuleBlock blk) {
484         Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode);
485         printSet(grammar.maxk, 1, follow);
486     }
487
488     protected void genGenericBlock(AlternativeBlock blk, String JavaDoc blkOp) {
489         if (blk.alternatives.size() > 1) {
490             // make sure we start on a new line
491
_println("");
492             if (!firstElementInAlt) {
493                 // only do newline if the last element wasn't a multi-line block
494
//if ( prevAltElem==null ||
495
// !(prevAltElem instanceof AlternativeBlock) ||
496
// ((AlternativeBlock)prevAltElem).alternatives.size()==1 )
497
//{
498
_println("(");
499                 //}
500
//else
501
//{
502
// _print("(");
503
//}
504
// _println("");
505
// print("(\t");
506
}
507             else {
508                 _print("(");
509             }
510         }
511         else {
512             _print("( ");
513         }
514         // RK: don't dump init actions
515
// genBlockPreamble(blk);
516
genCommonBlock(blk);
517         if (blk.alternatives.size() > 1) {
518             _println("");
519             print(")" + blkOp + " ");
520             // if not last element of alt, need newline & to indent
521
if (!(blk.next instanceof BlockEndElement)) {
522                 _println("");
523                 print("");
524             }
525         }
526         else {
527             _print(")" + blkOp + " ");
528         }
529     }
530
531     /** Generate a header that is common to all TXT files */
532     protected void genHeader() {
533         println("<?xml version=\"1.0\" standalone=\"no\"?>");
534         println("<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook V3.1//EN\">");
535         println("<book lang=\"en\">");
536         println("<bookinfo>");
537         println("<title>Grammar " + grammar.getClassName() + "</title>");
538         println(" <author>");
539         println(" <firstname></firstname>");
540         println(" <othername></othername>");
541         println(" <surname></surname>");
542         println(" <affiliation>");
543         println(" <address>");
544         println(" <email></email>");
545         println(" </address>");
546         println(" </affiliation>");
547         println(" </author>");
548         println(" <othercredit>");
549         println(" <contrib>");
550         println(" Generated by <ulink url=\"http://www.ANTLR.org/\">ANTLR</ulink>" + antlrTool.version);
551         println(" from " + antlrTool.grammarFile);
552         println(" </contrib>");
553         println(" </othercredit>");
554         println(" <pubdate></pubdate>");
555         println(" <abstract>");
556         println(" <para>");
557         println(" </para>");
558         println(" </abstract>");
559         println("</bookinfo>");
560         println("<chapter>");
561         println("<title></title>");
562     }
563
564     /**Generate the lookahead set for an alternate. */
565     protected void genLookaheadSetForAlt(Alternative alt) {
566         if (doingLexRules && alt.cache[1].containsEpsilon()) {
567             println("MATCHES ALL");
568             return;
569         }
570         int depth = alt.lookaheadDepth;
571         if (depth == GrammarAnalyzer.NONDETERMINISTIC) {
572             // if the decision is nondeterministic, do the best we can: LL(k)
573
// any predicates that are around will be generated later.
574
depth = grammar.maxk;
575         }
576         for (int i = 1; i <= depth; i++) {
577             Lookahead lookahead = alt.cache[i];
578             printSet(depth, i, lookahead);
579         }
580     }
581
582     /** Generate a textual representation of the lookahead set
583      * for a block.
584      * @param blk The block of interest
585      */

586     public void genLookaheadSetForBlock(AlternativeBlock blk) {
587         // Find the maximal lookahead depth over all alternatives
588
int depth = 0;
589         for (int i = 0; i < blk.alternatives.size(); i++) {
590             Alternative alt = blk.getAlternativeAt(i);
591             if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) {
592                 depth = grammar.maxk;
593                 break;
594             }
595             else if (depth < alt.lookaheadDepth) {
596                 depth = alt.lookaheadDepth;
597             }
598         }
599
600         for (int i = 1; i <= depth; i++) {
601             Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk);
602             printSet(depth, i, lookahead);
603         }
604     }
605
606     /** Generate the nextToken rule.
607      * nextToken is a synthetic lexer rule that is the implicit OR of all
608      * user-defined lexer rules.
609      */

610     public void genNextToken() {
611         println("");
612         println("/** Lexer nextToken rule:");
613         println(" * The lexer nextToken rule is synthesized from all of the user-defined");
614         println(" * lexer rules. It logically consists of one big alternative block with");
615         println(" * each user-defined rule being an alternative.");
616         println(" */");
617
618         // Create the synthesized rule block for nextToken consisting
619
// of an alternate block containing all the user-defined lexer rules.
620
RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
621
622         // Define the nextToken rule symbol
623
RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
624         nextTokenRs.setDefined();
625         nextTokenRs.setBlock(blk);
626         nextTokenRs.access = "private";
627         grammar.define(nextTokenRs);
628
629         /*
630         // Analyze the synthesized block
631         if (!grammar.theLLkAnalyzer.deterministic(blk))
632         {
633             println("The grammar analyzer has determined that the synthesized");
634             println("nextToken rule is non-deterministic (i.e., it has ambiguities)");
635             println("This means that there is some overlap of the character");
636             println("lookahead for two or more of your lexer rules.");
637         }
638         */

639
640         genCommonBlock(blk);
641     }
642
643     /** Generate code for a named rule block
644      * @param s The RuleSymbol describing the rule to generate
645      */

646     public void genRule(RuleSymbol s) {
647         if (s == null || !s.isDefined()) return; // undefined rule
648
println("");
649
650         if (s.access.length() != 0) {
651             if (!s.access.equals("public")) {
652                 _print("<para>" + s.access + " </para>");
653             }
654         }
655
656         println("<section id=\"" + QuoteForId(s.getId()) + "\">");
657         println("<title>" + s.getId() + "</title>");
658         if (s.comment != null) {
659             _println("<para>" + HTMLEncode(s.comment) + "</para>");
660         }
661         println("<para>");
662
663         // Get rule return type and arguments
664
RuleBlock rblk = s.getBlock();
665
666         // RK: for HTML output not of much value...
667
// Gen method return value(s)
668
// if (rblk.returnAction != null) {
669
// _print("["+rblk.returnAction+"]");
670
// }
671
// Gen arguments
672
// if (rblk.argAction != null)
673
// {
674
// _print(" returns [" + rblk.argAction+"]");
675
// }
676
_println("");
677         print(s.getId() + ":\t");
678         tabs++;
679
680         // Dump any init-action
681
// genBlockPreamble(rblk);
682

683         // Dump the alternates of the rule
684
genCommonBlock(rblk);
685
686         _println("");
687 // println(";");
688
tabs--;
689         _println("</para>");
690         _println("</section><!-- section \"" + s.getId() + "\" -->");
691     }
692
693     /** Generate the syntactic predicate. This basically generates
694      * the alternative block, buts tracks if we are inside a synPred
695      * @param blk The syntactic predicate block
696      */

697     protected void genSynPred(SynPredBlock blk) {
698         // no op
699
}
700
701     public void genTail() {
702         println("</chapter>");
703         println("</book>");
704     }
705
706     /** Generate the token types TXT file */
707     protected void genTokenTypes(TokenManager tm) throws IOException JavaDoc {
708         // Open the token output TXT file and set the currentOutput stream
709
System.out.println("Generating " + tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt);
710         currentOutput = antlrTool.openOutputFile(tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt);
711         //SAS: changed for proper text file io
712
tabs = 0;
713
714         // Generate the header common to all diagnostic files
715
genHeader();
716
717         // Generate a string for each token. This creates a static
718
// array of Strings indexed by token type.
719
println("");
720         println("*** Tokens used by the parser");
721         println("This is a list of the token numeric values and the corresponding");
722         println("token identifiers. Some tokens are literals, and because of that");
723         println("they have no identifiers. Literals are double-quoted.");
724         tabs++;
725
726         // Enumerate all the valid token types
727
Vector v = tm.getVocabulary();
728         for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
729             String JavaDoc s = (String JavaDoc)v.elementAt(i);
730             if (s != null) {
731                 println(s + " = " + i);
732             }
733         }
734
735         // Close the interface
736
tabs--;
737         println("*** End of tokens used by the parser");
738
739         // Close the tokens output file
740
currentOutput.close();
741         currentOutput = null;
742     }
743
744     /// unused.
745
protected String JavaDoc processActionForTreeSpecifiers(String JavaDoc actionStr,
746                                                     int line,
747                                                     RuleBlock currentRule,
748                                                     ActionTransInfo tInfo) {
749         return actionStr;
750     }
751
752     /** Get a string for an expression to generate creation of an AST subtree.
753      * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
754      */

755     public String JavaDoc getASTCreateString(Vector v) {
756         return null;
757     }
758
759     /** Get a string for an expression to generate creating of an AST node
760      * @param str The arguments to the AST constructor
761      */

762     public String JavaDoc getASTCreateString(GrammarAtom atom, String JavaDoc str) {
763         return null;
764     }
765
766     /** Map an identifier to it's corresponding tree-node variable.
767      * This is context-sensitive, depending on the rule and alternative
768      * being generated
769      * @param id The identifier name to map
770      * @param forInput true if the input tree node variable is to be returned, otherwise the output variable is returned.
771      */

772     public String JavaDoc mapTreeId(String JavaDoc id, ActionTransInfo tInfo) {
773         return id;
774     }
775
776     /** Format a lookahead or follow set.
777      * @param depth The depth of the entire lookahead/follow
778      * @param k The lookahead level to print
779      * @param lookahead The lookahead/follow set to print
780      */

781     public void printSet(int depth, int k, Lookahead lookahead) {
782         int numCols = 5;
783
784         int[] elems = lookahead.fset.toArray();
785
786         if (depth != 1) {
787             print("k==" + k + ": {");
788         }
789         else {
790             print("{ ");
791         }
792         if (elems.length > numCols) {
793             _println("");
794             tabs++;
795             print("");
796         }
797
798         int column = 0;
799         for (int i = 0; i < elems.length; i++) {
800             column++;
801             if (column > numCols) {
802                 _println("");
803                 print("");
804                 column = 0;
805             }
806             if (doingLexRules) {
807                 _print(charFormatter.literalChar(elems[i]));
808             }
809             else {
810                 _print((String JavaDoc)grammar.tokenManager.getVocabulary().elementAt(elems[i]));
811             }
812             if (i != elems.length - 1) {
813                 _print(", ");
814             }
815         }
816
817         if (elems.length > numCols) {
818             _println("");
819             tabs--;
820             print("");
821         }
822         _println(" }");
823     }
824 }
825
Popular Tags