1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
17 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
18 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
19 import net.sourceforge.phpdt.internal.compiler.parser.TerminalTokens;
22 * Number literal nodes.
25 * @noinstantiate This class is not intended to be instantiated by clients.
27 public class NumberLiteral extends Expression {
30 * The "token" structural property of this node type.
33 public static final SimplePropertyDescriptor TOKEN_PROPERTY =
34 new SimplePropertyDescriptor(NumberLiteral.class, "token", String.class, MANDATORY); //$NON-NLS-1$
37 * A list of property descriptors (element type:
38 * {@link StructuralPropertyDescriptor}),
39 * or null if uninitialized.
41 private static final List PROPERTY_DESCRIPTORS;
44 List propertyList = new ArrayList(2);
45 createPropertyList(NumberLiteral.class, propertyList);
46 addProperty(TOKEN_PROPERTY, propertyList);
47 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
51 * Returns a list of structural property descriptors for this node type.
52 * Clients must not modify the result.
54 * @param apiLevel the API level; one of the
55 * <code>AST.JLS*</code> constants
57 * @return a list of property descriptors (element type:
58 * {@link StructuralPropertyDescriptor})
61 public static List propertyDescriptors(int apiLevel) {
62 return PROPERTY_DESCRIPTORS;
66 * The token string; defaults to the integer literal "0".
68 private String tokenValue = "0";//$NON-NLS-1$
71 * Creates a new unparented number literal node owned by the given AST.
72 * By default, the number literal is the token "<code>0</code>".
74 * N.B. This constructor is package-private.
77 * @param ast the AST that is to own this node
79 NumberLiteral(AST ast) {
83 /* (omit javadoc for this method)
84 * Method declared on ASTNode.
86 final List internalStructuralPropertiesForType(int apiLevel) {
87 return propertyDescriptors(apiLevel);
90 /* (omit javadoc for this method)
91 * Method declared on ASTNode.
93 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
94 if (property == TOKEN_PROPERTY) {
98 setToken((String) value);
102 // allow default implementation to flag the error
103 return super.internalGetSetObjectProperty(property, get, value);
106 /* (omit javadoc for this method)
107 * Method declared on ASTNode.
109 final int getNodeType0() {
110 return NUMBER_LITERAL;
113 /* (omit javadoc for this method)
114 * Method declared on ASTNode.
116 ASTNode clone0(AST target) {
117 NumberLiteral result = new NumberLiteral(target);
118 result.setSourceRange(this.getStartPosition(), this.getLength());
119 result.setToken(getToken());
123 /* (omit javadoc for this method)
124 * Method declared on ASTNode.
126 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
127 // dispatch to correct overloaded match method
128 return matcher.match(this, other);
131 /* (omit javadoc for this method)
132 * Method declared on ASTNode.
134 void accept0(ASTVisitor visitor) {
136 visitor.endVisit(this);
140 * Returns the token of this number literal node. The value is the sequence
141 * of characters that would appear in the source program.
143 * @return the numeric literal token
145 public String getToken() {
146 return this.tokenValue;
150 * Sets the token of this number literal node. The value is the sequence
151 * of characters that would appear in the source program.
153 * @param token the numeric literal token
154 * @exception IllegalArgumentException if the argument is incorrect
156 public void setToken(String token) {
157 // update internalSetToken(String) if this is changed
158 if (token == null || token.length() == 0) {
159 throw new IllegalArgumentException();
161 Scanner scanner = this.ast.scanner;
162 char[] source = token.toCharArray();
163 scanner.setSource(source);
164 scanner.resetTo(0, source.length);
165 scanner.tokenizeComments = false;
166 scanner.tokenizeWhiteSpace = false;
168 int tokenType = scanner.getNextToken();
170 case TerminalTokens.TokenNameDoubleLiteral:
171 case TerminalTokens.TokenNameIntegerLiteral:
172 case TerminalTokens.TokenNameFloatingPointLiteral:
173 case TerminalTokens.TokenNameLongLiteral:
175 case TerminalTokens.TokenNameMINUS :
176 tokenType = scanner.getNextToken();
178 case TerminalTokens.TokenNameDoubleLiteral:
179 case TerminalTokens.TokenNameIntegerLiteral:
180 case TerminalTokens.TokenNameFloatingPointLiteral:
181 case TerminalTokens.TokenNameLongLiteral:
184 throw new IllegalArgumentException("Invalid number literal : >" + token + "<"); //$NON-NLS-1$//$NON-NLS-2$
188 throw new IllegalArgumentException("Invalid number literal : >" + token + "<");//$NON-NLS-1$//$NON-NLS-2$
190 } catch(InvalidInputException e) {
191 throw new IllegalArgumentException();
193 scanner.tokenizeComments = true;
194 scanner.tokenizeWhiteSpace = true;
196 preValueChange(TOKEN_PROPERTY);
197 this.tokenValue = token;
198 postValueChange(TOKEN_PROPERTY);
201 /* (omit javadoc for this method)
202 * This method is a copy of setToken(String) that doesn't do any validation.
204 void internalSetToken(String token) {
205 preValueChange(TOKEN_PROPERTY);
206 this.tokenValue = token;
207 postValueChange(TOKEN_PROPERTY);
209 /* (omit javadoc for this method)
210 * Method declared on ASTNode.
213 int size = BASE_NODE_SIZE + 1 * 4 + stringSize(tokenValue);
217 /* (omit javadoc for this method)
218 * Method declared on ASTNode.