KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > LispSyntaxIterator


1 /*
2  * LispSyntaxIterator.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: LispSyntaxIterator.java,v 1.2 2002/10/18 21:35:47 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 {
29     public LispSyntaxIterator(Position pos)
30     {
31         super(pos);
32     }
33
34     // Caller must make sure parseBuffer() has been called so flags will be
35
// correct.
36
public char[] hideSyntacticWhitespace(Line line)
37     {
38         if (line.flags() == STATE_QUOTE)
39             return hideSyntacticWhitespace(line.getText(), STATE_QUOTE);
40         else
41             return hideSyntacticWhitespace(line.getText(), STATE_NEUTRAL);
42     }
43
44     public char[] hideSyntacticWhitespace(String JavaDoc s)
45     {
46         return hideSyntacticWhitespace(s, STATE_NEUTRAL);
47     }
48
49     // Returns char array with syntactic whitespace (quotes and comments)
50
// replaced with actual space characters.
51
public char[] hideSyntacticWhitespace(String JavaDoc s, int initialState)
52     {
53         char[] chars = s.toCharArray();
54         int state = initialState;
55         int length = chars.length;
56         for (int i = 0; i < length; i++) {
57             char c = chars[i];
58             if (c == '\\' && i < length-1) {
59                 // Escape character.
60
chars[i++] = ' ';
61                 chars[i] = ' ';
62                 continue;
63             }
64             if (state == STATE_QUOTE) {
65                 chars[i] = ' ';
66                 if (c == '"')
67                     state = STATE_NEUTRAL;
68             } else if (c == '"') {
69                 state = STATE_QUOTE;
70                 chars[i] = ' ';
71             }
72         }
73         // Handle comment part if any.
74
int index = -1;
75         for (int i = 0; i < length-1; i++) {
76             if (chars[i] == '\\')
77                 ++i; // Escape character.
78
else if (chars[i] == ';') {
79                 index = i;
80                 break;
81             }
82         }
83         if (index >= 0) {
84             for (int i = index; i < length; i++)
85                 chars[i] = ' ';
86         }
87         return chars;
88     }
89 }
90
Popular Tags