1 21 22 package org.armedbear.j; 23 24 public final class PythonSyntaxIterator extends DefaultSyntaxIterator 25 { 26 private static final int STATE_NEUTRAL = 0; 27 private static final int STATE_QUOTE = 1; 28 29 public PythonSyntaxIterator(Position pos) 30 { 31 super(pos); 32 } 33 34 public char[] hideSyntacticWhitespace(String s) 37 { 38 char[] chars = s.toCharArray(); 39 char quoteChar = 0; 40 int state = STATE_NEUTRAL; 41 final int length = chars.length; 42 for (int i = 0; i < length; i++) { 43 char c = chars[i]; 44 if (c == '\\' && i < length-1) { 45 chars[++i] = ' '; 47 } else if (state == STATE_QUOTE) { 48 chars[i] = ' '; 49 if (c == quoteChar) 50 state = STATE_NEUTRAL; 51 } else if (c == '"' || c == '\'') { 52 quoteChar = c; 53 state = STATE_QUOTE; 54 chars[i] = ' '; 55 } 56 } 57 int index = -1; 59 for (int i = 0; i < length; i++) { 60 if (chars[i] == '#') { 61 if (i > 0) { 62 char c = chars[i-1]; 64 if (c == '\\') 65 continue; 66 } 67 index = i; 69 break; 70 } 71 } 72 if (index >= 0) { 73 for (int i = index; i < length; i++) 74 chars[i] = ' '; 75 } 76 return chars; 77 } 78 } 79 | Popular Tags |