01: /*
02: * RubySyntaxIterator.java
03: *
04: * Copyright (C) 2002 Jens Luedicke <jens@irs-net.com>
05: * based on PythonSyntaxIterator.java
06: * $Id: RubySyntaxIterator.java,v 1.1.1.1 2002/09/24 16:08:46 piso Exp $
07: *
08: * This program is free software; you can redistribute it and/or
09: * 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: private static final int STATE_NEUTRAL = 0;
27: private static final int STATE_QUOTE = 1;
28:
29: public RubySyntaxIterator(Position pos) {
30: super (pos);
31: }
32:
33: // Returns char array with syntactic whitespace (quotes and comments)
34: // replaced with actual space characters.
35: public char[] hideSyntacticWhitespace(String s) {
36: char[] chars = s.toCharArray();
37: char quoteChar = 0;
38: int state = STATE_NEUTRAL;
39: final int length = chars.length;
40: for (int i = 0; i < length; i++) {
41: char c = chars[i];
42: if (c == '\\' && i < length - 1) {
43: // Escape!
44: chars[++i] = ' ';
45: } else if (state == STATE_QUOTE) {
46: chars[i] = ' ';
47: if (c == quoteChar)
48: state = STATE_NEUTRAL;
49: } else if (c == '"' || c == '\'') {
50: quoteChar = c;
51: state = STATE_QUOTE;
52: chars[i] = ' ';
53: }
54: }
55: // Handle comment part if any.
56: int index = -1;
57: for (int i = 0; i < length; i++) {
58: if (chars[i] == '#') {
59: if (i > 0) {
60: // Ignore '#' if escaped.
61: char c = chars[i - 1];
62: if (c == '\\')
63: continue;
64: }
65: // Otherwise the rest of the line is a comment.
66: index = i;
67: break;
68: }
69: }
70: if (index >= 0) {
71: for (int i = index; i < length; i++)
72: chars[i] = ' ';
73: }
74: return chars;
75: }
76: }
|