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.modelimpl.csm.core;
043:
044: import antlr.Token;
045: import java.io.DataInput;
046: import java.io.DataOutput;
047: import java.io.IOException;
048: import org.netbeans.modules.cnd.apt.support.APTPreprocHandler;
049:
050: /**
051: *
052: * @author Alexander Simon
053: */
054: public class GuardBlockState {
055: public static final int NOT_INITIED = -2;
056: public static final int SKIPPED = 1;
057: public static final int PARSED = -1;
058: public static final int NO_GUARD = 0;
059: private volatile int guardBlockState = NOT_INITIED;
060: public Token guard;
061:
062: /** Creates a new instance of GuardBlockState */
063: public GuardBlockState() {
064: }
065:
066: public void setGuardBlockState(
067: final APTPreprocHandler preprocHandler, Token guard) {
068: synchronized (this ) {
069: if (guard != null) {
070: this .guard = new MyToken(guard.getText());
071: assert preprocHandler.getMacroMap() != null;
072: if (preprocHandler.getMacroMap().isDefined(guard)) {
073: if (guardBlockState != SKIPPED) {
074: guardBlockState = SKIPPED;
075: }
076: } else {
077: if (guardBlockState != PARSED) {
078: guardBlockState = PARSED;
079: }
080: }
081: } else {
082: this .guard = null;
083: if (guardBlockState != NO_GUARD) {
084: guardBlockState = NO_GUARD;
085: }
086: }
087: }
088: }
089:
090: /**
091: *----------------------------------------
092: *last state\macros Not defined defined
093: *----------------------------------------
094: *NOT_INITIED false false
095: *NO_GUARD false false
096: *PARSED false false
097: *SKIPPED true true
098: */
099: public boolean isNeedReparse(final APTPreprocHandler preprocHandler) {
100: synchronized (this ) {
101: switch (guardBlockState) {
102: case NOT_INITIED:
103: case NO_GUARD:
104: return false;
105: case PARSED:
106: return false;
107: case SKIPPED:
108: assert guard != null : "Guard is null";
109: assert preprocHandler.getMacroMap() != null : "Macro map is null";
110: return !preprocHandler.getMacroMap().isDefined(guard);
111: }
112: return false;
113: }
114: }
115:
116: // for tests only
117: public String testGetGuardName() {
118: if (guard != null) {
119: return guard.getText();
120: }
121: return null;
122: }
123:
124: public Token getGuard() {
125: return guard;
126: }
127:
128: public void write(DataOutput output) throws IOException {
129: output.writeInt(guardBlockState);
130: output.writeBoolean(guard != null);
131: if (guard != null) {
132: output.writeUTF(guard.getText());
133: }
134: }
135:
136: public GuardBlockState(DataInput input) throws IOException {
137: guardBlockState = input.readInt();
138: if (input.readBoolean()) {
139: guard = new MyToken(input.readUTF());
140: }
141: }
142:
143: public boolean isInited() {
144: synchronized (this ) {
145: return this .guardBlockState != NOT_INITIED;
146: }
147: }
148:
149: private class MyToken implements Token {
150: private final String text;
151:
152: private MyToken(String text) {
153: this .text = text;
154: }
155:
156: public int getColumn() {
157: throw new UnsupportedOperationException();
158: }
159:
160: public void setColumn(int c) {
161: throw new UnsupportedOperationException();
162: }
163:
164: public int getLine() {
165: throw new UnsupportedOperationException();
166: }
167:
168: public void setLine(int l) {
169: throw new UnsupportedOperationException();
170: }
171:
172: public String getFilename() {
173: throw new UnsupportedOperationException();
174: }
175:
176: public void setFilename(String name) {
177: throw new UnsupportedOperationException();
178: }
179:
180: public String getText() {
181: return text;
182: }
183:
184: public void setText(String t) {
185: throw new UnsupportedOperationException();
186: }
187:
188: public int getType() {
189: throw new UnsupportedOperationException();
190: }
191:
192: public void setType(int t) {
193: throw new UnsupportedOperationException();
194: }
195: }
196: }
|