001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.reducedmodel;
038:
039: /** This class acts as the representation of a brace in the reduced view. It also includes information about the gap
040: * of plaintext preceding the actual brace before the previous brace or the start of the file.
041: * WARNING: the code in this class critically depends on the fact that literal strings are interned.
042: * @version $Id: Brace.java 4255 2007-08-28 19:17:37Z mgricken $
043: */
044: class Brace extends ReducedToken implements ReducedModelStates {
045:
046: /** An array of the special characters that signify areas of text other than gaps. NOTE: this data structure is NOT
047: * simply a flat array. Matching characters are placed next to each other (except for the trailing elements,
048: * which have no matches). Notice that single and double quotes match themselves.
049: * @see String
050: */
051: public static final String[] braces = { "{", "}", "(", ")", "[",
052: "]", "/*", "*/", "//", "\n", "/", "*", "\"", "\"", "'",
053: "'", "\\\\", "\\", "\\'", "\\\"", "" };
054:
055: public static final int BRACES_LENGTH = braces.length;
056: public static final int LAST_BRACE_INDEX = braces.length - 1;
057:
058: public static final int BLK_CMT_BEG_TYPE = findBrace("/*");
059: public static final int BLK_CMT_END_TYPE = findBrace("*/");
060: public static final int EOLN_TYPE = findBrace("\n");
061: public static final int LINE_CMT_TYPE = findBrace("//");
062: public static final int SINGLE_QUOTE_TYPE = findBrace("'");
063: public static final int DOUBLE_QUOTE_TYPE = findBrace("\"");
064: public static final int STAR_TYPE = findBrace("*");
065: public static final int SLASH_TYPE = findBrace("/");
066: public static final int DOUBLE_ESCAPE_TYPE = findBrace("\\\\");
067: public static final int ESCAPED_SINGLE_QUOTE_TYPE = findBrace("\\'");
068: public static final int ESCAPED_DOUBLE_QUOTE_TYPE = findBrace("\\\"");
069:
070: /** the type of the brace */
071: private int _type;
072:
073: /** Virtual constructor.
074: * @param type the brace text
075: * @param state whether the brace is shadwowed by a comment, quote etc
076: * @return a new Brace if type is valid, otherwise null
077: * @throws BraceException if the given type is not a valid brace type.
078: */
079: public static Brace MakeBrace(String type, ReducedModelState state) {
080: int index = findBrace(type.intern());
081: if (index == BRACES_LENGTH)
082: throw new BraceException("Invalid brace type \"" + type
083: + "\"");
084: else
085: return new Brace(index, state);
086: }
087:
088: /** Constructor.
089: * @param type the brace type
090: * @param state the state of the reduced model
091: */
092: private Brace(int type, ReducedModelState state) {
093: super (state);
094: _type = type;
095: }
096:
097: /** Get the text of the brace.
098: * @return the text of the Brace
099: */
100: public String getType() {
101: return (_type == BRACES_LENGTH) ? "!" : braces[_type];
102: }
103:
104: /** @return the size of the brace and its preceding gap. */
105: public int getSize() {
106: return getType().length();
107: }
108:
109: /** Converts a Brace to a String.
110: * Used for debugging.
111: * @return the string representation of the Brace.
112: */
113: public String toString() {
114: //String val = "Brace(size: "+ getSize() +"): ";
115: final StringBuilder val = new StringBuilder();
116: int i;
117: for (i = 0; i < getSize(); i++) {
118: val.append(' ');
119: val.append(getType().charAt(i));
120: }
121: return val.toString();
122: }
123:
124: /** Flips the orientation of the brace. Useful for updating quote information. */
125: public void flip() {
126: if (isOpen())
127: _type += 1;
128: else if (_type < braces.length - 1)
129: _type -= 1;
130: }
131:
132: /** Indicates whether this is an opening brace.
133: * @return true if the brace is an opening brace.
134: */
135: public boolean isOpen() {
136: return (((_type % 2) == 0) && (_type < braces.length - 1));
137: }
138:
139: /** @return true if this is {|(|[ */
140: public boolean isOpenBrace() {
141: return ((_type == 0) || (_type == 2) || (_type == 4));
142: }
143:
144: /** @return true if this is }|)|] */
145: public boolean isClosedBrace() {
146: return ((_type == 1) || (_type == 3) || (_type == 5));
147: }
148:
149: /** Indicates whether this is a closing brace.
150: * @return true if the brace is a closing brace.
151: */
152: public boolean isClosed() {
153: return !isOpen();
154: }
155:
156: /** Reset the type of this brace.
157: * @param type the new String type for the brace
158: */
159: public void setType(String type) {
160: type = type.intern();
161: int index = findBrace(type);
162: if (index == braces.length)
163: throw new BraceException("Invalid brace type \"" + type
164: + "\"");
165: else {
166: _type = index;
167: }
168: }
169:
170: /** Determine the brace _type of the given String. The integer value returned is only used internally.
171: * ASSUMES that the given String is interned! Externally, the brace shows the text as its "type".
172: * @param type the text of the brace
173: * @return an integer indicating the type of brace
174: */
175: private static int findBrace(String type) {
176: assert type == type.intern();
177: int i;
178: for (i = 0; i < braces.length; i++) {
179: if (type == braces[i])
180: break;
181: }
182: return i;
183: }
184:
185: /** Check if two braces match.
186: * @param other the brace to compare
187: * @return true if this is a match for other.
188: */
189: public boolean isMatch(Brace other) {
190: int off = isOpen() ? 1 : -1;
191: return _type + off == other._type;
192: }
193:
194: /** Determines if this Brace is matchable (one of "{", "}", "(", ")", "[", "]"). */
195: public boolean isMatchable() {
196: return _type < BLK_CMT_BEG_TYPE;
197: }
198:
199: /** @return true if this is a double quote */
200: public boolean isDoubleQuote() {
201: return _type == DOUBLE_QUOTE_TYPE;
202: }
203:
204: /** @return true if this is a single quote */
205: public boolean isSingleQuote() {
206: return _type == SINGLE_QUOTE_TYPE;
207: }
208:
209: /** @return true if this is a line comment delimiter */
210: public boolean isLineComment() {
211: return _type == LINE_CMT_TYPE;
212: }
213:
214: /** @return true if this is a block comment open delimiter */
215: public boolean isBlockCommentStart() {
216: return _type == BLK_CMT_BEG_TYPE;
217: }
218:
219: /** @return true if this is a block comment close delimiter */
220: public boolean isBlockCommentEnd() {
221: return _type == BLK_CMT_END_TYPE;
222: }
223:
224: /** @return true if this is a newline delimiter */
225: public boolean isNewline() {
226: return _type == EOLN_TYPE;
227: }
228:
229: /** @return true if this is a multiple character brace */
230: public boolean isMultipleCharBrace() {
231: return isLineComment() || isBlockCommentStart()
232: || isBlockCommentEnd() || isDoubleEscapeSequence();
233: }
234:
235: /** @return true if this is \\ or \" */
236: public boolean isDoubleEscapeSequence() {
237: return isDoubleEscape() || isEscapedDoubleQuote()
238: || isEscapedSingleQuote();
239: }
240:
241: /** @return true if this is \\ */
242: public boolean isDoubleEscape() {
243: return _type == DOUBLE_ESCAPE_TYPE;
244: }
245:
246: /** @return true if this is \" */
247: public boolean isEscapedDoubleQuote() {
248: return _type == ESCAPED_DOUBLE_QUOTE_TYPE;
249: }
250:
251: /** @return true if this is \' */
252: public boolean isEscapedSingleQuote() {
253: return _type == ESCAPED_SINGLE_QUOTE_TYPE;
254: }
255:
256: /** Implementation of abstract function. Braces, of course, are never Gaps. */
257: public boolean isGap() {
258: return false;
259: }
260:
261: /** @return true if this is / */
262: public boolean isSlash() {
263: return _type == SLASH_TYPE;
264: }
265:
266: /** @return true if this is * */
267: public boolean isStar() {
268: return _type == STAR_TYPE;
269: }
270:
271: /** Braces can't grow.
272: * @throws RuntimeException
273: */
274: public void grow(int delta) {
275: throw new BraceException("Braces can't grow.");
276: }
277:
278: /** Braces can't shrink.
279: * @throws RuntimeException
280: */
281: public void shrink(int delta) {
282: throw new BraceException("Braces can't shrink.");
283: }
284: }
285:
286: /** An exception class used by methods in this class. */
287: class BraceException extends RuntimeException {
288:
289: /** Creates a new BraceException
290: * @param s the message
291: */
292: public BraceException(String s) {
293: super(s);
294: }
295: }
|