KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > Production


1 /*
2  * Production.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * 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 library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser;
23
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * A production node. This class represents a grammar production (i.e.
28  * a list of child nodes) in a parse tree. The productions are created
29  * by a parser, that adds children a according to a set of production
30  * patterns (i.e. grammar rules).
31  *
32  * @author Per Cederberg, <per at percederberg dot net>
33  * @version 1.1
34  */

35 public class Production extends Node {
36
37     /**
38      * The production pattern used for this production.
39      */

40     private ProductionPattern pattern;
41
42     /**
43      * The child nodes.
44      */

45     private ArrayList JavaDoc children;
46
47     /**
48      * Creates a new production node.
49      *
50      * @param pattern the production pattern
51      */

52     public Production(ProductionPattern pattern) {
53         this.pattern = pattern;
54         this.children = new ArrayList JavaDoc();
55     }
56
57     /**
58      * Checks if this node is hidden, i.e. if it should not be visible
59      * outside the parser.
60      *
61      * @return true if the node should be hidden, or
62      * false otherwise
63      */

64     boolean isHidden() {
65         return pattern.isSynthetic();
66     }
67
68     /**
69      * Returns the production pattern for this production.
70      *
71      * @return the production pattern
72      */

73     public ProductionPattern getPattern() {
74         return pattern;
75     }
76
77     /**
78      * Returns the production (pattern) id. This value is set as a
79      * unique identifier when creating the production pattern to
80      * simplify later identification.
81      *
82      * @return the production id
83      */

84     public int getId() {
85         return pattern.getId();
86     }
87
88     /**
89      * Returns the production node name.
90      *
91      * @return the production node name
92      */

93     public String JavaDoc getName() {
94         return pattern.getName();
95     }
96
97     /**
98      * Returns the number of child nodes.
99      *
100      * @return the number of child nodes
101      */

102     public int getChildCount() {
103         return children.size();
104     }
105
106     /**
107      * Returns the child node with the specified index.
108      *
109      * @param index the child index, 0 <= index < count
110      *
111      * @return the child node found, or
112      * null if index out of bounds
113      */

114     public Node getChildAt(int index) {
115         if (index < 0 || index >= children.size()) {
116             return null;
117         } else {
118             return (Node) children.get(index);
119         }
120     }
121
122     /**
123      * Adds a child node. The node will be added last in the list of
124      * children.
125      *
126      * @param child the child node to add
127      */

128     public void addChild(Node child) {
129         if (child != null) {
130             child.setParent(this);
131             children.add(child);
132         }
133     }
134
135     /**
136      * Returns a string representation of this production.
137      *
138      * @return a string representation of this production
139      */

140     public String JavaDoc toString() {
141         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
142
143         buffer.append(pattern.getName());
144         buffer.append('(');
145         buffer.append(pattern.getId());
146         buffer.append(')');
147
148         return buffer.toString();
149     }
150 }
151
Popular Tags