[Pydev-cvs] org.python.pydev.parser/src/org/python/pydev/parser/fastparser FastDefinitionsParser.j
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-07-05 21:52:54
|
Update of /cvsroot/pydev/org.python.pydev.parser/src/org/python/pydev/parser/fastparser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21875/src/org/python/pydev/parser/fastparser Modified Files: FastDefinitionsParser.java Log Message: FastParser handling attributes (still needs more testing). Index: FastDefinitionsParser.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.parser/src/org/python/pydev/parser/fastparser/FastDefinitionsParser.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FastDefinitionsParser.java 2 Jul 2008 20:38:39 -0000 1.3 --- FastDefinitionsParser.java 5 Jul 2008 21:53:01 -0000 1.4 *************** *** 5,15 **** --- 5,20 ---- import org.python.pydev.core.docutils.ParsingUtils; + import org.python.pydev.core.docutils.StringUtils; import org.python.pydev.core.structure.FastStack; import org.python.pydev.core.structure.FastStringBuffer; import org.python.pydev.parser.jython.SimpleNode; + import org.python.pydev.parser.jython.ast.Assign; + import org.python.pydev.parser.jython.ast.Attribute; import org.python.pydev.parser.jython.ast.ClassDef; import org.python.pydev.parser.jython.ast.FunctionDef; import org.python.pydev.parser.jython.ast.Module; + import org.python.pydev.parser.jython.ast.Name; import org.python.pydev.parser.jython.ast.NameTok; + import org.python.pydev.parser.jython.ast.exprType; import org.python.pydev.parser.jython.ast.stmtType; *************** *** 27,31 **** * @author Fabio */ ! public class FastDefinitionsParser { /** --- 32,36 ---- * @author Fabio */ ! public final class FastDefinitionsParser { /** *************** *** 44,52 **** /** - * Last char we found - */ - private char lastChar = '\n'; - - /** * Current iteration index */ --- 49,52 ---- *************** *** 158,162 **** ! case '\r': if(currIndex < length-1 && cs[currIndex+1] == '\n'){ --- 158,168 ---- ! case '{': ! case '[': ! case '(': ! //starting some call, dict, list, tuple... those don't count on getting some actual definition ! currIndex = ParsingUtils.eatPar(cs, currIndex, null, c); ! break; ! case '\r': if(currIndex < length-1 && cs[currIndex+1] == '\n'){ *************** *** 170,178 **** break; //No default - } lineBuffer.append(c); - lastChar = c; } --- 176,225 ---- break; + + case '=': + if(currIndex < length-1 && cs[currIndex+1] != '='){ + if(DEBUG){ + System.out.println("Found possible attribute:"+lineBuffer+" col:"+firstCharCol); + } + + + + String lineContents = lineBuffer.toString().trim(); + boolean add=true; + for(int i=0;i<lineContents.length();i++){ + char lineC = lineContents.charAt(i); + //can only be made of valid java chars (no spaces or similar things) + if(lineC != '.' && !Character.isJavaIdentifierPart(lineC)){ + add=false; + break; + } + } + if(add){ + //only add if it was something valid + if(lineContents.indexOf('.') != -1){ + String[] dotSplit = StringUtils.dotSplit(lineContents); + if(dotSplit.length == 2 && dotSplit[0].equals("self")){ + Attribute attribute = new Attribute(new Name("self", Name.Load), new NameTok(dotSplit[1], NameTok.Attrib), Attribute.Load); + exprType[] targets = new exprType[]{attribute}; + Assign assign = new Assign(targets, null); + + assign.beginColumn = this.col; + assign.beginLine = this.row; + addToPertinentScope(assign); + } + + }else{ + Name name = new Name(lineContents, Name.Store); + exprType[] targets = new exprType[]{name}; + Assign assign = new Assign(targets, null); + assign.beginColumn = this.col; + assign.beginLine = this.row; + addToPertinentScope(assign); + } + } + } //No default } lineBuffer.append(c); } *************** *** 199,204 **** --- 246,253 ---- lineBuffer.clear(); char c = cs[currIndex]; + boolean walkedIndex = false; while(currIndex < length-1 && Character.isWhitespace(c) && c != '\r' && c != '\n'){ + walkedIndex = true; currIndex ++; col++; *************** *** 221,225 **** startMethod(getNextIdentifier(c), row, startMethodCol); } ! currIndex --; } --- 270,277 ---- startMethod(getNextIdentifier(c), row, startMethodCol); } ! if(walkedIndex){ ! currIndex --; ! } ! firstCharCol = col; } *************** *** 304,321 **** * It'll find a correct scope based on the column it has to be added to. * ! * @param def the definition to be added */ ! private void addToPertinentScope(stmtType def) { //see where it should be added (global or class scope) while(stack.size() > 0){ ClassDef parent = stack.peek(); ! if(parent.beginColumn < def.beginColumn){ List<stmtType> peek = stackBody.peek(); ! if(def instanceof FunctionDef){ int size = peek.size(); if(size > 0){ stmtType existing = peek.get(size-1); ! if(existing.beginColumn < def.beginColumn){ //we don't want to add a method inside a method at this point. //all the items added should have the same column. --- 356,373 ---- * It'll find a correct scope based on the column it has to be added to. * ! * @param newStmt the definition to be added */ ! private void addToPertinentScope(stmtType newStmt) { //see where it should be added (global or class scope) while(stack.size() > 0){ ClassDef parent = stack.peek(); ! if(parent.beginColumn < newStmt.beginColumn){ List<stmtType> peek = stackBody.peek(); ! if(newStmt instanceof FunctionDef){ int size = peek.size(); if(size > 0){ stmtType existing = peek.get(size-1); ! if(existing.beginColumn < newStmt.beginColumn){ //we don't want to add a method inside a method at this point. //all the items added should have the same column. *************** *** 323,328 **** } } } ! peek.add(def); return; }else{ --- 375,420 ---- } } + }else if (newStmt instanceof Assign){ + Assign assign = (Assign) newStmt; + + //an assign could be in a method or in a class depending on where we're right now... + int size = peek.size(); + if(size > 0){ + stmtType existing = peek.get(size-1); + if(existing.beginColumn < newStmt.beginColumn){ + exprType target = assign.targets[0]; + //add the assign to the correct place + if(existing instanceof FunctionDef){ + FunctionDef functionDef = (FunctionDef) existing; + + if(target instanceof Attribute){ + //if it's an attribute at this point, it'll always start with self! + if(functionDef.body == null){ + if(functionDef.specialsAfter == null){ + functionDef.specialsAfter = new ArrayList<Object>(); + } + functionDef.body = new stmtType[10]; + functionDef.body[0] = newStmt; + functionDef.specialsAfter.add(1); //real len + }else{ + //already exists... let's add it... as it's an array, we may have to reallocate it + Integer currLen = (Integer) functionDef.specialsAfter.get(0); + currLen += 1; + functionDef.specialsAfter.set(0, currLen); + if(functionDef.body.length < currLen){ + stmtType[] newBody = new stmtType[functionDef.body.length*2]; + System.arraycopy(functionDef.body, 0, newBody, 0, functionDef.body.length); + functionDef.body = newBody; + } + functionDef.body[currLen-1] = newStmt; + } + } + return; + } + } + } + } ! peek.add(newStmt); return; }else{ *************** *** 331,335 **** } //if it still hasn't returned, add it to the global ! this.body.add(def); } --- 423,427 ---- } //if it still hasn't returned, add it to the global ! this.body.add(newStmt); } |