01: /*
02: * TclSyntaxIterator.java
03: *
04: * Copyright (C) 2002 Peter Graves
05: * $Id: TclSyntaxIterator.java,v 1.1.1.1 2002/09/24 16:09:10 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: // Supports movement through the syntactically important text of a buffer, i.e.
25: // skipping whitespace and comments.
26: public final class TclSyntaxIterator extends DefaultSyntaxIterator {
27: private static final int STATE_NEUTRAL = 0;
28: private static final int STATE_SINGLEQUOTE = 1;
29: private static final int STATE_DOUBLEQUOTE = 2;
30:
31: public TclSyntaxIterator(Position pos) {
32: super (pos);
33: }
34:
35: public char[] hideSyntacticWhitespace(Line line) {
36: return hideSyntacticWhitespace(line.getText());
37: }
38:
39: // Returns char array with syntactic whitespace (quotes and comments)
40: // replaced with actual space characters.
41: public char[] hideSyntacticWhitespace(String s) {
42: final char[] chars = s.toCharArray();
43: int state = STATE_NEUTRAL;
44: final int length = chars.length;
45: for (int i = 0; i < length; i++) {
46: char c = chars[i];
47: if (c == '\\' && i < length - 1) {
48: // Escape character.
49: chars[i++] = ' ';
50: chars[i] = ' ';
51: continue;
52: }
53: if (state == STATE_SINGLEQUOTE) {
54: chars[i] = ' ';
55: if (c == '\'')
56: state = STATE_NEUTRAL;
57: continue;
58: }
59: if (state == STATE_DOUBLEQUOTE) {
60: chars[i] = ' ';
61: if (c == '"')
62: state = STATE_NEUTRAL;
63: continue;
64: }
65: // Reaching here, STATE_NEUTRAL...
66: if (c == '\'') {
67: chars[i] = ' ';
68: state = STATE_SINGLEQUOTE;
69: continue;
70: }
71: if (c == '"') {
72: chars[i] = ' ';
73: state = STATE_DOUBLEQUOTE;
74: continue;
75: }
76: if (c == '/') {
77: if (i < length - 1) {
78: if (chars[i + 1] == '/') {
79: // "//" comment starting
80: for (int j = i; j < length; j++)
81: chars[j] = ' ';
82: return chars;
83: }
84: }
85: }
86: }
87: return chars;
88: }
89: }
|