[Pydev-cvs] org.python.pydev.core/src/org/python/pydev/core/docutils ParsingUtils.java, 1.23, 1.24
Brought to you by:
fabioz
Update of /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7795/src/org/python/pydev/core/docutils Modified Files: ParsingUtils.java DocUtils.java PySelection.java PyDocIterator.java ImportHandle.java Log Message: Changes in the ParsingUtils interface (creating correct version with factory to properly adapt to the object being read). Index: DocUtils.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/DocUtils.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** DocUtils.java 24 Mar 2008 15:57:35 -0000 1.13 --- DocUtils.java 6 Jul 2008 21:58:28 -0000 1.14 *************** *** 151,171 **** return newStr; } ! ! public static char getPeer(char c){ ! for (int i = 0; i < BRACKETS.length; i++) { ! if(c == BRACKETS[i]){ ! int mod = i % 2; ! if(mod == 0){ ! return BRACKETS[i + 1]; ! }else{ ! return BRACKETS[i - 1]; ! } ! } ! } ! ! throw new NoPeerAvailableException("Unable to find peer for :"+c); ! ! } ! /** * An array of Python pairs of characters that you will find in any Python code. --- 151,155 ---- return newStr; } ! /** * An array of Python pairs of characters that you will find in any Python code. *************** *** 179,182 **** --- 163,180 ---- public static final char[] BRACKETS = { '{', '}', '(', ')', '[', ']' }; + public static char getPeer(char c){ + switch(c){ + case '{':return '}'; + case '}':return '{'; + case '(':return ')'; + case ')':return '('; + case '[':return ']'; + case ']':return '['; + } + + throw new NoPeerAvailableException("Unable to find peer for :"+c); + + } + public static boolean isClosingPeer(char lastChar) { return lastChar == '}' || lastChar == ')' || lastChar == ']'; Index: PySelection.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java,v retrieving revision 1.63 retrieving revision 1.64 diff -C2 -d -r1.63 -r1.64 *** PySelection.java 15 Jun 2008 16:45:20 -0000 1.63 --- PySelection.java 6 Jul 2008 21:58:28 -0000 1.64 *************** *** 217,220 **** --- 217,221 ---- IDocument document = getDoc(); int lines = document.getNumberOfLines(); + ParsingUtils parsingUtils = ParsingUtils.create(document); for (int line = startingAtLine; line < lines; line++) { String str = getLine(line); *************** *** 240,244 **** throw new RuntimeException(e1); } ! int j = ParsingUtils.eatPar(document, lineOffset+i, null); try { line = document.getLineOfOffset(j); --- 241,245 ---- throw new RuntimeException(e1); } ! int j = parsingUtils.eatPar(lineOffset+i, null); try { line = document.getLineOfOffset(j); *************** *** 273,294 **** String strDoc = d.get(initialOffset, d.getLength() - initialOffset); ! if(initialOffset > strDoc.length()-1){ return new int[]{-1, -1}; } char current = strDoc.charAt(initialOffset); ! //for checking if it is global, it must be in the beggining of a line (must be right after a \r or \n). ! while (current != '\'' && current != '"' && initialOffset < strDoc.length()-1) { //if it is inside a parenthesis, we will not take it into consideration. if(current == '('){ ! initialOffset = ParsingUtils.eatPar(strDoc, initialOffset, buf); } initialOffset += 1; ! if(initialOffset < strDoc.length()-1){ current = strDoc.charAt(initialOffset); } --- 274,297 ---- String strDoc = d.get(initialOffset, d.getLength() - initialOffset); ! int docLen = strDoc.length(); ! ! if(initialOffset > docLen-1){ return new int[]{-1, -1}; } char current = strDoc.charAt(initialOffset); ! ParsingUtils parsingUtils = ParsingUtils.create(strDoc); //for checking if it is global, it must be in the beggining of a line (must be right after a \r or \n). ! while (current != '\'' && current != '"' && initialOffset < docLen-1) { //if it is inside a parenthesis, we will not take it into consideration. if(current == '('){ ! initialOffset = parsingUtils.eatPar(initialOffset, buf); } initialOffset += 1; ! if(initialOffset < docLen-1){ current = strDoc.charAt(initialOffset); } *************** *** 296,303 **** //either, we are at the end of the document or we found a literal ! if(initialOffset < strDoc.length()-1){ if(initialOffset == 0){ //first char of the document... this is ok ! int i = ParsingUtils.eatLiterals(strDoc, buf, initialOffset); return new int[]{initialOffset, i}; } --- 299,306 ---- //either, we are at the end of the document or we found a literal ! if(initialOffset < docLen-1){ if(initialOffset == 0){ //first char of the document... this is ok ! int i = parsingUtils.eatLiterals(buf, initialOffset); return new int[]{initialOffset, i}; } *************** *** 306,310 **** //it is only global if after \r or \n if(lastChar == '\r' || lastChar == '\n'){ ! int i = ParsingUtils.eatLiterals(strDoc, buf, initialOffset); return new int[]{initialOffset, i}; } --- 309,313 ---- //it is only global if after \r or \n if(lastChar == '\r' || lastChar == '\n'){ ! int i = parsingUtils.eatLiterals(buf, initialOffset); return new int[]{initialOffset, i}; } *************** *** 853,857 **** String docContents = doc.get(); int i = lineOffset + openParIndex; ! int j = ParsingUtils.eatPar(docContents, i, null); String insideParentesisTok = docContents.substring(i + 1, j); --- 856,860 ---- String docContents = doc.get(); int i = lineOffset + openParIndex; ! int j = ParsingUtils.create(docContents).eatPar(i, null); String insideParentesisTok = docContents.substring(i + 1, j); *************** *** 1266,1279 **** */ public static int getBeforeParentesisCall(Object doc, int calltipOffset) { ! char c = ParsingUtils.charAt(doc, calltipOffset); while(calltipOffset > 0 && c != '('){ calltipOffset --; ! c = ParsingUtils.charAt(doc, calltipOffset); } if(c == '('){ while(calltipOffset > 0 && Character.isWhitespace(c)){ calltipOffset --; ! c = ParsingUtils.charAt(doc, calltipOffset); } return calltipOffset; --- 1269,1283 ---- */ public static int getBeforeParentesisCall(Object doc, int calltipOffset) { ! ParsingUtils parsingUtils = ParsingUtils.create(doc); ! char c = parsingUtils.charAt(calltipOffset); while(calltipOffset > 0 && c != '('){ calltipOffset --; ! c = parsingUtils.charAt(calltipOffset); } if(c == '('){ while(calltipOffset > 0 && Character.isWhitespace(c)){ calltipOffset --; ! c = parsingUtils.charAt(calltipOffset); } return calltipOffset; Index: ParsingUtils.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/ParsingUtils.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** ParsingUtils.java 6 Jul 2008 17:10:39 -0000 1.23 --- ParsingUtils.java 6 Jul 2008 21:58:28 -0000 1.24 *************** *** 19,25 **** * @author Fabio */ ! public class ParsingUtils implements IPythonPartitions{ /** * @param cs the char array we are parsing --- 19,161 ---- * @author Fabio */ ! public abstract class ParsingUtils implements IPythonPartitions{ ! ! ! /** ! * Class that handles char[] ! * ! * @author Fabio ! */ ! private static class CharArrayParsingUtils extends ParsingUtils{ ! private char[] cs; ! public CharArrayParsingUtils(char[] cs) { ! this.cs = cs; ! } ! public int len() { ! return cs.length; ! } ! public char charAt(int i) { ! return cs[i]; ! } ! } ! ! ! /** ! * Class that handles FastStringBuffer ! * ! * @author Fabio ! */ ! private static class FastStringBufferParsingUtils extends ParsingUtils{ ! private FastStringBuffer cs; ! public FastStringBufferParsingUtils(FastStringBuffer cs) { ! this.cs = cs; ! } ! public int len() { ! return cs.length(); ! } ! public char charAt(int i) { ! return cs.charAt(i); ! } ! } ! ! /** ! * Class that handles StringBuffer ! * ! * @author Fabio ! */ ! private static class StringBufferParsingUtils extends ParsingUtils{ ! private StringBuffer cs; ! public StringBufferParsingUtils(StringBuffer cs) { ! this.cs = cs; ! } ! public int len() { ! return cs.length(); ! } ! public char charAt(int i) { ! return cs.charAt(i); ! } ! } ! ! /** ! * Class that handles String ! * ! * @author Fabio ! */ ! private static class StringParsingUtils extends ParsingUtils{ ! private String cs; ! public StringParsingUtils(String cs) { ! this.cs = cs; ! } ! public int len() { ! return cs.length(); ! } ! public char charAt(int i) { ! return cs.charAt(i); ! } ! } ! ! /** ! * Class that handles String ! * ! * @author Fabio ! */ ! private static class IDocumentParsingUtils extends ParsingUtils{ ! private IDocument cs; ! public IDocumentParsingUtils(IDocument cs) { ! this.cs = cs; ! } ! public int len() { ! return cs.getLength(); ! } ! public char charAt(int i) { ! try { ! return cs.getChar(i); ! } catch (BadLocationException e) { ! throw new RuntimeException(e); ! } ! } ! } ! ! /** ! * Factory method to create it. ! */ ! public static ParsingUtils create(Object cs) { ! if(cs instanceof char[]){ ! return new CharArrayParsingUtils((char[])cs); ! } ! if(cs instanceof FastStringBuffer){ ! return new FastStringBufferParsingUtils((FastStringBuffer)cs); ! } ! if(cs instanceof StringBuffer){ ! return new StringBufferParsingUtils((StringBuffer)cs); ! } ! if(cs instanceof String){ ! return new StringParsingUtils((String)cs); ! } ! if(cs instanceof IDocument){ ! return new IDocumentParsingUtils((IDocument)cs); ! } ! throw new RuntimeException("Don't know how to create instance for: "+cs.getClass()); ! } ! + //Abstract interfaces ------------------------------------------------------------- + + /** + * @return the char at a given position of the object + */ + public abstract char charAt(int i); + + + /** + * @return the length of the contained object + */ + public abstract int len(); + + + //API methods -------------------------------------------------------------------- + + /** * @param cs the char array we are parsing *************** *** 29,37 **** * @return the end of the comments position (end of document or new line char) */ ! public static int eatComments(Object cs, FastStringBuffer buf, int i) { ! int len = len(cs); char c; ! while(i < len && (c = charAt(cs,i)) != '\n' && c != '\r'){ if(buf != null){ buf.append(c); --- 165,173 ---- * @return the end of the comments position (end of document or new line char) */ ! public int eatComments(FastStringBuffer buf, int i) { ! int len = len(); char c; ! while(i < len && (c = charAt(i)) != '\n' && c != '\r'){ if(buf != null){ buf.append(c); *************** *** 42,46 **** if(i < len){ if(buf != null){ ! buf.append(charAt(cs,i)); } } --- 178,182 ---- if(i < len){ if(buf != null){ ! buf.append(charAt(i)); } } *************** *** 56,63 **** * @return the end of the literal position (or end of document) */ ! public static int eatLiterals(Object cs, FastStringBuffer buf, int i) { //ok, current pos is ' or " //check if we're starting a single or multiline comment... ! char curr = charAt(cs, i); if(curr != '"' && curr != '\''){ --- 192,199 ---- * @return the end of the literal position (or end of document) */ ! public int eatLiterals(FastStringBuffer buf, int i) { //ok, current pos is ' or " //check if we're starting a single or multiline comment... ! char curr = charAt(i); if(curr != '"' && curr != '\''){ *************** *** 65,74 **** } ! int j = getLiteralEnd(cs, i, curr); if(buf != null){ ! int len = len(cs); for (int k = i; k < len && k <= j; k++) { ! buf.append(charAt(cs, k)); } } --- 201,210 ---- } ! int j = getLiteralEnd(i, curr); if(buf != null){ ! int len = len(); for (int k = i; k < len && k <= j; k++) { ! buf.append(charAt(k)); } } *************** *** 83,94 **** * @return the end of the multiline literal */ ! public static int getLiteralEnd(Object cs, int i, char curr) { ! boolean multi = isMultiLiteral(cs, i, curr); int j; if(multi){ ! j = findNextMulti(cs, i+3, curr); }else{ ! j = findNextSingle(cs, i+1, curr); } return j; --- 219,230 ---- * @return the end of the multiline literal */ ! public int getLiteralEnd(int i, char curr) { ! boolean multi = isMultiLiteral(i, curr); int j; if(multi){ ! j = findNextMulti(i+3, curr); }else{ ! j = findNextSingle(i+1, curr); } return j; *************** *** 101,106 **** * @return the end of the literal position (or end of document) */ ! public static int eatPar(Object cs, int i, FastStringBuffer buf) { ! return eatPar(cs, i, buf, '('); } --- 237,242 ---- * @return the end of the literal position (or end of document) */ ! public int eatPar(int i, FastStringBuffer buf) { ! return eatPar(i, buf, '('); } *************** *** 108,112 **** * @param buf if null, it'll simply advance without adding anything to the buffer. */ ! public static int eatPar(Object cs, int i, FastStringBuffer buf, char par) { char c = ' '; --- 244,248 ---- * @param buf if null, it'll simply advance without adding anything to the buffer. */ ! public int eatPar(int i, FastStringBuffer buf, char par) { char c = ' '; *************** *** 114,130 **** int j = i+1; ! int len = len(cs); ! while(j < len && (c = charAt(cs,j)) != closingPar){ j++; if(c == '\'' || c == '"'){ //ignore comments or multiline comments... ! j = ParsingUtils.eatLiterals(cs, null, j-1)+1; }else if(c == '#'){ ! j = ParsingUtils.eatComments(cs, null, j-1)+1; }else if( c == par){ //open another par. ! j = eatPar(cs, j-1, null, par)+1; }else{ --- 250,266 ---- int j = i+1; ! int len = len(); ! while(j < len && (c = charAt(j)) != closingPar){ j++; if(c == '\'' || c == '"'){ //ignore comments or multiline comments... ! j = eatLiterals(null, j-1)+1; }else if(c == '#'){ ! j = eatComments(null, j-1)+1; }else if( c == par){ //open another par. ! j = eatPar(j-1, null, par)+1; }else{ *************** *** 141,149 **** * discover the position of the closing quote */ ! public static int findNextSingle(Object cs, int i, char curr) { boolean ignoreNext = false; ! int len = len(cs); while(i < len){ ! char c = charAt(cs,i); --- 277,285 ---- * discover the position of the closing quote */ ! public int findNextSingle(int i, char curr) { boolean ignoreNext = false; ! int len = len(); while(i < len){ ! char c = charAt(i); *************** *** 168,176 **** * check the end of the multiline quote */ ! public static int findNextMulti(Object cs, int i, char curr) { ! int len = len(cs); while(i+2 < len){ ! char c = charAt(cs,i); ! if (c == curr && charAt(cs,i+1) == curr && charAt(cs,i+2) == curr){ break; } --- 304,312 ---- * check the end of the multiline quote */ ! public int findNextMulti(int i, char curr) { ! int len = len(); while(i+2 < len){ ! char c = charAt(i); ! if (c == curr && charAt(i+1) == curr && charAt(i+2) == curr){ break; } *************** *** 186,230 **** } ! public static char charAt(Object o, int i){ ! if (o instanceof char[]) { ! return ((char[]) o)[i]; ! } ! if (o instanceof FastStringBuffer) { ! return ((FastStringBuffer) o).charAt(i); ! } ! if (o instanceof StringBuffer) { ! return ((StringBuffer) o).charAt(i); ! } ! if (o instanceof String) { ! return ((String) o).charAt(i); ! } ! if (o instanceof IDocument) { ! try { ! return ((IDocument) o).getChar(i); ! } catch (BadLocationException e) { ! throw new RuntimeException(e); ! } ! } ! throw new RuntimeException("unable to get char at of "+o.getClass()); ! } - public static int len(Object o){ - if (o instanceof char[]) { - return ((char[]) o).length; - } - if (o instanceof FastStringBuffer) { - return ((FastStringBuffer) o).length(); - } - if (o instanceof StringBuffer) { - return ((StringBuffer) o).length(); - } - if (o instanceof String) { - return ((String) o).length(); - } - if (o instanceof IDocument) { - return ((IDocument) o).getLength(); - } - throw new RuntimeException("unable to get len of "+o.getClass()); - } /** --- 322,347 ---- } ! ! ! ! ! ! ! ! ! ! ! ! ! //STATIC INTERFACES FROM NOW ON ---------------------------------------------------------------- ! //STATIC INTERFACES FROM NOW ON ---------------------------------------------------------------- ! //STATIC INTERFACES FROM NOW ON ---------------------------------------------------------------- ! //STATIC INTERFACES FROM NOW ON ---------------------------------------------------------------- ! //STATIC INTERFACES FROM NOW ON ---------------------------------------------------------------- ! ! ! ! /** *************** *** 235,244 **** * @return whether we are at the start of a multi line literal or not. */ ! public static boolean isMultiLiteral(Object cs, int i, char curr){ ! int len = len(cs); if(len <= i + 2){ return false; } ! if(charAt(cs, i+1) == curr && charAt(cs,i+2) == curr){ return true; } --- 352,361 ---- * @return whether we are at the start of a multi line literal or not. */ ! public boolean isMultiLiteral(int i, char curr){ ! int len = len(); if(len <= i + 2){ return false; } ! if(charAt(i+1) == curr && charAt(i+2) == curr){ return true; } *************** *** 250,253 **** --- 367,371 ---- removeCommentsWhitespacesAndLiterals(buf, true); } + /** * Removes all the comments, whitespaces and literals from a FastStringBuffer (might be useful when *************** *** 260,263 **** --- 378,382 ---- */ public static void removeCommentsWhitespacesAndLiterals(FastStringBuffer buf, boolean whitespacesToo) { + ParsingUtils parsingUtils = create(buf); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); *************** *** 273,277 **** if(ch == '\'' || ch == '"'){ ! int j = getLiteralEnd(buf, i, ch); if(whitespacesToo){ buf.delete(i, j+1); --- 392,396 ---- if(ch == '\'' || ch == '"'){ ! int j = parsingUtils.getLiteralEnd(i, ch); if(whitespacesToo){ buf.delete(i, j+1); *************** *** 295,298 **** --- 414,418 ---- } public static void removeLiterals(FastStringBuffer buf) { + ParsingUtils parsingUtils = create(buf); for (int i = 0; i < buf.length(); i++) { char ch = buf.charAt(i); *************** *** 306,310 **** if(ch == '\'' || ch == '"'){ ! int j = getLiteralEnd(buf, i, ch); for (int k = 0; i+k < j+1; k++) { buf.replace(i+k, i+k+1, " "); --- 426,430 ---- if(ch == '\'' || ch == '"'){ ! int j = parsingUtils.getLiteralEnd(i, ch); for (int k = 0; i+k < j+1; k++) { buf.replace(i+k, i+k+1, " "); *************** *** 356,359 **** --- 476,480 ---- public static String getContentType(String initial, int currPos) { FastStringBuffer buf = new FastStringBuffer(initial, 0); + ParsingUtils parsingUtils = create(initial); String curr = PY_DEFAULT; *************** *** 381,385 **** curr = PY_SINGLELINE_STRING2; } ! i = getLiteralEnd(buf, i, ch); } } --- 502,506 ---- curr = PY_SINGLELINE_STRING2; } ! i = parsingUtils.getLiteralEnd(i, ch); } } Index: ImportHandle.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/ImportHandle.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ImportHandle.java 15 Jun 2008 16:45:20 -0000 1.7 --- ImportHandle.java 6 Jul 2008 21:58:28 -0000 1.8 *************** *** 138,142 **** if(c == '#'){ comments = comments.clear(); ! i = ParsingUtils.eatComments(importedStr, comments, i); addImportAlias(lst, importComments, alias, comments.toString()); alias = alias.clear(); --- 138,142 ---- if(c == '#'){ comments = comments.clear(); ! i = ParsingUtils.create(importedStr).eatComments(comments, i); addImportAlias(lst, importComments, alias, comments.toString()); alias = alias.clear(); *************** *** 339,343 **** if(c == '#'){ ! i = ParsingUtils.eatComments(importFound, imp, i); }else if(c == ';'){ --- 339,343 ---- if(c == '#'){ ! i = ParsingUtils.create(importFound).eatComments(imp, i); }else if(c == ';'){ Index: PyDocIterator.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.core/src/org/python/pydev/core/docutils/PyDocIterator.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** PyDocIterator.java 17 May 2008 14:26:57 -0000 1.8 --- PyDocIterator.java 6 Jul 2008 21:58:28 -0000 1.9 *************** *** 132,140 **** char ch = 0; ! while(ch != '\r' && ch != '\n' && offset < doc.getLength()){ ch = doc.getChar(offset); if (ch == '#') { ! while (offset < doc.getLength() && ch != '\n' && ch != '\r') { ch = doc.getChar(offset); if(addComments && ch != '\n' && ch != '\r'){ --- 132,142 ---- char ch = 0; ! int docLen = doc.getLength(); ! ParsingUtils parsingUtils = ParsingUtils.create(doc); ! while(ch != '\r' && ch != '\n' && offset < docLen){ ch = doc.getChar(offset); if (ch == '#') { ! while (offset < docLen && ch != '\n' && ch != '\r') { ch = doc.getChar(offset); if(addComments && ch != '\n' && ch != '\r'){ *************** *** 147,151 **** if(returnNewLinesOnLiterals){ inLiteral = true; ! literalEnd = ParsingUtils.getLiteralEnd(doc, offset, ch); String ret = nextInLiteral(); if(ret.length() > 0){ --- 149,153 ---- if(returnNewLinesOnLiterals){ inLiteral = true; ! literalEnd = parsingUtils.getLiteralEnd(offset, ch); String ret = nextInLiteral(); if(ret.length() > 0){ *************** *** 164,168 **** throw new RuntimeException("Not supported in this case."); } ! offset = ParsingUtils.getLiteralEnd(doc, offset, ch); offset++; } --- 166,170 ---- throw new RuntimeException("Not supported in this case."); } ! offset = parsingUtils.getLiteralEnd(offset, ch); offset++; } *************** *** 183,187 **** } if(ch == '\r'){ ! if(offset < doc.getLength() && doc.getChar(offset) == '\n'){ offset++; if(addNewLinesToRet){ --- 185,189 ---- } if(ch == '\r'){ ! if(offset < docLen && doc.getChar(offset) == '\n'){ offset++; if(addNewLinesToRet){ |