KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaSyntaxIterator.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: JavaSyntaxIterator.java,v 1.4 2004/05/07 01:44:17 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, i.e.
25
// skipping whitespace and comments.
26
public final class JavaSyntaxIterator extends DefaultSyntaxIterator
27     implements Constants
28 {
29     public JavaSyntaxIterator(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_COMMENT)
39             return hideSyntacticWhitespace(line.getText(), STATE_COMMENT);
40         if (line.flags() == STATE_QUOTE)
41             return hideSyntacticWhitespace(line.getText(), STATE_QUOTE);
42         return hideSyntacticWhitespace(line.getText(), STATE_NEUTRAL);
43     }
44
45     public char[] hideSyntacticWhitespace(String JavaDoc s)
46     {
47         return hideSyntacticWhitespace(s, STATE_NEUTRAL);
48     }
49
50     // Replaces comments with space characters and double-quoted strings with
51
// 'X' characters.
52
private char[] hideSyntacticWhitespace(String JavaDoc s, int initialState)
53     {
54         final char[] chars = s.toCharArray();
55         final int length = chars.length;
56         if (length > 0 && chars[0] == '#') {
57             // Preprocessor line.
58
for (int i = length; i-- > 0;)
59                 chars[i] = ' ';
60             return chars;
61         }
62         int state = initialState;
63         for (int i = 0; i < length; i++) {
64             char c = chars[i];
65             if (c == '\\' && i < length-1) {
66                 // Escape character.
67
chars[i++] = ' ';
68                 chars[i] = ' ';
69                 continue;
70             }
71             if (state == STATE_QUOTE) {
72                 chars[i] = 'X';
73                 if (c == '"')
74                     state = STATE_NEUTRAL;
75                 continue;
76             }
77             if (state == STATE_SINGLEQUOTE) {
78                 chars[i] = ' ';
79                 if (c == '\'')
80                     state = STATE_NEUTRAL;
81                 continue;
82             }
83             if (state == STATE_COMMENT) {
84                 if (c == '*' && i < length-1 && chars[i+1] == '/') {
85                     // /* */ comment ending
86
chars[i++] = ' ';
87                     chars[i] = ' ';
88                     state = STATE_NEUTRAL;
89                 } else
90                     chars[i] = ' ';
91                 continue;
92             }
93
94             // Reaching here, STATE_NEUTRAL...
95
if (c == '"') {
96                 chars[i] = ' ';
97                 state = STATE_QUOTE;
98                 continue;
99             }
100             if (c == '\'') {
101                 chars[i] = ' ';
102                 state = STATE_SINGLEQUOTE;
103                 continue;
104             }
105             if (c == '/') {
106                 if (i < length-1) {
107                     if (chars[i+1] == '*') {
108                         // /* */ comment starting
109
chars[i++] = ' ';
110                         chars[i] = ' ';
111                         state = STATE_COMMENT;
112                         continue;
113                     }
114                     if (chars[i+1] == '/') {
115                         // "//" comment starting
116
for (int j = i; j < length; j++)
117                             chars[j] = ' ';
118                         return chars;
119                     }
120                 }
121             }
122         }
123         return chars;
124     }
125 }
126
Popular Tags