001: /*
002: * DefaultSyntaxIterator.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: DefaultSyntaxIterator.java,v 1.1.1.1 2002/09/24 16:07:44 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public class DefaultSyntaxIterator implements SyntaxIterator {
025: private Line line;
026: private int offset;
027: private char[] cachedChars;
028: private Line cachedLine;
029:
030: protected DefaultSyntaxIterator(Position pos) {
031: if (pos != null) {
032: line = pos.getLine();
033: offset = pos.getOffset();
034: }
035: }
036:
037: public final Position getPosition() {
038: return new Position(line, offset);
039: }
040:
041: public final Line getLine() {
042: return line;
043: }
044:
045: public final char nextChar() {
046: int limit = line.length();
047: if (offset < limit - 1) {
048: if (cachedLine != line) {
049: cachedChars = hideSyntacticWhitespace(line);
050: cachedLine = line;
051: }
052: while (++offset < limit) {
053: if (cachedChars[offset] > ' ')
054: return cachedChars[offset];
055: }
056: }
057: while (true) {
058: if (line.next() == null)
059: return DONE;
060: line = line.next();
061: offset = 0;
062: cachedChars = hideSyntacticWhitespace(line);
063: cachedLine = line;
064: limit = line.length();
065: while (offset < limit) {
066: if (cachedChars[offset] > ' ')
067: return cachedChars[offset];
068: ++offset;
069: }
070: }
071: }
072:
073: public final char prevChar() {
074: if (offset > 0) {
075: if (cachedLine != line) {
076: cachedChars = hideSyntacticWhitespace(line);
077: cachedLine = line;
078: }
079: while (--offset >= 0) {
080: if (cachedChars[offset] > ' ')
081: return cachedChars[offset];
082: }
083: }
084: while (true) {
085: if (line.previous() == null) {
086: offset = 0;
087: return DONE;
088: }
089: line = line.previous();
090: cachedChars = hideSyntacticWhitespace(line);
091: cachedLine = line;
092: offset = line.length();
093: while (--offset >= 0) {
094: if (cachedChars[offset] > ' ')
095: return cachedChars[offset];
096: }
097: }
098: }
099:
100: // Default implementation. Subclasses can override this method to examine
101: // the line flags.
102: public char[] hideSyntacticWhitespace(Line line) {
103: return hideSyntacticWhitespace(line.getText());
104: }
105:
106: public char[] hideSyntacticWhitespace(String s) {
107: return s.toCharArray();
108: }
109: }
|