KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PerlSyntaxIterator.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: PerlSyntaxIterator.java,v 1.2 2003/04/25 14:20:50 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 import gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27
28 // Supports movement through the syntactically important text of a buffer,
29
// i.e. skipping whitespace and comments.
30
public class PerlSyntaxIterator extends DefaultSyntaxIterator
31 {
32     private static final int STATE_NEUTRAL = 0;
33     private static final int STATE_QUOTE = 1;
34     private static final int STATE_REGEXP = 2;
35     private static final int STATE_SUBST = 3;
36
37     private static RE matchRE = new UncheckedRE("(=~|!~)[ \t]+m[^a-zA-Z0-9]");
38
39     public PerlSyntaxIterator(Position pos)
40     {
41         super(pos);
42     }
43
44     // Returns char array with syntactic whitespace (quotes and comments)
45
// replaced with actual space characters.
46
public char[] hideSyntacticWhitespace(String JavaDoc s)
47     {
48         char[] chars = s.toCharArray();
49         char quoteChar = 0;
50         char delimiter = 0;
51         int state = STATE_NEUTRAL;
52         final int length = chars.length;
53         for (int i = 0; i < length; i++) {
54             char c = chars[i];
55             if (c == '\\' && i < length-1) {
56                 // Escape!
57
chars[i++] = ' ';
58                 chars[i] = ' ';
59             } else if (state == STATE_QUOTE) {
60                 chars[i] = ' ';
61                 if (c == quoteChar)
62                     state = STATE_NEUTRAL;
63             } else if (state == STATE_REGEXP) {
64                 if (c == delimiter)
65                     state = STATE_NEUTRAL;
66                 else
67                     chars[i] = ' ';
68             } else if (state == STATE_SUBST) {
69                 if (c == delimiter)
70                     state = STATE_REGEXP;
71                 else
72                     chars[i] = ' ';
73             } else if (c == '"' || c == '\'' || c == '`') {
74                 quoteChar = c;
75                 state = STATE_QUOTE;
76                 chars[i] = ' ';
77             } else if (c == '/') {
78                 if (PerlFormatter.isSubst(s, i)) {
79                     state = STATE_SUBST;
80                     delimiter = '/';
81                 } else if (PerlFormatter.isRegExp(s, i)) {
82                     state = STATE_REGEXP;
83                     delimiter = '/';
84                 }
85             } else if (c == '=' || c == '!') {
86                 REMatch match = matchRE.getMatch(s.substring(i));
87                 if (match != null) {
88                     final String JavaDoc m = match.toString();
89                     final int len = m.length();
90                     delimiter = m.charAt(len - 1);
91                     if (delimiter == '{')
92                         delimiter = '}';
93                     state = STATE_REGEXP;
94                     i += len - 1;
95                 }
96             }
97         }
98         // Handle comment part if any.
99
int index = -1;
100         for (int i = 0; i < length; i++) {
101             if (chars[i] == '#') {
102                 if (i > 0) {
103                     // Ignore '#' if escaped or if preceding char is '$'.
104
char c = chars[i-1];
105                     if (c == '\\' || c == '$')
106                         continue;
107                 }
108                 // Otherwise the rest of the line is a comment.
109
index = i;
110                 break;
111             }
112         }
113         if (index >= 0) {
114             for (int i = index; i < length; i++)
115                 chars[i] = ' ';
116         }
117         return chars;
118     }
119 }
120
Popular Tags