KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > jjtree > ASTBNFAction


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.jjtree;
24
25
26 public class ASTBNFAction extends SimpleNode {
27   ASTBNFAction(int id) {
28     super(id);
29   }
30
31   private Node getScopingParent(NodeScope ns)
32   {
33     for (Node n = this.jjtGetParent(); n != null; n = n.jjtGetParent()) {
34       if (n instanceof ASTBNFNodeScope) {
35     if (((ASTBNFNodeScope)n).node_scope == ns) {
36       return n;
37     }
38       } else if (n instanceof ASTExpansionNodeScope) {
39     if (((ASTExpansionNodeScope)n).node_scope == ns) {
40       return n;
41     }
42       }
43     }
44     return null;
45   }
46
47
48   public void print(IO io)
49   {
50     /* Assume that this action requires an early node close, and then
51        try to decide whether this assumption is false. Do this by
52        looking outwards through the enclosing expansion units. If we
53        ever find that we are enclosed in a unit which is not the final
54        unit in a sequence we know that an early close is not
55        required. */

56
57     NodeScope ns = NodeScope.getEnclosingNodeScope(this);
58     if (ns != null && !ns.isVoid()) {
59       boolean needClose = true;
60       Node sp = getScopingParent(ns);
61
62       SimpleNode n = this;
63       while (true) {
64     Node p = n.jjtGetParent();
65     if (p instanceof ASTBNFSequence || p instanceof ASTBNFTryBlock) {
66       if (n.getOrdinal() != p.jjtGetNumChildren() - 1) {
67         /* We're not the final unit in the sequence. */
68         needClose = false;
69         break;
70       }
71     } else if (p instanceof ASTBNFZeroOrOne ||
72              p instanceof ASTBNFZeroOrMore ||
73              p instanceof ASTBNFOneOrMore) {
74       needClose = false;
75       break;
76     }
77     if (p == sp) {
78       /* No more parents to look at. */
79       break;
80     }
81     n = (SimpleNode)p;
82       }
83       if (needClose) {
84     openJJTreeComment(io, null);
85     io.println();
86     ns.insertCloseNodeAction(io, getIndentation(this));
87     closeJJTreeComment(io);
88       }
89     }
90     super.print(io);
91   }
92
93
94 }
95
96 /*end*/
97
Popular Tags