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.support;
043:
044: import antlr.TokenStreamException;
045: import java.io.File;
046: import java.util.logging.Level;
047: import org.netbeans.modules.cnd.apt.debug.DebugUtils;
048: import org.netbeans.modules.cnd.apt.structure.APT;
049: import org.netbeans.modules.cnd.apt.structure.APTDefine;
050: import org.netbeans.modules.cnd.apt.structure.APTFile;
051: import org.netbeans.modules.cnd.apt.structure.APTInclude;
052: import org.netbeans.modules.cnd.apt.structure.APTIncludeNext;
053: import org.netbeans.modules.cnd.apt.structure.APTUndefine;
054: import org.netbeans.modules.cnd.apt.utils.APTUtils;
055:
056: /**
057: * abstract Tree walker for APT
058: * @author Vladimir Voskresensky
059: */
060: public abstract class APTAbstractWalker extends APTWalker {
061:
062: private final APTPreprocHandler preprocHandler;
063: private final String startPath;
064:
065: protected APTAbstractWalker(APTFile apt,
066: APTPreprocHandler preprocHandler) {
067: super (apt, preprocHandler == null ? null : preprocHandler
068: .getMacroMap());
069: this .startPath = apt.getPath();
070: this .preprocHandler = preprocHandler;
071: }
072:
073: protected void onInclude(APT apt) {
074: if (getIncludeHandler() != null) {
075: APTIncludeResolver resolver = getIncludeHandler()
076: .getResolver(startPath);
077: ResolvedPath resolvedPath = resolver.resolveInclude(
078: (APTInclude) apt, getMacroMap());
079: if (resolvedPath == null) {
080: if (DebugUtils.STANDALONE) {
081: if (APTUtils.LOG.getLevel().intValue() <= Level.SEVERE
082: .intValue()) {
083: System.err.println("FAILED INCLUDE: from "
084: + new File(startPath).getName()
085: + " for:\n\t" + apt);// NOI18N
086: }
087: } else {
088: APTUtils.LOG.log(Level.WARNING,
089: "failed resolving path from {0} for {1}", // NOI18N
090: new Object[] { startPath, apt });
091: }
092: }
093: include(resolvedPath, (APTInclude) apt);
094: }
095: }
096:
097: protected void onIncludeNext(APT apt) {
098: if (getIncludeHandler() != null) {
099: APTIncludeResolver resolver = getIncludeHandler()
100: .getResolver(startPath);
101: ResolvedPath resolvedPath = resolver.resolveIncludeNext(
102: (APTIncludeNext) apt, getMacroMap());
103: if (resolvedPath == null) {
104: if (DebugUtils.STANDALONE) {
105: if (APTUtils.LOG.getLevel().intValue() <= Level.SEVERE
106: .intValue()) {
107: System.err.println("FAILED INCLUDE: from "
108: + new File(startPath).getName()
109: + " for:\n\t" + apt);// NOI18N
110: }
111: } else {
112: APTUtils.LOG.log(Level.WARNING,
113: "failed resolving path from {0} for {1}", // NOI18N
114: new Object[] { startPath, apt });
115: }
116: }
117: include(resolvedPath, (APTInclude) apt);
118: }
119: }
120:
121: abstract protected void include(ResolvedPath resolvedPath,
122: APTInclude aPTInclude);
123:
124: protected void onDefine(APT apt) {
125: APTDefine define = (APTDefine) apt;
126: if (define.isValid()) {
127: getMacroMap().define(define.getName(), define.getParams(),
128: define.getBody());
129: } else {
130: if (DebugUtils.STANDALONE) {
131: if (APTUtils.LOG.getLevel().intValue() <= Level.SEVERE
132: .intValue()) {
133: System.err
134: .println("INCORRECT #define directive: in "
135: + new File(startPath).getName()
136: + " for:\n\t" + apt);// NOI18N
137: }
138: } else {
139: APTUtils.LOG
140: .log(
141: Level.SEVERE,
142: "INCORRECT #define directive: in {0} for:\n\t{1}", // NOI18N
143: new Object[] {
144: new File(startPath).getName(),
145: apt });
146: }
147: }
148: }
149:
150: protected void onUndef(APT apt) {
151: APTUndefine undef = (APTUndefine) apt;
152: getMacroMap().undef(undef.getName());
153: }
154:
155: protected boolean onIf(APT apt) {
156: return eval(apt);
157: }
158:
159: protected boolean onIfdef(APT apt) {
160: return eval(apt);
161: }
162:
163: protected boolean onIfndef(APT apt) {
164: return eval(apt);
165: }
166:
167: protected boolean onElif(APT apt, boolean wasInPrevBranch) {
168: return !wasInPrevBranch && eval(apt);
169: }
170:
171: protected boolean onElse(APT apt, boolean wasInPrevBranch) {
172: return !wasInPrevBranch;
173: }
174:
175: protected void onEndif(APT apt, boolean wasInBranch) {
176: }
177:
178: protected APTPreprocHandler getPreprocHandler() {
179: return preprocHandler;
180: }
181:
182: protected APTIncludeHandler getIncludeHandler() {
183: return getPreprocHandler() == null ? null : getPreprocHandler()
184: .getIncludeHandler();
185: }
186:
187: ////////////////////////////////////////////////////////////////////////////
188: // implementation details
189:
190: private boolean eval(APT apt) {
191: APTUtils.LOG.log(Level.FINE, "eval condition for {0}",
192: new Object[] { apt });// NOI18N
193: boolean res = false;
194: try {
195: res = APTConditionResolver.evaluate(apt, getMacroMap());
196: } catch (TokenStreamException ex) {
197: APTUtils.LOG.log(Level.SEVERE,
198: "error on evaluating condition node " + apt, ex);// NOI18N
199: }
200: return res;
201: }
202: }
|