01: /*
02: * LispSyntaxIterator.java
03: *
04: * Copyright (C) 1998-2002 Peter Graves
05: * $Id: LispSyntaxIterator.java,v 1.2 2002/10/18 21:35:47 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,
25: // i.e. skipping whitespace and comments.
26: public final class LispSyntaxIterator extends DefaultSyntaxIterator
27: implements Constants {
28: public LispSyntaxIterator(Position pos) {
29: super (pos);
30: }
31:
32: // Caller must make sure parseBuffer() has been called so flags will be
33: // correct.
34: public char[] hideSyntacticWhitespace(Line line) {
35: if (line.flags() == STATE_QUOTE)
36: return hideSyntacticWhitespace(line.getText(), STATE_QUOTE);
37: else
38: return hideSyntacticWhitespace(line.getText(),
39: STATE_NEUTRAL);
40: }
41:
42: public char[] hideSyntacticWhitespace(String s) {
43: return hideSyntacticWhitespace(s, STATE_NEUTRAL);
44: }
45:
46: // Returns char array with syntactic whitespace (quotes and comments)
47: // replaced with actual space characters.
48: public char[] hideSyntacticWhitespace(String s, int initialState) {
49: char[] chars = s.toCharArray();
50: int state = initialState;
51: int length = chars.length;
52: for (int i = 0; i < length; i++) {
53: char c = chars[i];
54: if (c == '\\' && i < length - 1) {
55: // Escape character.
56: chars[i++] = ' ';
57: chars[i] = ' ';
58: continue;
59: }
60: if (state == STATE_QUOTE) {
61: chars[i] = ' ';
62: if (c == '"')
63: state = STATE_NEUTRAL;
64: } else if (c == '"') {
65: state = STATE_QUOTE;
66: chars[i] = ' ';
67: }
68: }
69: // Handle comment part if any.
70: int index = -1;
71: for (int i = 0; i < length - 1; i++) {
72: if (chars[i] == '\\')
73: ++i; // Escape character.
74: else if (chars[i] == ';') {
75: index = i;
76: break;
77: }
78: }
79: if (index >= 0) {
80: for (int i = index; i < length; i++)
81: chars[i] = ' ';
82: }
83: return chars;
84: }
85: }
|