KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > reducedmodel > ReducedModelState


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.reducedmodel;
35
36 /** The abstract notion of a shadowing state. We use shadowing to mean
37  * the state of text as it is interpreted during compile. Commented text
38  * is ignored, and quoted text does not factor into the ASTs generated
39  * by the compiler except as a text constant. This buys us a lot in
40  * terms of correctness when highlighting, indenting, and performing
41  * other editor functions.
42  * @version $Id: ReducedModelState.java 3553 2006-02-20 21:22:09Z rcartwright $
43  */

44 public abstract class ReducedModelState implements /* imports */ ReducedModelStates {
45   
46   abstract ReducedModelState update(TokenList.Iterator copyCursor);
47
48   /** Combines the current and next braces if they match the given types.
49    * If we have braces of first and second in immediate succession, and if
50    * second's gap is 0, combine them into first+second.
51    * The cursor remains on the same block after this method is called.
52    * @param first the first half of a multiple char brace
53    * @param second the second half of a multiple char brace
54    * @return true if we combined two braces or false if not
55    */

56   boolean _combineCurrentAndNextIfFind(String JavaDoc first, String JavaDoc second, TokenList.Iterator copyCursor) {
57     if (copyCursor.atStart() || copyCursor.atEnd() || copyCursor.atLastItem() ||
58         !copyCursor.current().getType().equals(first))
59       return false;
60
61     copyCursor.next(); // move to second one to check if we can combine
62

63     // The second one is eligible to combine if it exists (atLast is false),
64
// if it has the right brace type, and if it has no gap.
65
if (copyCursor.current().getType().equals(second)) {
66       if (copyCursor.current().getType().equals("") && copyCursor.prevItem().getType().equals("")) {
67         // delete first Gap and augment the second
68
copyCursor.prev();
69         int growth = copyCursor.current().getSize();
70         copyCursor.remove();
71         copyCursor.current().grow(growth);
72       }
73       else if (copyCursor.current().getType().length() == 2) {
74         String JavaDoc tail = copyCursor.current().getType().substring(1,2);
75         String JavaDoc head = copyCursor.prevItem().getType() +
76           copyCursor.current().getType().substring(0,1);
77         copyCursor.current().setType(tail);
78         copyCursor.prev();
79         copyCursor.current().setType(head);
80         copyCursor.current().setState(FREE);
81       }
82       else {
83         // delete the first Brace and augment the second
84
copyCursor.prev();
85         copyCursor.remove();
86         copyCursor.current().setType(first + second);
87       }
88       return true;
89     }
90     else {
91       // we couldn't combine, so move back and return
92
copyCursor.prev();
93       return false;
94     }
95   }
96
97   boolean _combineCurrentAndNextIfEscape(TokenList.Iterator copyCursor) {
98     boolean combined = _combineCurrentAndNextIfFind("\\","\\",copyCursor); // \-\
99
combined = combined || _combineCurrentAndNextIfFind("\\","\'",copyCursor); // \-'
100
combined = combined || _combineCurrentAndNextIfFind("\\","\\'",copyCursor);// \-\'
101
combined = combined || _combineCurrentAndNextIfFind("\\","\"",copyCursor); // \-"
102
combined = combined || _combineCurrentAndNextIfFind("\\","\\\"",copyCursor);// \-\"
103
combined = combined || _combineCurrentAndNextIfFind("\\","\\\\",copyCursor);// \-\\
104
return combined;
105   }
106 }
107
Popular Tags