}
private List<stmtType> parse() {
List<stmtType> body = new ArrayList<stmtType>();
PySelection ps = new PySelection(doc);
DocIterator it = new PySelection.DocIterator(forward, ps, currentLine, false);
Matcher functionMatcher = FUNCTION_PATTERN.matcher("");
List<Matcher> cythonMatchers = null;
if (this.cythonParse) {
cythonMatchers = new ArrayList<Matcher>();
cythonMatchers.add(FUNCTION_PATTERN_CYTHON.matcher(""));
cythonMatchers.add(FUNCTION_PATTERN_CYTHON2.matcher(""));
}
Matcher classMatcher = CLASS_PATTERN.matcher("");
while (it.hasNext()) {
Matcher functionFound = null;
String line = it.next();
//we don't care about empty lines
if (line.trim().length() == 0) {
continue;
}
if (findGloballyAccessiblePath) {
int currentFirstCharCol = PySelection.getFirstCharPosition(line);
if (firstCharCol == -1) {
firstCharCol = currentFirstCharCol;
} else {
//We must validate if this is a line we can accept based on the initial indentation
//E.g.:
//
//def m1():
// def m2():
// pass
// pass <- If we're here, m2() should not be considered when getting the path
// to the global scope.
if (firstCharCol <= currentFirstCharCol) {
continue; // don't check this line as it's not valid in the current context.
}
}
}
functionMatcher.reset(line);
if (functionMatcher.find()) {
functionFound = functionMatcher;
} else if (cythonMatchers != null) {
for (Matcher matcher : cythonMatchers) {
matcher.reset(line);
if (matcher.find()) {
functionFound = matcher;
break;
}
}
}
if (functionFound != null) {
int lastReturnedLine = it.getLastReturnedLine();
NameTok nameTok = createNameTok(functionFound, lastReturnedLine, NameTok.FunctionName, ps);
if (nameTok != null) {
FunctionDef functionDef = createFunctionDef(lastReturnedLine, nameTok,
PySelection.getFirstCharPosition(line));
if (!addStatement(body, functionDef)) {
return body;
}
if (stopOnFirstMatch) {
return body;
}
}
continue;
}
classMatcher.reset(line);
if (classMatcher.find()) {
int lastReturnedLine = it.getLastReturnedLine();
NameTok nameTok = createNameTok(classMatcher, lastReturnedLine, NameTok.ClassName, ps);
if (nameTok != null) {
ClassDef classDef = createClassDef(lastReturnedLine, nameTok,