001: /*
002: * PHPSyntaxIterator.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: PHPSyntaxIterator.java,v 1.1.1.1 2002/09/24 16:07:54 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 PHPSyntaxIterator extends DefaultSyntaxIterator
027: implements Constants {
028: public PHPSyntaxIterator(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: int initialState = PHPFormatter.getState(line.flags());
036: switch (initialState) {
037: case STATE_NEUTRAL:
038: case STATE_COMMENT:
039: case STATE_QUOTE:
040: case STATE_SINGLEQUOTE:
041: break;
042: default:
043: // We don't care about any other states.
044: initialState = STATE_NEUTRAL;
045: break;
046: }
047: return hideSyntacticWhitespace(line.getText(), initialState);
048: }
049:
050: public char[] hideSyntacticWhitespace(String s) {
051: return hideSyntacticWhitespace(s, STATE_NEUTRAL);
052: }
053:
054: // Returns char array with syntactic whitespace (quotes and comments)
055: // replaced with actual space characters.
056: private char[] hideSyntacticWhitespace(String s, int initialState) {
057: final char[] chars = s.toCharArray();
058: int state = initialState;
059: final int length = chars.length;
060: for (int i = 0; i < length; i++) {
061: char c = chars[i];
062: if (c == '\\' && i < length - 1) {
063: // Escape character.
064: chars[i++] = ' ';
065: chars[i] = ' ';
066: continue;
067: }
068: if (state == STATE_QUOTE) {
069: chars[i] = ' ';
070: if (c == '"')
071: state = STATE_NEUTRAL;
072: continue;
073: }
074: if (state == STATE_SINGLEQUOTE) {
075: chars[i] = ' ';
076: if (c == '\'')
077: state = STATE_NEUTRAL;
078: continue;
079: }
080: if (state == STATE_COMMENT) {
081: if (c == '*' && i < length - 1 && chars[i + 1] == '/') {
082: // /* */ comment ending
083: chars[i++] = ' ';
084: chars[i] = ' ';
085: state = STATE_NEUTRAL;
086: } else
087: chars[i] = ' ';
088: continue;
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: }
|