1 /** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 package cppast; 30 31 /** 32 * Provides a custom base node implementation. 33 * 34 * @author Mathieu Champlon 35 */ 36 public abstract class SimpleNode implements Node 37 { 38 private Node parent; 39 private Node[] children = new Node[0]; 40 private final String name; 41 private Token first, last; 42 private Scope scope; 43 44 /** 45 * Create a node. 46 * 47 * @param id the node identifier 48 */ 49 public SimpleNode( final int id ) 50 { 51 this.name = ParserTreeConstants.jjtNodeName[id]; 52 } 53 54 /** 55 * Create a node. 56 * 57 * @param parser the parser 58 * @param id the node identifier 59 */ 60 public SimpleNode( final Parser parser, final int id ) 61 { 62 this( id ); 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 public final void jjtOpen() 69 { 70 } 71 72 /** 73 * {@inheritDoc} 74 */ 75 public final void jjtClose() 76 { 77 } 78 79 /** 80 * Open the node scope. 81 * 82 * @param token the first token of the node 83 * @param scope the scope of the node 84 */ 85 public final void openScope( final Token token, final Scope scope ) 86 { 87 this.first = token; 88 this.scope = scope; 89 } 90 91 /** 92 * Close the node scope. 93 * 94 * @param token the last token of the node 95 */ 96 public final void closeScope( final Token token ) 97 { 98 last = token; 99 } 100 101 /** 102 * Resolve a symbol to a fully scoped name. 103 * 104 * @param name the symbol 105 * @return a fully scoped symbol name 106 */ 107 public final String resolve( final String name ) 108 { 109 return scope.resolve( name ); 110 } 111 112 /** 113 * Retrieve the node first token. 114 * 115 * @return a token 116 */ 117 public final Token getFirstToken() 118 { 119 return first; 120 } 121 122 /** 123 * Retrive the node last token. 124 * 125 * @return a token 126 */ 127 public final Token getLastToken() 128 { 129 return last; 130 } 131 132 /** 133 * {@inheritDoc} 134 */ 135 public final void jjtSetParent( final Node node ) 136 { 137 parent = node; 138 } 139 140 /** 141 * {@inheritDoc} 142 */ 143 public final Node jjtGetParent() 144 { 145 return parent; 146 } 147 148 /** 149 * {@inheritDoc} 150 */ 151 public final void jjtAddChild( final Node node, final int index ) 152 { 153 if( index >= children.length ) 154 { 155 final Node[] c = new Node[index + 1]; 156 System.arraycopy( children, 0, c, 0, children.length ); 157 children = c; 158 } 159 children[index] = node; 160 } 161 162 /** 163 * {@inheritDoc} 164 */ 165 public final Node jjtGetChild( final int index ) 166 { 167 return children[index]; 168 } 169 170 /** 171 * {@inheritDoc} 172 */ 173 public final int jjtGetNumChildren() 174 { 175 return children.length; 176 } 177 178 /** 179 * Visit the children of the node. 180 * 181 * @param visitor the visitor 182 * @param data the custom data 183 * @return a custom result 184 */ 185 public final Object accept( final ParserVisitor visitor, final Object data ) 186 { 187 Object result = data; 188 for( int index = 0; index < children.length; ++index ) 189 result = children[index].jjtAccept( visitor, result ); 190 return result; 191 } 192 193 /** 194 * {@inheritDoc} 195 */ 196 public final String toString() 197 { 198 return name; 199 } 200 }