KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RubySyntaxIterator.java
3  *
4  * Copyright (C) 2002 Jens Luedicke <jens@irs-net.com>
5  * based on PythonSyntaxIterator.java
6  * $Id: RubySyntaxIterator.java,v 1.1.1.1 2002/09/24 16:08:46 piso Exp $
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.armedbear.j;
24
25 public final class RubySyntaxIterator extends DefaultSyntaxIterator
26 {
27     private static final int STATE_NEUTRAL = 0;
28     private static final int STATE_QUOTE = 1;
29
30     public RubySyntaxIterator(Position pos)
31     {
32         super(pos);
33     }
34
35     // Returns char array with syntactic whitespace (quotes and comments)
36
// replaced with actual space characters.
37
public char[] hideSyntacticWhitespace(String JavaDoc s)
38     {
39         char[] chars = s.toCharArray();
40         char quoteChar = 0;
41         int state = STATE_NEUTRAL;
42         final int length = chars.length;
43         for (int i = 0; i < length; i++) {
44             char c = chars[i];
45             if (c == '\\' && i < length-1) {
46                 // Escape!
47
chars[++i] = ' ';
48             } else if (state == STATE_QUOTE) {
49                 chars[i] = ' ';
50                 if (c == quoteChar)
51                     state = STATE_NEUTRAL;
52             } else if (c == '"' || c == '\'') {
53                 quoteChar = c;
54                 state = STATE_QUOTE;
55                 chars[i] = ' ';
56             }
57         }
58         // Handle comment part if any.
59
int index = -1;
60         for (int i = 0; i < length; i++) {
61             if (chars[i] == '#') {
62                 if (i > 0) {
63                     // Ignore '#' if escaped.
64
char c = chars[i-1];
65                     if (c == '\\')
66                         continue;
67                 }
68                 // Otherwise the rest of the line is a comment.
69
index = i;
70                 break;
71             }
72         }
73         if (index >= 0) {
74             for (int i = index; i < length; i++)
75                 chars[i] = ' ';
76         }
77         return chars;
78     }
79 }
80
Popular Tags