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: /** The abstract notion of a shadowing state. We use shadowing to mean
040: * the state of text as it is interpreted during compile. Commented text
041: * is ignored, and quoted text does not factor into the ASTs generated
042: * by the compiler except as a text constant. This buys us a lot in
043: * terms of correctness when highlighting, indenting, and performing
044: * other editor functions.
045: * @version $Id: ReducedModelState.java 4255 2007-08-28 19:17:37Z mgricken $
046: */
047: public abstract class ReducedModelState implements
048: /* imports */ReducedModelStates {
049:
050: abstract ReducedModelState update(TokenList.Iterator copyCursor);
051:
052: /** Combines the current and next braces if they match the given types.
053: * If we have braces of first and second in immediate succession, and if
054: * second's gap is 0, combine them into first+second.
055: * The cursor remains on the same block after this method is called.
056: * @param first the first half of a multiple char brace
057: * @param second the second half of a multiple char brace
058: * @return true if we combined two braces or false if not
059: */
060: boolean _combineCurrentAndNextIfFind(String first, String second,
061: TokenList.Iterator copyCursor) {
062: if (copyCursor.atStart() || copyCursor.atEnd()
063: || copyCursor.atLastItem()
064: || !copyCursor.current().getType().equals(first))
065: return false;
066:
067: copyCursor.next(); // move to second one to check if we can combine
068:
069: // The second one is eligible to combine if it exists (atLast is false),
070: // if it has the right brace type, and if it has no gap.
071: if (copyCursor.current().getType().equals(second)) {
072: if (copyCursor.current().getType().equals("")
073: && copyCursor.prevItem().getType().equals("")) {
074: // delete first Gap and augment the second
075: copyCursor.prev();
076: int growth = copyCursor.current().getSize();
077: copyCursor.remove();
078: copyCursor.current().grow(growth);
079: } else if (copyCursor.current().getType().length() == 2) {
080: String tail = copyCursor.current().getType().substring(
081: 1, 2);
082: String head = copyCursor.prevItem().getType()
083: + copyCursor.current().getType()
084: .substring(0, 1);
085: copyCursor.current().setType(tail);
086: copyCursor.prev();
087: copyCursor.current().setType(head);
088: copyCursor.current().setState(FREE);
089: } else {
090: // delete the first Brace and augment the second
091: copyCursor.prev();
092: copyCursor.remove();
093: copyCursor.current().setType(first + second);
094: }
095: return true;
096: } else {
097: // we couldn't combine, so move back and return
098: copyCursor.prev();
099: return false;
100: }
101: }
102:
103: boolean _combineCurrentAndNextIfEscape(TokenList.Iterator copyCursor) {
104: boolean combined = _combineCurrentAndNextIfFind("\\", "\\",
105: copyCursor); // \-\
106: combined = combined
107: || _combineCurrentAndNextIfFind("\\", "\'", copyCursor); // \-'
108: combined = combined
109: || _combineCurrentAndNextIfFind("\\", "\\'", copyCursor);// \-\'
110: combined = combined
111: || _combineCurrentAndNextIfFind("\\", "\"", copyCursor); // \-"
112: combined = combined
113: || _combineCurrentAndNextIfFind("\\", "\\\"",
114: copyCursor);// \-\"
115: combined = combined
116: || _combineCurrentAndNextIfFind("\\", "\\\\",
117: copyCursor);// \-\\
118: return combined;
119: }
120: }
|