001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.apt.impl.structure;
043:
044: import antlr.Token;
045: import java.io.Serializable;
046: import java.util.logging.Level;
047: import org.netbeans.modules.cnd.apt.debug.DebugUtils;
048: import org.netbeans.modules.cnd.apt.support.APTToken;
049: import org.netbeans.modules.cnd.apt.support.APTTokenTypes;
050: import org.netbeans.modules.cnd.apt.utils.APTUtils;
051:
052: /**
053: * #ifndef/#ifdef directives base implementation
054: * @author Vladimir Voskresensky
055: */
056: public abstract class APTIfdefConditionBaseNode extends
057: APTTokenAndChildBasedNode implements Serializable {
058: private static final long serialVersionUID = -5900095440680811076L;
059: private Token macroName;
060: private int endOffset;
061:
062: /** Copy constructor */
063: /**package*/
064: APTIfdefConditionBaseNode(APTIfdefConditionBaseNode orig) {
065: super (orig);
066: this .macroName = orig.macroName;
067: this .endOffset = orig.endOffset;
068: }
069:
070: /** Constructor for serialization */
071: protected APTIfdefConditionBaseNode() {
072: }
073:
074: /** Creates a new instance of APTIfdefConditionBaseNode */
075: protected APTIfdefConditionBaseNode(Token token) {
076: super (token);
077: }
078:
079: public boolean accept(Token token) {
080: /** base implementation of #ifdef/#ifndef */
081: if (APTUtils.isID(token)) {
082: if (macroName != null) {
083: // init macro name only once
084: if (DebugUtils.STANDALONE) {
085: System.err
086: .printf(
087: "line %d: extra tokens after %s at end of %s directive\n", // NOI18N
088: getToken().getLine(), macroName
089: .getText(), getToken()
090: .getText().trim());
091: } else {
092: APTUtils.LOG
093: .log(
094: Level.SEVERE,
095: "line {0}: extra tokens after {1} at end of {2} directive", // NOI18N
096: new Object[] {
097: getToken().getLine(),
098: macroName.getText(),
099: getToken().getText().trim() });
100: }
101: } else {
102: this .macroName = token;
103: }
104: } else if (token.getType() == APTTokenTypes.DEFINED) {
105: // "defined" cannot be used as a macro name
106: if (DebugUtils.STANDALONE) {
107: System.err
108: .printf(
109: "line %d: \"defined\" cannot be used as a macro name\n", // NOI18N
110: getToken().getLine());
111: } else {
112: APTUtils.LOG
113: .log(
114: Level.SEVERE,
115: "line {0}: \"defined\" cannot be used as a macro name", // NOI18N
116: new Object[] { getToken().getLine() });
117: }
118: }
119: // eat all till END_PREPROC_DIRECTIVE
120: if (APTUtils.isEndDirectiveToken(token.getType())) {
121: endOffset = ((APTToken) token).getOffset();
122: if (macroName == null) {
123: if (DebugUtils.STANDALONE) {
124: System.err
125: .printf(
126: "line %d: no macro name given in %s directive\n", // NOI18N
127: getToken().getLine(), getToken()
128: .getText().trim());
129: } else {
130: APTUtils.LOG
131: .log(
132: Level.SEVERE,
133: "line {0}: no macro name given in {1} directive ", // NOI18N
134: new Object[] {
135: getToken().getLine(),
136: getToken().getText().trim() });
137: }
138: }
139: return false;
140: } else {
141: return true;
142: }
143: }
144:
145: public int getEndOffset() {
146: return endOffset;
147: }
148:
149: public String getText() {
150: assert (getToken() != null) : "must have valid preproc directive"; // NOI18N
151: // macro name could be null for incorrect constructions
152: // assert (getMacroName() != null) : "must have valid macro"; // NOI18N
153: String retValue = super .getText();
154: if (getMacroName() != null) {
155: retValue += " MACRO{" + getMacroName() + "}"; // NOI18N
156: }
157: return retValue;
158: }
159:
160: /** base implementation for #ifdef/#ifndef */
161: public Token getMacroName() {
162: return macroName;
163: }
164:
165: }
|