KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > TreeBlockContext


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 /**The context needed to add root,child elements to a Tree. There
10  * is only one alternative (i.e., a list of children). We subclass to
11  * specialize. MakeGrammar.addElementToCurrentAlt will work correctly
12  * now for either a block of alts or a Tree child list.
13  *
14  * The first time addAlternativeElement is called, it sets the root element
15  * rather than adding it to one of the alternative lists. Rather than have
16  * the grammar duplicate the rules for grammar atoms etc... we use the same
17  * grammar and same refToken behavior etc... We have to special case somewhere
18  * and here is where we do it.
19  */

20 class TreeBlockContext extends BlockContext {
21     protected boolean nextElementIsRoot = true;
22
23
24     public void addAlternativeElement(AlternativeElement e) {
25         TreeElement tree = (TreeElement)block;
26         if (nextElementIsRoot) {
27             tree.root = (GrammarAtom)e;
28             nextElementIsRoot = false;
29         }
30         else {
31             super.addAlternativeElement(e);
32         }
33     }
34 }
35
Popular Tags