KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > parsers > acc > NonTerminal


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core.parsers.acc;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.log4j.Logger;
24
25 /**
26  * Represents a non terminal rule
27  */

28 public class NonTerminal extends SyntaxElement {
29     static Logger logger = Logger.getLogger("acc.parser");
30
31     Vector JavaDoc children = new Vector JavaDoc();
32    
33     public NonTerminal(String JavaDoc name, SyntaxElement[] children) {
34         super(name,
35               children[0].getLeft(),
36               children[children.length-1].getRight());
37         for (int i=0; i<children.length; i++) {
38             addChild(children[i]);
39         }
40     }
41
42     public NonTerminal(String JavaDoc name, SyntaxElement child) {
43         super(name,child.getLeft(),child.getRight());
44         addChild(child);
45     }
46
47     public NonTerminal(String JavaDoc name) {
48         super(name,Integer.MAX_VALUE,-1);
49     }
50
51     /**
52      * Add child at the end
53      */

54     public void addChild(SyntaxElement se) {
55         children.add(se);
56         childAdded(se);
57     }
58
59     /**
60      * Add child at the beginning
61      */

62     public void insertChild(SyntaxElement se) {
63         children.add(0,se);
64         childAdded(se);
65     }
66
67     /**
68      * Returns the child at a given index
69      */

70     public SyntaxElement getChild(int index) {
71         return (SyntaxElement)children.get(index);
72     }
73
74     /**
75      * Returns a child with a given name, or null.
76      */

77     public SyntaxElement getChild(String JavaDoc name) {
78         Iterator JavaDoc it = children.iterator();
79         while (it.hasNext()) {
80             SyntaxElement child = (SyntaxElement)it.next();
81             if (child.getName().equals(name)) {
82                 return child;
83             }
84         }
85         return null;
86     }
87
88     protected void childAdded(SyntaxElement se) {
89         se.setParent(this);
90         if (se.getLeft()<left)
91             left = se.getLeft();
92         if (se.getRight()>right)
93             right = se.getRight();
94     }
95
96     /**
97      * Returns the terminal syntax element of a given position
98      * @param position the position
99      * @return A Terminal se such that se.getLeft()<=position &&
100      * se.getRight()>=position or null if there no such SyntaxElement.
101      */

102     public Terminal getTerminalAt(int position) {
103         Iterator JavaDoc it = children.iterator();
104         while (it.hasNext()) {
105             SyntaxElement se = (SyntaxElement)it.next();
106             if (se.getLeft()<=position && se.getRight()>=position) {
107                 logger.debug("Found "+se+" at "+position);
108                 if (se instanceof Terminal)
109                     return (Terminal)se;
110                 else
111                     return ((NonTerminal)se).getTerminalAt(position);
112             }
113         }
114         return null;
115     }
116
117
118     /**
119      * Returns the deepest syntax element at a given position
120      * @param position the position
121      * @return A SyntaxElement se such that se.getLeft()<=position &&
122      * se.getRight()>=position or null if there no such SyntaxElement.
123      */

124     public SyntaxElement getSyntaxElementAt(int position) {
125         Iterator JavaDoc it = children.iterator();
126         while (it.hasNext()) {
127             SyntaxElement se = (SyntaxElement)it.next();
128             if (se.getLeft()<=position && se.getRight()>=position) {
129                 logger.debug("Found "+se+" at "+position);
130                 if (se instanceof Terminal)
131                     return se;
132                 else
133                     return ((NonTerminal)se).getSyntaxElementAt(position);
134             }
135         }
136         if (getLeft()<=position && getRight()>=position)
137             return this;
138         else
139             return null;
140     }
141    
142 }
143
Popular Tags