|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Brace.java | 83.3% | 100% | 100% | 96.9% |
|
1 | /*BEGIN_COPYRIGHT_BLOCK | |
2 | * | |
3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * * Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
14 | * names of its contributors may be used to endorse or promote products | |
15 | * derived from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | * | |
29 | * This software is Open Source Initiative approved Open Source Software. | |
30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
31 | * | |
32 | * This file is part of DrJava. Download the current version of this project | |
33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
34 | * | |
35 | * END_COPYRIGHT_BLOCK*/ | |
36 | ||
37 | package edu.rice.cs.drjava.model.definitions.reducedmodel; | |
38 | ||
39 | /** This class acts as the representation of a brace in the reduced view. It also includes information about the gap | |
40 | * of plaintext preceding the actual brace before the previous brace or the start of the file. | |
41 | * WARNING: the code in this class critically depends on the fact that literal strings are interned. | |
42 | * @version $Id: Brace.java 5175 2010-01-20 08:46:32Z mgricken $ | |
43 | */ | |
44 | class Brace extends ReducedToken implements ReducedModelStates { | |
45 | ||
46 | /** An array of the special characters that signify areas of text other than gaps. NOTE: this data structure is NOT | |
47 | * simply a flat array. Matching characters are placed next to each other (except for the trailing elements, | |
48 | * which have no matches). Notice that single and double quotes match themselves. | |
49 | * @see String | |
50 | */ | |
51 | public static final String[] braces = { | |
52 | "{", "}", "(", ")", "[", "]", "/*", "*/", "//", "\n", "/", "*", "\"", "\"", "'", "'", "\\\\", "\\", "\\'", "\\\"", "" | |
53 | }; | |
54 | ||
55 | public static final int BRACES_LENGTH = braces.length; | |
56 | public static final int LAST_BRACE_INDEX = braces.length - 1; | |
57 | ||
58 | public static final int BLK_CMT_BEG_TYPE = findBrace("/*"); | |
59 | public static final int BLK_CMT_END_TYPE = findBrace("*/"); | |
60 | public static final int EOLN_TYPE = findBrace("\n"); | |
61 | public static final int LINE_CMT_TYPE = findBrace("//"); | |
62 | public static final int SINGLE_QUOTE_TYPE = findBrace("'"); | |
63 | public static final int DOUBLE_QUOTE_TYPE = findBrace("\""); | |
64 | public static final int STAR_TYPE = findBrace("*"); | |
65 | public static final int SLASH_TYPE = findBrace("/"); | |
66 | public static final int DOUBLE_ESCAPE_TYPE = findBrace("\\\\"); | |
67 | public static final int ESCAPED_SINGLE_QUOTE_TYPE = findBrace("\\'"); | |
68 | public static final int ESCAPED_DOUBLE_QUOTE_TYPE = findBrace("\\\""); | |
69 | ||
70 | ||
71 | private volatile int _type; /** the type of this brace, which is MUTABLE via flip and setType */ | |
72 | private volatile int _size; /** the size of this brace */ | |
73 | ||
74 | /** Virtual constructor. | |
75 | * @param type the brace text | |
76 | * @param state whether the brace is shadwowed by a comment, quote etc | |
77 | * @return a new Brace if type is valid, otherwise null | |
78 | * @throws BraceException if the given type is not a valid brace type. | |
79 | */ | |
80 | 7336 | public static Brace MakeBrace(String type, ReducedModelState state) { |
81 | 7336 | int index = findBrace(type.intern()); |
82 | 1 | if (index == BRACES_LENGTH) throw new BraceException("Invalid brace type \"" + type + "\""); |
83 | 7335 | else return new Brace(index, state); |
84 | } | |
85 | ||
86 | /** Constructor. | |
87 | * @param type the brace type | |
88 | * @param state the state of the reduced model | |
89 | */ | |
90 | 7335 | private Brace(int type, ReducedModelState state) { |
91 | 7335 | super(state); |
92 | 7335 | _type = type; |
93 | 7335 | _size = getType().length(); |
94 | } | |
95 | ||
96 | /** Get the text of the brace. | |
97 | * @return the text of the Brace | |
98 | */ | |
99 | 88679 | public String getType() { return (_type == BRACES_LENGTH) ? "!" : braces[_type]; } |
100 | ||
101 | /** @return the size of the brace and its preceding gap. */ | |
102 | 13193389 | public int getSize() { return _size; } |
103 | ||
104 | /** Converts a Brace to a String. | |
105 | * Used for debugging. | |
106 | * @return the string representation of the Brace. | |
107 | */ | |
108 | 2 | public String toString() { |
109 | //String val = "Brace(size: " + getSize() + "): "; | |
110 | 2 | final StringBuilder val = new StringBuilder("Brace<"); |
111 | // int i; | |
112 | // for (i = 0; i < getSize(); i++) { | |
113 | // val.append(getType().charAt(i)); | |
114 | // } | |
115 | // val.append("' "); | |
116 | 2 | val.append(getType()); |
117 | 2 | return val.append('>').toString(); |
118 | } | |
119 | ||
120 | /** Flips the orientation of the brace. Useful for updating quote information. Does not change _size. */ | |
121 | 198 | public void flip() { |
122 | 187 | if (isOpen()) _type += 1; |
123 | 11 | else if (_type < braces.length - 1) _type -= 1; |
124 | } | |
125 | ||
126 | /** Indicates whether this is an opening brace. | |
127 | * @return true if the brace is an opening brace. | |
128 | */ | |
129 | 10593 | public boolean isOpen() { return (((_type % 2) == 0) && (_type < braces.length - 1)); } |
130 | ||
131 | /** @return true if this is {|(|[ */ | |
132 | 1766 | public boolean isOpenBrace() { return ((_type == 0) || (_type == 2) || (_type == 4)); } |
133 | ||
134 | /** @return true if this is }|)|] */ | |
135 | 5698 | public boolean isClosedBrace() { return ((_type == 1) || (_type == 3) || (_type == 5)); } |
136 | ||
137 | /** Indicates whether this is a closing brace. | |
138 | * @return true if the brace is a closing brace. | |
139 | */ | |
140 | 363 | public boolean isClosed() { return ! isOpen(); } |
141 | ||
142 | /** Reset the type of this brace. | |
143 | * @param type the new String type for the brace | |
144 | */ | |
145 | 733 | public void setType(String type) { |
146 | 733 | type = type.intern(); |
147 | 733 | int index = findBrace(type); |
148 | 1 | if (index == braces.length) throw new BraceException("Invalid brace type \"" + type + "\""); |
149 | 732 | _type = index; |
150 | 732 | _size = getType().length(); |
151 | } | |
152 | ||
153 | /** Determine the brace _type of the given String. The integer value returned is only used internally. | |
154 | * ASSUMES that the given String is interned! Externally, the brace shows the text as its "type". | |
155 | * @param type the text of the brace | |
156 | * @return an integer indicating the type of brace | |
157 | */ | |
158 | 8729 | private static int findBrace(String type) { |
159 | 8729 | assert type == type.intern(); |
160 | 8729 | int i; |
161 | 8729 | for (i = 0; i < braces.length; i++) { |
162 | 8727 | if (type == braces[i]) break; |
163 | } | |
164 | 8729 | return i; |
165 | } | |
166 | ||
167 | /** Check if two braces match. | |
168 | * @param other the brace to compare | |
169 | * @return true if this is a match for other. | |
170 | */ | |
171 | 5423 | public boolean isMatch(Brace other) { |
172 | 5423 | int off = isOpen() ? 1 : -1; |
173 | 5423 | return _type + off == other._type; |
174 | } | |
175 | ||
176 | /** Determines if this Brace is matchable (one of "{", "}", "(", ")", "[", "]"). */ | |
177 | 4072 | public boolean isMatchable() { return _type < BLK_CMT_BEG_TYPE; } |
178 | ||
179 | /** @return true if this is a double quote */ | |
180 | 273422 | public boolean isDoubleQuote() { return _type == DOUBLE_QUOTE_TYPE; } |
181 | ||
182 | /** @return true if this is a single quote */ | |
183 | 273039 | public boolean isSingleQuote() { return _type == SINGLE_QUOTE_TYPE; } |
184 | ||
185 | /** @return true if this is a line comment delimiter */ | |
186 | 291062 | public boolean isLineComment() { return _type == LINE_CMT_TYPE; } |
187 | ||
188 | /** @return true if this is a block comment open delimiter */ | |
189 | 290617 | public boolean isBlockCommentStart() { return _type == BLK_CMT_BEG_TYPE; } |
190 | ||
191 | /** @return true if this is a block comment close delimiter */ | |
192 | 56 | public boolean isBlockCommentEnd() { return _type == BLK_CMT_END_TYPE; } |
193 | ||
194 | /** @return true if this is a newline delimiter */ | |
195 | 30461 | public boolean isNewline() { return _type == EOLN_TYPE; } |
196 | ||
197 | /** @return true if this is a multiple character brace */ | |
198 | 70 | public boolean isMultipleCharBrace() { |
199 | 70 | return isLineComment() || isBlockCommentStart() || isBlockCommentEnd() || isDoubleEscapeSequence(); |
200 | } | |
201 | ||
202 | /** @return true if this is \\ or \" */ | |
203 | 51 | public boolean isDoubleEscapeSequence() { |
204 | 51 | return isDoubleEscape() || isEscapedDoubleQuote() || isEscapedSingleQuote(); |
205 | } | |
206 | ||
207 | /** @return true if this is \\ */ | |
208 | 51 | public boolean isDoubleEscape() { return _type == DOUBLE_ESCAPE_TYPE; } |
209 | ||
210 | /** @return true if this is \" */ | |
211 | 37 | public boolean isEscapedDoubleQuote() {return _type == ESCAPED_DOUBLE_QUOTE_TYPE; } |
212 | ||
213 | /** @return true if this is \' */ | |
214 | 28 | public boolean isEscapedSingleQuote() { return _type == ESCAPED_SINGLE_QUOTE_TYPE; } |
215 | ||
216 | /** Implementation of abstract function. Braces, of course, are never Gaps. */ | |
217 | 13861 | public boolean isGap() { return false; } |
218 | ||
219 | /** @return true if this is / */ | |
220 | 2 | public boolean isSlash() { return _type == SLASH_TYPE; } |
221 | ||
222 | /** @return true if this is * */ | |
223 | 2 | public boolean isStar() { return _type == STAR_TYPE; } |
224 | ||
225 | /** Braces can't grow. | |
226 | * @throws RuntimeException | |
227 | */ | |
228 | 1 | public void grow(int delta) { throw new BraceException("Braces can't grow."); } |
229 | ||
230 | /** Braces can't shrink. | |
231 | * @throws RuntimeException | |
232 | */ | |
233 | 1 | public void shrink(int delta) { throw new BraceException("Braces can't shrink."); } |
234 | } | |
235 | ||
236 | ||
237 | ||
238 | ||
239 |
|