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