001: /*
002: * JavaSyntaxIterator.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: JavaSyntaxIterator.java,v 1.4 2004/05/07 01:44:17 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: // Supports movement through the syntactically important text of a buffer, i.e.
025: // skipping whitespace and comments.
026: public final class JavaSyntaxIterator extends DefaultSyntaxIterator
027: implements Constants {
028: public JavaSyntaxIterator(Position pos) {
029: super (pos);
030: }
031:
032: // Caller must make sure parseBuffer() has been called so flags will be
033: // correct.
034: public char[] hideSyntacticWhitespace(Line line) {
035: if (line.flags() == STATE_COMMENT)
036: return hideSyntacticWhitespace(line.getText(),
037: STATE_COMMENT);
038: if (line.flags() == STATE_QUOTE)
039: return hideSyntacticWhitespace(line.getText(), STATE_QUOTE);
040: return hideSyntacticWhitespace(line.getText(), STATE_NEUTRAL);
041: }
042:
043: public char[] hideSyntacticWhitespace(String s) {
044: return hideSyntacticWhitespace(s, STATE_NEUTRAL);
045: }
046:
047: // Replaces comments with space characters and double-quoted strings with
048: // 'X' characters.
049: private char[] hideSyntacticWhitespace(String s, int initialState) {
050: final char[] chars = s.toCharArray();
051: final int length = chars.length;
052: if (length > 0 && chars[0] == '#') {
053: // Preprocessor line.
054: for (int i = length; i-- > 0;)
055: chars[i] = ' ';
056: return chars;
057: }
058: int state = initialState;
059: for (int i = 0; i < length; i++) {
060: char c = chars[i];
061: if (c == '\\' && i < length - 1) {
062: // Escape character.
063: chars[i++] = ' ';
064: chars[i] = ' ';
065: continue;
066: }
067: if (state == STATE_QUOTE) {
068: chars[i] = 'X';
069: if (c == '"')
070: state = STATE_NEUTRAL;
071: continue;
072: }
073: if (state == STATE_SINGLEQUOTE) {
074: chars[i] = ' ';
075: if (c == '\'')
076: state = STATE_NEUTRAL;
077: continue;
078: }
079: if (state == STATE_COMMENT) {
080: if (c == '*' && i < length - 1 && chars[i + 1] == '/') {
081: // /* */ comment ending
082: chars[i++] = ' ';
083: chars[i] = ' ';
084: state = STATE_NEUTRAL;
085: } else
086: chars[i] = ' ';
087: continue;
088: }
089:
090: // Reaching here, STATE_NEUTRAL...
091: if (c == '"') {
092: chars[i] = ' ';
093: state = STATE_QUOTE;
094: continue;
095: }
096: if (c == '\'') {
097: chars[i] = ' ';
098: state = STATE_SINGLEQUOTE;
099: continue;
100: }
101: if (c == '/') {
102: if (i < length - 1) {
103: if (chars[i + 1] == '*') {
104: // /* */ comment starting
105: chars[i++] = ' ';
106: chars[i] = ' ';
107: state = STATE_COMMENT;
108: continue;
109: }
110: if (chars[i + 1] == '/') {
111: // "//" comment starting
112: for (int j = i; j < length; j++)
113: chars[j] = ' ';
114: return chars;
115: }
116: }
117: }
118: }
119: return chars;
120: }
121: }
|