KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PHPSyntaxIterator.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: PHPSyntaxIterator.java,v 1.1.1.1 2002/09/24 16:07:54 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 PHPSyntaxIterator extends DefaultSyntaxIterator
27     implements Constants
28 {
29     public PHPSyntaxIterator(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         int initialState = PHPFormatter.getState(line.flags());
39         switch (initialState) {
40             case STATE_NEUTRAL:
41             case STATE_COMMENT:
42             case STATE_QUOTE:
43             case STATE_SINGLEQUOTE:
44                 break;
45             default:
46                 // We don't care about any other states.
47
initialState = STATE_NEUTRAL;
48                 break;
49         }
50         return hideSyntacticWhitespace(line.getText(), initialState);
51     }
52
53     public char[] hideSyntacticWhitespace(String JavaDoc s)
54     {
55         return hideSyntacticWhitespace(s, STATE_NEUTRAL);
56     }
57
58     // Returns char array with syntactic whitespace (quotes and comments)
59
// replaced with actual space characters.
60
private char[] hideSyntacticWhitespace(String JavaDoc s, int initialState)
61     {
62         final char[] chars = s.toCharArray();
63         int state = initialState;
64         final int length = chars.length;
65         for (int i = 0; i < length; i++) {
66             char c = chars[i];
67             if (c == '\\' && i < length-1) {
68                 // Escape character.
69
chars[i++] = ' ';
70                 chars[i] = ' ';
71                 continue;
72             }
73             if (state == STATE_QUOTE) {
74                 chars[i] = ' ';
75                 if (c == '"')
76                     state = STATE_NEUTRAL;
77                 continue;
78             }
79             if (state == STATE_SINGLEQUOTE) {
80                 chars[i] = ' ';
81                 if (c == '\'')
82                     state = STATE_NEUTRAL;
83                 continue;
84             }
85             if (state == STATE_COMMENT) {
86                 if (c == '*' && i < length-1 && chars[i+1] == '/') {
87                     // /* */ comment ending
88
chars[i++] = ' ';
89                     chars[i] = ' ';
90                     state = STATE_NEUTRAL;
91                 } else
92                     chars[i] = ' ';
93                 continue;
94             }
95             // Reaching here, STATE_NEUTRAL...
96
if (c == '"') {
97                 chars[i] = ' ';
98                 state = STATE_QUOTE;
99                 continue;
100             }
101             if (c == '\'') {
102                 chars[i] = ' ';
103                 state = STATE_SINGLEQUOTE;
104                 continue;
105             }
106             if (c == '/') {
107                 if (i < length-1) {
108                     if (chars[i+1] == '*') {
109                         // /* */ comment starting
110
chars[i++] = ' ';
111                         chars[i] = ' ';
112                         state = STATE_COMMENT;
113                         continue;
114                     }
115                     if (chars[i+1] == '/') {
116                         // "//" comment starting
117
for (int j = i; j < length; j++)
118                             chars[j] = ' ';
119                         return chars;
120                     }
121                 }
122             }
123         }
124         return chars;
125     }
126 }
127
Popular Tags